-
Notifications
You must be signed in to change notification settings - Fork 0
/
xMovingCircle.js
35 lines (31 loc) · 950 Bytes
/
xMovingCircle.js
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
(function(){
let c = document.getElementById('xMovingCircle'),
ctx = c.getContext('2d'),
cWidth = c.width;
var requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
let ball = {
x: 30,
y: 100,
vx: 5,
draw: function() {
ctx.beginPath();
ctx.arc(this.x,this.y,30,0,2*Math.PI); // x, y, radius, starting angle (radians), ending angle (radians), counterclockwise
ctx.fillStyle = 'lightcoral';
}
}
function draw(){
let reverse = false;
ctx.clearRect(0, 0, 200, 200);
ctx.fill();
ball.draw();
if( ball.x < 0 || ball.x > c.width ){
ball.vx = -ball.vx;
}
ball.x += ball.vx;
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
})();