-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlob.js
436 lines (340 loc) · 10.8 KB
/
Blob.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Credit for base source to Zevan Rosser
// Codepen source available at the following link:
// https://codepen.io/ZevanRosser/pen/bde8e879b344202cf06379e44f9e80b2
//
// Heavily modified by Tanish Manku - 2020
const canvas = document.getElementById("vector_canvas");
const ctx = canvas.getContext("2d");
const blobStates = {
EXPANDED: 0,
EXPANDING: 1,
REGULAR: 2,
COLLAPSING: 3
};
const blobEnergyStates = {
REST: 0,
INCREASING: 1,
DECREASING: 2,
MAXIMUM: 3
};
const scaleDuration = 750; // milliseconds
const fillColor = "#41ffc9";
const reactiveSpeedDuration = 750;
const reactivePollInterval = 16.66;
const widthBreakPoint = 768;
var anchorResizeToken = null;
class Blob {
constructor(number, sectorAngle, minDeviation) {
// use this to change the size of the screen estate to cover, in the minimum dimension
this.screenEstateCoverageV = 0.6;
this.screenEstateCoverageH = 0.6;
if (minDeviation > Math.PI * 2) {
minDeviation = Math.PI * 2;
}
// think of this as detail level
// number of conections in the `bezierSkin`
this.segments = number;
this.step = sectorAngle / this.segments;
// Stores points that are "pinned" and are points where bumps occur
this.anchors = [];
// Controls angular deviation
this.thetaOff = [];
// Calculate radius
this.updateValues();
// Controls radius of buldge
this.bumpRadius = this.baseRadius / 7;
this.halfBumpRadius = this.bumpRadius / 2;
// Just an added value to base radius
this.radiusOffset = 0;
// Keeps track of current bump radii
this.radii = [];
for (let i = 0; i < this.segments + 2; i++) {
this.anchors.push(0, 0);
this.radii.push(Math.random() * this.bumpRadius - this.halfBumpRadius);
this.thetaOff.push(Math.random() * (Math.PI * 2 - minDeviation) + minDeviation);
}
this.theta = 0;
this.thetaRamp = 0;
this.thetaRampDest = 12;
this.rampDamp = 25;
this.thetaDelta = this.getBaseThetaDelta();
// Track animation state
this.state = blobStates.REGULAR;
this.lastTimeFraction = 0;
this.currentTimeFraction = 0;
this.recordedEnergyTime = 0;
this.lastThetaFraction = 0;
this.currentThetaFraction = 0;
this.blobEnergyState = blobEnergyStates.REST;
}
shouldRefresh() {
return this.isAnimating() || (this.thetaRamp < this.thetaRampDest * 0.99);
}
isAnimating() {
return (this.state === blobStates.EXPANDING || this.state === blobStates.COLLAPSING);
}
update() {
if (this.state === blobStates.EXPANDED) {
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (this.shouldRefresh()) {
this.updateValues();
}
this.updateAnchors();
ctx.beginPath();
ctx.moveTo(0, 0);
bezierSkin(this.anchors, false);
ctx.lineTo(canvas.width, 0);
ctx.fillStyle = fillColor;
ctx.shadowBlur = 20;
ctx.shadowColor = "black";
ctx.fill();
}
updateValues() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
this.baseRadius = this.getDiagonal() * 0.4;
}
getBaseThetaDelta() {
return (this.getDiagonal() / 2500) * 0.02;
}
getMaxThetaDelta() {
return Math.min(this.getBaseThetaDelta() * 6, 0.12);
}
updateAnchors() {
this.thetaRamp += (this.thetaRampDest - this.thetaRamp) / this.rampDamp;
this.theta += this.thetaDelta;
// Recreate anchors, initially with first anchor
this.anchors = [canvas.width, this.baseRadius];
for (let i = 0; i <= this.segments + 2; i++) {
const sine = Math.sin(this.thetaOff[i] + this.theta + this.thetaRamp);
const rad = this.baseRadius + this.radiusOffset + this.radii[i] * sine;
const x = rad * Math.sin(this.step * i);
const y = rad * Math.cos(this.step * i);
this.anchors.push(canvas.width - x, y);
}
}
getDiagonal() {
return Math.hypot(canvas.width, canvas.height);
}
animate() {
switch (this.state) {
case blobStates.EXPANDING:
this.expandRadius();
break;
case blobStates.COLLAPSING:
this.collapseRadius();
break;
default:
return;
}
}
cueExpansion() {
if (this.state !== blobStates.EXPANDED && this.state !== blobStates.EXPANDING) {
this.trackTime();
this.state = blobStates.EXPANDING;
}
}
cueCollapse() {
if (this.state !== blobStates.REGULAR && this.state !== blobStates.COLLAPSING) {
this.trackTime();
this.state = blobStates.COLLAPSING;
}
}
expandRadius() {
if (this.state === blobStates.EXPANDED) {
return;
}
if (this.isMaximized()) {
this.state = blobStates.EXPANDED;
this.radiusOffset = this.getMaxRadius();
this.currentTimeFraction = 1;
return;
}
this.currentTimeFraction = this.clampTimeFraction(this.getTimeFraction() + this.lastTimeFraction);
this.radiusOffset = this.getMultiplierToOffset(this.getMultiplierFromInterpolator(this.currentTimeFraction));
}
collapseRadius() {
if (this.state === blobStates.REGULAR) {
return;
}
if (this.currentTimeFraction === 0) {
this.state = blobStates.REGULAR;
this.radiusOffset = 0;
}
this.currentTimeFraction = this.clampTimeFraction(this.lastTimeFraction - this.getTimeFraction())
this.radiusOffset = this.getMultiplierToOffset(this.getMultiplierFromInterpolator(this.currentTimeFraction));
}
trackTime() {
this.recordedTime = performance.now();
this.lastTimeFraction = this.currentTimeFraction;
}
getTimeFraction() {
let difference = performance.now() - this.recordedTime;
let timeFraction = difference / scaleDuration;
return this.clampTimeFraction(timeFraction);
}
clampTimeFraction(timeFraction) {
if (timeFraction > 1) {
return 1;
} else if (timeFraction < 0) {
return 0;
}
return timeFraction;
}
getMultiplierFromInterpolator(tf) {
return tf < 0.5
? (1 - Math.sqrt(1 - Math.pow(2 * tf, 2))) / 2
: (Math.sqrt(1 - Math.pow(-2 * tf + 2, 2)) + 1) / 2;
}
getMultiplierToOffset(multiplier) {
return (this.getMaxRadius() - this.baseRadius) * multiplier;
}
getMultiplierToThetaDelta(multiplier) {
return (this.getMaxThetaDelta() - this.getBaseThetaDelta()) * multiplier + this.getBaseThetaDelta();
}
getMaxRadius() {
return this.getDiagonal() + this.bumpRadius;
}
isMaximized() {
return this.radiusOffset >= this.getDiagonal();
}
trackEnergyTime() {
this.recordedEnergyTime = performance.now();
this.lastThetaFraction = this.currentThetaFraction;
}
getEnergyThetaFraction() {
let difference = performance.now() - this.recordedEnergyTime;
let thetaFraction = difference / reactiveSpeedDuration;
return this.clampTimeFraction(thetaFraction);
}
/* Converts given px acceleration to energy value */
reactivePx(speed) {
if (this.blobEnergyState === blobEnergyStates.INCREASING) {
return;
}
// Formula: (+ve Speed pixels / Diagonal pixels) * 100
// This is at max 1 / reactivePollInterval (0.06 at 60fps) and 0 at min
// We wanna normalize it to "0.1% screen in 1 interval" as threshold
let isAboveThreshold = Math.abs(speed) > (this.getDiagonal() * 0.001);
if (isAboveThreshold) {
this.trackEnergyTime();
this.blobEnergyState = blobEnergyStates.INCREASING;
} else if (this.blobEnergyState !== blobEnergyStates.DECREASING) {
this.trackEnergyTime();
this.blobEnergyState = blobEnergyStates.DECREASING;
}
}
energize() {
switch (this.blobEnergyState) {
case blobEnergyStates.INCREASING:
this.increaseEnergy();
break;
case blobEnergyStates.DECREASING:
this.reduceEnergy();
break;
default:
return;
}
}
reduceEnergy() {
if (this.blobEnergyState === blobEnergyStates.REST) {
return;
}
if (this.thetaDelta === this.getBaseThetaDelta() || this.currentThetaFraction < 0) {
this.blobEnergyState = blobEnergyStates.REST;
}
this.currentThetaFraction = this.clampTimeFraction(this.lastThetaFraction - this.getEnergyThetaFraction());
this.thetaDelta = this.getMultiplierToThetaDelta(this.getMultiplierFromInterpolator(this.currentThetaFraction));
}
increaseEnergy() {
if (this.blobEnergyState === blobEnergyStates.MAXIMUM) {
return;
}
if (this.currentThetaFraction === 1) {
this.blobEnergyState = blobEnergyStates.MAXIMUM;
}
this.currentThetaFraction = this.clampTimeFraction(this.lastThetaFraction + this.getEnergyThetaFraction());
this.thetaDelta = this.getMultiplierToThetaDelta(this.getMultiplierFromInterpolator(this.currentThetaFraction));
}
}
const blob = new Blob(10, Math.PI / 2, Math.PI / 2);
function commitResize() {
blob.updateValues();
anchorResizeToken = null;
}
function loop() {
blob.energize();
blob.animate();
blob.update();
window.requestAnimationFrame(loop);
}
// Repeat the animation frames
loop();
// array of xy coords, closed boolean
function bezierSkin(bez, closed = true) {
const avg = calcAvgs(bez);
const leng = bez.length;
if (closed) {
ctx.moveTo(avg[0], avg[1]);
for (let i = 2; i < leng; i += 2) {
let n = i + 1;
ctx.quadraticCurveTo(bez[i], bez[n], avg[i], avg[n]);
}
ctx.quadraticCurveTo(bez[0], bez[1], avg[0], avg[1]);
} else {
ctx.moveTo(bez[0], bez[1]);
ctx.lineTo(avg[0], avg[1]);
for (let i = 2; i < leng - 2; i += 2) {
let n = i + 1;
ctx.quadraticCurveTo(bez[i], bez[n], avg[i], avg[n]);
}
ctx.lineTo(bez[leng - 2], bez[leng - 1]);
}
}
// create anchor points by averaging the control points
function calcAvgs(p) {
const avg = [];
const leng = p.length;
let prev;
for (let i = 2; i < leng; i++) {
prev = i - 2;
avg.push((p[prev] + p[i]) / 2);
}
// close
avg.push((p[0] + p[leng - 2]) / 2, (p[1] + p[leng - 1]) / 2);
return avg;
}
export function getBlob() {
return blob;
}
/* ----------- Attach Listeners ----------- */
window.addEventListener("resize", function (event) {
if (window.innerWidth < widthBreakPoint) {
return;
}
if (anchorResizeToken != null) {
clearTimeout(anchorResizeToken);
}
anchorResizeToken = setTimeout(commitResize, 200);
}, false);
/* ----------- Attach Mouse Reaction ----------- */
var lastEvent, currentEvent;
document.onmousemove = function (event) {
currentEvent = event || window.event;
}
// Should be about 60fps
setInterval(motionReactiveHook, reactivePollInterval);
function motionReactiveHook() {
var speed = 0;
// If we have information for two intances to diff, proceed.
if (lastEvent && currentEvent) {
var movementX = Math.abs(currentEvent.screenX - lastEvent.screenX);
var movementY = Math.abs(currentEvent.screenY - lastEvent.screenY);
var movement = Math.hypot(movementX, movementY);
speed = movement / reactivePollInterval;
blob.reactivePx(speed);
}
lastEvent = currentEvent;
}