HTML Canvas: Methods, Drawing, and Examples
What is HTML Canvas: Methods, Drawing, and Examples? The HTML5 element provides a resolution-dependent bitmap canvas on which JavaScript can draw 2D graphics, geometric shapes, text, charts, image filters, and ga...
How does the HTML5 <canvas> element render 2D graphics, animations, and charts via JavaScript?
Explanation & In-Depth Details
2) Canvas Width/Height vs CSS Dimensions:
- The 'width' and 'height' attributes on the <canvas> tag set the coordinate drawing resolution in pixels (default is 300x150).
- Setting width and height in CSS stretches or shrinks the rendered bitmap, causing blurriness if the CSS dimensions differ from the attribute dimensions.
3) The 2D Rendering Context:
- const ctx = canvas.getContext('2d');
- The 'ctx' object provides all drawing APIs for paths, rectangles, circles, gradients, and text.
4) Common Drawing Methods:
- ctx.fillRect(x, y, w, h): Draws a solid filled rectangle.
- ctx.strokeRect(x, y, w, h): Outlines a rectangular border.
- ctx.beginPath(), ctx.moveTo(x, y), ctx.lineTo(x, y), ctx.stroke(): Draws vector paths and polygons.
- ctx.arc(x, y, radius, startAngle, endAngle): Draws curves and full circles.
- ctx.fillText('Text', x, y): Renders text directly into pixels.
5) Animation Loops with requestAnimationFrame: By clearing the canvas (ctx.clearRect) and redrawing updated coordinates inside requestAnimationFrame(), developers create 60 FPS games and data visualizations.
Code Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5 Canvas Showcase</title>
</head>
<body>
<h2>HTML5 Canvas 2D Graphics</h2>
<canvas id="myCanvas" width="500" height="250" style="border: 1px solid #cbd5e1; border-radius: 8px; background: #0f172a;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// 1. Draw a filled colored rectangle
ctx.fillStyle = '#38bdf8';
ctx.fillRect(40, 40, 120, 80);
// 2. Draw a circle / arc
ctx.beginPath();
ctx.arc(280, 80, 45, 0, Math.PI * 2);
ctx.fillStyle = '#f59e0b';
ctx.fill();
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 3;
ctx.stroke();
// 3. Draw a vector line
ctx.beginPath();
ctx.moveTo(40, 180);
ctx.lineTo(460, 180);
ctx.strokeStyle = '#10b981';
ctx.lineWidth = 4;
ctx.stroke();
// 4. Render Canvas Text
ctx.font = 'bold 18px sans-serif';
ctx.fillStyle = '#f8fafc';
ctx.fillText('Interactive Canvas Graphics', 40, 215);
</script>
</body>
</html>Interview Question & Tips
A1: Canvas is a raster-based pixel sandbox (drawing commands manipulate bitmap pixels; objects cannot be accessed via DOM). SVG is vector-based XML (every shape is a distinct DOM element that supports CSS styling and click events).
Q2: Why does drawing look blurry on high-resolution Retina displays, and how do you fix it?
A2: Retina displays have a devicePixelRatio of 2x or 3x. Fix blurriness by multiplying the canvas.width and height attributes by window.devicePixelRatio, and setting the CSS style width and height to the original dimensions.
Q3: Can screen readers read text rendered inside an HTML5 <canvas>?
A3: No. Text rendered via ctx.fillText() is converted into flat pixels, making it completely invisible to screen readers. Fallback text must be placed inside the <canvas> tag or mirrored in an accessible DOM overlay.
Best Practices
This study reference for HTML Canvas: Methods, Drawing, and Examples is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: