Learn how to use the HTML5 Canvas API to draw graphics, shapes, and animations directly on a webpage using JavaScript.
- Canvas element basics
- 2D rendering context
- Drawing shapes (rectangles, circles, lines)
- Colors and styles
- Paths and strokes
- Canvas animations
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Draw a rectangle
ctx.fillStyle = 'blue';
ctx.fillRect(50, 50, 200, 100);
// Draw a circle
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(250, 250, 50, 0, Math.PI * 2);
ctx.fill();- Canvas provides a drawing surface for JavaScript
- Use
getContext('2d')to get 2D rendering context - Many methods available for shapes, text, images
- Great for games and visualizations
Difficulty: Intermediate
Category: Canvas & Graphics