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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
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
77
78
|
+
-
+
-
+
+
-
-
+
+
-
+
-
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
-
+
-
+
-
-
-
-
+
+
-
-
-
-
-
-
+
+
-
-
-
-
+
-
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
|
// copyright (c)) 2015 jadon belezos, tom burris
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var circleX = 400;
var circleY = 250;
var circleY = 25
var mouseX = 400;
var mouseY = 250;
var close = 0;
var mass = 5;
var radius = 0;
function start()
{
function start() {
drawCircles();
var loop = setInterval(mainloop, 50);
var interval = setInterval(mainloop, 50);
}
function mainloop() {
moveTheCircles();
moveCircles();
if (close == 0) {
clearCanvas();
drawCircles();
clearCanvas();
drawCircles();
}
sleep(100);
}
function getCoordinates(e) {
mouseX = e.clientX;
mouseY = e.clientY;
document.getElementById("xycoordinates").innerHTML="Coordinates: (" + mouseX + "," + mouseY + ")";
}
function clearCoordinates() {
document.getElementById("xycoordinates").innerHTML="";
}
function clearCanvas() {
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, 801, 501);
}
function moveTheCircles() {
function moveCircles() {
var speed = .08;
radius = Math.sqrt((mass * 100) / Math.PI);
if ( mouseX > circleX ) {
var difX = mouseX - circleX;
if (difX > 5) {
if (difX > 5)
circleX = circleX + difX * speed;
}
} else {
var difX = circleX - mouseX;
if (difX > 5) {
if (difX > 5)
circleX = circleX - difX * speed;
}
}
if ( mouseY > circleY ) {
var difY = mouseY - circleY;
if (difY > 5) {
circleY = circleY + difY * speed;
if (difY > 5)
circleY = circleY + difY * speed;
close = 0;
} else if (difX < 5) {
close = 1;
}
} else {
var difY = circleY - mouseY;
if (difY > 5) {
circleY = circleY - difY * speed;
if (difY > 5)
circleY = circleY - difY * speed;
close = 0;
} else {
close = 1;
}
}
}
function drawCircles() {
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(circleX, circleY, 50, 0, 2 * Math.PI);
ctx.beginPath(); //draw the player
ctx.arc(circleX, circleY, radius, 0, 2 * Math.PI);
ctx.closePath();
ctx.lineWidth = 5;
ctx.fillStyle = 'green';
ctx.fill();
ctx.strokeStyle = 'darkgreen';
ctx.stroke();
}
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
|