Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The HTML Canvas element is a container for graphics that are drawn using JavaScript. Unlike SVG, Canvas uses a raster (pixel-based) approach, giving you pixel-level control for dynamic content like games, animations, and data visualizations.
<canvas id="myCanvas" width="400" height="300">
Your browser doesn't support Canvas.
</canvas>const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');// Set style
ctx.fillStyle = '#3b82f6';
// Draw a rectangle
ctx.fillRect(50, 50, 150, 100);Rectangle, circle, and text in Canvas
<div class="canvas-container">
<h3>Simple Canvas Drawing</h3>
<canvas id="basicCanvas" width="300" height="250"></canvas>
</div>Loading preview...
Filled rectangle
ctx.fillRect(x, y, width, height)Rectangle outline
ctx.strokeRect(x, y, width, height)Circle or arc
ctx.arc(x, y, radius, startAngle, endAngle)Draw lines
ctx.moveTo(x1, y1); ctx.lineTo(x2, y2)Rectangles, circles, triangle, star, curves, and arcs
<div class="shapes-container">
<h3>Various Canvas Shapes</h3>
<canvas id="shapesCanvas" width="400" height="300"></canvas>
</div>Loading preview...
Fill color for shapes
ctx.fillStyle = '#3b82f6'Outline/border color
ctx.strokeStyle = '#f97316'Thickness of lines
ctx.lineWidth = 3Transparency (0-1)
ctx.globalAlpha = 0.5Use the Canvas API to render text with different fonts, sizes, and alignments.
Draw filled text
ctx.fillText("Hello", x, y)Draw text outline
ctx.strokeText("Hello", x, y)Different fonts, sizes, and text positioning
<div class="text-container">
<h3>Text in Canvas</h3>
<canvas id="textCanvas" width="400" height="250"></canvas>
</div>Loading preview...
Animation on Canvas requires clearing and redrawing the entire scene on each frame using requestAnimationFrame().
function animate() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update objects (position, rotation, etc.)
updateObjects();
// Draw everything
drawObjects();
// Request next frame
requestAnimationFrame(animate);
}
animate();Circle bounces around with changing colors
<div class="animation-container">
<h3>Animated Canvas</h3>
<canvas id="animCanvas" width="400" height="250"></canvas>
<p class="anim-info">Circle moves and changes color</p>
</div>Loading preview...
Canvas is part of the DOM, so you can add event listeners for mouse clicks, movement, keyboard input, and more.
mousedown / mouseup- Mouse button pressed/releasedmousemove- Mouse position changedclick- Canvas clickedkeydown / keyup- Keyboard inputClick and drag your mouse to paint on canvas
<div class="interactive-container">
<h3>Interactive Canvas - Paint</h3>
<canvas id="paintCanvas" width="400" height="250"></canvas>
<p class="interact-info">Click and drag to draw</p>
</div>Loading preview...
| Feature | Canvas | SVG |
|---|---|---|
| Type | Raster (pixel-based) | Vector (math-based) |
| Performance (many objects) | ✓ Better | Good for fewer elements |
| Scalability | Pixelated when enlarged | ✓ Scales perfectly |
| DOM Access | Single element | ✓ Each shape accessible |
| Best For | Games, animations, effects | ✓ Icons, diagrams, charts |
| CSS Styling | No direct support | ✓ Full CSS support |
Browser-based games
Interactive visualizations
Motion graphics
Image manipulation
Paint and sketch tools
Algorithmic artwork