@shazmoghaddam/orbs 0.0.0-stage → 1.8.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.
Files changed (6) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +339 -2
  3. package/orb.d.ts +157 -0
  4. package/orb.js +1172 -0
  5. package/orb.min.js +44 -0
  6. package/package.json +70 -4
package/orb.js ADDED
@@ -0,0 +1,1172 @@
1
+ /*!
2
+ * Orbs v1.8.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, layers,
11
+ * clusters, 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
+ // Layers: four tall, slanted diamonds packed tightly and stepping down to the right, so they overlap
275
+ // like sheets. It faces the viewer and rocks gently rather than spinning, so it always reads as drawn.
276
+ // A slow wave lights each layer in turn, like data passing through.
277
+ define('layers', {
278
+ face: true,
279
+ tilt: 0,
280
+ lineGap: 0.12,
281
+ build: function (n) {
282
+ var L = 4, A = [0.74, 0.4], B = [0.4, 0.74], pts = [], lay = []; // the two edge directions
283
+ var O = [[-0.28, -0.82], [-0.5, -0.69], [-0.64, -0.45], [-0.86, -0.33]]; // each diamond's top corner
284
+ var k = Math.max(0, Math.floor((n - L * 4) / (L * 4))); // dots between corners on each edge
285
+ for (var l = 0; l < L; l++) {
286
+ var o = O[l], V = [o, [o[0] + A[0], o[1] + A[1]], [o[0] + A[0] + B[0], o[1] + A[1] + B[1]], [o[0] + B[0], o[1] + B[1]]];
287
+ for (var e = 0; e < 4; e++) {
288
+ var a = V[e], b = V[(e + 1) % 4];
289
+ for (var j = 0; j <= k; j++) {
290
+ var f = j / (k + 1);
291
+ pts.push([a[0] + (b[0] - a[0]) * f, a[1] + (b[1] - a[1]) * f, (l - 1.5) * 0.1]); lay.push(l);
292
+ }
293
+ }
294
+ }
295
+ for (var i = 0; pts.length < n; i++) { // spare dots double up on the corners
296
+ var c = (i % (L * 4)) * (k + 1);
297
+ pts.push(pts[c].slice()); lay.push(lay[c]);
298
+ }
299
+ return { pts: pts.slice(0, n), lay: lay };
300
+ },
301
+ deform: function (i, d, t, p, o) {
302
+ var a = 0.13 * Math.sin(t * 0.8), b = 0.06 * Math.sin(t * 0.55 + 1), ca = Math.cos(a), sa = Math.sin(a);
303
+ var x = p[0], y = p[1] - 0.03 * Math.sin(t * 1.5 - d.lay[i] * 0.9), z = p[2]; // the lit layer lifts a touch
304
+ o[0] = x * ca - z * sa; z = x * sa + z * ca; // rock side to side, and a little up and down
305
+ o[1] = y * Math.cos(b) - z * Math.sin(b); o[2] = y * Math.sin(b) + z * Math.cos(b);
306
+ },
307
+ alpha: function (i, d, t) { return 0.75 + 0.6 * Math.pow(0.5 + 0.5 * Math.sin(t * 1.5 - d.lay[i] * 0.9), 3); },
308
+ size: function (i, d, t) { return 0.9 + 0.35 * Math.pow(0.5 + 0.5 * Math.sin(t * 1.5 - d.lay[i] * 0.9), 3); },
309
+ audio: function (i, d, A, p, o) { // low notes swell the back layer, high notes the front
310
+ var l = d.lay[i] * 4, b = 1 + 0.25 * (A.bands[l] + A.bands[l + 1] + A.bands[l + 2] + A.bands[l + 3]) / 4;
311
+ o[0] = p[0] * b; o[1] = p[1] * b; o[2] = p[2];
312
+ }
313
+ });
314
+
315
+ // k-means, literally: four clusters breathing, members linked to their centroid.
316
+ define('clusters', {
317
+ spin: 0.6,
318
+ edgeAlpha: 0.24,
319
+ build: function (n) {
320
+ 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]];
321
+ var pts = [], cl = [], edges = [];
322
+ for (var i = 0; i < n; i++) {
323
+ var c = i % 4, q = C[c];
324
+ if (i < 4) pts.push(q.slice());
325
+ else {
326
+ pts.push([q[0] + gauss(r) * 0.42, q[1] + gauss(r) * 0.42, q[2] + gauss(r) * 0.42]);
327
+ if (i % 2 === 0) edges.push([c, i]);
328
+ }
329
+ cl.push(c);
330
+ }
331
+ return { pts: pts, edges: edges, cl: cl, C: C };
332
+ },
333
+ size: function (i) { return i < 4 ? 2.3 : 0.9; },
334
+ deform: function (i, d, t, p, o) {
335
+ var c = d.C[d.cl[i]], k = 1 + 0.22 * Math.sin(t * 1.6 + d.cl[i] * 1.7);
336
+ 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;
337
+ }
338
+ });
339
+
340
+ // Listening: rings spread out from the centre and fade at the edge, like sound on water.
341
+ define('ripple', {
342
+ tilt: 1.05,
343
+ spin: 0.15,
344
+ build: function (n) {
345
+ var rings = 6, per = n / rings, pts = [], f = [], a = [];
346
+ for (var i = 0; i < n; i++) {
347
+ var r = Math.floor(i / per), ang = (i - r * per) / per * TAU + r, fr = r / rings;
348
+ pts.push([Math.cos(ang) * fr * 0.95, 0, Math.sin(ang) * fr * 0.95]);
349
+ f.push(fr); a.push(ang);
350
+ }
351
+ return { pts: pts, f: f, a: a };
352
+ },
353
+ deform: function (i, d, t, p, o) {
354
+ var r = ((d.f[i] + t * 0.16) % 1) * 0.95;
355
+ o[0] = Math.cos(d.a[i]) * r; o[1] = 0; o[2] = Math.sin(d.a[i]) * r;
356
+ },
357
+ alpha: function (i, d, t) { return 1.1 * Math.sin(((d.f[i] + t * 0.16) % 1) * Math.PI); },
358
+ size: function (i, d, t) { return 0.7 + 0.6 * ((d.f[i] + t * 0.16) % 1); }
359
+ });
360
+
361
+ // Speaking: columns of dots rising and falling, tallest in the middle.
362
+ define('bars', {
363
+ face: true,
364
+ tilt: 0,
365
+ alpha: 1.6,
366
+ build: function (n) {
367
+ var B = 11, pts = [], bar = [], fy = [];
368
+ for (var i = 0; i < n; i++) { // spread dots evenly across bars, so none double up
369
+ var b = Math.floor(i * B / n), first = Math.ceil(b * n / B), count = Math.ceil((b + 1) * n / B) - first;
370
+ var y = count > 1 ? (i - first) / (count - 1) * 2 - 1 : 0;
371
+ pts.push([(b - (B - 1) / 2) * 0.15, y * 0.6, 0]);
372
+ bar.push(b); fy.push(y);
373
+ }
374
+ return { pts: pts, bar: bar, fy: fy, mid: (B - 1) / 2 };
375
+ },
376
+ deform: function (i, d, t, p, o) {
377
+ 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);
378
+ var h = 0.1 + 0.7 * v * (1 - Math.abs(b - d.mid) / (d.mid + 2));
379
+ o[0] = p[0]; o[1] = d.fy[i] * h; o[2] = p[2];
380
+ },
381
+ audio: function (i, d, A, p, o) { // low notes in the middle, high notes at the edges
382
+ var b = d.bar[i], k = Math.round(Math.abs(b - d.mid) / d.mid * 11);
383
+ var h = 0.06 + 0.86 * A.bands[k] * (1 - Math.abs(b - d.mid) / (d.mid * 4));
384
+ o[0] = p[0]; o[1] = d.fy[i] * h; o[2] = p[2];
385
+ }
386
+ });
387
+
388
+ // Atom: a small nucleus with three tilted orbits, each carrying a bright electron.
389
+ define('atom', {
390
+ tilt: 0.45,
391
+ spin: 0.5,
392
+ build: function (n) {
393
+ var core = Math.max(8, Math.round(n * 0.14)), rest = n - core, per = Math.ceil(rest / 3), phi = 1.2;
394
+ var pts = fib(core).map(function (p) { return [p[0] * 0.13, p[1] * 0.13, p[2] * 0.13]; });
395
+ var orbit = pts.map(function () { return -1; }), ang = pts.map(function () { return 0; });
396
+ for (var i = 0; i < rest; i++) {
397
+ var o = i % 3, a = Math.floor(i / 3) / per * TAU, rot = o * Math.PI / 3;
398
+ var ux = Math.cos(rot), uz = -Math.sin(rot); // orbit axes,
399
+ var vx = Math.sin(phi) * Math.sin(rot), vy = Math.cos(phi), vz = Math.sin(phi) * Math.cos(rot); // turned 60° apart
400
+ 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]);
401
+ orbit.push(o); ang.push(a);
402
+ }
403
+ return { pts: pts, orbit: orbit, ang: ang };
404
+ },
405
+ size: function (i, d, t) {
406
+ if (d.orbit[i] < 0) return 1.5;
407
+ 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);
408
+ },
409
+ alpha: function (i, d, t) {
410
+ if (d.orbit[i] < 0) return 1;
411
+ 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);
412
+ }
413
+ });
414
+
415
+ // Flat marks (infinity, check, cross) are drawn as clean single strokes of evenly spaced dots.
416
+ // stroke() spreads n dots along a path of [x, y] points by length; y runs down the screen.
417
+ function stroke(path, n) {
418
+ var lens = [], total = 0, pts = [], u = [], i;
419
+ for (i = 1; i < path.length; i++) {
420
+ var l = Math.sqrt(Math.pow(path[i][0] - path[i - 1][0], 2) + Math.pow(path[i][1] - path[i - 1][1], 2));
421
+ lens.push(l); total += l;
422
+ }
423
+ for (i = 0; i < n; i++) {
424
+ var f = n > 1 ? i / (n - 1) : 0, d = f * total, s = 0;
425
+ while (s < lens.length - 1 && d > lens[s]) { d -= lens[s]; s++; }
426
+ var a = path[s], b = path[s + 1], q = lens[s] ? Math.min(1, d / lens[s]) : 0;
427
+ pts.push([a[0] + (b[0] - a[0]) * q, a[1] + (b[1] - a[1]) * q, 0]);
428
+ u.push(f);
429
+ }
430
+ return { pts: pts, u: u };
431
+ }
432
+ function sway(i, d, t, p, o) { // a slight turn, so flat marks still feel alive
433
+ var a = 0.12 * Math.sin(t * 1.1), c = Math.cos(a), s = Math.sin(a);
434
+ o[0] = p[0] * c - p[2] * s; o[1] = p[1]; o[2] = p[0] * s + p[2] * c;
435
+ }
436
+
437
+ // Loader: a figure of eight with a bright pulse running round it.
438
+ define('infinity', {
439
+ face: true,
440
+ tilt: 0,
441
+ build: function (n) {
442
+ var path = [];
443
+ for (var j = 0; j <= 160; j++) {
444
+ var s = j / 160 * TAU, den = 1 + Math.sin(s) * Math.sin(s);
445
+ path.push([0.9 * Math.cos(s) / den, 0.9 * Math.sin(s) * Math.cos(s) / den]);
446
+ }
447
+ return stroke(path, n);
448
+ },
449
+ deform: sway,
450
+ 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); },
451
+ 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); }
452
+ });
453
+
454
+ // Thinking: dots spiral inwards and down a funnel, turning faster near the centre.
455
+ define('vortex', {
456
+ tilt: 0.75,
457
+ spin: 0.15,
458
+ build: function (n) {
459
+ var pts = [], f = [], a = [];
460
+ for (var i = 0; i < n; i++) {
461
+ var fr = (i * 0.618034) % 1, arm = i % 3;
462
+ f.push(fr); a.push(arm * TAU / 3 + fr * TAU * 1.6);
463
+ var r = 0.95 * (1 - fr) + 0.05;
464
+ pts.push([Math.cos(a[i]) * r, fr * fr * 0.7 - 0.25, Math.sin(a[i]) * r]);
465
+ }
466
+ return { pts: pts, f: f, a: a };
467
+ },
468
+ deform: function (i, d, t, p, o) {
469
+ var fr = (d.f[i] + t * 0.11) % 1, r = 0.95 * (1 - fr) + 0.05;
470
+ var ang = d.a[i] + (fr - d.f[i]) * TAU * 1.6 + t * 0.9; // three arms turn together; dots slide inwards along them
471
+ o[0] = Math.cos(ang) * r; o[1] = fr * fr * 0.7 - 0.25; o[2] = Math.sin(ang) * r;
472
+ },
473
+ alpha: function (i, d, t) { return 1.15 * Math.sin(((d.f[i] + t * 0.11) % 1) * Math.PI); },
474
+ size: function (i, d, t) { return 1.25 - 0.55 * ((d.f[i] + t * 0.11) % 1); }
475
+ });
476
+
477
+ // Active: a sphere whose surface wobbles slowly, like a drop of liquid.
478
+ define('blob', {
479
+ spin: 0.6,
480
+ build: function (n) { return { pts: fib(n) }; },
481
+ deform: function (i, d, t, p, o) {
482
+ var x = p[0], y = p[1], z = p[2], l = Math.sqrt(x * x + y * y + z * z) || 1;
483
+ x /= l; y /= l; z /= l;
484
+ 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);
485
+ var k = 0.78 + 0.11 * w;
486
+ o[0] = x * k; o[1] = y * k; o[2] = z * k;
487
+ }
488
+ });
489
+
490
+ // Listening: an eye that faces you. Radial fibres, a pupil that widens and narrows,
491
+ // a slow glance around and a blink now and then.
492
+ define('iris', {
493
+ face: true,
494
+ tilt: 0,
495
+ alpha: 1.5,
496
+ lineGap: 0.3,
497
+ build: function (n) {
498
+ var rim = Math.round(n * 0.22), pupil = Math.round(n * 0.18), fibres = n - rim - pupil;
499
+ var spokes = Math.max(6, Math.round(Math.sqrt(fibres * 4))), per = fibres / spokes;
500
+ var pts = [], kind = [], ang = [], f = [], i;
501
+ 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); }
502
+ 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); }
503
+ for (i = 0; i < fibres; i++) {
504
+ var s = Math.floor(i / per), c = s / spokes * TAU + (s % 2) * 0.08, q = (i - s * per) / per;
505
+ var r = 0.3 + q * 0.46;
506
+ pts.push([Math.cos(c) * r, Math.sin(c) * r, 0]); kind.push(2); ang.push(c); f.push(q);
507
+ }
508
+ return { pts: pts, kind: kind, ang: ang, f: f };
509
+ },
510
+ deform: function (i, d, t, p, o) {
511
+ var pr = 0.24 + 0.07 * Math.sin(t * 0.9) + 0.03 * Math.sin(t * 2.3); // pupil breathes
512
+ var r = d.kind[i] === 0 ? 0.86 : d.kind[i] === 1 ? pr : pr + 0.06 + d.f[i] * (0.76 - pr - 0.06);
513
+ var lx = d.kind[i] === 0 ? 0 : 0.07 * Math.sin(t * 0.37) + 0.03 * Math.sin(t * 1.1); // glance
514
+ var ly = d.kind[i] === 0 ? 0 : 0.04 * Math.sin(t * 0.29 + 1);
515
+ var ph = t % 5.5, lid = ph > 5.2 ? 1 - 0.92 * Math.sin((ph - 5.2) / 0.3 * Math.PI) : 1; // blink
516
+ o[0] = Math.cos(d.ang[i]) * r + lx; o[1] = (Math.sin(d.ang[i]) * r + ly) * lid; o[2] = 0;
517
+ },
518
+ 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]); }
519
+ });
520
+
521
+ // Ambient: four soft, large dots of different sizes, each circling on its own tilted path.
522
+ define('orbit', {
523
+ tilt: 0.5,
524
+ spin: 0.25,
525
+ build: function (n) {
526
+ 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]];
527
+ var pts = [], body = [], i;
528
+ var share = [0.4, 0.27, 0.2, 0.13];
529
+ for (i = 0; i < n; i++) {
530
+ var u = (i + 0.5) / n, b = 0, acc = share[0];
531
+ while (b < 3 && u > acc) { b++; acc += share[b]; }
532
+ body.push(b);
533
+ }
534
+ var count = [0, 0, 0, 0], seen = [0, 0, 0, 0];
535
+ body.forEach(function (b) { count[b]++; });
536
+ var balls = count.map(function (c) { return fib(Math.max(1, c)); });
537
+ for (i = 0; i < n; i++) {
538
+ var bb = body[i], q = balls[bb][seen[bb]++ % balls[bb].length], sz = B[bb][0] * 0.45;
539
+ pts.push([q[0] * sz, q[1] * sz, q[2] * sz]);
540
+ }
541
+ return { pts: pts, body: body, B: B };
542
+ },
543
+ deform: function (i, d, t, p, o) {
544
+ var b = d.B[d.body[i]], a = b[4] + t * b[3];
545
+ var cx = Math.cos(a) * b[1], cz = Math.sin(a) * b[1];
546
+ var cy = Math.sin(a) * b[1] * Math.sin(b[2]) * 0.6; // each path tilted its own way
547
+ o[0] = p[0] + cx; o[1] = p[1] + cy; o[2] = p[2] + cz;
548
+ },
549
+ bigDots: true,
550
+ alpha: 0.32, // many overlapping circles build up a soft, bright core
551
+ size: function (i, d) { return d.B[d.body[i]][0] * 55; }
552
+ });
553
+
554
+ // Status badges: a dotted ring with a mark inside. Check and cross share the ring,
555
+ // so going from one to the other only redraws the mark. Kept out of the default cycle.
556
+ function badge(marks) {
557
+ return function (n) {
558
+ var ring = Math.round(n * 0.6), rest = n - ring, pts = [], u = [], k = [], i;
559
+ for (i = 0; i < ring; i++) {
560
+ var a = i / ring * TAU - Math.PI / 2;
561
+ pts.push([Math.cos(a) * 0.86, Math.sin(a) * 0.86, 0]); u.push(i / ring); k.push(0);
562
+ }
563
+ for (var m = 0; m < marks.length; m++) {
564
+ var c = m === marks.length - 1 ? rest - Math.floor(rest / marks.length) * m : Math.floor(rest / marks.length);
565
+ var s = stroke(marks[m], c);
566
+ for (i = 0; i < c; i++) { pts.push(s.pts[i]); u.push(s.u[i]); k.push(1); }
567
+ }
568
+ return { pts: pts, u: u, k: k };
569
+ };
570
+ }
571
+ var badgeLook = {
572
+ face: true,
573
+ tilt: 0,
574
+ inCycle: false,
575
+ lineGap: 0.28,
576
+ deform: sway,
577
+ size: function (i, d) { return d.k[i] ? 1.35 : 0.85; },
578
+ alpha: function (i, d, t) {
579
+ if (d.k[i]) return 1.8;
580
+ 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
581
+ }
582
+ };
583
+ function look(build) { var o = { build: build }; for (var key in badgeLook) o[key] = badgeLook[key]; return o; }
584
+
585
+ define('check', look(badge([[[-0.4, 0.03], [-0.12, 0.31], [0.42, -0.27]]])));
586
+ define('cross', look(badge([[[-0.3, -0.3], [0.3, 0.3]], [[0.3, -0.3], [-0.3, 0.3]]])));
587
+
588
+ /* ── Named states: what an orb is doing, mapped to a shape and a pace ── */
589
+ var STATES = {
590
+ idle: { shape: 'orbit', speed: 0.25 },
591
+ listening: { shape: 'iris' },
592
+ thinking: { shape: 'vortex', speed: 0.5 },
593
+ working: { shape: 'ticks' },
594
+ speaking: { shape: 'bars' },
595
+ active: { shape: 'blob' },
596
+ loading: { shape: 'infinity' },
597
+ success: { shape: 'check' },
598
+ error: { shape: 'cross' }
599
+ };
600
+
601
+ /* ── Sound ── */
602
+ var AC = null;
603
+ function audioContext() { // one shared context for every orb
604
+ if (!AC) { var C = global.AudioContext || global.webkitAudioContext; if (C) AC = new C(); }
605
+ return AC;
606
+ }
607
+
608
+ /* ── Orb ── */
609
+ function Orb(canvas, opts) {
610
+ if (!(this instanceof Orb)) return new Orb(canvas, opts);
611
+ var ds = canvas.dataset;
612
+ opts = opts || {};
613
+ this.canvas = canvas;
614
+ this.ctx = canvas.getContext('2d');
615
+ this.style = (opts.style || ds.style) === 'lines' ? 'lines' : 'dots';
616
+ this.autoPoints = !(opts.points || ds.points);
617
+ this.n = +(opts.points || ds.points || (this.style === 'lines' ? 320 : 180));
618
+ this.line = +(opts.line || ds.line || 0);
619
+ this.glow = amount(opts.glow != null ? opts.glow : ds.glow, 8);
620
+ this.reflect = Math.min(1, amount(opts.reflect != null ? opts.reflect : ds.reflect, 0.35));
621
+ this.audioAmount = +(opts.audioAmount != null ? opts.audioAmount : (ds.audioAmount || 1));
622
+ this.audio = null;
623
+ this.speed = +(opts.speed != null ? opts.speed : (ds.speed || 0.35));
624
+ this.tiltAdd = (+(opts.tilt != null ? opts.tilt : (ds.tilt || 0)) || 0) * Math.PI / 180;
625
+ this.dot = +(opts.dot || ds.dot || 1.2);
626
+ this.cycle = +(opts.cycle || ds.cycle || 0);
627
+ this.order = (opts.shapes || ds.shapes || Object.keys(SHAPES).filter(function (s) { return SHAPES[s].inCycle !== false; }).join(','))
628
+ .split(',').map(function (s) { return s.trim(); }).filter(function (s) { return SHAPES[s]; });
629
+ this.baseSpeed = this.speed;
630
+ this.follow = !!(opts.follow || ds.follow !== undefined);
631
+ this.scrollTilt = !!(opts.scrollTilt || ds.scrollTilt !== undefined);
632
+ this.look = [0, 0]; this.lookGoal = [0, 0];
633
+ var st = STATES[opts.state || ds.state];
634
+ var first = opts.shape || ds.orb || (st && st.shape);
635
+ this.shape = SHAPES[first] ? first : this.order[0];
636
+ if (st && !(opts.shape || ds.orb)) this.shape = st.shape;
637
+ if (this.order.indexOf(this.shape) < 0) this.order.unshift(this.shape);
638
+
639
+ this.cache = {};
640
+ this.src = this.shape;
641
+ this.cur = this.data(this.shape).pts.map(function () { return [0, 0, 0]; }); // collapsed, for the intro
642
+ this.from = this.cur;
643
+ this.g = 1;
644
+ this.X = new Float32Array(this.n); this.Y = new Float32Array(this.n); this.F = new Float32Array(this.n);
645
+ this.tmp = [0, 0, 0]; this.pos = [0, 0, 0];
646
+ this.angle = 0.6; this.tilt = 0.38; this.spinMul = 1;
647
+ this.color = parseRGB(getComputedStyle(canvas).color);
648
+ this.colorWatchUntil = 0;
649
+ // With IntersectionObserver available, stay idle until it confirms we're on screen.
650
+ this.visible = !global.IntersectionObserver; this.running = false; this.last = 0; this.lastCycle = 0; this.t0 = performance.now();
651
+
652
+ this._frame = this.frame.bind(this);
653
+ this._resize = this.resize.bind(this);
654
+ this._vis = this.onVisibility.bind(this);
655
+ this.resize();
656
+ this.observe(opts, ds);
657
+ this.morph(this.shape, { intro: true });
658
+ if (st) { this.stateName = opts.state || ds.state; if (st.speed != null) this.speed = st.speed; }
659
+ }
660
+
661
+ Orb.define = define;
662
+ Orb.shapes = SHAPES;
663
+ Orb.states = STATES;
664
+ Orb.audioContext = audioContext;
665
+
666
+ Orb.prototype.data = function (name) {
667
+ return this.cache[name] || (this.cache[name] = SHAPES[name].build(this.n));
668
+ };
669
+
670
+ Orb.prototype.observe = function (opts, ds) {
671
+ var self = this;
672
+ if (global.ResizeObserver) { this.ro = new ResizeObserver(this._resize); this.ro.observe(this.canvas); }
673
+ else global.addEventListener('resize', this._resize);
674
+
675
+ if (global.IntersectionObserver) { // only animate while on screen
676
+ this.io = new IntersectionObserver(function (e) { self.visible = e[0].isIntersecting; self.wake(); });
677
+ this.io.observe(this.canvas);
678
+ }
679
+ document.addEventListener('visibilitychange', this._vis);
680
+
681
+ if ((opts.hover || ds.hover) === 'morph') {
682
+ var lastHover = 0;
683
+ this._hover = function () {
684
+ var now = performance.now();
685
+ if (now - lastHover > self.morphDur + 150) { lastHover = now; self.next(); }
686
+ };
687
+ var el = this.canvas.parentElement || this.canvas;
688
+ el.addEventListener('pointerenter', this._hover);
689
+ el.addEventListener('click', this._hover);
690
+ }
691
+
692
+ if ((opts.click || ds.click) === 'morph') {
693
+ this._click = function () { self.next(); };
694
+ (this.canvas.parentElement || this.canvas).addEventListener('click', this._click);
695
+ }
696
+
697
+ var media = opts.audio || ds.audio;
698
+ if (media) { // react to a media element once it plays (a play is a user gesture)
699
+ var el2 = typeof media === 'string' ? document.querySelector(media) : media;
700
+ if (el2) {
701
+ this._play = function () { if (!self.audio) self.listen(el2); };
702
+ el2.addEventListener('play', this._play);
703
+ this._media = el2;
704
+ if (!el2.paused) this._play();
705
+ }
706
+ }
707
+
708
+ // Changing data-state on the canvas switches state, so frameworks only need to set an attribute.
709
+ this.so = new MutationObserver(function () { var v = self.canvas.dataset.state; if (v && v !== self.stateName) self.state(v); });
710
+ this.so.observe(this.canvas, { attributes: true, attributeFilter: ['data-state'] });
711
+
712
+ // Lean towards the pointer: where it sits relative to the orb, as a share of the window.
713
+ this._pointer = function (e) {
714
+ if (!self.follow) return;
715
+ var r = self.canvas.getBoundingClientRect();
716
+ var dx = (e.clientX - (r.left + r.width / 2)) / (global.innerWidth / 2);
717
+ var dy = (e.clientY - (r.top + r.height / 2)) / (global.innerHeight / 2);
718
+ self.lookGoal[0] = Math.max(-1, Math.min(1, dx)); self.lookGoal[1] = Math.max(-1, Math.min(1, dy));
719
+ };
720
+ this._pointerOut = function () { self.lookGoal[0] = self.lookGoal[1] = 0; };
721
+ global.addEventListener('pointermove', this._pointer, { passive: true });
722
+ document.documentElement.addEventListener('pointerleave', this._pointerOut);
723
+
724
+ var themeMorph = opts.themeMorph || ds.themeMorph !== undefined;
725
+ this.mo = new MutationObserver(function () {
726
+ self.refreshColor();
727
+ if (themeMorph) self.next();
728
+ });
729
+ this.mo.observe(document.body, { attributes: true, attributeFilter: ['data-theme', 'class'] });
730
+ };
731
+
732
+ Orb.prototype.resize = function () {
733
+ var r = this.canvas.getBoundingClientRect(), dpr = Math.min(global.devicePixelRatio || 1, 2);
734
+ this.w = r.width; this.h = r.height;
735
+ this.canvas.width = Math.max(1, Math.round(r.width * dpr));
736
+ this.canvas.height = Math.max(1, Math.round(r.height * dpr));
737
+ this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
738
+ this.dpr = dpr;
739
+ this.fx = null; // effect canvases are rebuilt at the new size
740
+ this.draw();
741
+ };
742
+
743
+ // Follow the canvas's computed colour for a moment after a theme change. That way the dots
744
+ // track whatever CSS transition the page uses, instead of guessing one.
745
+ Orb.prototype.refreshColor = function () {
746
+ this.colorWatchUntil = performance.now() + 1500;
747
+ this.color = parseRGB(getComputedStyle(this.canvas).color);
748
+ this.wake();
749
+ if (reduceMQ.matches) { var self = this; setTimeout(function () { self.color = parseRGB(getComputedStyle(self.canvas).color); self.draw(); }, 1000); }
750
+ };
751
+
752
+ Orb.prototype.morph = function (name, o) {
753
+ if (!SHAPES[name]) return this;
754
+ o = o || {};
755
+ if (!o.intro && name === this.shape) return this;
756
+ // Interrupted mid-morph? Fade out whichever shape is currently dominant on screen.
757
+ if (!o.intro) this.src = (this.morphStart && this.g < 0.5) ? this.src : this.shape;
758
+ else this.src = name;
759
+ this.shape = name;
760
+ // Start every morph from exactly what is drawn right now, motion included, so nothing jumps.
761
+ this.from = this.cur.map(function (p) { return (p._p || p).slice(); });
762
+ this.cur = this.from.map(function (p) { return p.slice(); });
763
+ this.morphStart = performance.now();
764
+ this.morphDur = o.intro ? 1400 : 1100;
765
+ this.pendingIntro = !!o.intro; // the intro waits until the orb is first on screen
766
+ this.g = 0;
767
+ if (reduceMQ.matches) {
768
+ var t = this.data(name).pts;
769
+ this.cur = t.map(function (p) { return p.slice(); });
770
+ this.src = name; this.g = 1; this.morphStart = 0;
771
+ this.tilt = (SHAPES[name].tilt != null ? SHAPES[name].tilt : 0.38) + this.tiltAdd;
772
+ if (SHAPES[name].face) this.angle = Math.round(this.angle / TAU) * TAU;
773
+ this.draw();
774
+ }
775
+ this.wake();
776
+ if (!o.intro && global.CustomEvent) this.canvas.dispatchEvent(new CustomEvent('orb:morph', { detail: { shape: name } }));
777
+ return this;
778
+ };
779
+
780
+ // Change settings on a running orb. Changing the number of points (or the style, when no
781
+ // point count was given) rebuilds the shape in place, without the intro.
782
+ Orb.prototype.set = function (o) {
783
+ o = o || {};
784
+ var rebuild = false;
785
+ if (o.style != null) {
786
+ var st = o.style === 'lines' ? 'lines' : 'dots';
787
+ if (st !== this.style && o.points == null && this.autoPoints) { o.points = st === 'lines' ? 320 : 180; }
788
+ this.style = st;
789
+ }
790
+ if (o.points != null && +o.points !== this.n) { this.n = Math.max(8, +o.points | 0); this.autoPoints = false; rebuild = true; }
791
+ if (o.speed != null) { this.speed = +o.speed; this.baseSpeed = this.speed; }
792
+ if (o.follow != null) { this.follow = !!o.follow; if (!this.follow) this.lookGoal[0] = this.lookGoal[1] = 0; }
793
+ if (o.scrollTilt != null) this.scrollTilt = !!o.scrollTilt;
794
+ if (o.state != null) this.state(o.state);
795
+ if (o.dot != null) this.dot = +o.dot;
796
+ if (o.line != null) this.line = +o.line;
797
+ if (o.glow != null) this.glow = amount(o.glow, 8);
798
+ if (o.reflect != null) this.reflect = Math.min(1, amount(o.reflect, 0.35));
799
+ if (o.tilt != null) this.tiltAdd = (+o.tilt || 0) * Math.PI / 180;
800
+ if (o.audioAmount != null) this.audioAmount = +o.audioAmount;
801
+ if (o.cycle != null) { this.cycle = +o.cycle; this.lastCycle = 0; }
802
+ if (o.shapes != null) {
803
+ 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]; });
804
+ if (list.length) { this.order = list; if (list.indexOf(this.shape) < 0) this.order.unshift(this.shape); }
805
+ }
806
+ if (o.glow != null || o.reflect != null) { this.fx = null; this.floor = null; }
807
+ if (rebuild) {
808
+ this.cache = {};
809
+ this.X = new Float32Array(this.n); this.Y = new Float32Array(this.n); this.F = new Float32Array(this.n);
810
+ this.cur = this.data(this.shape).pts.map(function (p) { return p.slice(); });
811
+ this.from = this.cur; this.src = this.shape; this.g = 1; this.morphStart = 0;
812
+ }
813
+ if (o.shape != null && o.shape !== this.shape) this.morph(o.shape);
814
+ if (reduceMQ.matches) {
815
+ var def = SHAPES[this.shape];
816
+ this.tilt = (def.tilt != null ? def.tilt : 0.38) + this.tiltAdd;
817
+ }
818
+ this.refreshColor();
819
+ this.draw();
820
+ return this;
821
+ };
822
+
823
+ // Named states: morph to the state's shape and pace, and stop any cycling (a state is deliberate).
824
+ Orb.prototype.state = function (name) {
825
+ var st = STATES[name];
826
+ if (!st || !SHAPES[st.shape]) return this;
827
+ this.stateName = name;
828
+ this.cycle = 0;
829
+ this.speed = st.speed != null ? st.speed : this.baseSpeed;
830
+ if (this.canvas.dataset.state !== undefined && this.canvas.dataset.state !== name) this.canvas.dataset.state = name;
831
+ this.morph(st.shape);
832
+ if (global.CustomEvent) this.canvas.dispatchEvent(new CustomEvent('orb:state', { detail: { state: name, shape: st.shape } }));
833
+ return this;
834
+ };
835
+
836
+ Orb.prototype.next = function () {
837
+ var i = this.order.indexOf(this.shape);
838
+ return this.morph(this.order[(i + 1) % this.order.length]);
839
+ };
840
+
841
+ Orb.prototype.onVisibility = function () { if (!document.hidden) this.wake(); };
842
+
843
+ Orb.prototype.wake = function () {
844
+ if (this.running || !this.visible || document.hidden) return;
845
+ if (reduceMQ.matches) { this.draw(); return; }
846
+ this.running = true; this.last = performance.now();
847
+ this.color = parseRGB(getComputedStyle(this.canvas).color); // catch theme changes made while off screen
848
+ if (this.pendingIntro) { this.pendingIntro = false; this.morphStart = this.last; }
849
+ requestAnimationFrame(this._frame);
850
+ };
851
+
852
+ // Sound: pass an <audio> or <video>, a MediaStream, any Web Audio node, or 'mic'. Returns a promise.
853
+ // Media elements play through as normal; a microphone is analysed only, never played back.
854
+ Orb.prototype.listen = function (src) {
855
+ var self = this, ctx = audioContext();
856
+ this.unlisten();
857
+ if (!ctx) return Promise.reject(new Error('Web Audio is not supported'));
858
+ if (src === 'mic') {
859
+ if (!global.navigator || !navigator.mediaDevices) return Promise.reject(new Error('Microphone is not available'));
860
+ return navigator.mediaDevices.getUserMedia({ audio: true }).then(function (stream) {
861
+ return self.listen(stream).then(function () { self.audio.stream = stream; return self; });
862
+ });
863
+ }
864
+ var node;
865
+ if (global.MediaStream && src instanceof MediaStream) node = ctx.createMediaStreamSource(src);
866
+ else if (src && /^(AUDIO|VIDEO)$/.test(src.nodeName)) {
867
+ // An element can only be wired into Web Audio once, so the source is kept on the element.
868
+ if (!src._orbNode) { src._orbNode = ctx.createMediaElementSource(src); src._orbNode.connect(ctx.destination); }
869
+ node = src._orbNode;
870
+ } else if (src && src.connect) node = src;
871
+ else return Promise.reject(new Error('Unsupported audio source'));
872
+ if (ctx.state === 'suspended') ctx.resume();
873
+ var an = ctx.createAnalyser();
874
+ an.fftSize = 256; an.smoothingTimeConstant = 0.55;
875
+ node.connect(an);
876
+ this.audio = { node: node, an: an, bins: new Uint8Array(an.frequencyBinCount), bands: new Float32Array(16), level: 0 };
877
+ this.wake();
878
+ return Promise.resolve(this);
879
+ };
880
+
881
+ Orb.prototype.unlisten = function () {
882
+ var A = this.audio;
883
+ if (!A) return this;
884
+ try { A.node.disconnect(A.an); } catch (e) { /* already disconnected */ }
885
+ if (A.stream) A.stream.getTracks().forEach(function (tr) { tr.stop(); });
886
+ this.audio = null;
887
+ return this;
888
+ };
889
+
890
+ // 16 bands, spaced like hearing (more detail in the lows), with a quick rise and a slower fall.
891
+ Orb.prototype.sampleAudio = function () {
892
+ var A = this.audio, bins = A.bins, B = A.bands, sum = 0;
893
+ A.an.getByteFrequencyData(bins);
894
+ for (var k = 0; k < 16; k++) {
895
+ 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));
896
+ var v = 0;
897
+ for (var j = lo; j < hi; j++) v += bins[j];
898
+ v = Math.min(1, Math.pow(v / (hi - lo) / 255, 1.3) * (1 + k * 0.06) * this.audioAmount); // lift the quieter highs
899
+ B[k] += (v - B[k]) * (v > B[k] ? 0.6 : 0.15);
900
+ sum += B[k];
901
+ }
902
+ A.level += (sum / 16 - A.level) * 0.3;
903
+ };
904
+
905
+ Orb.prototype.frame = function (now) {
906
+ if (!this.visible || document.hidden || reduceMQ.matches) { this.running = false; return; }
907
+ var dt = Math.min(0.05, (now - this.last) / 1000); this.last = now;
908
+ if (this.audio) this.sampleAudio();
909
+ var def = SHAPES[this.shape];
910
+ var tiltGoal = (def.tilt != null ? def.tilt : 0.38) + this.tiltAdd, spinGoal = def.spin != null ? def.spin : 1;
911
+ var ease = Math.min(1, dt * 2.5);
912
+ this.tilt += (tiltGoal - this.tilt) * ease;
913
+ this.spinMul += (spinGoal - this.spinMul) * ease;
914
+ if (def.face) { var home = Math.round(this.angle / TAU) * TAU; this.angle += (home - this.angle) * ease; } // turn to face the viewer
915
+ else this.angle += this.speed * this.spinMul * dt;
916
+
917
+ // Cursor and scroll: a gentle lean on top of the shape's own motion, eased so it never snaps.
918
+ var gx = this.follow ? this.lookGoal[0] * 0.55 : 0, gy = this.follow ? this.lookGoal[1] * 0.4 : 0;
919
+ if (this.scrollTilt) {
920
+ var rr = this.canvas.getBoundingClientRect(), vh = global.innerHeight || 1;
921
+ gy += Math.max(-1, Math.min(1, (rr.top + rr.height / 2 - vh / 2) / (vh / 2))) * 0.5;
922
+ }
923
+ var le = Math.min(1, dt * 3);
924
+ this.look[0] += (gx - this.look[0]) * le; this.look[1] += (gy - this.look[1]) * le;
925
+
926
+ // Morph: dots leave top to bottom in a soft cascade, with a slight outward swell mid-flight.
927
+ if (this.morphStart) {
928
+ var raw = (now - this.morphStart) / this.morphDur, tgt = this.data(this.shape).pts, done = true;
929
+ for (var i = 0; i < this.n; i++) {
930
+ var b = tgt[i], a = this.from[i], c = this.cur[i];
931
+ var delay = (1 - (Math.max(-1, Math.min(1, b[1])) + 1) / 2) * 0.25;
932
+ var t = Math.min(1, Math.max(0, (raw - delay) / 0.75));
933
+ if (t < 1) done = false;
934
+ var e = easeInOut(t), swell = 1 + Math.sin(e * Math.PI) * 0.08;
935
+ c[0] = (a[0] + (b[0] - a[0]) * e) * swell;
936
+ c[1] = (a[1] + (b[1] - a[1]) * e) * swell;
937
+ c[2] = (a[2] + (b[2] - a[2]) * e) * swell;
938
+ }
939
+ this.g = easeInOut(Math.min(1, raw));
940
+ if (done) { this.morphStart = 0; this.g = 1; this.src = this.shape; }
941
+ }
942
+
943
+ if (this.colorWatchUntil && now < this.colorWatchUntil) this.color = parseRGB(getComputedStyle(this.canvas).color);
944
+
945
+ if (this.cycle && now - this.lastCycle > this.cycle && !this.morphStart) {
946
+ if (this.lastCycle) this.next();
947
+ this.lastCycle = now;
948
+ }
949
+
950
+ this.draw(now);
951
+ requestAnimationFrame(this._frame);
952
+ };
953
+
954
+ Orb.prototype.draw = function (now) {
955
+ var ctx = this.ctx, w = this.w, h = this.h;
956
+ if (!w || !h) return;
957
+ ctx.clearRect(0, 0, w, h);
958
+
959
+ var t = reduceMQ.matches ? 0 : ((now || performance.now()) - this.t0) / 1000;
960
+ var S = SHAPES[this.src], T = SHAPES[this.shape], sd = this.data(this.src), td = this.data(this.shape);
961
+ var g = this.g, gs = 1 - g;
962
+ var R = Math.min(w, h) * (this.reflect ? 0.33 : 0.36), cx = w / 2, cy = this.reflect ? h * 0.4 : h / 2;
963
+ var yaw = this.angle + this.look[0], pitch = this.tilt + this.look[1];
964
+ var ca = Math.cos(yaw), sa = Math.sin(yaw), ct = Math.cos(pitch), st = Math.sin(pitch);
965
+ var col = this.color, rgb = 'rgba(' + (col[0] | 0) + ',' + (col[1] | 0) + ',' + (col[2] | 0) + ',';
966
+ var dot = this.dot * (0.5 + 0.5 * Math.min(1, Math.min(w, h) / 90)); // airier on small canvases
967
+ var X = this.X, Y = this.Y, F = this.F, pos = this.pos, tmp = this.tmp, n = this.n;
968
+
969
+ function project(p, out) {
970
+ var x = p[0] * ca - p[2] * sa, z = p[0] * sa + p[2] * ca; // spin around Y
971
+ var y = p[1] * ct - z * st; z = p[1] * st + z * ct; // tilt around X
972
+ var persp = 1 / (1 - z * 0.12);
973
+ out[0] = cx + x * R * persp; out[1] = cy + y * R * persp; out[2] = (z + 1) / 2;
974
+ }
975
+ function blendDeform(def, d, weight, i, base) {
976
+ if (!def.deform || weight <= 0) return;
977
+ def.deform(i, d, t, base, tmp);
978
+ pos[0] += (tmp[0] - base[0]) * weight; pos[1] += (tmp[1] - base[1]) * weight; pos[2] += (tmp[2] - base[2]) * weight;
979
+ }
980
+
981
+ // Sound: shapes with their own audio() use it; flat marks only pulse; the rest also push out on the beat.
982
+ var A = this.audio && !reduceMQ.matches ? this.audio : null, amt = this.audioAmount;
983
+ var pulse = A ? 1 + A.level * 0.1 : 1;
984
+ function react(i, def, d, weight) {
985
+ if (def.audio) {
986
+ tmp[0] = pos[0]; tmp[1] = pos[1]; tmp[2] = pos[2];
987
+ def.audio(i, d, A, tmp, tmp);
988
+ pos[0] += (tmp[0] - pos[0]) * weight; pos[1] += (tmp[1] - pos[1]) * weight; pos[2] += (tmp[2] - pos[2]) * weight;
989
+ return;
990
+ }
991
+ var k = pulse;
992
+ if (!def.face) {
993
+ var u = Math.atan2(pos[2], pos[0]) / TAU + 0.5; // low notes one side, high notes the other
994
+ k += A.bands[Math.min(15, Math.floor((1 - Math.abs(2 * u - 1)) * 16))] * 0.22 * Math.min(1, amt);
995
+ }
996
+ k = 1 + (k - 1) * weight;
997
+ pos[0] *= k; pos[1] *= k; pos[2] *= k;
998
+ }
999
+
1000
+ // 1. positions: base + each shape's live motion, weighted by how far the morph has got
1001
+ var proj = [0, 0, 0];
1002
+ for (var i = 0; i < n; i++) {
1003
+ var base = this.cur[i];
1004
+ pos[0] = base[0]; pos[1] = base[1]; pos[2] = base[2];
1005
+ blendDeform(T, td, g, i, base);
1006
+ if (A) react(i, T, td, 1);
1007
+ project(pos, proj);
1008
+ X[i] = proj[0]; Y[i] = proj[1]; F[i] = proj[2];
1009
+ this.cur[i]._p = this.cur[i]._p || [0, 0, 0];
1010
+ this.cur[i]._p[0] = pos[0]; this.cur[i]._p[1] = pos[1]; this.cur[i]._p[2] = pos[2];
1011
+ }
1012
+
1013
+ if (this.style === 'lines') { this.effects(this.slices(t, S, T, sd, td, g, R, dot)); return; }
1014
+
1015
+ // 2. connecting lines (constellation, helix rungs, cluster links) fade with the morph
1016
+ ctx.lineWidth = this.line || Math.max(0.5, dot * 0.45);
1017
+ ctx.lineCap = 'round';
1018
+ function edges(d, def, weight) {
1019
+ if (!d.edges || weight <= 0.01) return;
1020
+ var ea = (def.edgeAlpha != null ? def.edgeAlpha : 0.3) * weight;
1021
+ for (var e = 0; e < d.edges.length; e++) {
1022
+ var a = d.edges[e][0], b = d.edges[e][1];
1023
+ ctx.strokeStyle = rgb + (ea * (0.25 + 0.75 * (F[a] + F[b]) / 2)).toFixed(3) + ')';
1024
+ ctx.beginPath(); ctx.moveTo(X[a], Y[a]); ctx.lineTo(X[b], Y[b]); ctx.stroke();
1025
+ }
1026
+ }
1027
+ edges(sd, S, gs);
1028
+ edges(td, T, g);
1029
+
1030
+ function dotR(def, d, i, t, p, front) {
1031
+ var sz = val(def.size, i, d, t, p);
1032
+ return def.bigDots ? R * 0.01 * sz * (0.75 + front * 0.25) : dot * (0.45 + front * 0.85) * sz;
1033
+ }
1034
+
1035
+ // 3. dots and dashes, back to front is not needed: alpha already fades the far side
1036
+ var end = [0, 0, 0], low = -1e9, lift = A ? 0.8 + A.level * 0.6 : 1;
1037
+ for (i = 0; i < n; i++) {
1038
+ var p = this.cur[i]._p, front = F[i];
1039
+ var alpha = gs * val(S.alpha, i, sd, t, p) + g * val(T.alpha, i, td, t, p);
1040
+ var dash = (S.dash ? gs * S.dash(i, sd, t, p) : 0) + (T.dash ? g * T.dash(i, td, t, p) : 0);
1041
+ // Most shapes size dots in pixels; bigDots shapes size them as a share of the orb, so they scale with it.
1042
+ var r = gs * dotR(S, sd, i, t, p, front) + g * dotR(T, td, i, t, p, front);
1043
+ var a2 = Math.max(0, Math.min(1, (0.12 + front * 0.88) * alpha * lift));
1044
+ if (dash > 0.004) {
1045
+ tmp[0] = p[0] * (1 + dash); tmp[1] = p[1] * (1 + dash); tmp[2] = p[2] * (1 + dash);
1046
+ project(tmp, end);
1047
+ ctx.strokeStyle = rgb + a2.toFixed(3) + ')';
1048
+ ctx.lineWidth = r * 1.4;
1049
+ ctx.beginPath(); ctx.moveTo(X[i], Y[i]); ctx.lineTo(end[0], end[1]); ctx.stroke();
1050
+ } else {
1051
+ ctx.fillStyle = rgb + a2.toFixed(3) + ')';
1052
+ ctx.beginPath(); ctx.arc(X[i], Y[i], r, 0, TAU); ctx.fill();
1053
+ if (a2 > 0.08 && Y[i] + r > low) low = Y[i] + r;
1054
+ }
1055
+ }
1056
+ this.effects(low);
1057
+ };
1058
+
1059
+ // Lines style: sort the projected dots into vertical columns, then join dots that sit close
1060
+ // together in a column into one line, lit by the dots along it. Solid shapes become sliced
1061
+ // silhouettes; rings, holes and marks keep their gaps.
1062
+ Orb.prototype.slices = function (t, S, T, sd, td, g, R, dot) {
1063
+ var ctx = this.ctx, n = this.n, X = this.X, Y = this.Y, F = this.F, gs = 1 - g;
1064
+ var cols = Math.max(20, Math.min(90, Math.round(n / 5)));
1065
+ var sp = Math.max(3, 2.3 * R / cols), lw = this.line || Math.max(1, sp * 0.46);
1066
+ var gap = R * (g < 0.5 ? (S.lineGap || 0.45) : (T.lineGap || 0.45)), cap = Math.max(lw, R * 0.025), low = -1e9;
1067
+ var col = this.color, rgb = 'rgba(' + (col[0] | 0) + ',' + (col[1] | 0) + ',' + (col[2] | 0) + ',';
1068
+ var C = this.sC || (this.sC = []), Ys = this.sY || (this.sY = []), A = this.sA || (this.sA = []), idx = [], m = 0;
1069
+ for (var i = 0; i < n; i++) {
1070
+ var p = this.cur[i]._p;
1071
+ var alpha = gs * val(S.alpha, i, sd, t, p) + g * val(T.alpha, i, td, t, p);
1072
+ 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
1073
+ if (a < 0.02) continue;
1074
+ if (a > 0.08 && Y[i] > low) low = Y[i];
1075
+ var fx = X[i] / sp, c0 = Math.floor(fx), fr = fx - c0; // share each dot between its two nearest
1076
+ if (fr < 0.97) { C[m] = c0; Ys[m] = Y[i]; A[m] = a * (1 - fr); idx.push(m++); } // columns, so motion stays smooth
1077
+ if (fr > 0.03) { C[m] = c0 + 1; Ys[m] = Y[i]; A[m] = a * fr; idx.push(m++); }
1078
+ }
1079
+ idx.sort(function (u, v) { return C[u] - C[v] || Ys[u] - Ys[v]; });
1080
+ ctx.lineWidth = lw; ctx.lineCap = 'round';
1081
+ for (var s = 0; s < idx.length;) {
1082
+ var e = s; // grow a run of close dots in one column
1083
+ while (e + 1 < idx.length && C[idx[e + 1]] === C[idx[s]] && Ys[idx[e + 1]] - Ys[idx[e]] < gap) e++;
1084
+ var x = C[idx[s]] * sp, y0 = Ys[idx[s]] - cap, y1 = Ys[idx[e]] + cap;
1085
+ if (e === s) ctx.strokeStyle = rgb + A[idx[s]].toFixed(3) + ')';
1086
+ else {
1087
+ var grad = ctx.createLinearGradient(0, y0, 0, y1);
1088
+ for (var k = s; k <= e; k++) grad.addColorStop((Ys[idx[k]] - y0) / (y1 - y0), rgb + A[idx[k]].toFixed(3) + ')');
1089
+ ctx.strokeStyle = grad;
1090
+ }
1091
+ ctx.beginPath(); ctx.moveTo(x, y0); ctx.lineTo(x, y1); ctx.stroke();
1092
+ s = e + 1;
1093
+ }
1094
+ return low + cap;
1095
+ };
1096
+
1097
+ // Glow and reflection, drawn from a copy of the finished frame. Both are off unless asked for.
1098
+ Orb.prototype.effects = function (low) {
1099
+ if (!this.glow && !this.reflect) return;
1100
+ // The reflection sits just under the shape's lowest point, eased so it glides rather than jitters.
1101
+ if (low > -1e8) this.floor = this.floor == null || reduceMQ.matches ? low : this.floor + (low - this.floor) * 0.08;
1102
+ var floor = (this.floor || this.h * 0.75) + 4;
1103
+ var ctx = this.ctx, cv = this.canvas, W = cv.width, H = cv.height, dpr = this.dpr || 1;
1104
+ if (!this.fx) {
1105
+ var mk = function (k) { var c = document.createElement('canvas'); c.width = Math.ceil(W / k); c.height = Math.ceil(H / k); return c; };
1106
+ this.fx = { copy: mk(1), mirror: mk(1), small: mk(4) };
1107
+ }
1108
+ var copy = this.fx.copy, cc = copy.getContext('2d');
1109
+ cc.clearRect(0, 0, W, H); cc.drawImage(cv, 0, 0);
1110
+ ctx.save();
1111
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
1112
+ if (this.glow && 'filter' in ctx) { // blur a quarter-size copy (cheap), then lay it behind
1113
+ var sm = this.fx.small, sc = sm.getContext('2d');
1114
+ sc.clearRect(0, 0, sm.width, sm.height);
1115
+ sc.filter = 'blur(' + (this.glow * dpr / 4).toFixed(2) + 'px)';
1116
+ sc.drawImage(copy, 0, 0, sm.width, sm.height);
1117
+ sc.filter = 'none';
1118
+ ctx.globalCompositeOperation = 'destination-over';
1119
+ ctx.drawImage(sm, 0, 0, W, H);
1120
+ ctx.globalCompositeOperation = 'source-over';
1121
+ }
1122
+ if (this.reflect) { // flip about the floor, squash, then fade out downwards
1123
+ var mir = this.fx.mirror, mc = mir.getContext('2d'), fy = floor * dpr, sq = 0.55;
1124
+ mc.setTransform(1, 0, 0, 1, 0, 0); mc.globalCompositeOperation = 'source-over';
1125
+ mc.clearRect(0, 0, W, H);
1126
+ mc.setTransform(1, 0, 0, -sq, 0, fy * (1 + sq));
1127
+ mc.drawImage(copy, 0, 0);
1128
+ mc.setTransform(1, 0, 0, 1, 0, 0);
1129
+ mc.globalCompositeOperation = 'destination-in';
1130
+ var fade = mc.createLinearGradient(0, fy, 0, Math.min(H, fy + (H - fy) * 0.95));
1131
+ fade.addColorStop(0, 'rgba(0,0,0,' + this.reflect + ')'); fade.addColorStop(1, 'rgba(0,0,0,0)');
1132
+ mc.fillStyle = fade; mc.fillRect(0, fy, W, H - fy);
1133
+ mc.clearRect(0, 0, W, fy);
1134
+ ctx.drawImage(mir, 0, 0);
1135
+ }
1136
+ ctx.restore();
1137
+ };
1138
+
1139
+ Orb.prototype.destroy = function () {
1140
+ this.visible = false;
1141
+ this.unlisten();
1142
+ if (this._play) this._media.removeEventListener('play', this._play);
1143
+ if (this.ro) this.ro.disconnect(); else global.removeEventListener('resize', this._resize);
1144
+ if (this.io) this.io.disconnect();
1145
+ if (this.mo) this.mo.disconnect();
1146
+ if (this.so) this.so.disconnect();
1147
+ global.removeEventListener('pointermove', this._pointer);
1148
+ document.documentElement.removeEventListener('pointerleave', this._pointerOut);
1149
+ document.removeEventListener('visibilitychange', this._vis);
1150
+ if (this._hover) {
1151
+ var el = this.canvas.parentElement || this.canvas;
1152
+ el.removeEventListener('pointerenter', this._hover); el.removeEventListener('click', this._hover);
1153
+ }
1154
+ if (this._click) (this.canvas.parentElement || this.canvas).removeEventListener('click', this._click);
1155
+ };
1156
+
1157
+ /* ── Auto-init every <canvas data-orb> ── */
1158
+ function init(root) {
1159
+ var els = (root && root.querySelectorAll ? root : document).querySelectorAll('canvas[data-orb]');
1160
+ for (var i = 0; i < els.length; i++) {
1161
+ if (els[i]._orb) continue;
1162
+ els[i]._orb = new Orb(els[i]);
1163
+ if (els[i].parentElement) els[i].parentElement.classList.add('orb-ready'); // hide fallback glyph only once running
1164
+ }
1165
+ }
1166
+ Orb.init = init;
1167
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', function () { init(); });
1168
+ else init();
1169
+
1170
+ global.Orb = Orb;
1171
+ if (typeof module === 'object' && module.exports) module.exports = Orb; // npm and bundlers
1172
+ })(typeof window !== 'undefined' ? window : undefined);