-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspring.html
41 lines (36 loc) · 862 Bytes
/
spring.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
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring</title>
<link rel="stylesheet" type="text/css" href="./css/style.css" />
<script src="./js/utils.js"></script>
<script src="./js/ball.js"></script>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
window.onload = function(){
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
mouse = utils.captureMouse(canvas),
ball = new Ball(),
spring = 0.03,
targetX = canvas.width / 2,
vx = 0,
friction = 0.98;
ball.y = canvas.height / 2;
(function drawFrame(){
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
var dx = targetX - ball.x,
ax = dx * spring;
vx += ax;
vx *= friction;
ball.x += vx;
ball.draw(context);
}());
}
</script>
</body>
</html>