انتقل للمحتوى
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

requestAnimationFrame: a moving ball

Section 9: Canvas: drawing and gamesLesson 9.2 · ⏱ 10 min

Movement in games is many fast pictures: we clear the canvas, draw the ball in its new place, and repeat. requestAnimationFrame asks for the next picture about 60 times a second.

  • dx and dy are the ball's speed in the two directions.
  • At the edge we turn the sign round: dx = -dx, and the ball bounces back.
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { background: #1e1e2e; border-radius: 12px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let x = 50, y = 50;
let dx = 3, dy = 2;

function frame() {
  ctx.clearRect(0, 0, 300, 200);
  ctx.fillStyle = "orange";
  ctx.beginPath();
  ctx.arc(x, y, 15, 0, Math.PI * 2);
  ctx.fill();

  x += dx;
  y += dy;
  if (x < 15 || x > 285) dx = -dx;   // bounce off the sides
  if (y < 15 || y > 185) dy = -dy;   // bounce off top and bottom
  requestAnimationFrame(frame);
}
frame();
🖥️ The page in Robo Web:The page in Robo Web
🖥️ What happens on the stage?
In the lab the ball moves and bounces off the edges. The picture is one moment of it.
  1. Add a second ball with another colour and another speed.

📌 Another example: a ball that falls and bounces

Gravity: the downward speed grows every frame, and at the ground it bounces.

🌐 A ball that falls and bounces
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
canvas { background: #E8FFF3; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let y = 20, vy = 0;

function frame() {
  vy += 0.4;           // gravity
  y += vy;
  if (y > 180) {       // the floor
    y = 180;
    vy = -vy * 0.8;    // bounce, a little lower each time
  }
  ctx.clearRect(0, 0, 300, 200);
  ctx.fillStyle = "tomato";
  ctx.beginPath();
  ctx.arc(150, y, 20, 0, Math.PI * 2);
  ctx.fill();
  requestAnimationFrame(frame);
}
frame();
🖥️ 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 square that walks to the right.

👀 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");
let x = 0;
function frame() {
  x += 2;
  ctx.clearRect(0, 0, 300, 200);
  ctx.fillStyle = "teal";
  ctx.fillRect(x, 80, 40, 40);
  requestAnimationFrame(frame);
}
frame();
🖥️ The page in Robo Web:The page in Robo Web

Result: the square moves.

Medium

The square comes back from the left when it goes out on the right.

👀 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");
let x = 0;
function frame() {
  x += 4;
  if (x > 300) x = -40;
  ctx.clearRect(0, 0, 300, 200);
  ctx.fillStyle = "teal";
  ctx.fillRect(x, 80, 40, 40);
  requestAnimationFrame(frame);
}
frame();
🖥️ The page in Robo Web:The page in Robo Web

Result: it goes round and comes back.

Hard

The ball leaves a trail: instead of clearing we draw a see-through rectangle.

👀 Show the solution
🌐 Solution
HTML
<canvas id="c" width="300" height="200"></canvas>
CSS
canvas { border: 1px solid #ccc; border-radius: 12px; }
canvas { background: black; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let x = 30, y = 30, dx = 3, dy = 2;
function frame() {
  ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
  ctx.fillRect(0, 0, 300, 200);
  ctx.fillStyle = "cyan";
  ctx.beginPath();
  ctx.arc(x, y, 10, 0, Math.PI * 2);
  ctx.fill();
  x += dx; y += dy;
  if (x < 10 || x > 290) dx = -dx;
  if (y < 10 || y > 190) dy = -dy;
  requestAnimationFrame(frame);
}
frame();
🖥️ The page in Robo Web:The page in Robo Web

Result: a ball with a tail.

Finished the lesson? Mark it so your progress counts.