-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
302 lines (269 loc) · 7.73 KB
/
script.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//Declaring some letiables and parameters
let unitVel = 100; //used to normalize pen velocity. normalvel = penvel/unitVel
let ff = 0; //Boolean. fast forward
let timeDelay = 1; //How quickly it draws lines
let endMove = 0;
// Getting html5 canvas ready
let canvas = document.getElementById("firstCanvas");
console.log(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
console.log(canvas.height, canvas.width);
let c = canvas.getContext("2d");
c.strokeStyle = "rgb(200, 0,0)";
c.lineWidth = 1;
// The below line is what resizes the graph to fit screensize in all devices.
let scaleFactor = Math.min(canvas.height / 968, canvas.width / 1260);
//More letiables. r, g, b are declared to make a gradient depending on pen velocity
let state = 2;
let std_dt = 0.005; //Standard time-step
let count = 0;
let x = -7.13;
let y = -7.11;
let z = 25.41;
let b = 0;
let g = 0;
let r = 0;
let dt = 0.005; //Actual used time-step
a = Promise.resolve(); //Starts with a resolved promise. We do .then to draw the next movement of the pen.
// Promises were painful to learn since I had never used them before.
// Button click functions
const clearScreen = () => {
c.clearRect(0, 0, canvas.width, canvas.height);
};
const ffBut = () => {
dt = std_dt / 2; //the smaller the dt, the more accurate
ff = 1; //Command to fastforward
};
const reloadScreen = () => {
endMove = 1;
location.reload(); //reloads page when you press new
clearScreen();
};
const speedUp = () => {
dt = std_dt * 1.5; //this is what makes it speed up. however, this is slightly unstable
};
const slowDown = () => {
dt = std_dt / 5;
};
const normalSpeed = () => {
dt = std_dt;
};
let normalizeX = (x) => {
return 0;
};
let normalizeY = (y) => {
return 0;
};
c.beginPath();
const command = (func, x, y, k) => {
return new Promise((resolve) => {
setTimeout(() => {
func(x, y, k);
resolve();
}, 1000);
});
};
c.moveTo(window.innerWidth / 2, window.innerHeight / 2);
let prev_pos = { x: normalizeX(y), y: normalizeY(z) };
const move = (x, y, col) => {
if ((count = 1)) {
c.beginPath();
c.strokeStyle = `rgb(${col.r}, 0, ${col.b})`;
// c.strokeStyle = `rgb(${Math.floor(Math.random()*255)}, ${Math.floor(Math.random()*255)}, ${Math.floor(Math.random()*255)})`;
count = 0;
} else {
count = 1;
}
// console.log(x, y, z);
c.moveTo(prev_pos.x, prev_pos.y);
c.lineTo(x, y);
c.stroke();
c.moveTo(x, y);
prev_pos.x = x;
prev_pos.y = y;
};
//Declare placeholder functions
let dxdt = (x, y, z) => {
return 0;
};
let dydt = (x, y, z) => {
return 0;
};
let dzdt = (x, y, z) => {
return 0;
};
//Main function
const proceed = (k) => {
for (let i = 0; i < k; i++) {
a = a
.then(() => {
if (state == 0) {
command(move, normalizeX(x), normalizeY(z), { r: r, g: g, b: b });
}
if (state == 1) {
command(
move,
normalizeX(y) * 1.86 - normalizeX(x) * 0.86,
normalizeY(z),
{ r: r, g: g, b: b }
);
}
})
.then(() => {
return new Promise((resolve) => {
//calculate rate of change
let dfxdt = dxdt(x, y, z);
let dfydt = dydt(x, y, z);
let dfzdt = dzdt(x, y, z);
//find velocity to calculate color
let vel = (dfxdt ** 2 + dfydt ** 2 + dfzdt ** 2) ** 0.5 / unitVel;
r = Math.round(240 * vel);
b = Math.round(240 * (1.5 - vel));
// x + dx <==> x + dxdt *dt
x = x + dfxdt * dt;
y = y + dfydt * dt;
z = z + dfzdt * dt;
if (ff == 1) {
//no delay set. unstable as it consumes all available processing power to speed steps
resolve();
} else {
//for stable performance, delay is set.
setTimeout(resolve, timeDelay);
}
});
});
}
};
//Depending on which attractor is called, different normalization equations, differential equations and time steps are used
const lorenz = () => {
//Starting positions for lorenz attractor
x = -7.13;
y = -7.11;
z = 25.41;
state = 0;
std_dt = 0.003;
dt = std_dt;
unitVel = 100;
normalizeX = (x) => {
return window.innerWidth / 2 + 30 * scaleFactor * x;
};
normalizeY = (y) => {
return window.innerHeight / 2 - 18 * scaleFactor * (y - 25 * scaleFactor);
};
prev_pos = { x: normalizeX(y), y: normalizeY(z) };
//Differential equations for lorenz attractor
dxdt = (x, y, z) => {
return 10 * (y - x);
};
dydt = (x, y, z) => {
return x * (28 - z) - y;
};
dzdt = (x, y, z) => {
return x * y - (8 * z) / 3;
};
proceed(Math.round(600000 * scaleFactor ** 0.2)); //Calls the main function with above delcared conditions.
};
const chen = () => {
//Starting positions for chen attractor
x = 1.96;
y = 2.04;
z = 12.51;
state = 0;
std_dt = 0.0004; //Chen attractor is more sensitive
dt = std_dt;
unitVel = 1500;
normalizeX = (x) => {
return window.innerWidth / 2 + 20 * scaleFactor * x;
};
normalizeY = (y) => {
return window.innerHeight / 2 - 20 * scaleFactor * (y - 20 * scaleFactor);
};
prev_pos = { x: normalizeX(x), y: normalizeY(z) };
//Differential equations for chen attractor
dxdt = (x, y, z) => {
return 400 * (y - x);
};
dydt = (x, y, z) => {
return -120 * x - 10 * (x * z) + 280 * y;
};
dzdt = (x, y, z) => {
return 10 * x * y - 30 * z;
};
proceed(Math.round(200000 * scaleFactor ** 0.2)); //Calls the main function with above delcared conditions.
};
const halvorsen = () => {
console.log("Doing halvorsen...");
// x= -5.48;
// y= -4.51;
// z= 1.04;
x = -1.48;
y = -1.51;
z = 2.04;
state = 1;
std_dt = 0.002; //Chen attractor is more sensitive
dt = std_dt;
unitVel = 60;
normalizeX = (t) => {
return window.innerWidth / (2 * 1.36) + 10 * scaleFactor * (t + 20);
};
normalizeY = (t) => {
return window.innerHeight / (2 * 1.36) - 20 * scaleFactor * t;
};
prev_pos = { x: normalizeX(y), y: normalizeY(z) };
let alpha = 1.4;
dxdt = (x, y, z) => {
return -(alpha * x) - 4 * y - 4 * z - y ** 2;
};
dydt = (x, y, z) => {
return -(alpha * y) - 4 * z - 4 * x - z ** 2;
};
dzdt = (x, y, z) => {
return -(alpha * z) - 4 * x - 4 * y - x ** 2;
};
proceed(Math.round(900000 * scaleFactor ** 0.2));
};
//Controls functionality of buttons
const fourWing = () => {
console.log("Doing fourWing...");
// x= -5.48;
// y= -4.51;
// z= 1.04;
x = -0.48;
y = 3.51;
z = 0.04;
state = 0;
std_dt = 0.05;
dt = std_dt;
unitVel = 1.5;
normalizeX = (t) => {
return window.innerWidth / (2 * 1.36) + 100 * scaleFactor * (t + 2);
};
normalizeY = (t) => {
return window.innerHeight / (2 * 1.36) - 100 * scaleFactor * (t - 2);
};
prev_pos = {
x: normalizeX(0.8 * x + 0.14 * y - 0.5 * z),
y: normalizeY(0.14 * x + 0.85 * y + 0.5 * z),
};
dxdt = (x, y, z) => {
return 0.2 * x + y * z;
};
dydt = (x, y, z) => {
return 0.01 * x - 0.4 * y - x * z;
};
dzdt = (x, y, z) => {
return -z - x * y;
};
proceed(Math.round(100000 * scaleFactor ** 0.2));
};
document.getElementById("but1").addEventListener("click", ffBut);
document.getElementById("but2").addEventListener("click", speedUp);
document.getElementById("but3").addEventListener("click", normalSpeed);
document.getElementById("but4").addEventListener("click", slowDown);
document.getElementById("but5").addEventListener("click", clearScreen);
document.getElementById("but6").addEventListener("click", reloadScreen);
document.getElementById("but7").addEventListener("click", chen);
document.getElementById("but8").addEventListener("click", lorenz);
document.getElementById("but9").addEventListener("click", halvorsen);
document.getElementById("but10").addEventListener("click", fourWing);