Draw on the canvas
Section 9: Canvas: drawing and gamesLesson 9.1 · ⏱ 10 min
<canvas> is a drawing board. We draw on it with code: we choose the colour with fillStyle, then draw a rectangle with fillRect(x, y, width, height) or a circle with arc.
📌 Note
The point (0, 0) is in the top left corner, and y gets bigger as we go down.
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
// sky and grass
ctx.fillStyle = "#bfe6ff";
ctx.fillRect(0, 0, 300, 200);
ctx.fillStyle = "#6cc26c";
ctx.fillRect(0, 150, 300, 50);
// sun
ctx.fillStyle = "gold";
ctx.beginPath();
ctx.arc(250, 45, 25, 0, Math.PI * 2);
ctx.fill();
// house
ctx.fillStyle = "#e9a15a";
ctx.fillRect(80, 90, 100, 60);
ctx.fillStyle = "#c0392b";
ctx.beginPath();
ctx.moveTo(70, 90);
ctx.lineTo(130, 45);
ctx.lineTo(190, 90);
ctx.closePath();
ctx.fill();
// door
ctx.fillStyle = "#6b3e1f";
ctx.fillRect(118, 115, 24, 35);
🖥️ The page in Robo Web:

- Add two windows to the house.
- Draw a tree: a brown trunk and a green circle.
📌 Another example: a flag with three colours
Three rectangles on top of each other: red, white and black.
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 300, 66);
ctx.fillStyle = "white";
ctx.fillRect(0, 66, 300, 67);
ctx.fillStyle = "black";
ctx.fillRect(0, 133, 300, 67);
🖥️ The page in Robo Web:

✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
A blue square in the middle of the canvas.
👀 Show the solution🙈 Hide the solution
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(100, 50, 100, 100);
🖥️ The page in Robo Web:

Result: a blue square.
Medium
Three circles in the colours of a traffic light.
👀 Show the solution🙈 Hide the solution
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
canvas { background: #333; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
const colors = ["red", "orange", "lime"];
for (let i = 0; i < 3; i++) {
ctx.fillStyle = colors[i];
ctx.beginPath();
ctx.arc(60 + i * 90, 100, 35, 0, Math.PI * 2);
ctx.fill();
}
🖥️ The page in Robo Web:

Result: red, orange and green.
Hard
A zigzag line with 5 peaks (lineTo inside for).
👀 Show the solution🙈 Hide the solution
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
ctx.strokeStyle = "purple";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(0, 150);
for (let i = 0; i < 5; i++) {
ctx.lineTo(30 + i * 60, 50);
ctx.lineTo(60 + i * 60, 150);
}
ctx.stroke();
🖥️ The page in Robo Web:

Result: a purple zigzag line.

