← Back to HTML Course | Chapter 6: Canvas, SVG & Media | Lesson 1 of 6

HTML Canvas

कल्पना कीजिए कि आप अपने क्लासरूम की दीवार पर एक बड़ा, खाली व्हाइटबोर्ड टाँग रहे हैं। व्हाइटबोर्ड खुद साफ और खाली है, लेकिन इसके साथ रंग-बिरंगे dry-erase मार्कर्स और एक इरेज़र की ट्रे आती है। कोई मैप बनाने या नोट्स लिखने के लिए, आपको खुद एक मार्कर उठाकर उस पर ड्रॉ करना होगा। एक HTML canvas वही डिजिटल व्हाइटबोर्ड है। खुद से, canvas एलिमेंट आपके वेबपेज पर सिर्फ एक खाली कंटेनर है। शेप्स ड्रॉ करने, टेक्स्ट लिखने, या एनिमेशन बनाने के लिए, आपको इस पर पेंट करने के लिए JavaScript स्क्रिप्ट्स लिखनी होंगी। यह ऑनलाइन ड्रॉइंग बोर्ड्स, कस्टम चार्ट्स, इंटरैक्टिव गेम्स, या डायनामिक फोटो एडिटर्स बनाने के लिए एकदम सही टूल है।
Syntax
markup
<canvas id="id" width="width" height="height"></canvas>
<script>
  const ctx = document.getElementById("id").getContext("2d");
  // draw with ctx
</script>

Canvas कंटेनर

canvas टैग आपके वेबपेज पर एक खाली ड्राइंग बोर्ड डिफाइन करता है। यह एक साधारण कंटेनर एलिमेंट है जिसे अपना resolution तय करने के लिए स्पष्ट width और height attributes की ज़रूरत होती है, और पुराने ब्राउज़र के लिए इसके अंदर कुछ fallback टेक्स्ट की भी।

Note:
  • हमेशा HTML attributes का उपयोग करके अपने canvas का resolution तय करें, और एलिमेंट को डायनामिक रूप से स्केल करने के लिए CSS का उपयोग करें।
  • Accepted attributes:
  • width — integer pixels (default 300)
  • height — integer pixels (default 150)
  • id — for scripting via getElementById
Warning: अगर आप width और height attributes छोड़ देते हैं, तो ब्राउज़र canvas को 300x150 पिक्सल के एक छोटे डिफ़ॉल्ट साइज़ में सेट कर देगा।

उदाहरण: The Canvas Container

markup
<canvas id="myCanvas" width="300" height="150">
  Your browser does not support the canvas element.
</canvas>

Drawing Context

अपने canvas पर ड्रॉ करने से पहले, आपको एक स्क्रिप्ट लिखनी होगी जो एलिमेंट की ID कैप्चर करे और उसका drawing context (जैसे 2d) डिक्लेयर करे, जो ब्राउज़र को उस पर पेंट करने के लिए ज़रूरी टूल्स और coordinate system के साथ तैयार करता है।

Note: 2d context एक स्टैण्डर्ड coordinate system देता है जिसमें (0,0) canvas के ऊपर-बाएं कोने पर होता है।
Warning: सुनिश्चित करें कि आपकी स्क्रिप्ट canvas context कैप्चर करने की कोशिश करने से पहले वेबपेज पूरी तरह लोड हो चुका हो, वरना आपका कोड एरर देगा।

उदाहरण: The Drawing Context

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const ctx = document.getElementById('myCanvas').getContext('2d');
</script>

Rectangles ड्रॉ करना

2D canvas context सीधे आपके बोर्ड पर rectangles ड्रॉ करने के लिए तीन आसान methods देता है: fillRect (एक ठोस rectangle ड्रॉ करने के लिए), strokeRect (एक खोखली rectangular आउटलाइन ड्रॉ करने के लिए), और clearRect (किसी क्षेत्र को मिटाने के लिए)।

Note: अपने shapes को कस्टम रंगों से स्टाइल करने के लिए fillStyle और strokeStyle properties को अपने rectangle methods के साथ मिलाएं।
Warning: अपने drawing methods कॉल करने से पहले हमेशा अपने fillStyle या strokeStyle colors सेट करें, वरना वे स्टैंडर्ड काले रंग में रेंडर होंगे।

उदाहरण: Drawing Rectangles

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const ctx = document.getElementById('myCanvas').getContext('2d');
  ctx.fillStyle = 'blue';
  ctx.fillRect(10, 10, 100, 50);
</script>

Lines और Paths ड्रॉ करना

कस्टम shapes, curves, या circles ड्रॉ करने के लिए, आपको path methods का उपयोग करना होगा। एक path ड्रॉ करना पेंसिल से ड्रॉइंग करने जैसा है: आप ब्राउज़र को बताते हैं कि पेंसिल को बोर्ड पर रखो (beginPath), लाइनों को ट्रेस करो (lineTo), और नतीजे को पेंट करो (stroke या fill)।

