-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.js
More file actions
27 lines (27 loc) · 653 Bytes
/
21.js
File metadata and controls
27 lines (27 loc) · 653 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
const speed = 4;
let position = 0;
let moveSpeed = speed;
let radius = 50;
function moveBall(){
if(position + radius > 640){
moveSpeed = -speed;
} else if(position - radius < 0){
moveSpeed = speed;
}
position += moveSpeed;
}
function drawBall(){
ctx.clearRect(0, 0, 640, 480);
ctx.fillStyle = "#FF0000";
ctx.beginPath();
ctx.arc(position, 100, radius, 0, 2 * Math.PI);
ctx.fill();
}
function animate(){
moveBall();
drawBall();
window.requestAnimationFrame(animate);
}
window.requestAnimationFrame(animate);