-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolarrotation.html
76 lines (62 loc) · 1.49 KB
/
polarrotation.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
66
67
68
69
70
71
72
73
74
75
76
<!doctype html>
<html>
<head>
<style type='text/css'>
canvas {
border: 1px solid #aaa;
cursor: none;
}
</style>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript">
var context;
var width = 500;
var height = 500;
var x = -50;
var y = -50;
var goalX = 0;
var goalY = 0;
var start;
var hitDist = 20;
function draw() {
context.clearRect(0, 0, width, height);
if (Math.abs(x - goalX) < hitDist && Math.abs(y - goalY) < hitDist) {
pickGoal()
}
context.fillStyle = "blue";
context.fillRect(x - 10, y - 10, 20, 20);
context.fillStyle = "green";
context.fillRect(goalX - 10, goalY - 10, 20, 20);
}
function pickGoal() {
goalX = Math.random() * width/2 + width/4;
goalY = Math.random() * height/2 + height/4;
}
function getTime() {
return new Date().getTime() / 1000;
}
function elapsedTime() {
return getTime() - start;
}
$(document).ready(function() {
start = getTime();
pickGoal();
var canvas = $('#canvas');
context = canvas.get(0).getContext('2d');
canvas.mousemove(function(e) {
var mousex = e.pageX - canvas.offset().left;
var mousey = e.pageY - canvas.offset().top;
mousex -= width/2;
mousey -= width/2;
var angle = elapsedTime() / 50;
x = mousex * Math.cos(angle) - mousey * Math.sin(angle) + width/2;
y = mousex * Math.sin(angle) + mousey * Math.cos(angle) + height/2;
});
setInterval(draw, 10);
});
</script>
</head>
<body>
<canvas id='canvas' width=500 height=500></canvas>
</body>
</html>