-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (112 loc) · 3.51 KB
/
Copy pathscript.js
File metadata and controls
140 lines (112 loc) · 3.51 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// USING BIGNUMBER LIB
// DOCUMENTATION: http://jsfromhell.com/classes/bignumber
// INITIALISING CANVAS
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// RESET BUTTON
function resetBtn() {
Ball0.reset()
Ball1.reset()
Ball2.reset()
ctx.clearRect(0, 0, canvas.width, canvas.height);
count = 0;
}
function apply() {
Ball0.reset()
Ball1.reset()
Ball2.reset()
ctx.clearRect(0, 0, canvas.width, canvas.height);
Ball0.mod = eval(document.getElementById("angleModBall0").value);
Ball1.mod = eval(document.getElementById("angleModBall1").value);
Ball2.mod = eval(document.getElementById("angleModBall2").value);
}
// CLASS FOR CREATING BALLS THAT ROTATE
class Ball {
constructor(color, ballRadius, radius) {
this.x0 = 400;
this.y0 = 400;
this.color = color;
this.ballRadius = ballRadius;
this.clearTime = 0;
this.angle = new BigNumber(0);
this.radius = radius;
this.angleManipulation = 1;
this.mod;
}
// CALCULATES THE NEW X/Y POSITIONS WITH THE CENTER AND ANGLE
rotate(xOrigin, yOrigin) {
var radians = (Math.PI / 180) * this.angle;
if (this.angle > 359) {this.angle = 0}
if (this.angle < -359) {this.angle = 0}
var cos = Math.cos(radians);
var sin = Math.sin(radians);
var nx = this.radius * cos + xOrigin;
var ny = this.radius * sin + yOrigin;
return [nx, ny];
}
// DRAWS BALL ONTO CANVAS
drawBall() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.ballRadius, 0, Math.PI*2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
reset() {
this.angle = 0;
}
}
// ATTACHES BALL1 TO THE COORDINATES OF BALL0 ...
function move(mod0, mod1, mod2) {
Ball0.x = Ball0.rotate(Ball0.x0, Ball0.y0)[0];
Ball0.y = Ball0.rotate(Ball0.x0, Ball0.y0)[1];
Ball1.x = Ball1.rotate(Ball0.x, Ball0.y)[0];
Ball1.y = Ball1.rotate(Ball0.x, Ball0.y)[1];
Ball2.x = Ball2.rotate(Ball1.x, Ball1.y)[0];
Ball2.y = Ball2.rotate(Ball1.x, Ball1.y)[1];
console.log([Ball0.angle, Ball1.angle, Ball2.angle])
// CHANGING ANGLE
Ball0.angle = Ball0.angle + (mod0);
Ball1.angle = Ball1.angle + (mod1);
Ball2.angle = Ball2.angle + (mod2);
}
// CREATING BALL OBJECTS
var Ball0 = new Ball("#0000cc", 5, 100);
var Ball1 = new Ball("#cc0000", 5, 100);
var Ball2 = new Ball("#00cc00", 5, 100);
var count = 0
function draw() {
// CLEAR CANVAS TO REMOVE TRACERS
if (document.getElementById("clearPaths").checked == true){
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// MAKES MOVEMENT
move(Ball0.mod, Ball1.mod, Ball2.mod);
// DRAWS ALL THE BALLS
Ball0.drawBall();
Ball1.drawBall();
Ball2.drawBall();
count++
document.getElementById("counter").textContent = count;
if (document.getElementById("continuous").checked == false) {
if (count == 360) {
return;
}
}
}
// RUNS THE FUNCTION DRAW EVERY MILLISECOND (TO CREATE FLUENCY)
running = false;
function run() {
if (running == false) {
running = true;
setInterval(function() {draw();},1);
} else {
running = false;
resetBtn()
return;
}
}
document.getElementById("angleModBall0").value = "-1";
document.getElementById("angleModBall1").value = "+1/2";
document.getElementById("angleModBall2").value = "+3/5";
console.log("Js Loaded");