Note: अपने canvas पर गोल लाइनें और shapes आसानी से ट्रेस करने के लिए arc method का उपयोग करें।
Warning: अपने shapes को गलत तरीके से आपस में मिलने से रोकने के लिए नई लाइनें ट्रेस करने से पहले हमेशा beginPath कॉल करें।

उदाहरण: Drawing Lines and Paths

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const ctx = document.getElementById('myCanvas').getContext('2d');
  ctx.beginPath();
  ctx.lineTo(150, 80);
  ctx.stroke();
</script>

Canvas को Clear और Update करना

डायनामिक एनिमेशन, इंटरैक्टिव ड्रॉइंग बोर्ड, या गेम फ्रेम बनाने के लिए, आपको अपने canvas को बार-बार अपडेट करना होता है।

यह आप पूरे बोर्ड को clearRect से मिटाकर, अपने shape coordinates को अपडेट करके, और तेज़ी से नए फ्रेम पेंट करके करते हैं।

Note: अपने canvas पर smooth, high-performance वेब एनिमेशन बनाने के लिए requestAnimationFrame method का उपयोग करें।
Warning: अगर आप नया फ्रेम ड्रॉ करने से पहले अपना canvas क्लियर नहीं करते, तो आपके एनिमेटेड शेप्स स्क्रीन पर ट्रेल मार्क्स छोड़ जाएंगे।

उदाहरण: Clearing and Updating the Canvas

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const ctx = document.getElementById('myCanvas').getContext('2d');
  ctx.clearRect(0, 0, 200, 100);
  ctx.fillRect(10, 10, 50, 50);
</script>

Canvas पर Text ड्रॉ करना

fillText और strokeText methods आपको टेक्स्ट को सीधे canvas पर ड्रॉ करने देते हैं, जिसमें font property पहले से साइज़ और typeface कंट्रोल करती है।

साधारण HTML टेक्स्ट के उलट, canvas टेक्स्ट सिर्फ pixels होता है — इसे सिलेक्ट, सर्च या screen readers द्वारा पढ़ा नहीं जा सकता।

Note: fillText कॉल करने से पहले font property सेट करें, क्योंकि canvas हर आगे की drawing operation के लिए मौजूदा font सेटिंग को याद रखता है।
Warning: canvas पर ड्रॉ किया गया टेक्स्ट screen readers और search engines के लिए अदृश्य होता है — इसे कभी भी ऐसे content के लिए इस्तेमाल न करें जिसे accessible या indexable होना ज़रूरी है।

उदाहरण: Drawing Text on Canvas

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const ctx = document.getElementById('myCanvas').getContext('2d');
  ctx.font = '20px Arial';
  ctx.fillText('Hello Canvas', 10, 50);
</script>

Canvas पर Images ड्रॉ करना

drawImage method आपको किसी मौजूदा image फाइल को canvas पर रखने देता है, और आप उसकी पोजीशन व साइज़ को कंट्रोल कर सकते हैं, या यहाँ तक कि उसे crop और scale भी कर सकते हैं।

यह ब्राउज़र में पूरी तरह image editors, filters, या डायनामिक thumbnail generators जैसी चीज़ें बनाने का आधार है।

Note: drawImage कॉल करने से पहले हमेशा image के onload इवेंट के फायर होने का इंतज़ार करें, वरना canvas खाली रह सकता है।
Warning: किसी अलग डोमेन से images को canvas पर ड्रॉ करने से cross-origin restrictions ट्रिगर हो सकती हैं, जो बाद में canvas डेटा वापस पढ़ने से रोक देती हैं।

उदाहरण: Drawing Images onto Canvas

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const img = new Image();
  img.src = 'photo.jpg';
  img.onload = function () {
    document.getElementById('myCanvas').getContext('2d').drawImage(img, 0, 0);
  };
</script>

Canvas को Image के रूप में Save करना

toDataURL method canvas पर मौजूदा समय में जो कुछ भी ड्रॉ है, उसे base64 data URL के रूप में एन्कोड की गई एक डाउनलोड करने योग्य image फाइल में बदल देता है। इसी तरह in-browser tools यूज़र को chart, ड्रॉइंग, या एडिट की गई फोटो को असली PNG या JPEG फाइल के रूप में एक्सपोर्ट करने देते हैं।

Note: सबसे अच्छी क्वालिटी के आउटपुट के लिए toDataURL("image/png") का उपयोग करें, या छोटी फ़ाइल साइज़ के लिए quality argument के साथ "image/jpeg" पास करें।
Warning: अगर canvas में किसी ऐसे अलग origin से ड्रॉ की गई कोई image है जो cross-origin access की अनुमति नहीं देती, तो toDataURL एक security error देगा।

उदाहरण: Saving Canvas as an Image

markup
<canvas id="myCanvas" width="200" height="100"></canvas>
<script>
  const dataURL = document.getElementById('myCanvas').toDataURL('image/png');
</script>
Live Example
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. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.