Learn Canvas
Draw graphics and animations on canvas - shapes, images, and pixel manipulation.
0 of 4 concepts completed (0%)
What You'll Learn
Tutorial Contents
1.Drawing API
const canvas = document.getElementById("canvas");// Rectangles ctx.fillStyle = "#6200ee"; ctx.fillRect(10, 10, 150, 80); // filled
ctx.strokeStyle = "#03dac6"; ctx.lineWidth = 3; ctx.strokeRect(180, 10, 150, 80); // outlined
ctx.clearRect(30, 30, 50, 40); // erase area
// Paths ctx.beginPath(); ctx.moveTo(50, 200); ctx.lineTo(150, 150); ctx.lineTo(200, 220); ctx.closePath(); ctx.fillStyle = "rgba(98, 0, 238, 0.5)"; ctx.fill(); ctx.strokeStyle = "#6200ee"; ctx.stroke();
// Circle / arc ctx.beginPath(); ctx.arc(300, 180, 50, 0, Math.PI * 2); ctx.fillStyle = "#03dac6"; ctx.fill();
// Gradient fill const grad = ctx.createLinearGradient(0, 0, 400, 0); grad.addColorStop(0, "#6200ee"); grad.addColorStop(1, "#03dac6"); ctx.fillStyle = grad; ctx.fillRect(0, 280, 400, 60);
// Text ctx.font = "bold 24px system-ui"; ctx.fillStyle = "#333"; ctx.textAlign = "center"; ctx.fillText("Canvas Text", 200, 380); ```
2.Animation Loop
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 600;// Bouncing ball example const ball = { x: 100, y: 100, vx: 3, vy: 2, r: 20 };
function update() { ball.x += ball.vx; ball.y += ball.vy;
if (ball.x + ball.r > canvas.width || ball.x - ball.r < 0) ball.vx *= -1; if (ball.y + ball.r > canvas.height || ball.y - ball.r < 0) ball.vy *= -1; }
function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height);
// Background ctx.fillStyle = "#f8f9ff"; ctx.fillRect(0, 0, canvas.width, canvas.height);
// Ball with shadow ctx.shadowColor = "rgba(98, 0, 238, 0.4)"; ctx.shadowBlur = 15; ctx.beginPath(); ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2); ctx.fillStyle = "#6200ee"; ctx.fill(); ctx.shadowBlur = 0; // reset shadow }
function loop() { update(); draw(); requestAnimationFrame(loop); }
loop(); ```
Code Example
<!-- Canvas element -->
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Draw rectangle
ctx.fillStyle = 'blue';
ctx.fillRect(50, 50, 200, 100);
// Draw circle
ctx.beginPath();
ctx.arc(200, 200, 50, 0, Math.PI * 2);
ctx.fill();
</script>Pro Tips
- 1Always set the canvas width/height attributes in HTML, not CSS. CSS scales the canvas element but HTML attributes set the actual pixel buffer.
- 2Call ctx.save() before and ctx.restore() after any transformation (translate, rotate, scale) to avoid state leaks across draw calls.
- 3For animations, use requestAnimationFrame not setInterval. It syncs to the display refresh rate and pauses when the tab is hidden.
- 4Clear the canvas on each frame with ctx.clearRect(0, 0, canvas.width, canvas.height) before redrawing to prevent ghosting.
Topics Covered
Continue Learning
Explore the official documentation and deepen your knowledge with real-world practice.
Explore the Full Portfolio
This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.
The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.
For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.