انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

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:The page in Robo Web
  1. Add two windows to the house.
  2. 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.

🌐 A flag with three colours
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: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 lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A blue square in the middle of the canvas.

👀 Show the solution
🌐 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:The page in Robo Web

Result: a blue square.

Medium

Three circles in the colours of a traffic light.

👀 Show the solution
🌐 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:The page in Robo Web

Result: red, orange and green.

Hard

A zigzag line with 5 peaks (lineTo inside for).

👀 Show the solution
🌐 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:The page in Robo Web

Result: a purple zigzag line.

Finished the lesson? Mark it so your progress counts.