requestAnimationFrame: كرة تتحرك
القسم 9: Canvas: الرسم والألعابالدرس 9.2 · ⏱ 10 دقائق
الحركة في الألعاب صور كثيرة سريعة: نمسح اللوحة، ونرسم الكرة في مكانها الجديد، ونكرر. requestAnimationFrame يطلب الصورة التالية نحو 60 مرة في الثانية.
dxوdyسرعة الكرة في الاتجاهين.- عند الحافة نعكس الإشارة:
dx = -dx، فترتدّ الكرة.
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();
🖥️ الصفحة في روبو ويب:

🖥️ ماذا يحدث على المسرح؟
الكرة تتحرك في المختبر وترتدّ عن الحواف. الصورة لحظة واحدة منها.
- أضف كرة ثانية بلون آخر وسرعة أخرى.
📌 مثال آخر: كرة تسقط وترتدّ
الجاذبية: السرعة لأسفل تزيد كل إطار، وعند الأرض ترتدّ.
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();
🖥️ الصفحة في روبو ويب:

✍️ طبّق بنفسك
جرّب المثال هنا: انسخ الكود إلى تبويباته، وغيّر فيه، وشاهد الصفحة تتغيّر.
✍️ طبّق هنا في الدرس✖ أغلق المعملأو افتح المعمل في صفحة جديدة ↗
🎯 تحدَّ نفسك
سهل
مربع يمشي إلى اليمين.
👀 اعرض الحل🙈 أخفِ الحل
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();
🖥️ الصفحة في روبو ويب:

النتيجة: المربع يتحرك.
متوسط
المربع يرجع من اليسار عندما يخرج من اليمين.
👀 اعرض الحل🙈 أخفِ الحل
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();
🖥️ الصفحة في روبو ويب:

النتيجة: يلفّ ويرجع.
صعب
الكرة تترك أثرًا: بدل المسح نرسم مستطيلًا شفافًا.
👀 اعرض الحل🙈 أخفِ الحل
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();
🖥️ الصفحة في روبو ويب:

النتيجة: كرة بذيل.

