← Back to JavaScript Course | Chapter 10: DOM Advanced | Lesson 6 of 8

JS ग्राफिक्स

HTML आपको text, images, और forms दिखाने के लिए elements देता है, पर pixel-by-pixel मनमाने shapes, charts, या animations बनाने के लिए बिल्कुल एक अलग tool चाहिए। JavaScript की graphics क्षमताएं -- मुख्यतः Canvas API, और उसके ऊपर बनी Chart.js जैसी libraries -- आपको page में embedded एक rectangular drawing surface पर सीधे पेंट करने देती हैं।

JavaScript Graphics क्या कर सकते हैं

मौजूदा HTML elements को style करने से आगे, JavaScript graphics tools आपको बिल्कुल custom visuals बनाने देते हैं -- charts, game sprites, data visualizations, generated patterns -- सीधे एक drawing surface पर, pixel by pixel, पूरे programmatic control के तहत।

Note: graphics tools का इस्तेमाल खासतौर पर तब करें जब आपको custom, dynamic, या data-driven visuals चाहिए जो साधारण HTML और CSS आसानी से नहीं बना सकते।
Warning: जटिल custom graphics शुरू से बनाना HTML elements को style करने से काफी ज़्यादा काम है -- इसे उन visuals के लिए रखें जिन्हें सच में उतने control की ज़रूरत हो।

उदाहरण: What JavaScript Graphics Can Do

javascript
<canvas id="chart" width="200" height="100"></canvas>
<script>
  const ctx = document.getElementById("chart").getContext("2d");
  ctx.fillStyle = "blue";
  ctx.fillRect(10, 10, 50, 50); // pixel-level drawing, not just styled HTML
</script>

एक Drawing Surface के रूप में Canvas Element

एक <canvas> element मूल रूप से page में एक खाली rectangular area है जिसका अपना कोई visual content नहीं है -- यह तभी उपयोगी बनता है जब JavaScript इसका drawing context लेता है और इसके खिलाफ drawing commands भेजना शुरू करता है।

Note: canvas element पर हमेशा explicit width और height attributes सेट करें, क्योंकि सिर्फ CSS से sizing drawing में इस्तेमाल होने वाले coordinate system को बिगाड़ सकती है।
Warning: जिस canvas पर कोई JavaScript drawing न हो वह पूरी तरह खाली, अदृश्य rectangle के रूप में render होता है -- सिर्फ खाली होने पर इसे एक bug समझ लेना आसान है।

उदाहरण: The Canvas Element as a Drawing Surface

javascript
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const canvas = document.getElementById("myCanvas"); // blank until drawn on
  const ctx = canvas.getContext("2d");
  ctx.fillRect(20, 20, 100, 50);
</script>

Data को Visually Render करना

static shapes से आगे, canvas आमतौर पर data-driven visuals render करने के लिए इस्तेमाल होता है -- values की किसी array से बने bar charts, समय के साथ trends plot करने वाले साधारण line graphs -- हर बार underlying data बदलने पर pixel positions calculate करते हुए।

Note: अपने data (साधारण JavaScript values) को अपने drawing code से अलग रखें, pixel coordinates hardcode करने के बजाय जब भी data update हो pixel positions फिर से calculate करते हुए।
Warning: किसी बहुत साधारण visualization से आगे chart coordinates और scaling manually calculate करना जल्दी ही जटिल हो जाता है -- यही ठीक वह काम है जिसे संभालने के लिए एक charting library मौजूद है।

उदाहरण: Rendering Data Visually

javascript
<canvas id="chart" width="200" height="100"></canvas>
<script>
  // Declare the constant `data`, set to `[30, 80, 45, 60]`
  const data = [30, 80, 45, 60];
  // Declare the constant `ctx`, set to `document.getElementById("chart").getContext("2d")`
  const ctx = document.getElementById("chart").getContext("2d");
  // Call `data.forEach((value, i) => ctx.fillRect(i * 40, 100 - value, 30, value))`
  data.forEach((value, i) => ctx.fillRect(i * 40, 100 - value, 30, value));
</script>

Hand-Rolled Drawing बनाम एक Library इस्तेमाल करना

किसी साधारण shape या एक one-off visualization के लिए raw Canvas API calls लिखना उचित है, पर axes, labels, legends, tooltips, और animations वाले आम chart types (bar, line, pie, scatter) के लिए, Chart.js जैसी library उतनी complexity संभालती है जो आपको वरना खुद बनानी पड़ती।

Note: बहुत साधारण या highly custom visual के लिए raw canvas से जल्दी prototype करें; axes, legends, या tooltips जैसे standard chart features चाहिए होते ही Chart.js का इस्तेमाल करें।
Warning: standard charting features (axis labels, legends, responsive resizing) को फिर से बनाना जिन्हें एक mature library पहले ही हल कर चुकी है, custom dashboards पर बर्बाद हुई मेहनत का एक आम कारण है।

उदाहरण: Hand-Rolled Drawing vs Using a Library

javascript
<canvas id="c1" width="100" height="100"></canvas>
<script>
  // Call `document.getElementById("c1").getContext("2d").fillRect(10, 10, 50, 50)`
  document.getElementById("c1").getContext("2d").fillRect(10, 10, 50, 50);
</script>
<!-- For real charts with axes/legends/tooltips, use a library like Chart.js instead -->

Canvas और दूसरे Graphics Approaches में चुनना

Canvas pixel-based, बार-बार redraw होने वाली, या animated graphics (games, live data visualizations) के लिए सबसे उपयुक्त है; SVG अक्सर उन graphics के लिए बेहतर fit है जिन्हें resolution-independent और individually interactive रहना है, क्योंकि canvas pixels के उलट SVG shapes असली, selectable DOM elements बने रहते हैं।

Note: high-frequency redraws (animations, games) के लिए canvas चुनें और crisp scaling या individual shape interactivity चाहने वाले graphics के लिए SVG।
Warning: एक canvas-based graphic एक bitmap image की तरह scale होता है, बड़ा stretch करने पर धुंधला हो जाता है, SVG के उलट जो किसी भी size पर sharp रहता है।

उदाहरण: Choosing Between Canvas and Other Graphics Approaches

javascript
<canvas id="pixels" width="50" height="50"></canvas>
<svg width="50" height="50"><circle cx="25" cy="25" r="20" fill="green"/></svg>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि अकेला CSS canvas की तरह arbitrary shapes और animations draw कर सकता है -- CSS existing elements को style करता है, free-form pixel drawing नहीं।
  2. यह भूल जाना कि canvas पर draw की गई हर चीज़ सिर्फ pixels हैं, individual shapes की कोई memory नहीं -- आप बाद में 'पहले draw किया circle' को किसी DOM element की तरह select या move नहीं कर सकते।
  3. raw Canvas API calls से complex chart rendering hand-roll करना चुनना जब Chart.js जैसी एक well-tested library significant time बचा सकती और edge cases पहले से handle कर सकती।
चैप्टर सारांश
  • Canvas API shapes, images, text, और animations के लिए एक JavaScript-controlled drawing surface देता है।
  • Canvas drawings pixel-based हैं, object-based नहीं -- एक बार draw होने के बाद, individual shapes को redraw किए बिना independently select या move नहीं किया जा सकता।
  • Chart.js और similar libraries canvas के ऊपर build होती हैं ताकि low-level drawing code hand-write किए बिना common chart types handle कर सकें।
ब्राउज़र सपोर्ट

Canvas API हर modern browser में supported है; Chart.js वहीं काम करता है जहाँ canvas काम करता है, क्योंकि यह अंदर ही अंदर canvas के ज़रिए render होता है।

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.