forked from supperjet/H5-Animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance-1.html
50 lines (43 loc) · 1.49 KB
/
distance-1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>distance</title>
</head>
<body>
<canvas id="canvas" width="500" height="500" style="background:#000;">
your browser not support canvas!
</canvas>
<textarea name="" id="log" cols="30" rows="10"></textarea>
<script src="../js/utils.js"></script>
<script src="../js/ball.js"></script>
<script>
window.onload = function(){
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
mouse = utils.captureMouse(canvas),
ball_1 = new Ball(20, "#49f"),
ball_2 = new Ball(20, "#f00");
ball_2.x = canvas.width/2;
ball_2.y = canvas.height/2;
ball_1.draw(context);
ball_2.draw(context);
canvas.addEventListener('mousemove',function(e){
context.clearRect(0, 0, canvas.width, canvas.height);
ball_1.x = mouse.x;
ball_1.y = mouse.y;
var dx = ball_2.x - ball_1.x;
var dy = ball_2.y - ball_1.y;
var dist = Math.sqrt(dx*dx + dy*dy);
if(dist < 100){
log.innerHTML = '碰着了';
}else{
log.innerHTML = "";
}
ball_1.draw(context);
ball_2.draw(context);
},false);
}
</script>
</body>
</html>