-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03. SoftUni_Ball Animation.html
54 lines (46 loc) · 1.49 KB
/
03. SoftUni_Ball Animation.html
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html>
<head>
<title>Ball Animation</title>
</head>
<body>
<div style="width: 200px; display: inline-block;"></div>
<canvas id="canvas" width="800" height="500" style="border: 2px solid black">
Canvas not supported
</canvas>
<script type="text/javascript">
drawABall();
function drawABall() {
let ctx = document.getElementById("canvas").getContext("2d");
ctx.fillStyle = "green";
let ball = {x:100, y:100};
//let timet = setInterval(animate, 15);
let directionX = true; // for true move rigtht
let directionY = true; // for true move down
animate(); // for requestAnimatinFeamre();
function animate() {
ctx.clearRect(0,0,800,500);
ctx.beginPath();
ctx.arc(ball.x,ball.y,50,0,Math.PI*2);
ctx.fill();
(directionX == true) ? ball.x += 5 : ball.x -= 5;
(directionY == true) ? ball.y += 5 : ball.y -= 5;
//for X direction constraints
if (ball.x >= 750) {
directionX = false;
}
else if (ball.x <= 50) {
directionX = true;
}
//for Y directin constraints
if (ball.y >= 450) {
directionY = false;
} else if (ball.y <= 50){
directionY = true;
}
requestAnimationFrame(animate);
}
}
</script>
</body>
</html>