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

JS कैनवस

Canvas 2D drawing context वह असली API है जिसे आप किसी canvas element पर पेंट करने के लिए इस्तेमाल करते हैं -- rectangles, paths, text, और images बनाने, fill और stroke colors नियंत्रित करने, और इन primitives को किसी भी custom shape में मिलाने के methods का एक rich सेट जिसे आप code से describe कर सकते हैं।
Syntax
javascript
const canvas = document.getElementById("id");
const ctx = canvas.getContext("2d");
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);

Basic Rectangles बनाना

fillRect(x, y, width, height) एक filled rectangle बनाता है, strokeRect() सिर्फ उसकी outline बनाता है, और clearRect() एक rectangular area को वापस transparent कर देता है -- ये तीन सबसे साधारण drawing operations हैं, और अक्सर सबसे पहले सीखे जाते हैं।

Note: किसी animation में हर redraw की शुरुआत में clearRect(0, 0, canvas.width, canvas.height) इस्तेमाल करें ताकि नए frame को draw करने से पहले पिछला frame मिट जाए।
Warning: fillRect() current fillStyle color से पेंट करता है, जो बदले जाने तक सेट रहता है -- अगले shape से पहले इसे reset करना भूल जाना अनपेक्षित रूप से रंगीन output दे सकता है।

उदाहरण: Drawing Basic Rectangles

javascript
<canvas id="c" width="150" height="100"></canvas>
<script>
  // Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
  const ctx = document.getElementById("c").getContext("2d");
  // Call `ctx.fillRect(10, 10, 50, 30)`
  ctx.fillRect(10, 10, 50, 30);
  // Call `ctx.strokeRect(70, 10, 50, 30)`
  ctx.strokeRect(70, 10, 50, 30);
  // Call `ctx.clearRect(20, 15, 10, 10)`
  ctx.clearRect(20, 15, 10, 10);
</script>

Custom Paths बनाना

beginPath() एक नया shape शुरू करता है, moveTo(x, y) बिना draw किए drawing "pen" को move करता है, lineTo(x, y) current position से एक सीधी line draw करता है, और closePath() वापस शुरुआती point से जोड़ देता है -- साथ में, ये point by point कोई भी custom polygon shape बनाते हैं।

Note: एक नया, असंबंधित shape शुरू करने से पहले हमेशा beginPath() कॉल करें -- इसे भूल जाना नए shape की lines को पहले से draw किए गए किसी भी path से जोड़ सकता है।
Warning: moveTo/lineTo से बना एक path तब तक कोई visible output नहीं देता जब तक आप बाद में fill() या stroke() कॉल न करें -- path खुद अदृश्य है।

उदाहरण: Drawing Custom Paths

javascript
<canvas id="c" width="150" height="100"></canvas>
<script>
  // Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
  const ctx = document.getElementById("c").getContext("2d");
  // Call `ctx.beginPath()`
  ctx.beginPath();
  // Call `ctx.moveTo(10, 10)`
  ctx.moveTo(10, 10);
  // Call `ctx.lineTo(100, 10)`
  ctx.lineTo(100, 10);
  // Call `ctx.lineTo(50, 80)`
  ctx.lineTo(50, 80);
  // Call `ctx.closePath()`
  ctx.closePath();
  // Call `ctx.stroke()`
  ctx.stroke();
</script>

Circles और Arcs बनाना

arc(x, y, radius, startAngle, endAngle) एक circular arc draw करता है -- angles के रूप में 0 से Math.PI * 2 पास करना एक पूरा circle draw करता है, जबकि दूसरी angle ranges partial arcs draw करती हैं, जो pie-slice shapes और गोल visual elements की बुनियाद है।

Note: याद रखें कि arc() के angles radians में मापे जाते हैं, degrees में नहीं -- पूरे circle के लिए 360 के बजाय Math.PI * 2 इस्तेमाल करें।
Warning: अकेला arc() सिर्फ curved path define करता है -- किसी भी दूसरे path की तरह इसे असल में render करने के लिए fill() या stroke() के साथ मिलाएं।

उदाहरण: Drawing Circles and Arcs

