Skip to content

Commit f38edc3

Browse files
committed
episode 27
1 parent c09482f commit f38edc3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

episode27/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<script type="text/javascript" src="main.js"></script>
6+
<style type="text/css">
7+
html, body {
8+
margin: 0px;
9+
}
10+
11+
canvas {
12+
display: block;
13+
position: absolute;
14+
top: 0px;
15+
left: 0px;
16+
}
17+
18+
</style>
19+
</head>
20+
<body>
21+
<canvas id="canvas"></canvas>
22+
</body>
23+
</html>

episode27/main.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
window.onload = function() {
2+
var canvas = document.getElementById("canvas"),
3+
context = canvas.getContext("2d"),
4+
width = canvas.width = window.innerWidth,
5+
height = canvas.height = window.innerHeight,
6+
7+
target = {
8+
x: width,
9+
y: Math.random() * height
10+
},
11+
12+
position = {
13+
x: 0,
14+
y: Math.random() * height
15+
},
16+
17+
ease = 0.1;
18+
19+
update();
20+
21+
document.body.addEventListener("mousemove", function(event) {
22+
target.x = event.clientX;
23+
target.y = event.clientY;
24+
});
25+
26+
function update() {
27+
context.clearRect(0, 0, width, height);
28+
29+
context.beginPath();
30+
context.arc(position.x, position.y, 10, 0, Math.PI * 2, false);
31+
context.fill();
32+
33+
34+
var dx = target.x - position.x,
35+
dy = target.y - position.y,
36+
vx = dx * ease,
37+
vy = dy * ease;
38+
39+
position.x += vx;
40+
position.y += vy;
41+
42+
43+
requestAnimationFrame(update);
44+
}
45+
46+
};

0 commit comments

Comments
 (0)