-
Notifications
You must be signed in to change notification settings - Fork 2
/
ispositionin.html
65 lines (62 loc) · 1.33 KB
/
ispositionin.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
65
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<style type="text/css">
body{
text-align: center;
}
canvas{
margin:0 auto;
border:1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<script type="text/javascript">
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
canvas.width=600;
canvas.height=400;
var balls=[];
window.onload=function(){
drawBalls();
document.onclick=function(){
var x=event.clientX-canvas.getBoundingClientRect().left;
var y=event.clientY-canvas.getBoundingClientRect().top;
for(var i=0;i<balls.length;i++){
context.beginPath()
context.arc(balls[i].x,balls[i].y,balls[i].r,0,2*Math.PI);
if(context.isPointInPath(x,y)){
context.fillStyle="#0f0";
context.fill();
context.closePath();
}
else{
context.fillStyle="#00f";
context.fill();
context.closePath();
}
}
}
}
function drawBalls(){
context.fillStyle="#ff3300";
for(var i=0;i<5;i++){
var r=Math.random()*20+20;
var x=Math.random()*canvas.width;
var y=Math.random()*canvas.height;
context.beginPath();
context.arc(x,y,r,0,2*Math.PI);
context.fill();
context.closePath();
balls.push({
"x":x,"y":y,"r":r
})
}
}
</script>
</html>