javascript
<canvas id="c" width="150" height="100"></canvas>
<script>
  const ctx = document.getElementById("c").getContext("2d");
  ctx.beginPath();
  ctx.arc(50, 50, 40, 0, Math.PI * 2); // full circle
  ctx.stroke();
</script>

Text और Images बनाना

fillText(text, x, y) current font setting इस्तेमाल करके canvas पर सीधे text draw करता है, और drawImage(imageElement, x, y) एक पहले से loaded <img> element (या कोई दूसरा canvas) canvas पर draw करता है -- जो आपको graphics, photos, और labels एक साथ मिलाने देता है।

Note: fillText() कॉल करने से पहले font property सेट करें, बिल्कुल वैसे ही जैसे fill() से पहले fillStyle सेट होना ज़रूरी है, क्योंकि दोनों draw के समय पढ़े जाते हैं।
Warning: source image के load पूरा होने से पहले कॉल किया गया drawImage() कुछ नहीं (या एक खाली frame) draw करता है -- पहले image के load event का इंतज़ार करें।

उदाहरण: Drawing Text and Images

javascript
<canvas id="c" width="150" height="100"></canvas>
<script>
  // Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
  const ctx = document.getElementById("c").getContext("2d");
  // Assign "20px sans-serif" to `ctx.font`
  ctx.font = "20px sans-serif";
  // Call `ctx.fillText("Hello Canvas", 10, 50)`
  ctx.fillText("Hello Canvas", 10, 50);
</script>

एक Simple Animation Loop बनाना

requestAnimationFrame(callback) एक function को browser के अगले repaint से ठीक पहले चलने के लिए schedule करता है, display की refresh rate के साथ smoothly synced -- callback के अंदर ही इसे फिर से कॉल करना एक लगातार, कुशल animation loop बनाता है।

Note: जब तक कोई trailing visual effect खासतौर पर इरादा न हो, redraw करने से पहले हर animation frame की शुरुआत में हमेशा canvas साफ करें।
Warning: animation के लिए requestAnimationFrame() के बजाय setInterval() इस्तेमाल करना कम smooth result दे सकता है और tab visible न होने पर भी चलता रहता है, resources बर्बाद करते हुए।

उदाहरण: Building a Simple Animation Loop

javascript
<canvas id="c" width="150" height="100"></canvas>
<script>
  // Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
  const ctx = document.getElementById("c").getContext("2d");
  // Declare the variable `x`, set to `0`
  let x = 0;
  // Define the function `animate` with no parameters
  function animate() {
    // Call `ctx.clearRect(0, 0, 150, 100)`
    ctx.clearRect(0, 0, 150, 100);
    // Call `ctx.fillRect(x, 40, 20, 20)`
    ctx.fillRect(x, 40, 20, 20);
    x = (x + 1) % 150;
    // Call `requestAnimationFrame(animate)`
    requestAnimationFrame(animate);
  }
  // Call `animate()`
  animate();
</script>
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. यह भूल जाना कि moveTo() और lineTo() जैसे path-drawing methods सिर्फ एक path define करते हैं -- कुछ भी actually render नहीं होता जब तक आप उसे paint करने के लिए fill() या stroke() कॉल न करें।
  2. animation में एक नया frame redraw करने से पहले clearRect() से canvas clear न करना, जिससे हर frame नई चीज़ replace करने के बजाय पिछली के ऊपर draw होता है।
  3. fillStyle (fill() से इस्तेमाल) को strokeStyle (stroke() से इस्तेमाल) से मिला देना और हैरान होना कि कोई color setting का कोई visible effect क्यों नहीं दिख रहा।
चैप्टर सारांश
  • getContext("2d") drawing context retrieve करता है, वह object जिस पर हर drawing method कॉल होता है।
  • fillRect(), strokeRect(), और clearRect() सीधे simple rectangles draw, outline, और erase करते हैं।
  • beginPath(), moveTo(), lineTo(), और closePath() custom shapes बनाते हैं, जिन्हें बाद में एक fill() या stroke() call से render किया जाता है।
ब्राउज़र सपोर्ट

Canvas 2D API हर modern browser में supported है और HTML5 से एक stable web standard रहा है।

🔒

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.