-
Notifications
You must be signed in to change notification settings - Fork 4
/
bounce.html
64 lines (54 loc) · 1.02 KB
/
bounce.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
55
56
57
58
59
60
61
62
63
64
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Bounce [Canvas]</title>
</head>
<body>
<section>
<div>
<canvas id="canvas" width="1000" height="300"></canvas>
</div>
<script type="text/javascript">
var canvas,
ctx,
x = 400,
y = 300,
dx = 2,
dy = 4,
WIDTH = 1000,
HEIGHT = 300;
function circle(x,y,r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2, true);
ctx.fill();
}
function rect(x,y,w,h) {
ctx.beginPath();
ctx.rect(x,y,w,h);
ctx.closePath();
ctx.fill();
}
function start() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
return setInterval(draw, 10);
}
function draw() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
ctx.fillStyle = "#FAE06E";
rect(0,0, WIDTH, HEIGHT);
ctx.fillStyle = "#444444";
circle(x, y, 10);
if (x + dx > WIDTH || x + dx < 0)
dx = -dx;
if (y + dy > HEIGHT || y + dy < 0)
dy = -dy;
x += dx;
y += dy;
}
window.onload = start;
</script>
</section>
</body>
</html>