@shazmoghaddam/orbs 0.0.0-stage → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +338 -2
- package/orb.d.ts +157 -0
- package/orb.js +1131 -0
- package/orb.min.js +44 -0
- package/package.json +70 -4
package/orb.js
ADDED
|
@@ -0,0 +1,1131 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Orbs v1.7.0 · dot-formation animations on <canvas>. Zero dependencies. MIT.
|
|
3
|
+
* https://github.com/ShazMoghaddam/orbs · Shaz Moghaddam
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* <canvas data-orb="sphere" aria-hidden="true"></canvas>
|
|
7
|
+
* <script src="orb.js" defer></script> (or inline it)
|
|
8
|
+
* npm install @shazmoghaddam/orbs, then: import Orb from '@shazmoghaddam/orbs'
|
|
9
|
+
*
|
|
10
|
+
* Shapes: sphere, wave, spikes, constellation, torus, helix, galaxy, ticks, cube, clusters,
|
|
11
|
+
* ripple, bars, atom, infinity, vortex, blob, iris, orbit, check, cross
|
|
12
|
+
*
|
|
13
|
+
* Attributes (all optional):
|
|
14
|
+
* data-orb first shape (default sphere)
|
|
15
|
+
* data-points number of dots (default 180, or 320 with lines)
|
|
16
|
+
* data-speed base rotation, radians/second (default 0.35)
|
|
17
|
+
* data-dot dot radius in CSS px (default 1.2)
|
|
18
|
+
* data-cycle ms between auto-morphs, 0 = off (default 0)
|
|
19
|
+
* data-shapes comma list to cycle through (default all except check and cross)
|
|
20
|
+
* data-hover "morph" to change shape on hover/tap
|
|
21
|
+
* data-click "morph" to change shape only on click/tap (put the canvas in a <button> for keyboard users)
|
|
22
|
+
* data-theme-morph morph when <body data-theme> changes
|
|
23
|
+
* data-style "dots" or "lines" (glowing vertical slices) (default dots)
|
|
24
|
+
* data-line line thickness in CSS px, for slices and connecting lines
|
|
25
|
+
* data-glow soft glow behind the shape; a number sets its size in px (default 8)
|
|
26
|
+
* data-reflect faded mirror image underneath; a number sets its opacity (default 0.35)
|
|
27
|
+
* data-audio CSS selector of an <audio> or <video> to react to once it plays
|
|
28
|
+
* data-audio-amount how strongly it reacts (default 1)
|
|
29
|
+
* data-tilt extra tilt in degrees, -90 to 90 (default 0)
|
|
30
|
+
* data-state a named state: idle, listening, thinking, working, speaking,
|
|
31
|
+
* active, loading, success, error. Change it later to switch.
|
|
32
|
+
* data-follow lean towards the pointer
|
|
33
|
+
* data-scroll-tilt tilt as the orb scrolls through the page
|
|
34
|
+
*
|
|
35
|
+
* Colour comes from the canvas's CSS `color` and fades on theme change.
|
|
36
|
+
* JS: var o = new Orb(canvas, {...}); o.morph('torus'); o.next(); o.destroy();
|
|
37
|
+
* o.listen(audioEl | mediaStream | audioNode | 'mic'); o.unlisten();
|
|
38
|
+
* o.set({ style: 'lines', glow: 10, speed: 0.6 }); change settings while it runs
|
|
39
|
+
* canvas.addEventListener('orb:morph', e => e.detail.shape); fires on every morph
|
|
40
|
+
* Orb.init(root); start any <canvas data-orb> added after the page loaded
|
|
41
|
+
* o.state('thinking'); named states pick a shape and speed; add your own to Orb.states
|
|
42
|
+
* Orb.define('myshape', { build: function (n) { return { pts: [...] }; } });
|
|
43
|
+
*/
|
|
44
|
+
(function (global) {
|
|
45
|
+
'use strict';
|
|
46
|
+
|
|
47
|
+
// Server rendering (Next.js, Nuxt and the like) has no page to draw on. Export a stand-in so the
|
|
48
|
+
// import is safe there; create orbs in the browser, for example inside useEffect or onMounted.
|
|
49
|
+
if (!global || !global.document) {
|
|
50
|
+
var Stub = function () { throw new Error('Orbs draws on a <canvas>, so create it in the browser, not during server rendering.'); };
|
|
51
|
+
Stub.shapes = {}; Stub.states = {};
|
|
52
|
+
Stub.define = Stub.init = function () {};
|
|
53
|
+
Stub.audioContext = function () { return null; };
|
|
54
|
+
if (typeof module === 'object' && module.exports) module.exports = Stub;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
var TAU = Math.PI * 2;
|
|
59
|
+
var GOLDEN = Math.PI * (3 - Math.sqrt(5));
|
|
60
|
+
var reduceMQ = global.matchMedia ? global.matchMedia('(prefers-reduced-motion: reduce)') : { matches: false };
|
|
61
|
+
|
|
62
|
+
/* ── Small helpers ── */
|
|
63
|
+
function sample(pts, n) { // n evenly spaced items, so every shape has the same count
|
|
64
|
+
var out = [];
|
|
65
|
+
for (var i = 0; i < n; i++) out.push(pts[Math.floor(i * pts.length / n)].slice());
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function rng(seed) { // seeded, so shapes look the same on every load
|
|
69
|
+
var s = seed;
|
|
70
|
+
return function () { s = (s * 16807) % 2147483647; return s / 2147483647; };
|
|
71
|
+
}
|
|
72
|
+
function gauss(r) { return (r() + r() + r() - 1.5) / 1.5; } // soft bell, range -1..1
|
|
73
|
+
function fib(n) {
|
|
74
|
+
var pts = [];
|
|
75
|
+
for (var i = 0; i < n; i++) {
|
|
76
|
+
var y = n > 1 ? 1 - (i / (n - 1)) * 2 : 0, k = Math.sqrt(1 - y * y), t = GOLDEN * i; // one point sits at the centre line
|
|
77
|
+
pts.push([Math.cos(t) * k, y, Math.sin(t) * k]);
|
|
78
|
+
}
|
|
79
|
+
return pts;
|
|
80
|
+
}
|
|
81
|
+
function easeInOut(t) { return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; }
|
|
82
|
+
function parseRGB(str) {
|
|
83
|
+
var m = str && str.match(/[\d.]+/g);
|
|
84
|
+
return m ? [+m[0], +m[1], +m[2]] : [222, 218, 210];
|
|
85
|
+
}
|
|
86
|
+
function amount(v, def) { // attribute present or true -> def; a number -> that; absent, false or 0 -> off
|
|
87
|
+
if (v == null || v === false || v === 'false') return 0;
|
|
88
|
+
if (v === true || v === '') return def;
|
|
89
|
+
return isNaN(+v) ? def : +v;
|
|
90
|
+
}
|
|
91
|
+
function val(v, i, d, t, p) { return typeof v === 'function' ? v(i, d, t, p) : (v == null ? 1 : v); }
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* ── Shapes ──
|
|
95
|
+
* build(n) -> { pts:[[x,y,z]...], meta?, edges?:[[i,j]...], ... } positions inside the unit sphere
|
|
96
|
+
* Optional per-shape character:
|
|
97
|
+
* tilt, spin camera tilt (rad) and rotation multiplier
|
|
98
|
+
* deform(i,d,t,p,o) live motion: write a displaced copy of p into o
|
|
99
|
+
* dash(i,d,t,p) >0 draws a radial stroke of that length instead of a dot
|
|
100
|
+
* size, alpha number or function(i,d,t,p) multiplier per dot
|
|
101
|
+
* edgeAlpha opacity of the lines listed in edges
|
|
102
|
+
* face turn to face the viewer instead of spinning (for flat marks)
|
|
103
|
+
* inCycle false keeps the shape out of the default cycle
|
|
104
|
+
* audio(i,d,A,p,o) react to sound: write a moved copy of p into o. A has bands[16] and level, 0..1
|
|
105
|
+
* bigDots size is a percentage of the orb's radius rather than pixels, for large soft dots
|
|
106
|
+
* lineGap in the lines style, the widest gap (in radii) still joined into one line (default 0.45)
|
|
107
|
+
*/
|
|
108
|
+
var SHAPES = {};
|
|
109
|
+
function define(name, def) { SHAPES[name] = def; }
|
|
110
|
+
|
|
111
|
+
// The classic: even dots on a sphere.
|
|
112
|
+
define('sphere', { build: function (n) { return { pts: fib(n) }; } });
|
|
113
|
+
|
|
114
|
+
// Liquid surface, like a voice assistant listening.
|
|
115
|
+
define('wave', {
|
|
116
|
+
build: function (n) { return { pts: fib(n) }; },
|
|
117
|
+
deform: function (i, d, t, p, o) {
|
|
118
|
+
var k = 1 + 0.17 * Math.sin(p[1] * 3.2 + t * 2.1) * Math.cos(3 * Math.atan2(p[2], p[0]) + t * 1.3)
|
|
119
|
+
+ 0.04 * Math.sin(t * 1.8);
|
|
120
|
+
o[0] = p[0] * k; o[1] = p[1] * k; o[2] = p[2] * k;
|
|
121
|
+
},
|
|
122
|
+
alpha: function (i, d, t, p) {
|
|
123
|
+
return 0.55 + 0.45 * (0.5 + 0.5 * Math.sin(p[1] * 3.2 + t * 2.1) * Math.cos(3 * Math.atan2(p[2], p[0]) + t * 1.3));
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Every dot becomes a spike whose length ripples across the surface.
|
|
128
|
+
define('spikes', {
|
|
129
|
+
build: function (n) { return { pts: fib(n) }; },
|
|
130
|
+
size: 0.75,
|
|
131
|
+
dash: function (i, d, t, p) {
|
|
132
|
+
var s = (Math.sin(p[0] * 3.1 + t * 2.3) + Math.sin(p[1] * 4.3 - t * 1.7) + Math.sin(p[2] * 3.7 + t * 1.1)) / 3;
|
|
133
|
+
var v = (s + 1) / 2;
|
|
134
|
+
return 0.02 + 0.22 * v * v * v;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// A network graph: bright nodes, each linked to its two nearest neighbours, twinkling.
|
|
139
|
+
define('constellation', {
|
|
140
|
+
build: function (n) {
|
|
141
|
+
var r = rng(11), slots = Math.max(16, Math.round(n / 7)), nodes = [], pts = [], meta = [], rep = [];
|
|
142
|
+
for (var s = 0; s < slots; s++) {
|
|
143
|
+
var u = r() * 2 - 1, a = r() * TAU, k = Math.sqrt(1 - u * u), rad = 0.62 + r() * 0.38;
|
|
144
|
+
nodes.push([Math.cos(a) * k * rad, u * rad, Math.sin(a) * k * rad]);
|
|
145
|
+
meta.push(r() * TAU);
|
|
146
|
+
}
|
|
147
|
+
for (var i = 0; i < n; i++) { // several dots stack on each node, so nodes read as stars
|
|
148
|
+
var q = Math.floor(i * slots / n);
|
|
149
|
+
if (rep[q] === undefined) rep[q] = i;
|
|
150
|
+
pts.push(nodes[q].slice());
|
|
151
|
+
}
|
|
152
|
+
var edges = [], seen = {};
|
|
153
|
+
for (s = 0; s < slots; s++) {
|
|
154
|
+
var dist = [];
|
|
155
|
+
for (var j = 0; j < slots; j++) if (j !== s) {
|
|
156
|
+
var dx = nodes[s][0] - nodes[j][0], dy = nodes[s][1] - nodes[j][1], dz = nodes[s][2] - nodes[j][2];
|
|
157
|
+
dist.push([dx * dx + dy * dy + dz * dz, j]);
|
|
158
|
+
}
|
|
159
|
+
dist.sort(function (a, b) { return a[0] - b[0]; });
|
|
160
|
+
for (var e = 0; e < 2; e++) {
|
|
161
|
+
var o = dist[e][1], key = Math.min(s, o) + '-' + Math.max(s, o);
|
|
162
|
+
if (!seen[key]) { seen[key] = 1; edges.push([rep[s], rep[o]]); }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return { pts: pts, edges: edges, phase: meta, slots: slots, n: n };
|
|
166
|
+
},
|
|
167
|
+
size: 1.25,
|
|
168
|
+
edgeAlpha: 0.4,
|
|
169
|
+
alpha: function (i, d, t) { return 0.45 + 0.55 * (0.5 + 0.5 * Math.sin(t * 2.2 + d.phase[Math.floor(i * d.slots / d.n)])); }
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// A donut seen almost face-on, so in the hero it still reads as an "o". Its surface rolls.
|
|
173
|
+
define('torus', {
|
|
174
|
+
tilt: 1.1,
|
|
175
|
+
spin: 0.6,
|
|
176
|
+
build: function (n) {
|
|
177
|
+
// Full rings only, so the surface has no gaps; spare dots double up on the first ring.
|
|
178
|
+
var rows = Math.max(6, Math.round(Math.sqrt(n / 4))), cols = Math.max(3, Math.floor(n / rows)), pts = [];
|
|
179
|
+
for (var a = 0; a < cols; a++) for (var b = 0; b < rows; b++) {
|
|
180
|
+
var u = a / cols * TAU, v = b / rows * TAU + a * 0.25;
|
|
181
|
+
pts.push([(0.68 + 0.27 * Math.cos(v)) * Math.cos(u), 0.27 * Math.sin(v), (0.68 + 0.27 * Math.cos(v)) * Math.sin(u)]);
|
|
182
|
+
}
|
|
183
|
+
for (var i = 0; pts.length < n; i++) pts.push(pts[i % rows].slice());
|
|
184
|
+
return { pts: pts.slice(0, n) };
|
|
185
|
+
},
|
|
186
|
+
deform: function (i, d, t, p, o) { // rotate each dot around the tube, so the surface flows inside-out
|
|
187
|
+
var u = Math.atan2(p[2], p[0]), cu = Math.cos(u), su = Math.sin(u);
|
|
188
|
+
var rad = p[0] * cu + p[2] * su - 0.68, a = t * 1.1, ca = Math.cos(a), sa = Math.sin(a);
|
|
189
|
+
var r2 = rad * ca - p[1] * sa, y2 = rad * sa + p[1] * ca;
|
|
190
|
+
o[0] = (0.68 + r2) * cu; o[1] = y2; o[2] = (0.68 + r2) * su;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Double helix with rungs, side-on and turning faster.
|
|
195
|
+
define('helix', {
|
|
196
|
+
tilt: 0.12,
|
|
197
|
+
spin: 1.7,
|
|
198
|
+
edgeAlpha: 0.5,
|
|
199
|
+
build: function (n) {
|
|
200
|
+
var pts = [], edges = [], half = Math.max(2, Math.floor(n / 2));
|
|
201
|
+
for (var i = 0; i < n; i++) {
|
|
202
|
+
var k = Math.floor(i / 2), s = i % 2, f = Math.min(1, k / (half - 1));
|
|
203
|
+
var a = f * TAU * 1.6 + s * Math.PI;
|
|
204
|
+
pts.push([Math.cos(a) * 0.42, f * 1.8 - 0.9, Math.sin(a) * 0.42]);
|
|
205
|
+
if (s === 1 && k % 3 === 0) edges.push([i - 1, i]);
|
|
206
|
+
}
|
|
207
|
+
return { pts: pts, edges: edges };
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Three spiral arms, dense bright core, arms gently swaying.
|
|
212
|
+
define('galaxy', {
|
|
213
|
+
tilt: 1.0,
|
|
214
|
+
spin: 0.7,
|
|
215
|
+
build: function (n) {
|
|
216
|
+
var r = rng(5), pts = [], dist = [];
|
|
217
|
+
for (var i = 0; i < n; i++) {
|
|
218
|
+
var d = Math.pow(r(), 0.65) * 0.95, a = (i % 3) * TAU / 3 + d * 4.2 + gauss(r) * 0.4 * (1 - d * 0.4);
|
|
219
|
+
pts.push([Math.cos(a) * d, gauss(r) * 0.07 * (1 - d), Math.sin(a) * d]);
|
|
220
|
+
dist.push(d);
|
|
221
|
+
}
|
|
222
|
+
return { pts: pts, dist: dist };
|
|
223
|
+
},
|
|
224
|
+
size: function (i, d) { return 1.5 - d.dist[i] * 0.9; },
|
|
225
|
+
deform: function (i, d, t, p, o) {
|
|
226
|
+
var a = 0.35 * Math.sin(t * 0.8) * (1 - d.dist[i]), c = Math.cos(a), s = Math.sin(a);
|
|
227
|
+
o[0] = p[0] * c - p[2] * s; o[1] = p[1]; o[2] = p[0] * s + p[2] * c;
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// Loader ring: short radial dashes, a bright pulse chasing round.
|
|
232
|
+
define('ticks', {
|
|
233
|
+
tilt: 1.42,
|
|
234
|
+
spin: 0.25,
|
|
235
|
+
build: function (n) {
|
|
236
|
+
var slots = Math.max(36, Math.min(64, Math.round(n / 3))), pts = [], ang = [];
|
|
237
|
+
for (var i = 0; i < n; i++) {
|
|
238
|
+
var a = Math.floor(i * slots / n) / slots * TAU;
|
|
239
|
+
pts.push([Math.cos(a) * 0.8, 0, Math.sin(a) * 0.8]);
|
|
240
|
+
ang.push(a);
|
|
241
|
+
}
|
|
242
|
+
return { pts: pts, ang: ang };
|
|
243
|
+
},
|
|
244
|
+
size: 0.7,
|
|
245
|
+
dash: function (i, d, t) { var w = 0.5 + 0.5 * Math.sin(d.ang[i] * 2 - t * 3); return 0.05 + 0.2 * w * w * w * w; },
|
|
246
|
+
alpha: function (i, d, t) { var w = 0.5 + 0.5 * Math.sin(d.ang[i] * 2 - t * 3); return 0.3 + 0.7 * w; }
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Wireframe cube made of dots: the one hard-edged shape, which makes the morphs into it satisfying.
|
|
250
|
+
// Every edge gets the same number of evenly spaced dots; any spare dots double up on the corners.
|
|
251
|
+
define('cube', {
|
|
252
|
+
tilt: 0.62,
|
|
253
|
+
spin: 0.8,
|
|
254
|
+
build: function (n) {
|
|
255
|
+
var c = 0.55, V = [], E = [], pts = [];
|
|
256
|
+
for (var x = -1; x <= 1; x += 2) for (var y = -1; y <= 1; y += 2) for (var z = -1; z <= 1; z += 2) V.push([x * c, y * c, z * c]);
|
|
257
|
+
for (var a = 0; a < 8; a++) for (var b = a + 1; b < 8; b++) {
|
|
258
|
+
var diff = (V[a][0] !== V[b][0]) + (V[a][1] !== V[b][1]) + (V[a][2] !== V[b][2]);
|
|
259
|
+
if (diff === 1) E.push([V[a], V[b]]);
|
|
260
|
+
}
|
|
261
|
+
var k = Math.max(1, Math.floor((n - 8) / 12)); // dots between corners on each edge
|
|
262
|
+
for (var i = 0; i < 8; i++) pts.push(V[i].slice());
|
|
263
|
+
E.forEach(function (e) {
|
|
264
|
+
for (var j = 1; j <= k; j++) {
|
|
265
|
+
var f = j / (k + 1);
|
|
266
|
+
pts.push([e[0][0] + (e[1][0] - e[0][0]) * f, e[0][1] + (e[1][1] - e[0][1]) * f, e[0][2] + (e[1][2] - e[0][2]) * f]);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
for (i = 0; pts.length < n; i++) pts.push(V[i % 8].slice());
|
|
270
|
+
return { pts: pts.slice(0, n) };
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// k-means, literally: four clusters breathing, members linked to their centroid.
|
|
275
|
+
define('clusters', {
|
|
276
|
+
spin: 0.6,
|
|
277
|
+
edgeAlpha: 0.24,
|
|
278
|
+
build: function (n) {
|
|
279
|
+
var r = rng(23), C = [[0.55, 0.42, 0.1], [-0.6, 0.28, -0.25], [0.08, -0.58, 0.45], [-0.15, -0.2, -0.65]];
|
|
280
|
+
var pts = [], cl = [], edges = [];
|
|
281
|
+
for (var i = 0; i < n; i++) {
|
|
282
|
+
var c = i % 4, q = C[c];
|
|
283
|
+
if (i < 4) pts.push(q.slice());
|
|
284
|
+
else {
|
|
285
|
+
pts.push([q[0] + gauss(r) * 0.42, q[1] + gauss(r) * 0.42, q[2] + gauss(r) * 0.42]);
|
|
286
|
+
if (i % 2 === 0) edges.push([c, i]);
|
|
287
|
+
}
|
|
288
|
+
cl.push(c);
|
|
289
|
+
}
|
|
290
|
+
return { pts: pts, edges: edges, cl: cl, C: C };
|
|
291
|
+
},
|
|
292
|
+
size: function (i) { return i < 4 ? 2.3 : 0.9; },
|
|
293
|
+
deform: function (i, d, t, p, o) {
|
|
294
|
+
var c = d.C[d.cl[i]], k = 1 + 0.22 * Math.sin(t * 1.6 + d.cl[i] * 1.7);
|
|
295
|
+
o[0] = c[0] + (p[0] - c[0]) * k; o[1] = c[1] + (p[1] - c[1]) * k; o[2] = c[2] + (p[2] - c[2]) * k;
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Listening: rings spread out from the centre and fade at the edge, like sound on water.
|
|
300
|
+
define('ripple', {
|
|
301
|
+
tilt: 1.05,
|
|
302
|
+
spin: 0.15,
|
|
303
|
+
build: function (n) {
|
|
304
|
+
var rings = 6, per = n / rings, pts = [], f = [], a = [];
|
|
305
|
+
for (var i = 0; i < n; i++) {
|
|
306
|
+
var r = Math.floor(i / per), ang = (i - r * per) / per * TAU + r, fr = r / rings;
|
|
307
|
+
pts.push([Math.cos(ang) * fr * 0.95, 0, Math.sin(ang) * fr * 0.95]);
|
|
308
|
+
f.push(fr); a.push(ang);
|
|
309
|
+
}
|
|
310
|
+
return { pts: pts, f: f, a: a };
|
|
311
|
+
},
|
|
312
|
+
deform: function (i, d, t, p, o) {
|
|
313
|
+
var r = ((d.f[i] + t * 0.16) % 1) * 0.95;
|
|
314
|
+
o[0] = Math.cos(d.a[i]) * r; o[1] = 0; o[2] = Math.sin(d.a[i]) * r;
|
|
315
|
+
},
|
|
316
|
+
alpha: function (i, d, t) { return 1.1 * Math.sin(((d.f[i] + t * 0.16) % 1) * Math.PI); },
|
|
317
|
+
size: function (i, d, t) { return 0.7 + 0.6 * ((d.f[i] + t * 0.16) % 1); }
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Speaking: columns of dots rising and falling, tallest in the middle.
|
|
321
|
+
define('bars', {
|
|
322
|
+
face: true,
|
|
323
|
+
tilt: 0,
|
|
324
|
+
alpha: 1.6,
|
|
325
|
+
build: function (n) {
|
|
326
|
+
var B = 11, pts = [], bar = [], fy = [];
|
|
327
|
+
for (var i = 0; i < n; i++) { // spread dots evenly across bars, so none double up
|
|
328
|
+
var b = Math.floor(i * B / n), first = Math.ceil(b * n / B), count = Math.ceil((b + 1) * n / B) - first;
|
|
329
|
+
var y = count > 1 ? (i - first) / (count - 1) * 2 - 1 : 0;
|
|
330
|
+
pts.push([(b - (B - 1) / 2) * 0.15, y * 0.6, 0]);
|
|
331
|
+
bar.push(b); fy.push(y);
|
|
332
|
+
}
|
|
333
|
+
return { pts: pts, bar: bar, fy: fy, mid: (B - 1) / 2 };
|
|
334
|
+
},
|
|
335
|
+
deform: function (i, d, t, p, o) {
|
|
336
|
+
var b = d.bar[i], v = 0.5 + 0.5 * Math.sin(t * 3.3 + b * 1.9) * Math.sin(t * 1.4 + b * 0.7);
|
|
337
|
+
var h = 0.1 + 0.7 * v * (1 - Math.abs(b - d.mid) / (d.mid + 2));
|
|
338
|
+
o[0] = p[0]; o[1] = d.fy[i] * h; o[2] = p[2];
|
|
339
|
+
},
|
|
340
|
+
audio: function (i, d, A, p, o) { // low notes in the middle, high notes at the edges
|
|
341
|
+
var b = d.bar[i], k = Math.round(Math.abs(b - d.mid) / d.mid * 11);
|
|
342
|
+
var h = 0.06 + 0.86 * A.bands[k] * (1 - Math.abs(b - d.mid) / (d.mid * 4));
|
|
343
|
+
o[0] = p[0]; o[1] = d.fy[i] * h; o[2] = p[2];
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Atom: a small nucleus with three tilted orbits, each carrying a bright electron.
|
|
348
|
+
define('atom', {
|
|
349
|
+
tilt: 0.45,
|
|
350
|
+
spin: 0.5,
|
|
351
|
+
build: function (n) {
|
|
352
|
+
var core = Math.max(8, Math.round(n * 0.14)), rest = n - core, per = Math.ceil(rest / 3), phi = 1.2;
|
|
353
|
+
var pts = fib(core).map(function (p) { return [p[0] * 0.13, p[1] * 0.13, p[2] * 0.13]; });
|
|
354
|
+
var orbit = pts.map(function () { return -1; }), ang = pts.map(function () { return 0; });
|
|
355
|
+
for (var i = 0; i < rest; i++) {
|
|
356
|
+
var o = i % 3, a = Math.floor(i / 3) / per * TAU, rot = o * Math.PI / 3;
|
|
357
|
+
var ux = Math.cos(rot), uz = -Math.sin(rot); // orbit axes,
|
|
358
|
+
var vx = Math.sin(phi) * Math.sin(rot), vy = Math.cos(phi), vz = Math.sin(phi) * Math.cos(rot); // turned 60° apart
|
|
359
|
+
pts.push([(Math.cos(a) * ux + Math.sin(a) * vx) * 0.88, Math.sin(a) * vy * 0.88, (Math.cos(a) * uz + Math.sin(a) * vz) * 0.88]);
|
|
360
|
+
orbit.push(o); ang.push(a);
|
|
361
|
+
}
|
|
362
|
+
return { pts: pts, orbit: orbit, ang: ang };
|
|
363
|
+
},
|
|
364
|
+
size: function (i, d, t) {
|
|
365
|
+
if (d.orbit[i] < 0) return 1.5;
|
|
366
|
+
return 0.6 + 1.6 * Math.pow(0.5 + 0.5 * Math.cos(d.ang[i] - t * 2.2 - d.orbit[i] * 2.1), 14);
|
|
367
|
+
},
|
|
368
|
+
alpha: function (i, d, t) {
|
|
369
|
+
if (d.orbit[i] < 0) return 1;
|
|
370
|
+
return 0.5 + 0.9 * Math.pow(0.5 + 0.5 * Math.cos(d.ang[i] - t * 2.2 - d.orbit[i] * 2.1), 8);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
// Flat marks (infinity, check, cross) are drawn as clean single strokes of evenly spaced dots.
|
|
375
|
+
// stroke() spreads n dots along a path of [x, y] points by length; y runs down the screen.
|
|
376
|
+
function stroke(path, n) {
|
|
377
|
+
var lens = [], total = 0, pts = [], u = [], i;
|
|
378
|
+
for (i = 1; i < path.length; i++) {
|
|
379
|
+
var l = Math.sqrt(Math.pow(path[i][0] - path[i - 1][0], 2) + Math.pow(path[i][1] - path[i - 1][1], 2));
|
|
380
|
+
lens.push(l); total += l;
|
|
381
|
+
}
|
|
382
|
+
for (i = 0; i < n; i++) {
|
|
383
|
+
var f = n > 1 ? i / (n - 1) : 0, d = f * total, s = 0;
|
|
384
|
+
while (s < lens.length - 1 && d > lens[s]) { d -= lens[s]; s++; }
|
|
385
|
+
var a = path[s], b = path[s + 1], q = lens[s] ? Math.min(1, d / lens[s]) : 0;
|
|
386
|
+
pts.push([a[0] + (b[0] - a[0]) * q, a[1] + (b[1] - a[1]) * q, 0]);
|
|
387
|
+
u.push(f);
|
|
388
|
+
}
|
|
389
|
+
return { pts: pts, u: u };
|
|
390
|
+
}
|
|
391
|
+
function sway(i, d, t, p, o) { // a slight turn, so flat marks still feel alive
|
|
392
|
+
var a = 0.12 * Math.sin(t * 1.1), c = Math.cos(a), s = Math.sin(a);
|
|
393
|
+
o[0] = p[0] * c - p[2] * s; o[1] = p[1]; o[2] = p[0] * s + p[2] * c;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Loader: a figure of eight with a bright pulse running round it.
|
|
397
|
+
define('infinity', {
|
|
398
|
+
face: true,
|
|
399
|
+
tilt: 0,
|
|
400
|
+
build: function (n) {
|
|
401
|
+
var path = [];
|
|
402
|
+
for (var j = 0; j <= 160; j++) {
|
|
403
|
+
var s = j / 160 * TAU, den = 1 + Math.sin(s) * Math.sin(s);
|
|
404
|
+
path.push([0.9 * Math.cos(s) / den, 0.9 * Math.sin(s) * Math.cos(s) / den]);
|
|
405
|
+
}
|
|
406
|
+
return stroke(path, n);
|
|
407
|
+
},
|
|
408
|
+
deform: sway,
|
|
409
|
+
size: function (i, d, t) { return 0.8 + 0.9 * Math.pow(0.5 + 0.5 * Math.cos((d.u[i] - t * 0.35) * TAU), 6); },
|
|
410
|
+
alpha: function (i, d, t) { return 0.5 + 1.2 * Math.pow(0.5 + 0.5 * Math.cos((d.u[i] - t * 0.35) * TAU), 6); }
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// Thinking: dots spiral inwards and down a funnel, turning faster near the centre.
|
|
414
|
+
define('vortex', {
|
|
415
|
+
tilt: 0.75,
|
|
416
|
+
spin: 0.15,
|
|
417
|
+
build: function (n) {
|
|
418
|
+
var pts = [], f = [], a = [];
|
|
419
|
+
for (var i = 0; i < n; i++) {
|
|
420
|
+
var fr = (i * 0.618034) % 1, arm = i % 3;
|
|
421
|
+
f.push(fr); a.push(arm * TAU / 3 + fr * TAU * 1.6);
|
|
422
|
+
var r = 0.95 * (1 - fr) + 0.05;
|
|
423
|
+
pts.push([Math.cos(a[i]) * r, fr * fr * 0.7 - 0.25, Math.sin(a[i]) * r]);
|
|
424
|
+
}
|
|
425
|
+
return { pts: pts, f: f, a: a };
|
|
426
|
+
},
|
|
427
|
+
deform: function (i, d, t, p, o) {
|
|
428
|
+
var fr = (d.f[i] + t * 0.11) % 1, r = 0.95 * (1 - fr) + 0.05;
|
|
429
|
+
var ang = d.a[i] + (fr - d.f[i]) * TAU * 1.6 + t * 0.9; // three arms turn together; dots slide inwards along them
|
|
430
|
+
o[0] = Math.cos(ang) * r; o[1] = fr * fr * 0.7 - 0.25; o[2] = Math.sin(ang) * r;
|
|
431
|
+
},
|
|
432
|
+
alpha: function (i, d, t) { return 1.15 * Math.sin(((d.f[i] + t * 0.11) % 1) * Math.PI); },
|
|
433
|
+
size: function (i, d, t) { return 1.25 - 0.55 * ((d.f[i] + t * 0.11) % 1); }
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
// Active: a sphere whose surface wobbles slowly, like a drop of liquid.
|
|
437
|
+
define('blob', {
|
|
438
|
+
spin: 0.6,
|
|
439
|
+
build: function (n) { return { pts: fib(n) }; },
|
|
440
|
+
deform: function (i, d, t, p, o) {
|
|
441
|
+
var x = p[0], y = p[1], z = p[2], l = Math.sqrt(x * x + y * y + z * z) || 1;
|
|
442
|
+
x /= l; y /= l; z /= l;
|
|
443
|
+
var w = Math.sin(x * 2.1 + t * 1.1) + Math.sin(y * 2.4 - t * 0.9) + Math.sin(z * 1.8 + t * 0.75) + Math.sin((x - z) * 1.5 + t * 0.55);
|
|
444
|
+
var k = 0.78 + 0.11 * w;
|
|
445
|
+
o[0] = x * k; o[1] = y * k; o[2] = z * k;
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// Listening: an eye that faces you. Radial fibres, a pupil that widens and narrows,
|
|
450
|
+
// a slow glance around and a blink now and then.
|
|
451
|
+
define('iris', {
|
|
452
|
+
face: true,
|
|
453
|
+
tilt: 0,
|
|
454
|
+
alpha: 1.5,
|
|
455
|
+
lineGap: 0.3,
|
|
456
|
+
build: function (n) {
|
|
457
|
+
var rim = Math.round(n * 0.22), pupil = Math.round(n * 0.18), fibres = n - rim - pupil;
|
|
458
|
+
var spokes = Math.max(6, Math.round(Math.sqrt(fibres * 4))), per = fibres / spokes;
|
|
459
|
+
var pts = [], kind = [], ang = [], f = [], i;
|
|
460
|
+
for (i = 0; i < rim; i++) { var a = i / rim * TAU; pts.push([Math.cos(a) * 0.86, Math.sin(a) * 0.86, 0]); kind.push(0); ang.push(a); f.push(1); }
|
|
461
|
+
for (i = 0; i < pupil; i++) { var b = i / pupil * TAU; pts.push([Math.cos(b) * 0.24, Math.sin(b) * 0.24, 0]); kind.push(1); ang.push(b); f.push(0); }
|
|
462
|
+
for (i = 0; i < fibres; i++) {
|
|
463
|
+
var s = Math.floor(i / per), c = s / spokes * TAU + (s % 2) * 0.08, q = (i - s * per) / per;
|
|
464
|
+
var r = 0.3 + q * 0.46;
|
|
465
|
+
pts.push([Math.cos(c) * r, Math.sin(c) * r, 0]); kind.push(2); ang.push(c); f.push(q);
|
|
466
|
+
}
|
|
467
|
+
return { pts: pts, kind: kind, ang: ang, f: f };
|
|
468
|
+
},
|
|
469
|
+
deform: function (i, d, t, p, o) {
|
|
470
|
+
var pr = 0.24 + 0.07 * Math.sin(t * 0.9) + 0.03 * Math.sin(t * 2.3); // pupil breathes
|
|
471
|
+
var r = d.kind[i] === 0 ? 0.86 : d.kind[i] === 1 ? pr : pr + 0.06 + d.f[i] * (0.76 - pr - 0.06);
|
|
472
|
+
var lx = d.kind[i] === 0 ? 0 : 0.07 * Math.sin(t * 0.37) + 0.03 * Math.sin(t * 1.1); // glance
|
|
473
|
+
var ly = d.kind[i] === 0 ? 0 : 0.04 * Math.sin(t * 0.29 + 1);
|
|
474
|
+
var ph = t % 5.5, lid = ph > 5.2 ? 1 - 0.92 * Math.sin((ph - 5.2) / 0.3 * Math.PI) : 1; // blink
|
|
475
|
+
o[0] = Math.cos(d.ang[i]) * r + lx; o[1] = (Math.sin(d.ang[i]) * r + ly) * lid; o[2] = 0;
|
|
476
|
+
},
|
|
477
|
+
size: function (i, d) { return d.kind[i] === 1 ? 1.25 : d.kind[i] === 0 ? 0.85 : 0.75 + 0.35 * (1 - d.f[i]); }
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
// Ambient: four soft, large dots of different sizes, each circling on its own tilted path.
|
|
481
|
+
define('orbit', {
|
|
482
|
+
tilt: 0.5,
|
|
483
|
+
spin: 0.25,
|
|
484
|
+
build: function (n) {
|
|
485
|
+
var B = [[0.13, 0.5, 0.0, 0.55, 0.0], [0.1, 0.62, 0.9, -0.42, 2.1], [0.08, 0.38, -0.6, 0.8, 4.0], [0.06, 0.72, 0.4, 0.33, 1.0]];
|
|
486
|
+
var pts = [], body = [], i;
|
|
487
|
+
var share = [0.4, 0.27, 0.2, 0.13];
|
|
488
|
+
for (i = 0; i < n; i++) {
|
|
489
|
+
var u = (i + 0.5) / n, b = 0, acc = share[0];
|
|
490
|
+
while (b < 3 && u > acc) { b++; acc += share[b]; }
|
|
491
|
+
body.push(b);
|
|
492
|
+
}
|
|
493
|
+
var count = [0, 0, 0, 0], seen = [0, 0, 0, 0];
|
|
494
|
+
body.forEach(function (b) { count[b]++; });
|
|
495
|
+
var balls = count.map(function (c) { return fib(Math.max(1, c)); });
|
|
496
|
+
for (i = 0; i < n; i++) {
|
|
497
|
+
var bb = body[i], q = balls[bb][seen[bb]++ % balls[bb].length], sz = B[bb][0] * 0.45;
|
|
498
|
+
pts.push([q[0] * sz, q[1] * sz, q[2] * sz]);
|
|
499
|
+
}
|
|
500
|
+
return { pts: pts, body: body, B: B };
|
|
501
|
+
},
|
|
502
|
+
deform: function (i, d, t, p, o) {
|
|
503
|
+
var b = d.B[d.body[i]], a = b[4] + t * b[3];
|
|
504
|
+
var cx = Math.cos(a) * b[1], cz = Math.sin(a) * b[1];
|
|
505
|
+
var cy = Math.sin(a) * b[1] * Math.sin(b[2]) * 0.6; // each path tilted its own way
|
|
506
|
+
o[0] = p[0] + cx; o[1] = p[1] + cy; o[2] = p[2] + cz;
|
|
507
|
+
},
|
|
508
|
+
bigDots: true,
|
|
509
|
+
alpha: 0.32, // many overlapping circles build up a soft, bright core
|
|
510
|
+
size: function (i, d) { return d.B[d.body[i]][0] * 55; }
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// Status badges: a dotted ring with a mark inside. Check and cross share the ring,
|
|
514
|
+
// so going from one to the other only redraws the mark. Kept out of the default cycle.
|
|
515
|
+
function badge(marks) {
|
|
516
|
+
return function (n) {
|
|
517
|
+
var ring = Math.round(n * 0.6), rest = n - ring, pts = [], u = [], k = [], i;
|
|
518
|
+
for (i = 0; i < ring; i++) {
|
|
519
|
+
var a = i / ring * TAU - Math.PI / 2;
|
|
520
|
+
pts.push([Math.cos(a) * 0.86, Math.sin(a) * 0.86, 0]); u.push(i / ring); k.push(0);
|
|
521
|
+
}
|
|
522
|
+
for (var m = 0; m < marks.length; m++) {
|
|
523
|
+
var c = m === marks.length - 1 ? rest - Math.floor(rest / marks.length) * m : Math.floor(rest / marks.length);
|
|
524
|
+
var s = stroke(marks[m], c);
|
|
525
|
+
for (i = 0; i < c; i++) { pts.push(s.pts[i]); u.push(s.u[i]); k.push(1); }
|
|
526
|
+
}
|
|
527
|
+
return { pts: pts, u: u, k: k };
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
var badgeLook = {
|
|
531
|
+
face: true,
|
|
532
|
+
tilt: 0,
|
|
533
|
+
inCycle: false,
|
|
534
|
+
lineGap: 0.28,
|
|
535
|
+
deform: sway,
|
|
536
|
+
size: function (i, d) { return d.k[i] ? 1.35 : 0.85; },
|
|
537
|
+
alpha: function (i, d, t) {
|
|
538
|
+
if (d.k[i]) return 1.8;
|
|
539
|
+
return 0.9 + 0.5 * Math.pow(0.5 + 0.5 * Math.cos((d.u[i] - t * 0.25) * TAU), 4); // a soft light circling the ring
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
function look(build) { var o = { build: build }; for (var key in badgeLook) o[key] = badgeLook[key]; return o; }
|
|
543
|
+
|
|
544
|
+
define('check', look(badge([[[-0.4, 0.03], [-0.12, 0.31], [0.42, -0.27]]])));
|
|
545
|
+
define('cross', look(badge([[[-0.3, -0.3], [0.3, 0.3]], [[0.3, -0.3], [-0.3, 0.3]]])));
|
|
546
|
+
|
|
547
|
+
/* ── Named states: what an orb is doing, mapped to a shape and a pace ── */
|
|
548
|
+
var STATES = {
|
|
549
|
+
idle: { shape: 'orbit', speed: 0.25 },
|
|
550
|
+
listening: { shape: 'iris' },
|
|
551
|
+
thinking: { shape: 'vortex', speed: 0.5 },
|
|
552
|
+
working: { shape: 'ticks' },
|
|
553
|
+
speaking: { shape: 'bars' },
|
|
554
|
+
active: { shape: 'blob' },
|
|
555
|
+
loading: { shape: 'infinity' },
|
|
556
|
+
success: { shape: 'check' },
|
|
557
|
+
error: { shape: 'cross' }
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
/* ── Sound ── */
|
|
561
|
+
var AC = null;
|
|
562
|
+
function audioContext() { // one shared context for every orb
|
|
563
|
+
if (!AC) { var C = global.AudioContext || global.webkitAudioContext; if (C) AC = new C(); }
|
|
564
|
+
return AC;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/* ── Orb ── */
|
|
568
|
+
function Orb(canvas, opts) {
|
|
569
|
+
if (!(this instanceof Orb)) return new Orb(canvas, opts);
|
|
570
|
+
var ds = canvas.dataset;
|
|
571
|
+
opts = opts || {};
|
|
572
|
+
this.canvas = canvas;
|
|
573
|
+
this.ctx = canvas.getContext('2d');
|
|
574
|
+
this.style = (opts.style || ds.style) === 'lines' ? 'lines' : 'dots';
|
|
575
|
+
this.autoPoints = !(opts.points || ds.points);
|
|
576
|
+
this.n = +(opts.points || ds.points || (this.style === 'lines' ? 320 : 180));
|
|
577
|
+
this.line = +(opts.line || ds.line || 0);
|
|
578
|
+
this.glow = amount(opts.glow != null ? opts.glow : ds.glow, 8);
|
|
579
|
+
this.reflect = Math.min(1, amount(opts.reflect != null ? opts.reflect : ds.reflect, 0.35));
|
|
580
|
+
this.audioAmount = +(opts.audioAmount != null ? opts.audioAmount : (ds.audioAmount || 1));
|
|
581
|
+
this.audio = null;
|
|
582
|
+
this.speed = +(opts.speed != null ? opts.speed : (ds.speed || 0.35));
|
|
583
|
+
this.tiltAdd = (+(opts.tilt != null ? opts.tilt : (ds.tilt || 0)) || 0) * Math.PI / 180;
|
|
584
|
+
this.dot = +(opts.dot || ds.dot || 1.2);
|
|
585
|
+
this.cycle = +(opts.cycle || ds.cycle || 0);
|
|
586
|
+
this.order = (opts.shapes || ds.shapes || Object.keys(SHAPES).filter(function (s) { return SHAPES[s].inCycle !== false; }).join(','))
|
|
587
|
+
.split(',').map(function (s) { return s.trim(); }).filter(function (s) { return SHAPES[s]; });
|
|
588
|
+
this.baseSpeed = this.speed;
|
|
589
|
+
this.follow = !!(opts.follow || ds.follow !== undefined);
|
|
590
|
+
this.scrollTilt = !!(opts.scrollTilt || ds.scrollTilt !== undefined);
|
|
591
|
+
this.look = [0, 0]; this.lookGoal = [0, 0];
|
|
592
|
+
var st = STATES[opts.state || ds.state];
|
|
593
|
+
var first = opts.shape || ds.orb || (st && st.shape);
|
|
594
|
+
this.shape = SHAPES[first] ? first : this.order[0];
|
|
595
|
+
if (st && !(opts.shape || ds.orb)) this.shape = st.shape;
|
|
596
|
+
if (this.order.indexOf(this.shape) < 0) this.order.unshift(this.shape);
|
|
597
|
+
|
|
598
|
+
this.cache = {};
|
|
599
|
+
this.src = this.shape;
|
|
600
|
+
this.cur = this.data(this.shape).pts.map(function () { return [0, 0, 0]; }); // collapsed, for the intro
|
|
601
|
+
this.from = this.cur;
|
|
602
|
+
this.g = 1;
|
|
603
|
+
this.X = new Float32Array(this.n); this.Y = new Float32Array(this.n); this.F = new Float32Array(this.n);
|
|
604
|
+
this.tmp = [0, 0, 0]; this.pos = [0, 0, 0];
|
|
605
|
+
this.angle = 0.6; this.tilt = 0.38; this.spinMul = 1;
|
|
606
|
+
this.color = parseRGB(getComputedStyle(canvas).color);
|
|
607
|
+
this.colorWatchUntil = 0;
|
|
608
|
+
// With IntersectionObserver available, stay idle until it confirms we're on screen.
|
|
609
|
+
this.visible = !global.IntersectionObserver; this.running = false; this.last = 0; this.lastCycle = 0; this.t0 = performance.now();
|
|
610
|
+
|
|
611
|
+
this._frame = this.frame.bind(this);
|
|
612
|
+
this._resize = this.resize.bind(this);
|
|
613
|
+
this._vis = this.onVisibility.bind(this);
|
|
614
|
+
this.resize();
|
|
615
|
+
this.observe(opts, ds);
|
|
616
|
+
this.morph(this.shape, { intro: true });
|
|
617
|
+
if (st) { this.stateName = opts.state || ds.state; if (st.speed != null) this.speed = st.speed; }
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
Orb.define = define;
|
|
621
|
+
Orb.shapes = SHAPES;
|
|
622
|
+
Orb.states = STATES;
|
|
623
|
+
Orb.audioContext = audioContext;
|
|
624
|
+
|
|
625
|
+
Orb.prototype.data = function (name) {
|
|
626
|
+
return this.cache[name] || (this.cache[name] = SHAPES[name].build(this.n));
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
Orb.prototype.observe = function (opts, ds) {
|
|
630
|
+
var self = this;
|
|
631
|
+
if (global.ResizeObserver) { this.ro = new ResizeObserver(this._resize); this.ro.observe(this.canvas); }
|
|
632
|
+
else global.addEventListener('resize', this._resize);
|
|
633
|
+
|
|
634
|
+
if (global.IntersectionObserver) { // only animate while on screen
|
|
635
|
+
this.io = new IntersectionObserver(function (e) { self.visible = e[0].isIntersecting; self.wake(); });
|
|
636
|
+
this.io.observe(this.canvas);
|
|
637
|
+
}
|
|
638
|
+
document.addEventListener('visibilitychange', this._vis);
|
|
639
|
+
|
|
640
|
+
if ((opts.hover || ds.hover) === 'morph') {
|
|
641
|
+
var lastHover = 0;
|
|
642
|
+
this._hover = function () {
|
|
643
|
+
var now = performance.now();
|
|
644
|
+
if (now - lastHover > self.morphDur + 150) { lastHover = now; self.next(); }
|
|
645
|
+
};
|
|
646
|
+
var el = this.canvas.parentElement || this.canvas;
|
|
647
|
+
el.addEventListener('pointerenter', this._hover);
|
|
648
|
+
el.addEventListener('click', this._hover);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if ((opts.click || ds.click) === 'morph') {
|
|
652
|
+
this._click = function () { self.next(); };
|
|
653
|
+
(this.canvas.parentElement || this.canvas).addEventListener('click', this._click);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
var media = opts.audio || ds.audio;
|
|
657
|
+
if (media) { // react to a media element once it plays (a play is a user gesture)
|
|
658
|
+
var el2 = typeof media === 'string' ? document.querySelector(media) : media;
|
|
659
|
+
if (el2) {
|
|
660
|
+
this._play = function () { if (!self.audio) self.listen(el2); };
|
|
661
|
+
el2.addEventListener('play', this._play);
|
|
662
|
+
this._media = el2;
|
|
663
|
+
if (!el2.paused) this._play();
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// Changing data-state on the canvas switches state, so frameworks only need to set an attribute.
|
|
668
|
+
this.so = new MutationObserver(function () { var v = self.canvas.dataset.state; if (v && v !== self.stateName) self.state(v); });
|
|
669
|
+
this.so.observe(this.canvas, { attributes: true, attributeFilter: ['data-state'] });
|
|
670
|
+
|
|
671
|
+
// Lean towards the pointer: where it sits relative to the orb, as a share of the window.
|
|
672
|
+
this._pointer = function (e) {
|
|
673
|
+
if (!self.follow) return;
|
|
674
|
+
var r = self.canvas.getBoundingClientRect();
|
|
675
|
+
var dx = (e.clientX - (r.left + r.width / 2)) / (global.innerWidth / 2);
|
|
676
|
+
var dy = (e.clientY - (r.top + r.height / 2)) / (global.innerHeight / 2);
|
|
677
|
+
self.lookGoal[0] = Math.max(-1, Math.min(1, dx)); self.lookGoal[1] = Math.max(-1, Math.min(1, dy));
|
|
678
|
+
};
|
|
679
|
+
this._pointerOut = function () { self.lookGoal[0] = self.lookGoal[1] = 0; };
|
|
680
|
+
global.addEventListener('pointermove', this._pointer, { passive: true });
|
|
681
|
+
document.documentElement.addEventListener('pointerleave', this._pointerOut);
|
|
682
|
+
|
|
683
|
+
var themeMorph = opts.themeMorph || ds.themeMorph !== undefined;
|
|
684
|
+
this.mo = new MutationObserver(function () {
|
|
685
|
+
self.refreshColor();
|
|
686
|
+
if (themeMorph) self.next();
|
|
687
|
+
});
|
|
688
|
+
this.mo.observe(document.body, { attributes: true, attributeFilter: ['data-theme', 'class'] });
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
Orb.prototype.resize = function () {
|
|
692
|
+
var r = this.canvas.getBoundingClientRect(), dpr = Math.min(global.devicePixelRatio || 1, 2);
|
|
693
|
+
this.w = r.width; this.h = r.height;
|
|
694
|
+
this.canvas.width = Math.max(1, Math.round(r.width * dpr));
|
|
695
|
+
this.canvas.height = Math.max(1, Math.round(r.height * dpr));
|
|
696
|
+
this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
697
|
+
this.dpr = dpr;
|
|
698
|
+
this.fx = null; // effect canvases are rebuilt at the new size
|
|
699
|
+
this.draw();
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
// Follow the canvas's computed colour for a moment after a theme change. That way the dots
|
|
703
|
+
// track whatever CSS transition the page uses, instead of guessing one.
|
|
704
|
+
Orb.prototype.refreshColor = function () {
|
|
705
|
+
this.colorWatchUntil = performance.now() + 1500;
|
|
706
|
+
this.color = parseRGB(getComputedStyle(this.canvas).color);
|
|
707
|
+
this.wake();
|
|
708
|
+
if (reduceMQ.matches) { var self = this; setTimeout(function () { self.color = parseRGB(getComputedStyle(self.canvas).color); self.draw(); }, 1000); }
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
Orb.prototype.morph = function (name, o) {
|
|
712
|
+
if (!SHAPES[name]) return this;
|
|
713
|
+
o = o || {};
|
|
714
|
+
if (!o.intro && name === this.shape) return this;
|
|
715
|
+
// Interrupted mid-morph? Fade out whichever shape is currently dominant on screen.
|
|
716
|
+
if (!o.intro) this.src = (this.morphStart && this.g < 0.5) ? this.src : this.shape;
|
|
717
|
+
else this.src = name;
|
|
718
|
+
this.shape = name;
|
|
719
|
+
// Start every morph from exactly what is drawn right now, motion included, so nothing jumps.
|
|
720
|
+
this.from = this.cur.map(function (p) { return (p._p || p).slice(); });
|
|
721
|
+
this.cur = this.from.map(function (p) { return p.slice(); });
|
|
722
|
+
this.morphStart = performance.now();
|
|
723
|
+
this.morphDur = o.intro ? 1400 : 1100;
|
|
724
|
+
this.pendingIntro = !!o.intro; // the intro waits until the orb is first on screen
|
|
725
|
+
this.g = 0;
|
|
726
|
+
if (reduceMQ.matches) {
|
|
727
|
+
var t = this.data(name).pts;
|
|
728
|
+
this.cur = t.map(function (p) { return p.slice(); });
|
|
729
|
+
this.src = name; this.g = 1; this.morphStart = 0;
|
|
730
|
+
this.tilt = (SHAPES[name].tilt != null ? SHAPES[name].tilt : 0.38) + this.tiltAdd;
|
|
731
|
+
if (SHAPES[name].face) this.angle = Math.round(this.angle / TAU) * TAU;
|
|
732
|
+
this.draw();
|
|
733
|
+
}
|
|
734
|
+
this.wake();
|
|
735
|
+
if (!o.intro && global.CustomEvent) this.canvas.dispatchEvent(new CustomEvent('orb:morph', { detail: { shape: name } }));
|
|
736
|
+
return this;
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
// Change settings on a running orb. Changing the number of points (or the style, when no
|
|
740
|
+
// point count was given) rebuilds the shape in place, without the intro.
|
|
741
|
+
Orb.prototype.set = function (o) {
|
|
742
|
+
o = o || {};
|
|
743
|
+
var rebuild = false;
|
|
744
|
+
if (o.style != null) {
|
|
745
|
+
var st = o.style === 'lines' ? 'lines' : 'dots';
|
|
746
|
+
if (st !== this.style && o.points == null && this.autoPoints) { o.points = st === 'lines' ? 320 : 180; }
|
|
747
|
+
this.style = st;
|
|
748
|
+
}
|
|
749
|
+
if (o.points != null && +o.points !== this.n) { this.n = Math.max(8, +o.points | 0); this.autoPoints = false; rebuild = true; }
|
|
750
|
+
if (o.speed != null) { this.speed = +o.speed; this.baseSpeed = this.speed; }
|
|
751
|
+
if (o.follow != null) { this.follow = !!o.follow; if (!this.follow) this.lookGoal[0] = this.lookGoal[1] = 0; }
|
|
752
|
+
if (o.scrollTilt != null) this.scrollTilt = !!o.scrollTilt;
|
|
753
|
+
if (o.state != null) this.state(o.state);
|
|
754
|
+
if (o.dot != null) this.dot = +o.dot;
|
|
755
|
+
if (o.line != null) this.line = +o.line;
|
|
756
|
+
if (o.glow != null) this.glow = amount(o.glow, 8);
|
|
757
|
+
if (o.reflect != null) this.reflect = Math.min(1, amount(o.reflect, 0.35));
|
|
758
|
+
if (o.tilt != null) this.tiltAdd = (+o.tilt || 0) * Math.PI / 180;
|
|
759
|
+
if (o.audioAmount != null) this.audioAmount = +o.audioAmount;
|
|
760
|
+
if (o.cycle != null) { this.cycle = +o.cycle; this.lastCycle = 0; }
|
|
761
|
+
if (o.shapes != null) {
|
|
762
|
+
var list = (Array.isArray(o.shapes) ? o.shapes : String(o.shapes).split(',')).map(function (x) { return String(x).trim(); }).filter(function (x) { return SHAPES[x]; });
|
|
763
|
+
if (list.length) { this.order = list; if (list.indexOf(this.shape) < 0) this.order.unshift(this.shape); }
|
|
764
|
+
}
|
|
765
|
+
if (o.glow != null || o.reflect != null) { this.fx = null; this.floor = null; }
|
|
766
|
+
if (rebuild) {
|
|
767
|
+
this.cache = {};
|
|
768
|
+
this.X = new Float32Array(this.n); this.Y = new Float32Array(this.n); this.F = new Float32Array(this.n);
|
|
769
|
+
this.cur = this.data(this.shape).pts.map(function (p) { return p.slice(); });
|
|
770
|
+
this.from = this.cur; this.src = this.shape; this.g = 1; this.morphStart = 0;
|
|
771
|
+
}
|
|
772
|
+
if (o.shape != null && o.shape !== this.shape) this.morph(o.shape);
|
|
773
|
+
if (reduceMQ.matches) {
|
|
774
|
+
var def = SHAPES[this.shape];
|
|
775
|
+
this.tilt = (def.tilt != null ? def.tilt : 0.38) + this.tiltAdd;
|
|
776
|
+
}
|
|
777
|
+
this.refreshColor();
|
|
778
|
+
this.draw();
|
|
779
|
+
return this;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
// Named states: morph to the state's shape and pace, and stop any cycling (a state is deliberate).
|
|
783
|
+
Orb.prototype.state = function (name) {
|
|
784
|
+
var st = STATES[name];
|
|
785
|
+
if (!st || !SHAPES[st.shape]) return this;
|
|
786
|
+
this.stateName = name;
|
|
787
|
+
this.cycle = 0;
|
|
788
|
+
this.speed = st.speed != null ? st.speed : this.baseSpeed;
|
|
789
|
+
if (this.canvas.dataset.state !== undefined && this.canvas.dataset.state !== name) this.canvas.dataset.state = name;
|
|
790
|
+
this.morph(st.shape);
|
|
791
|
+
if (global.CustomEvent) this.canvas.dispatchEvent(new CustomEvent('orb:state', { detail: { state: name, shape: st.shape } }));
|
|
792
|
+
return this;
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
Orb.prototype.next = function () {
|
|
796
|
+
var i = this.order.indexOf(this.shape);
|
|
797
|
+
return this.morph(this.order[(i + 1) % this.order.length]);
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
Orb.prototype.onVisibility = function () { if (!document.hidden) this.wake(); };
|
|
801
|
+
|
|
802
|
+
Orb.prototype.wake = function () {
|
|
803
|
+
if (this.running || !this.visible || document.hidden) return;
|
|
804
|
+
if (reduceMQ.matches) { this.draw(); return; }
|
|
805
|
+
this.running = true; this.last = performance.now();
|
|
806
|
+
this.color = parseRGB(getComputedStyle(this.canvas).color); // catch theme changes made while off screen
|
|
807
|
+
if (this.pendingIntro) { this.pendingIntro = false; this.morphStart = this.last; }
|
|
808
|
+
requestAnimationFrame(this._frame);
|
|
809
|
+
};
|
|
810
|
+
|
|
811
|
+
// Sound: pass an <audio> or <video>, a MediaStream, any Web Audio node, or 'mic'. Returns a promise.
|
|
812
|
+
// Media elements play through as normal; a microphone is analysed only, never played back.
|
|
813
|
+
Orb.prototype.listen = function (src) {
|
|
814
|
+
var self = this, ctx = audioContext();
|
|
815
|
+
this.unlisten();
|
|
816
|
+
if (!ctx) return Promise.reject(new Error('Web Audio is not supported'));
|
|
817
|
+
if (src === 'mic') {
|
|
818
|
+
if (!global.navigator || !navigator.mediaDevices) return Promise.reject(new Error('Microphone is not available'));
|
|
819
|
+
return navigator.mediaDevices.getUserMedia({ audio: true }).then(function (stream) {
|
|
820
|
+
return self.listen(stream).then(function () { self.audio.stream = stream; return self; });
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
var node;
|
|
824
|
+
if (global.MediaStream && src instanceof MediaStream) node = ctx.createMediaStreamSource(src);
|
|
825
|
+
else if (src && /^(AUDIO|VIDEO)$/.test(src.nodeName)) {
|
|
826
|
+
// An element can only be wired into Web Audio once, so the source is kept on the element.
|
|
827
|
+
if (!src._orbNode) { src._orbNode = ctx.createMediaElementSource(src); src._orbNode.connect(ctx.destination); }
|
|
828
|
+
node = src._orbNode;
|
|
829
|
+
} else if (src && src.connect) node = src;
|
|
830
|
+
else return Promise.reject(new Error('Unsupported audio source'));
|
|
831
|
+
if (ctx.state === 'suspended') ctx.resume();
|
|
832
|
+
var an = ctx.createAnalyser();
|
|
833
|
+
an.fftSize = 256; an.smoothingTimeConstant = 0.55;
|
|
834
|
+
node.connect(an);
|
|
835
|
+
this.audio = { node: node, an: an, bins: new Uint8Array(an.frequencyBinCount), bands: new Float32Array(16), level: 0 };
|
|
836
|
+
this.wake();
|
|
837
|
+
return Promise.resolve(this);
|
|
838
|
+
};
|
|
839
|
+
|
|
840
|
+
Orb.prototype.unlisten = function () {
|
|
841
|
+
var A = this.audio;
|
|
842
|
+
if (!A) return this;
|
|
843
|
+
try { A.node.disconnect(A.an); } catch (e) { /* already disconnected */ }
|
|
844
|
+
if (A.stream) A.stream.getTracks().forEach(function (tr) { tr.stop(); });
|
|
845
|
+
this.audio = null;
|
|
846
|
+
return this;
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
// 16 bands, spaced like hearing (more detail in the lows), with a quick rise and a slower fall.
|
|
850
|
+
Orb.prototype.sampleAudio = function () {
|
|
851
|
+
var A = this.audio, bins = A.bins, B = A.bands, sum = 0;
|
|
852
|
+
A.an.getByteFrequencyData(bins);
|
|
853
|
+
for (var k = 0; k < 16; k++) {
|
|
854
|
+
var lo = Math.floor(1 + Math.pow(k / 16, 2) * 90), hi = Math.max(lo + 1, Math.floor(1 + Math.pow((k + 1) / 16, 2) * 90));
|
|
855
|
+
var v = 0;
|
|
856
|
+
for (var j = lo; j < hi; j++) v += bins[j];
|
|
857
|
+
v = Math.min(1, Math.pow(v / (hi - lo) / 255, 1.3) * (1 + k * 0.06) * this.audioAmount); // lift the quieter highs
|
|
858
|
+
B[k] += (v - B[k]) * (v > B[k] ? 0.6 : 0.15);
|
|
859
|
+
sum += B[k];
|
|
860
|
+
}
|
|
861
|
+
A.level += (sum / 16 - A.level) * 0.3;
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
Orb.prototype.frame = function (now) {
|
|
865
|
+
if (!this.visible || document.hidden || reduceMQ.matches) { this.running = false; return; }
|
|
866
|
+
var dt = Math.min(0.05, (now - this.last) / 1000); this.last = now;
|
|
867
|
+
if (this.audio) this.sampleAudio();
|
|
868
|
+
var def = SHAPES[this.shape];
|
|
869
|
+
var tiltGoal = (def.tilt != null ? def.tilt : 0.38) + this.tiltAdd, spinGoal = def.spin != null ? def.spin : 1;
|
|
870
|
+
var ease = Math.min(1, dt * 2.5);
|
|
871
|
+
this.tilt += (tiltGoal - this.tilt) * ease;
|
|
872
|
+
this.spinMul += (spinGoal - this.spinMul) * ease;
|
|
873
|
+
if (def.face) { var home = Math.round(this.angle / TAU) * TAU; this.angle += (home - this.angle) * ease; } // turn to face the viewer
|
|
874
|
+
else this.angle += this.speed * this.spinMul * dt;
|
|
875
|
+
|
|
876
|
+
// Cursor and scroll: a gentle lean on top of the shape's own motion, eased so it never snaps.
|
|
877
|
+
var gx = this.follow ? this.lookGoal[0] * 0.55 : 0, gy = this.follow ? this.lookGoal[1] * 0.4 : 0;
|
|
878
|
+
if (this.scrollTilt) {
|
|
879
|
+
var rr = this.canvas.getBoundingClientRect(), vh = global.innerHeight || 1;
|
|
880
|
+
gy += Math.max(-1, Math.min(1, (rr.top + rr.height / 2 - vh / 2) / (vh / 2))) * 0.5;
|
|
881
|
+
}
|
|
882
|
+
var le = Math.min(1, dt * 3);
|
|
883
|
+
this.look[0] += (gx - this.look[0]) * le; this.look[1] += (gy - this.look[1]) * le;
|
|
884
|
+
|
|
885
|
+
// Morph: dots leave top to bottom in a soft cascade, with a slight outward swell mid-flight.
|
|
886
|
+
if (this.morphStart) {
|
|
887
|
+
var raw = (now - this.morphStart) / this.morphDur, tgt = this.data(this.shape).pts, done = true;
|
|
888
|
+
for (var i = 0; i < this.n; i++) {
|
|
889
|
+
var b = tgt[i], a = this.from[i], c = this.cur[i];
|
|
890
|
+
var delay = (1 - (Math.max(-1, Math.min(1, b[1])) + 1) / 2) * 0.25;
|
|
891
|
+
var t = Math.min(1, Math.max(0, (raw - delay) / 0.75));
|
|
892
|
+
if (t < 1) done = false;
|
|
893
|
+
var e = easeInOut(t), swell = 1 + Math.sin(e * Math.PI) * 0.08;
|
|
894
|
+
c[0] = (a[0] + (b[0] - a[0]) * e) * swell;
|
|
895
|
+
c[1] = (a[1] + (b[1] - a[1]) * e) * swell;
|
|
896
|
+
c[2] = (a[2] + (b[2] - a[2]) * e) * swell;
|
|
897
|
+
}
|
|
898
|
+
this.g = easeInOut(Math.min(1, raw));
|
|
899
|
+
if (done) { this.morphStart = 0; this.g = 1; this.src = this.shape; }
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
if (this.colorWatchUntil && now < this.colorWatchUntil) this.color = parseRGB(getComputedStyle(this.canvas).color);
|
|
903
|
+
|
|
904
|
+
if (this.cycle && now - this.lastCycle > this.cycle && !this.morphStart) {
|
|
905
|
+
if (this.lastCycle) this.next();
|
|
906
|
+
this.lastCycle = now;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
this.draw(now);
|
|
910
|
+
requestAnimationFrame(this._frame);
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
Orb.prototype.draw = function (now) {
|
|
914
|
+
var ctx = this.ctx, w = this.w, h = this.h;
|
|
915
|
+
if (!w || !h) return;
|
|
916
|
+
ctx.clearRect(0, 0, w, h);
|
|
917
|
+
|
|
918
|
+
var t = reduceMQ.matches ? 0 : ((now || performance.now()) - this.t0) / 1000;
|
|
919
|
+
var S = SHAPES[this.src], T = SHAPES[this.shape], sd = this.data(this.src), td = this.data(this.shape);
|
|
920
|
+
var g = this.g, gs = 1 - g;
|
|
921
|
+
var R = Math.min(w, h) * (this.reflect ? 0.33 : 0.36), cx = w / 2, cy = this.reflect ? h * 0.4 : h / 2;
|
|
922
|
+
var yaw = this.angle + this.look[0], pitch = this.tilt + this.look[1];
|
|
923
|
+
var ca = Math.cos(yaw), sa = Math.sin(yaw), ct = Math.cos(pitch), st = Math.sin(pitch);
|
|
924
|
+
var col = this.color, rgb = 'rgba(' + (col[0] | 0) + ',' + (col[1] | 0) + ',' + (col[2] | 0) + ',';
|
|
925
|
+
var dot = this.dot * (0.5 + 0.5 * Math.min(1, Math.min(w, h) / 90)); // airier on small canvases
|
|
926
|
+
var X = this.X, Y = this.Y, F = this.F, pos = this.pos, tmp = this.tmp, n = this.n;
|
|
927
|
+
|
|
928
|
+
function project(p, out) {
|
|
929
|
+
var x = p[0] * ca - p[2] * sa, z = p[0] * sa + p[2] * ca; // spin around Y
|
|
930
|
+
var y = p[1] * ct - z * st; z = p[1] * st + z * ct; // tilt around X
|
|
931
|
+
var persp = 1 / (1 - z * 0.12);
|
|
932
|
+
out[0] = cx + x * R * persp; out[1] = cy + y * R * persp; out[2] = (z + 1) / 2;
|
|
933
|
+
}
|
|
934
|
+
function blendDeform(def, d, weight, i, base) {
|
|
935
|
+
if (!def.deform || weight <= 0) return;
|
|
936
|
+
def.deform(i, d, t, base, tmp);
|
|
937
|
+
pos[0] += (tmp[0] - base[0]) * weight; pos[1] += (tmp[1] - base[1]) * weight; pos[2] += (tmp[2] - base[2]) * weight;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// Sound: shapes with their own audio() use it; flat marks only pulse; the rest also push out on the beat.
|
|
941
|
+
var A = this.audio && !reduceMQ.matches ? this.audio : null, amt = this.audioAmount;
|
|
942
|
+
var pulse = A ? 1 + A.level * 0.1 : 1;
|
|
943
|
+
function react(i, def, d, weight) {
|
|
944
|
+
if (def.audio) {
|
|
945
|
+
tmp[0] = pos[0]; tmp[1] = pos[1]; tmp[2] = pos[2];
|
|
946
|
+
def.audio(i, d, A, tmp, tmp);
|
|
947
|
+
pos[0] += (tmp[0] - pos[0]) * weight; pos[1] += (tmp[1] - pos[1]) * weight; pos[2] += (tmp[2] - pos[2]) * weight;
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
var k = pulse;
|
|
951
|
+
if (!def.face) {
|
|
952
|
+
var u = Math.atan2(pos[2], pos[0]) / TAU + 0.5; // low notes one side, high notes the other
|
|
953
|
+
k += A.bands[Math.min(15, Math.floor((1 - Math.abs(2 * u - 1)) * 16))] * 0.22 * Math.min(1, amt);
|
|
954
|
+
}
|
|
955
|
+
k = 1 + (k - 1) * weight;
|
|
956
|
+
pos[0] *= k; pos[1] *= k; pos[2] *= k;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
// 1. positions: base + each shape's live motion, weighted by how far the morph has got
|
|
960
|
+
var proj = [0, 0, 0];
|
|
961
|
+
for (var i = 0; i < n; i++) {
|
|
962
|
+
var base = this.cur[i];
|
|
963
|
+
pos[0] = base[0]; pos[1] = base[1]; pos[2] = base[2];
|
|
964
|
+
blendDeform(T, td, g, i, base);
|
|
965
|
+
if (A) react(i, T, td, 1);
|
|
966
|
+
project(pos, proj);
|
|
967
|
+
X[i] = proj[0]; Y[i] = proj[1]; F[i] = proj[2];
|
|
968
|
+
this.cur[i]._p = this.cur[i]._p || [0, 0, 0];
|
|
969
|
+
this.cur[i]._p[0] = pos[0]; this.cur[i]._p[1] = pos[1]; this.cur[i]._p[2] = pos[2];
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
if (this.style === 'lines') { this.effects(this.slices(t, S, T, sd, td, g, R, dot)); return; }
|
|
973
|
+
|
|
974
|
+
// 2. connecting lines (constellation, helix rungs, cluster links) fade with the morph
|
|
975
|
+
ctx.lineWidth = this.line || Math.max(0.5, dot * 0.45);
|
|
976
|
+
ctx.lineCap = 'round';
|
|
977
|
+
function edges(d, def, weight) {
|
|
978
|
+
if (!d.edges || weight <= 0.01) return;
|
|
979
|
+
var ea = (def.edgeAlpha != null ? def.edgeAlpha : 0.3) * weight;
|
|
980
|
+
for (var e = 0; e < d.edges.length; e++) {
|
|
981
|
+
var a = d.edges[e][0], b = d.edges[e][1];
|
|
982
|
+
ctx.strokeStyle = rgb + (ea * (0.25 + 0.75 * (F[a] + F[b]) / 2)).toFixed(3) + ')';
|
|
983
|
+
ctx.beginPath(); ctx.moveTo(X[a], Y[a]); ctx.lineTo(X[b], Y[b]); ctx.stroke();
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
edges(sd, S, gs);
|
|
987
|
+
edges(td, T, g);
|
|
988
|
+
|
|
989
|
+
function dotR(def, d, i, t, p, front) {
|
|
990
|
+
var sz = val(def.size, i, d, t, p);
|
|
991
|
+
return def.bigDots ? R * 0.01 * sz * (0.75 + front * 0.25) : dot * (0.45 + front * 0.85) * sz;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// 3. dots and dashes, back to front is not needed: alpha already fades the far side
|
|
995
|
+
var end = [0, 0, 0], low = -1e9, lift = A ? 0.8 + A.level * 0.6 : 1;
|
|
996
|
+
for (i = 0; i < n; i++) {
|
|
997
|
+
var p = this.cur[i]._p, front = F[i];
|
|
998
|
+
var alpha = gs * val(S.alpha, i, sd, t, p) + g * val(T.alpha, i, td, t, p);
|
|
999
|
+
var dash = (S.dash ? gs * S.dash(i, sd, t, p) : 0) + (T.dash ? g * T.dash(i, td, t, p) : 0);
|
|
1000
|
+
// Most shapes size dots in pixels; bigDots shapes size them as a share of the orb, so they scale with it.
|
|
1001
|
+
var r = gs * dotR(S, sd, i, t, p, front) + g * dotR(T, td, i, t, p, front);
|
|
1002
|
+
var a2 = Math.max(0, Math.min(1, (0.12 + front * 0.88) * alpha * lift));
|
|
1003
|
+
if (dash > 0.004) {
|
|
1004
|
+
tmp[0] = p[0] * (1 + dash); tmp[1] = p[1] * (1 + dash); tmp[2] = p[2] * (1 + dash);
|
|
1005
|
+
project(tmp, end);
|
|
1006
|
+
ctx.strokeStyle = rgb + a2.toFixed(3) + ')';
|
|
1007
|
+
ctx.lineWidth = r * 1.4;
|
|
1008
|
+
ctx.beginPath(); ctx.moveTo(X[i], Y[i]); ctx.lineTo(end[0], end[1]); ctx.stroke();
|
|
1009
|
+
} else {
|
|
1010
|
+
ctx.fillStyle = rgb + a2.toFixed(3) + ')';
|
|
1011
|
+
ctx.beginPath(); ctx.arc(X[i], Y[i], r, 0, TAU); ctx.fill();
|
|
1012
|
+
if (a2 > 0.08 && Y[i] + r > low) low = Y[i] + r;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
this.effects(low);
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
// Lines style: sort the projected dots into vertical columns, then join dots that sit close
|
|
1019
|
+
// together in a column into one line, lit by the dots along it. Solid shapes become sliced
|
|
1020
|
+
// silhouettes; rings, holes and marks keep their gaps.
|
|
1021
|
+
Orb.prototype.slices = function (t, S, T, sd, td, g, R, dot) {
|
|
1022
|
+
var ctx = this.ctx, n = this.n, X = this.X, Y = this.Y, F = this.F, gs = 1 - g;
|
|
1023
|
+
var cols = Math.max(20, Math.min(90, Math.round(n / 5)));
|
|
1024
|
+
var sp = Math.max(3, 2.3 * R / cols), lw = this.line || Math.max(1, sp * 0.46);
|
|
1025
|
+
var gap = R * (g < 0.5 ? (S.lineGap || 0.45) : (T.lineGap || 0.45)), cap = Math.max(lw, R * 0.025), low = -1e9;
|
|
1026
|
+
var col = this.color, rgb = 'rgba(' + (col[0] | 0) + ',' + (col[1] | 0) + ',' + (col[2] | 0) + ',';
|
|
1027
|
+
var C = this.sC || (this.sC = []), Ys = this.sY || (this.sY = []), A = this.sA || (this.sA = []), idx = [], m = 0;
|
|
1028
|
+
for (var i = 0; i < n; i++) {
|
|
1029
|
+
var p = this.cur[i]._p;
|
|
1030
|
+
var alpha = gs * val(S.alpha, i, sd, t, p) + g * val(T.alpha, i, td, t, p);
|
|
1031
|
+
var a = Math.max(0, Math.min(1, (0.2 + F[i] * 0.8) * alpha * 1.5 * (this.audio ? 0.8 + this.audio.level * 0.6 : 1))); // lines are thinner than dots, so lift them
|
|
1032
|
+
if (a < 0.02) continue;
|
|
1033
|
+
if (a > 0.08 && Y[i] > low) low = Y[i];
|
|
1034
|
+
var fx = X[i] / sp, c0 = Math.floor(fx), fr = fx - c0; // share each dot between its two nearest
|
|
1035
|
+
if (fr < 0.97) { C[m] = c0; Ys[m] = Y[i]; A[m] = a * (1 - fr); idx.push(m++); } // columns, so motion stays smooth
|
|
1036
|
+
if (fr > 0.03) { C[m] = c0 + 1; Ys[m] = Y[i]; A[m] = a * fr; idx.push(m++); }
|
|
1037
|
+
}
|
|
1038
|
+
idx.sort(function (u, v) { return C[u] - C[v] || Ys[u] - Ys[v]; });
|
|
1039
|
+
ctx.lineWidth = lw; ctx.lineCap = 'round';
|
|
1040
|
+
for (var s = 0; s < idx.length;) {
|
|
1041
|
+
var e = s; // grow a run of close dots in one column
|
|
1042
|
+
while (e + 1 < idx.length && C[idx[e + 1]] === C[idx[s]] && Ys[idx[e + 1]] - Ys[idx[e]] < gap) e++;
|
|
1043
|
+
var x = C[idx[s]] * sp, y0 = Ys[idx[s]] - cap, y1 = Ys[idx[e]] + cap;
|
|
1044
|
+
if (e === s) ctx.strokeStyle = rgb + A[idx[s]].toFixed(3) + ')';
|
|
1045
|
+
else {
|
|
1046
|
+
var grad = ctx.createLinearGradient(0, y0, 0, y1);
|
|
1047
|
+
for (var k = s; k <= e; k++) grad.addColorStop((Ys[idx[k]] - y0) / (y1 - y0), rgb + A[idx[k]].toFixed(3) + ')');
|
|
1048
|
+
ctx.strokeStyle = grad;
|
|
1049
|
+
}
|
|
1050
|
+
ctx.beginPath(); ctx.moveTo(x, y0); ctx.lineTo(x, y1); ctx.stroke();
|
|
1051
|
+
s = e + 1;
|
|
1052
|
+
}
|
|
1053
|
+
return low + cap;
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
// Glow and reflection, drawn from a copy of the finished frame. Both are off unless asked for.
|
|
1057
|
+
Orb.prototype.effects = function (low) {
|
|
1058
|
+
if (!this.glow && !this.reflect) return;
|
|
1059
|
+
// The reflection sits just under the shape's lowest point, eased so it glides rather than jitters.
|
|
1060
|
+
if (low > -1e8) this.floor = this.floor == null || reduceMQ.matches ? low : this.floor + (low - this.floor) * 0.08;
|
|
1061
|
+
var floor = (this.floor || this.h * 0.75) + 4;
|
|
1062
|
+
var ctx = this.ctx, cv = this.canvas, W = cv.width, H = cv.height, dpr = this.dpr || 1;
|
|
1063
|
+
if (!this.fx) {
|
|
1064
|
+
var mk = function (k) { var c = document.createElement('canvas'); c.width = Math.ceil(W / k); c.height = Math.ceil(H / k); return c; };
|
|
1065
|
+
this.fx = { copy: mk(1), mirror: mk(1), small: mk(4) };
|
|
1066
|
+
}
|
|
1067
|
+
var copy = this.fx.copy, cc = copy.getContext('2d');
|
|
1068
|
+
cc.clearRect(0, 0, W, H); cc.drawImage(cv, 0, 0);
|
|
1069
|
+
ctx.save();
|
|
1070
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
1071
|
+
if (this.glow && 'filter' in ctx) { // blur a quarter-size copy (cheap), then lay it behind
|
|
1072
|
+
var sm = this.fx.small, sc = sm.getContext('2d');
|
|
1073
|
+
sc.clearRect(0, 0, sm.width, sm.height);
|
|
1074
|
+
sc.filter = 'blur(' + (this.glow * dpr / 4).toFixed(2) + 'px)';
|
|
1075
|
+
sc.drawImage(copy, 0, 0, sm.width, sm.height);
|
|
1076
|
+
sc.filter = 'none';
|
|
1077
|
+
ctx.globalCompositeOperation = 'destination-over';
|
|
1078
|
+
ctx.drawImage(sm, 0, 0, W, H);
|
|
1079
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
1080
|
+
}
|
|
1081
|
+
if (this.reflect) { // flip about the floor, squash, then fade out downwards
|
|
1082
|
+
var mir = this.fx.mirror, mc = mir.getContext('2d'), fy = floor * dpr, sq = 0.55;
|
|
1083
|
+
mc.setTransform(1, 0, 0, 1, 0, 0); mc.globalCompositeOperation = 'source-over';
|
|
1084
|
+
mc.clearRect(0, 0, W, H);
|
|
1085
|
+
mc.setTransform(1, 0, 0, -sq, 0, fy * (1 + sq));
|
|
1086
|
+
mc.drawImage(copy, 0, 0);
|
|
1087
|
+
mc.setTransform(1, 0, 0, 1, 0, 0);
|
|
1088
|
+
mc.globalCompositeOperation = 'destination-in';
|
|
1089
|
+
var fade = mc.createLinearGradient(0, fy, 0, Math.min(H, fy + (H - fy) * 0.95));
|
|
1090
|
+
fade.addColorStop(0, 'rgba(0,0,0,' + this.reflect + ')'); fade.addColorStop(1, 'rgba(0,0,0,0)');
|
|
1091
|
+
mc.fillStyle = fade; mc.fillRect(0, fy, W, H - fy);
|
|
1092
|
+
mc.clearRect(0, 0, W, fy);
|
|
1093
|
+
ctx.drawImage(mir, 0, 0);
|
|
1094
|
+
}
|
|
1095
|
+
ctx.restore();
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
Orb.prototype.destroy = function () {
|
|
1099
|
+
this.visible = false;
|
|
1100
|
+
this.unlisten();
|
|
1101
|
+
if (this._play) this._media.removeEventListener('play', this._play);
|
|
1102
|
+
if (this.ro) this.ro.disconnect(); else global.removeEventListener('resize', this._resize);
|
|
1103
|
+
if (this.io) this.io.disconnect();
|
|
1104
|
+
if (this.mo) this.mo.disconnect();
|
|
1105
|
+
if (this.so) this.so.disconnect();
|
|
1106
|
+
global.removeEventListener('pointermove', this._pointer);
|
|
1107
|
+
document.documentElement.removeEventListener('pointerleave', this._pointerOut);
|
|
1108
|
+
document.removeEventListener('visibilitychange', this._vis);
|
|
1109
|
+
if (this._hover) {
|
|
1110
|
+
var el = this.canvas.parentElement || this.canvas;
|
|
1111
|
+
el.removeEventListener('pointerenter', this._hover); el.removeEventListener('click', this._hover);
|
|
1112
|
+
}
|
|
1113
|
+
if (this._click) (this.canvas.parentElement || this.canvas).removeEventListener('click', this._click);
|
|
1114
|
+
};
|
|
1115
|
+
|
|
1116
|
+
/* ── Auto-init every <canvas data-orb> ── */
|
|
1117
|
+
function init(root) {
|
|
1118
|
+
var els = (root && root.querySelectorAll ? root : document).querySelectorAll('canvas[data-orb]');
|
|
1119
|
+
for (var i = 0; i < els.length; i++) {
|
|
1120
|
+
if (els[i]._orb) continue;
|
|
1121
|
+
els[i]._orb = new Orb(els[i]);
|
|
1122
|
+
if (els[i].parentElement) els[i].parentElement.classList.add('orb-ready'); // hide fallback glyph only once running
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
Orb.init = init;
|
|
1126
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', function () { init(); });
|
|
1127
|
+
else init();
|
|
1128
|
+
|
|
1129
|
+
global.Orb = Orb;
|
|
1130
|
+
if (typeof module === 'object' && module.exports) module.exports = Orb; // npm and bundlers
|
|
1131
|
+
})(typeof window !== 'undefined' ? window : undefined);
|