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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 | /*
BSD Zero Clause License
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
window.addEventListener('load', () => {
'use strict'
class Vector {
x = 0;
y = 0;
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
add(other) {
return new Vector(this.x + other.x, this.y + other.y);
}
sub(other) {
return new Vector(this.x - other.x, this.y - other.y);
}
mul(scalar) {
return new Vector(this.x * scalar, this.y * scalar);
}
divide(scalar) {
return new Vector(this.x / scalar, this.y / scalar);
}
};
function getCirclePoint(centre, radius, angle) {
return new Vector(centre.x + radius * Math.cos(angle),
centre.y + radius * Math.sin(angle));
}
// Model a pair of spirograph wheels (or one wheel inside a ring)
class Wheels {
centre;
_boundingRadius;
_travellerRadius;
_orbitRadius;
_ratio;
multiplier;
constructor(sizeRatio, multiplier) {
this._ratio = sizeRatio;
this.multiplier = multiplier;
}
setRadius(newRadius) {
this._boundingRadius = newRadius;
this._travellerRadius = newRadius * this._ratio;
this._orbitRadius = newRadius - this._travellerRadius;
}
pointFromRotation(theta) {
const orbitAngle = theta * this._ratio * Math.PI * 2;
const innerCentre = getCirclePoint(this.centre, this._orbitRadius,
orbitAngle);
const innerAngle = (orbitAngle + (theta % 1) *
this.multiplier * Math.PI);
return getCirclePoint(innerCentre, this._travellerRadius,
innerAngle);
}
};
const canvas = document.getElementById('spirographCanvas');
const ctx = canvas.getContext('2d');
const canvasSize = new Vector(canvas.width, canvas.height);
function updateCanvasSize() {
const w = canvas.clientWidth;
const h = canvas.clientHeight;
if (canvas.width != w || canvas.height != h) {
// Only perform this assignment if required.
// Even if the value don't change, the buffer is cleared.
canvas.width = w;
canvas.height = h;
canvasSize.x = w;
canvasSize.y = h;
}
}
let curRotation = Math.random();
const MULTIPLIER_OPTIONS = [-8, -6, -4, -2, 2, 4, 6, 8];
let curHue = Math.random() * 360;
function randomWheels() {
const multiplierIndex = Math.floor(Math.random()
* MULTIPLIER_OPTIONS.length);
return new Wheels(0.3 + Math.random() * 0.178,
MULTIPLIER_OPTIONS[multiplierIndex])
}
let wheels = randomWheels();
const btnFuncs = {
clear: () => {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
},
reroll: () => {
btnFuncs.clear();
wheels = randomWheels();
},
};
const btns = document.getElementById(
'spirographCtrls').getElementsByTagName('button');
Array.prototype.forEach.call(btns, b => {
const func = btnFuncs[b.dataset?.func];
if (func) {
b.addEventListener('click', func);
}
});
const INTERPOLATE_STEPS = 16;
function draw(posDelta) {
// low alpha black fill, to fade previous frame(s)
ctx.fillStyle = "rgba(0, 0, 0, 0.007)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (posDelta.x == 0) {
return;
}
wheels.centre = canvasSize.mul(0.5);
const outerRadius = Math.min(wheels.centre.x, wheels.centre.y) * 0.9;
wheels.setRadius(outerRadius);
curHue += posDelta.y / outerRadius * 45;
while (curHue < 0) {
curHue += 360;
}
curHue %= 360;
ctx.strokeStyle = "hsl(" + (curHue).toString() + ", 70%, 50%)";
ctx.lineCap = "round";
ctx.lineWidth = Math.max(1, outerRadius / 64);
ctx.beginPath();
const startPoint = wheels.pointFromRotation(curRotation);
ctx.moveTo(startPoint.x, startPoint.y);
const rotDelta = posDelta.x / outerRadius / Math.abs(wheels.multiplier);
for (let i = 1; i <= INTERPOLATE_STEPS; i++) {
const stepRot = curRotation + rotDelta * i / INTERPOLATE_STEPS;
const stepPoint = wheels.pointFromRotation(stepRot);
ctx.lineTo(stepPoint.x, stepPoint.y);
}
ctx.stroke();
curRotation += rotDelta;
}
let penDown = false;
const MAX_VELOCITY = 2.0;
let currentTouches = [];
let curPos = new Vector(0, 0);
let prevPos = curPos;
let currentTouchId = 0;
function setPenDown(pos) {
penDown = true;
curPos = pos;
prevPos = pos;
}
function setPenUp() {
penDown = false;
}
function penMove(pos) {
curPos = pos;
if (penDown) {
}
}
function handleMouseDown(evt) {
if (evt.button == 0) {
setPenDown(new Vector(evt.clientX, evt.clientY));
}
}
function handleMouseUp(evt) {
if (evt.button == 0) {
setPenUp();
}
}
function handleMouseOut(evt) {
setPenUp();
}
function handleMouseMove(evt) {
penMove(new Vector(evt.clientX, evt.clientY));
}
function handleTouchStart(evt) {
evt.preventDefault();
let touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) {
let t = touches[i];
let pos = new Vector(t.pageX, t.pageY);
currentTouches[t.identifier] = pos;
if (!penDown) {
setPenDown(pos);
currentTouchId = t.identifier;
}
}
}
function handleTouchEnd(evt) {
evt.preventDefault();
let touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) {
delete currentTouches[touches[i].identifier];
if (touches[i].identifier == currentTouchId) {
setPenUp();
}
}
}
function handleTouchCancel(evt) {
evt.preventDefault();
let touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) {
delete currentTouches[touches[i].identifier];
if (touches[i].identifier == currentTouchId) {
setPenUp();
}
}
}
function handleTouchMove(evt) {
evt.preventDefault();
let touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) {
let t = touches[i];
let pos = new Vector(t.pageX, t.pageY);
currentTouches[t.identifier] = pos;
if (t.identifier == currentTouchId) {
penMove(pos);
}
}
}
canvas.addEventListener("mousedown", handleMouseDown);
canvas.addEventListener("mouseup", handleMouseUp);
canvas.addEventListener("mouseout", handleMouseOut);
canvas.addEventListener("mouseleave", handleMouseOut);
canvas.addEventListener("mousemove", handleMouseMove);
canvas.addEventListener("touchstart", handleTouchStart);
canvas.addEventListener("touchend", handleTouchEnd);
canvas.addEventListener("touchcancel", handleTouchCancel);
canvas.addEventListener("touchmove", handleTouchMove);
function calcPosDelta() {
let delta = curPos.sub(prevPos);
prevPos = curPos;
return delta;
}
let lastFrame = Date.now();
const velocityHistory = [new Vector(256, 0.1)];
function update() {
updateCanvasSize();
// get frame duration in seconds
const curFrame = Date.now();
const delta = (curFrame - lastFrame) / 1000
lastFrame = curFrame;
let posDelta = calcPosDelta();
if (penDown) {
// track recent swipe velocity
// so we keep spinning after the gesture if the touch ends
if (delta > 0) {
let velocity = posDelta.divide(delta);
velocityHistory.push(velocity);
if (velocityHistory.length > 3) {
velocityHistory.shift();
}
}
} else {
// if no touch, continue to spin base on previous velocity
let holdVelocity = velocityHistory.reduce(
(a, b) => a.add(b)).divide(velocityHistory.length);
posDelta = holdVelocity.mul(delta);
}
draw(posDelta);
window.requestAnimationFrame(update);
}
window.requestAnimationFrame(update);
});
|