cascading-reel 1.0.2 → 2.0.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/dist/index.es.js DELETED
@@ -1,1069 +0,0 @@
1
- var U = [
2
- 0.04,
3
- 0,
4
- -0.04
5
- ], S = {
6
- columnStaggerMs: 76,
7
- fallMs: 800,
8
- outroOverlapMs: 88,
9
- outroRowGapMs: 14,
10
- rowBaseSpacingRatio: 0.05,
11
- incomingAlphaRampMs: 34,
12
- fixedStepMs: 1e3 / 120,
13
- maxCatchUpStepsPerFrame: 6
14
- }, nt = S.outroRowGapMs, ot = S.rowBaseSpacingRatio, v = 1800, N = 0.15, L = [
15
- 255,
16
- 235,
17
- 110
18
- ], lt = S.columnStaggerMs, ht = S.fallMs, at = S.outroOverlapMs;
19
- function M(e, t, i) {
20
- return e < t ? t : e > i ? i : e;
21
- }
22
- function q(e) {
23
- return Math.floor(Math.random() * e);
24
- }
25
- function b(e, t) {
26
- return (e % t + t) % t;
27
- }
28
- function R(e) {
29
- return M(Math.round(e), 0, 255);
30
- }
31
- function T(e) {
32
- const t = [];
33
- for (let i = 0; i < 3; i += 1) {
34
- const s = [];
35
- for (let r = 0; r < 3; r += 1) s.push(q(e));
36
- t.push(s);
37
- }
38
- return t;
39
- }
40
- function W(e) {
41
- const t = /* @__PURE__ */ new Map();
42
- for (let n = 0; n < 3; n += 1) for (let o = 0; o < 3; o += 1) {
43
- const l = e[n][o];
44
- t.set(l, (t.get(l) ?? 0) + 1);
45
- }
46
- let i = e[0][0], s = -1;
47
- for (const [n, o] of t.entries()) o > s && (s = o, i = n);
48
- const r = [];
49
- for (let n = 0; n < 3; n += 1) for (let o = 0; o < 3; o += 1) e[n][o] === i && r.push({
50
- col: n,
51
- row: o
52
- });
53
- return r;
54
- }
55
- function g() {
56
- return Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => 0));
57
- }
58
- function m(e, t) {
59
- for (let i = 0; i < 3; i += 1) for (let s = 0; s < 3; s += 1) e[i][s] = t;
60
- }
61
- var z = class {
62
- rafId = null;
63
- step = null;
64
- start(e) {
65
- this.rafId === null && (this.step = e, this.rafId = requestAnimationFrame(this.tick));
66
- }
67
- stop() {
68
- this.rafId !== null && (cancelAnimationFrame(this.rafId), this.rafId = null), this.step = null;
69
- }
70
- isRunning() {
71
- return this.rafId !== null;
72
- }
73
- tick = (e) => {
74
- if (!this.step) {
75
- this.stop();
76
- return;
77
- }
78
- if (this.step(e) === !1) {
79
- this.stop();
80
- return;
81
- }
82
- this.rafId !== null && (this.rafId = requestAnimationFrame(this.tick));
83
- };
84
- };
85
- function X(e) {
86
- const t = M(e, 0, 1);
87
- return t * t * t * (t * (t * 6 - 15) + 10);
88
- }
89
- function y(e, t) {
90
- if (e.endMs <= e.startMs) return {
91
- value: e.to,
92
- t: 1,
93
- done: !0
94
- };
95
- const i = M((t - e.startMs) / (e.endMs - e.startMs), 0, 1), s = X(i);
96
- return {
97
- value: e.from + (e.to - e.from) * s,
98
- t: i,
99
- done: i >= 1
100
- };
101
- }
102
- function k(e, t, i, s) {
103
- const r = [
104
- 0,
105
- 0,
106
- 0
107
- ];
108
- let n = 0;
109
- for (let o = 2; o >= 0; o -= 1) {
110
- if (e[o] === 0) {
111
- r[o] = 0;
112
- continue;
113
- }
114
- r[o] = n;
115
- const l = Math.floor(t * s);
116
- n += l + i;
117
- }
118
- return r;
119
- }
120
- function V(e) {
121
- const t = e.height - e.boardY + e.cellH + 2, i = [
122
- t,
123
- t,
124
- t
125
- ];
126
- return {
127
- columnStaggerMs: e.motionProfile.columnStaggerMs,
128
- fallMs: e.motionProfile.fallMs,
129
- incomingAlphaRampMs: e.motionProfile.incomingAlphaRampMs,
130
- outgoingDistance: t,
131
- incomingFromOffsets: [
132
- -e.cellH,
133
- -e.cellH * 2,
134
- -e.cellH * 3
135
- ],
136
- rowStartDelays: k(i, e.motionProfile.fallMs, e.motionProfile.outroRowGapMs, e.motionProfile.rowBaseSpacingRatio),
137
- incomingStartShift: Math.max(0, e.motionProfile.fallMs - e.motionProfile.outroOverlapMs)
138
- };
139
- }
140
- function D(e) {
141
- let t = !0, i = !0;
142
- for (let s = 0; s < e.scriptedOutgoingOffsets.length; s += 1) {
143
- const r = e.elapsedMs - s * e.motionPlan.columnStaggerMs;
144
- for (let n = 0; n < 3; n += 1) {
145
- const o = r - e.motionPlan.rowStartDelays[n];
146
- if (o <= 0) {
147
- e.scriptedOutgoingOffsets[s][n] = 0, e.scriptedIncomingOffsets[s][n] = e.motionPlan.incomingFromOffsets[n], e.scriptedIncomingAlpha[s][n] = 0, e.scriptedIncomingVisibility[s][n] = "hidden", t = !1, i = !1;
148
- continue;
149
- }
150
- const l = y({
151
- startMs: 0,
152
- endMs: e.motionPlan.fallMs,
153
- from: 0,
154
- to: e.motionPlan.outgoingDistance
155
- }, o);
156
- e.scriptedOutgoingOffsets[s][n] = l.value, e.scriptedIncomingVisibility[s][n] = l.done ? "entering" : "exiting", l.done || (t = !1);
157
- const u = o - e.motionPlan.incomingStartShift;
158
- if (u <= 0) {
159
- e.scriptedIncomingOffsets[s][n] = e.motionPlan.incomingFromOffsets[n], e.scriptedIncomingAlpha[s][n] = 0, e.scriptedIncomingVisibility[s][n] = "hidden", i = !1;
160
- continue;
161
- }
162
- const c = y({
163
- startMs: 0,
164
- endMs: e.motionPlan.fallMs,
165
- from: e.motionPlan.incomingFromOffsets[n],
166
- to: 0
167
- }, u);
168
- e.scriptedIncomingOffsets[s][n] = c.value, e.scriptedIncomingAlpha[s][n] = M(u / Math.max(1, e.motionPlan.incomingAlphaRampMs), 0, 1), e.scriptedIncomingVisibility[s][n] = c.done ? "active" : "entering", c.done || (i = !1);
169
- }
170
- }
171
- return {
172
- allOutgoingDone: t,
173
- allIncomingDone: i
174
- };
175
- }
176
- function Y(e) {
177
- if (e === "rainbow") return {
178
- mode: "rainbow",
179
- rgb: L
180
- };
181
- const t = e ?? L;
182
- return {
183
- mode: "solid",
184
- rgb: [
185
- R(t[0]),
186
- R(t[1]),
187
- R(t[2])
188
- ]
189
- };
190
- }
191
- function $(e, t) {
192
- return typeof e != "number" || !Number.isFinite(e) ? t : M(e, 0.5, 1.2);
193
- }
194
- function x(e) {
195
- if (e.length !== 3) throw new Error("rows must contain 3 rows");
196
- for (let t = 0; t < 3; t += 1) if (!Array.isArray(e[t]) || e[t].length !== 3) throw new Error(`rows[${t}] must contain 3 columns`);
197
- return [
198
- [
199
- e[0][0],
200
- e[1][0],
201
- e[2][0]
202
- ],
203
- [
204
- e[0][1],
205
- e[1][1],
206
- e[2][1]
207
- ],
208
- [
209
- e[0][2],
210
- e[1][2],
211
- e[2][2]
212
- ]
213
- ];
214
- }
215
- function C(e, t) {
216
- if (e.length !== 3) throw new Error("stopGrid must contain 3 columns");
217
- const i = [];
218
- for (let s = 0; s < 3; s += 1) {
219
- const r = e[s];
220
- if (!Array.isArray(r) || r.length !== 3) throw new Error(`stopGrid[${s}] must contain 3 rows`);
221
- i[s] = [
222
- b(r[0], t),
223
- b(r[1], t),
224
- b(r[2], t)
225
- ];
226
- }
227
- return i;
228
- }
229
- function K(e, t) {
230
- return C(x(e), t);
231
- }
232
- function Q(e) {
233
- return {
234
- stopGrid: e.stopGrid?.map((t) => [...t]),
235
- stopRows: e.stopRows?.map((t) => [...t]),
236
- finaleSequence: e.finaleSequence?.map((t) => t.map((i) => [...i])),
237
- finaleSequenceRows: e.finaleSequenceRows?.map((t) => t.map((i) => [...i])),
238
- highlightWin: e.highlightWin,
239
- callback: e.callback
240
- };
241
- }
242
- var j = class {
243
- queue;
244
- constructor(e) {
245
- this.queue = (e ?? []).map((t) => Q(t));
246
- }
247
- hasPending() {
248
- return this.queue.length > 0;
249
- }
250
- consume() {
251
- return this.queue.length === 0 ? null : this.queue.shift() ?? null;
252
- }
253
- };
254
- function Z() {
255
- return {
256
- isSpinning: !1,
257
- hasStartedFirstSpin: !1,
258
- queueFinished: !1,
259
- shouldHighlightCurrentSpin: !1,
260
- activeSpinState: null,
261
- phase: "idle",
262
- winFlashStartedAt: 0,
263
- outroStartedAt: 0,
264
- idleStartedAt: 0,
265
- preSpinStartedAt: 0,
266
- winEffectsEnvelope: 1
267
- };
268
- }
269
- function J(e, t) {
270
- e.hasStartedFirstSpin = !0, e.isSpinning = !0, e.phase = "outro", e.outroStartedAt = t.startedAt, e.activeSpinState = t.activeSpinState, e.shouldHighlightCurrentSpin = t.shouldHighlightCurrentSpin;
271
- }
272
- function tt(e, t, i) {
273
- e.phase = "idle", e.idleStartedAt = i, e.isSpinning = !1, e.shouldHighlightCurrentSpin = !1, e.queueFinished = !t, e.activeSpinState = null;
274
- }
275
- function F(e, t) {
276
- e.winFlashStartedAt = t, e.phase = "winFlash";
277
- }
278
- function it(e) {
279
- e.isSpinning = !1, e.queueFinished = !0;
280
- }
281
- var et = `
282
- attribute vec2 a_pos;
283
- uniform vec2 u_resolution;
284
- uniform mediump vec4 u_destRect;
285
- varying vec2 v_uv;
286
-
287
- void main() {
288
- vec2 local = a_pos;
289
- vec2 pixel = vec2(
290
- u_destRect.x + local.x * u_destRect.z,
291
- u_destRect.y + local.y * u_destRect.w
292
- );
293
-
294
- vec2 zeroToOne = pixel / u_resolution;
295
- vec2 clip = vec2(
296
- zeroToOne.x * 2.0 - 1.0,
297
- 1.0 - zeroToOne.y * 2.0
298
- );
299
-
300
- gl_Position = vec4(clip, 0.0, 1.0);
301
- v_uv = local;
302
- }
303
- `, st = `
304
- precision mediump float;
305
-
306
- uniform sampler2D u_texture;
307
- uniform mediump vec4 u_destRect;
308
- uniform vec4 u_srcRect;
309
- uniform vec4 u_color;
310
- uniform float u_useTexture;
311
- uniform float u_shapeMode;
312
- uniform float u_time;
313
- uniform float u_borderPx;
314
- uniform float u_borderInsetPx;
315
- uniform float u_cornerRadiusPx;
316
- uniform float u_noiseAmp;
317
- uniform float u_pulseStrength;
318
- varying vec2 v_uv;
319
-
320
- float hash1(float n) {
321
- return fract(sin(n) * 43758.5453);
322
- }
323
-
324
- void main() {
325
- if (u_shapeMode > 1.5) {
326
- vec2 sizePx = max(vec2(1.0), u_destRect.zw);
327
- vec2 px = v_uv * sizePx;
328
- float cornerRadius = clamp(u_cornerRadiusPx, 0.0, max(0.0, min(sizePx.x, sizePx.y) * 0.5 - 0.01));
329
-
330
- vec2 halfSize = sizePx * 0.5;
331
- vec2 p = px - halfSize;
332
- vec2 q = abs(p) - (halfSize - vec2(cornerRadius));
333
- float outside = length(max(q, vec2(0.0)));
334
- float inside = min(max(q.x, q.y), 0.0);
335
- float d = -(outside + inside - cornerRadius);
336
-
337
- float left = px.x;
338
- float right = sizePx.x - px.x;
339
- float top = px.y;
340
- float bottom = sizePx.y - px.y;
341
-
342
- float side = 0.0;
343
- float s = 0.0;
344
- float perimeter = max(1.0, (sizePx.x + sizePx.y) * 2.0);
345
- if (top <= left && top <= right && top <= bottom) {
346
- side = 0.0;
347
- s = px.x;
348
- } else if (right <= left && right <= top && right <= bottom) {
349
- side = 1.0;
350
- s = sizePx.x + px.y;
351
- } else if (bottom <= left && bottom <= right && bottom <= top) {
352
- side = 2.0;
353
- s = sizePx.x + sizePx.y + (sizePx.x - px.x);
354
- } else {
355
- side = 3.0;
356
- s = sizePx.x + sizePx.y + sizePx.x + (sizePx.y - px.y);
357
- }
358
-
359
- float sideSeed = hash1(side * 17.31 + sizePx.x * 0.013 + sizePx.y * 0.007);
360
- float phase = s * (0.12 + sideSeed * 0.06) + u_time * (6.0 + sideSeed * 4.0);
361
- float n1 = sin(phase);
362
- float n2 = sin(phase * 1.87 + 1.6 + sideSeed * 5.1);
363
- float n3 = sin(phase * 2.53 + 0.73);
364
- float waviness = (n1 * 0.58 + n2 * 0.3 + n3 * 0.12) * u_noiseAmp;
365
-
366
- float borderCenter = max(0.5, u_borderInsetPx + waviness);
367
- float borderDist = d - borderCenter;
368
- float core = exp(-pow(borderDist / max(1.0, u_borderPx * 0.72), 2.0));
369
- float glow = exp(-pow(borderDist / max(1.0, u_borderPx * 2.1), 2.0)) * 0.55;
370
-
371
- float pulseA = fract(u_time * 0.11 + 0.07);
372
- float pulseB = fract(u_time * 0.11 + 0.41);
373
- float pulseC = fract(u_time * 0.11 + 0.78);
374
- float posA = pulseA * perimeter;
375
- float posB = pulseB * perimeter;
376
- float posC = pulseC * perimeter;
377
- float dsA = min(abs(s - posA), perimeter - abs(s - posA));
378
- float dsB = min(abs(s - posB), perimeter - abs(s - posB));
379
- float dsC = min(abs(s - posC), perimeter - abs(s - posC));
380
- float pulseBand = exp(-pow(dsA / 18.0, 2.0)) + exp(-pow(dsB / 22.0, 2.0)) + exp(-pow(dsC / 16.0, 2.0));
381
- pulseBand *= exp(-pow(borderDist / max(1.0, u_borderPx * 1.25), 2.0));
382
- float outerFade = smoothstep(0.0, max(0.75, u_borderInsetPx * 0.95), d);
383
-
384
- float flicker = 0.84 + 0.16 * sin(u_time * 21.0 + s * 0.2 + side * 3.1);
385
- float alpha = (core + glow + pulseBand * u_pulseStrength) * u_color.a * flicker * outerFade;
386
- if (alpha <= 0.003) {
387
- discard;
388
- }
389
- gl_FragColor = vec4(u_color.rgb * (0.9 + pulseBand * 0.24), alpha);
390
- } else if (u_shapeMode > 0.5) {
391
- vec2 centered = v_uv - vec2(0.5, 0.5);
392
- float dist = length(centered) * 2.0;
393
- if (dist > 1.0) {
394
- discard;
395
- }
396
- float feather = smoothstep(1.0, 0.72, dist);
397
- gl_FragColor = vec4(u_color.rgb, u_color.a * feather);
398
- } else if (u_useTexture > 0.5) {
399
- vec2 uv = vec2(
400
- mix(u_srcRect.x, u_srcRect.z, v_uv.x),
401
- mix(u_srcRect.y, u_srcRect.w, v_uv.y)
402
- );
403
- vec4 tex = texture2D(u_texture, uv);
404
- gl_FragColor = tex * u_color;
405
- } else {
406
- gl_FragColor = u_color;
407
- }
408
- }
409
- `, rt = class {
410
- canvas;
411
- spriteImage;
412
- spriteElementsCount;
413
- gl;
414
- program;
415
- uniforms;
416
- quadBuffer;
417
- texture;
418
- viewportW = 1;
419
- viewportH = 1;
420
- spriteWidth;
421
- spriteHeight;
422
- spriteSegmentHeight;
423
- constructor(e) {
424
- this.canvas = e.canvas, this.spriteImage = e.spriteImage, this.spriteElementsCount = Math.max(1, e.spriteElementsCount), this.spriteWidth = this.spriteImage.width, this.spriteHeight = this.spriteImage.height, this.spriteSegmentHeight = this.spriteHeight / this.spriteElementsCount;
425
- const t = this.canvas.getContext("webgl2", {
426
- alpha: !0,
427
- antialias: !1
428
- }) ?? this.canvas.getContext("webgl", {
429
- alpha: !0,
430
- antialias: !1
431
- });
432
- if (!t) throw new Error("WebGL context is not available");
433
- this.gl = t;
434
- const i = this.createShader(this.gl.VERTEX_SHADER, et), s = this.createShader(this.gl.FRAGMENT_SHADER, st);
435
- this.program = this.createProgram(i, s), this.gl.deleteShader(i), this.gl.deleteShader(s);
436
- const r = this.gl.createBuffer();
437
- if (!r) throw new Error("Failed to create WebGL quad buffer");
438
- this.quadBuffer = r, this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.quadBuffer), this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array([
439
- 0,
440
- 0,
441
- 1,
442
- 0,
443
- 0,
444
- 1,
445
- 0,
446
- 1,
447
- 1,
448
- 0,
449
- 1,
450
- 1
451
- ]), this.gl.STATIC_DRAW);
452
- const n = this.gl.createTexture();
453
- if (!n) throw new Error("Failed to create WebGL texture");
454
- this.texture = n, this.gl.bindTexture(this.gl.TEXTURE_2D, this.texture), this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, 0), this.gl.pixelStorei(this.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1), this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, this.spriteImage), this.gl.pixelStorei(this.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0), this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR), this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR), this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE), this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE), this.gl.useProgram(this.program);
455
- const o = this.gl.getAttribLocation(this.program, "a_pos");
456
- this.gl.enableVertexAttribArray(o), this.gl.vertexAttribPointer(o, 2, this.gl.FLOAT, !1, 8, 0), this.uniforms = {
457
- resolution: this.gl.getUniformLocation(this.program, "u_resolution"),
458
- destRect: this.gl.getUniformLocation(this.program, "u_destRect"),
459
- srcRect: this.gl.getUniformLocation(this.program, "u_srcRect"),
460
- color: this.gl.getUniformLocation(this.program, "u_color"),
461
- useTexture: this.gl.getUniformLocation(this.program, "u_useTexture"),
462
- shapeMode: this.gl.getUniformLocation(this.program, "u_shapeMode"),
463
- texture: this.gl.getUniformLocation(this.program, "u_texture"),
464
- time: this.gl.getUniformLocation(this.program, "u_time"),
465
- borderPx: this.gl.getUniformLocation(this.program, "u_borderPx"),
466
- borderInsetPx: this.gl.getUniformLocation(this.program, "u_borderInsetPx"),
467
- cornerRadiusPx: this.gl.getUniformLocation(this.program, "u_cornerRadiusPx"),
468
- noiseAmp: this.gl.getUniformLocation(this.program, "u_noiseAmp"),
469
- pulseStrength: this.gl.getUniformLocation(this.program, "u_pulseStrength")
470
- }, this.gl.uniform1i(this.uniforms.texture, 0), this.gl.uniform1f(this.uniforms.time, 0), this.gl.uniform1f(this.uniforms.borderPx, 1), this.gl.uniform1f(this.uniforms.borderInsetPx, 0), this.gl.uniform1f(this.uniforms.cornerRadiusPx, 0), this.gl.uniform1f(this.uniforms.noiseAmp, 0), this.gl.uniform1f(this.uniforms.pulseStrength, 0), this.gl.clearColor(0, 0, 0, 0), this.gl.enable(this.gl.BLEND), this.gl.blendFunc(this.gl.ONE, this.gl.ONE_MINUS_SRC_ALPHA);
471
- }
472
- resize(e, t) {
473
- this.viewportW = Math.max(1, Math.floor(e)), this.viewportH = Math.max(1, Math.floor(t)), this.gl.viewport(0, 0, this.viewportW, this.viewportH);
474
- }
475
- beginFrame() {
476
- this.gl.useProgram(this.program), this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.quadBuffer), this.gl.activeTexture(this.gl.TEXTURE0), this.gl.bindTexture(this.gl.TEXTURE_2D, this.texture), this.gl.uniform2f(this.uniforms.resolution, this.viewportW, this.viewportH), this.gl.uniform1f(this.uniforms.shapeMode, 0), this.gl.clear(this.gl.COLOR_BUFFER_BIT);
477
- }
478
- drawSprite(e, t, i, s, r, n = 1) {
479
- const o = b(e, this.spriteElementsCount) * this.spriteSegmentHeight, l = o + this.spriteSegmentHeight, u = 0.5, c = u / this.spriteWidth, a = 1 - u / this.spriteWidth, d = 1 - (l - u) / this.spriteHeight, f = 1 - (o + u) / this.spriteHeight;
480
- this.gl.uniform4f(this.uniforms.destRect, t, i, s, r), this.gl.uniform4f(this.uniforms.srcRect, c, d, a, f), this.gl.uniform4f(this.uniforms.color, 1, 1, 1, n), this.gl.uniform1f(this.uniforms.shapeMode, 0), this.gl.uniform1f(this.uniforms.useTexture, 1), this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
481
- }
482
- drawSolidRect(e, t, i, s, r) {
483
- this.gl.uniform4f(this.uniforms.destRect, e, t, i, s), this.gl.uniform4f(this.uniforms.srcRect, 0, 0, 1, 1), this.gl.uniform4f(this.uniforms.color, r[0], r[1], r[2], r[3]), this.gl.uniform1f(this.uniforms.shapeMode, 0), this.gl.uniform1f(this.uniforms.useTexture, 0), this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
484
- }
485
- drawSoftCircle(e, t, i, s) {
486
- const r = i * 2;
487
- this.gl.uniform4f(this.uniforms.destRect, e - i, t - i, r, r), this.gl.uniform4f(this.uniforms.srcRect, 0, 0, 1, 1), this.gl.uniform4f(this.uniforms.color, s[0], s[1], s[2], s[3]), this.gl.uniform1f(this.uniforms.useTexture, 0), this.gl.uniform1f(this.uniforms.shapeMode, 1), this.gl.drawArrays(this.gl.TRIANGLES, 0, 6), this.gl.uniform1f(this.uniforms.shapeMode, 0);
488
- }
489
- drawElectricBorder(e) {
490
- this.gl.uniform4f(this.uniforms.destRect, e.x, e.y, e.width, e.height), this.gl.uniform4f(this.uniforms.srcRect, 0, 0, 1, 1), this.gl.uniform4f(this.uniforms.color, e.rgba[0], e.rgba[1], e.rgba[2], e.rgba[3]), this.gl.uniform1f(this.uniforms.useTexture, 0), this.gl.uniform1f(this.uniforms.shapeMode, 2), this.gl.uniform1f(this.uniforms.time, e.timeMs * 1e-3), this.gl.uniform1f(this.uniforms.borderPx, Math.max(0.5, e.borderThicknessPx)), this.gl.uniform1f(this.uniforms.borderInsetPx, Math.max(0, e.borderInsetPx)), this.gl.uniform1f(this.uniforms.cornerRadiusPx, Math.max(0, e.cornerRadiusPx)), this.gl.uniform1f(this.uniforms.noiseAmp, Math.max(0, e.noiseAmplitudePx)), this.gl.uniform1f(this.uniforms.pulseStrength, Math.max(0, e.pulseStrength)), this.gl.drawArrays(this.gl.TRIANGLES, 0, 6), this.gl.uniform1f(this.uniforms.shapeMode, 0);
491
- }
492
- beginAdditiveBlend() {
493
- this.gl.blendFunc(this.gl.SRC_ALPHA, this.gl.ONE);
494
- }
495
- endAdditiveBlend() {
496
- this.gl.blendFunc(this.gl.ONE, this.gl.ONE_MINUS_SRC_ALPHA);
497
- }
498
- dispose() {
499
- this.gl.deleteTexture(this.texture), this.gl.deleteBuffer(this.quadBuffer), this.gl.deleteProgram(this.program);
500
- }
501
- createShader(e, t) {
502
- const i = this.gl.createShader(e);
503
- if (!i) throw new Error("Failed to create WebGL shader");
504
- if (this.gl.shaderSource(i, t), this.gl.compileShader(i), !this.gl.getShaderParameter(i, this.gl.COMPILE_STATUS)) {
505
- const s = this.gl.getShaderInfoLog(i) ?? "unknown error";
506
- throw this.gl.deleteShader(i), new Error(`WebGL shader compile failed: ${s}`);
507
- }
508
- return i;
509
- }
510
- createProgram(e, t) {
511
- const i = this.gl.createProgram();
512
- if (!i) throw new Error("Failed to create WebGL program");
513
- if (this.gl.attachShader(i, e), this.gl.attachShader(i, t), this.gl.linkProgram(i), !this.gl.getProgramParameter(i, this.gl.LINK_STATUS)) {
514
- const s = this.gl.getProgramInfoLog(i) ?? "unknown error";
515
- throw this.gl.deleteProgram(i), new Error(`WebGL program link failed: ${s}`);
516
- }
517
- return i;
518
- }
519
- }, ct = class h {
520
- static RAINBOW_HUE_BUCKETS = 24;
521
- static PARTICLE_GLOBAL_ALPHA = 0.9;
522
- static PARTICLE_MAX_DISTANCE = 0.72;
523
- static PARTICLE_BASE_RADIUS = 0.028;
524
- static DEFAULT_SYMBOL_SCALE = 0.9;
525
- canvas;
526
- container;
527
- button;
528
- spinQueueController;
529
- spriteSource;
530
- spriteCrossOrigin;
531
- spriteElementsCount;
532
- highlightInitialWinningCells;
533
- particleColorRgb;
534
- particleColorMode;
535
- symbolScale;
536
- motionProfile;
537
- isCoarsePointerDevice;
538
- spriteImage = null;
539
- spriteLoadError = null;
540
- webglRenderer = null;
541
- rafLoop = new z();
542
- runtime = Z();
543
- width = 0;
544
- height = 0;
545
- cellW = 0;
546
- cellH = 0;
547
- boardX = 0;
548
- boardY = 0;
549
- scriptedCascadeQueue = [];
550
- scriptedOutgoingGrid = null;
551
- scriptedPendingGrid = null;
552
- scriptedOutroStartedAt = 0;
553
- scriptedOutroElapsedMs = 0;
554
- outroMotionPlan = null;
555
- scriptedOutgoingOffsets = g();
556
- scriptedOutgoingOffsetsPrev = g();
557
- scriptedIncomingOffsets = g();
558
- scriptedIncomingOffsetsPrev = g();
559
- scriptedIncomingAlpha = g();
560
- scriptedIncomingAlphaPrev = g();
561
- scriptedIncomingVisibility = h.createVisibilityGrid("hidden");
562
- scriptedIncomingVisibilityPrev = h.createVisibilityGrid("hidden");
563
- winningCells = [];
564
- winningCellKeys = /* @__PURE__ */ new Set();
565
- grid;
566
- particlesPerCell = 34;
567
- lastRafTime = 0;
568
- initialHighlightRequestedAt = 0;
569
- simulationLastNow = 0;
570
- simulationAccumulatorMs = 0;
571
- outroInterpolationAlpha = 1;
572
- isOutroPipelineWarmedUp = !1;
573
- isGpuPipelineWarmedUp = !1;
574
- perfWindowStartedAt = 0;
575
- perfFrameCount = 0;
576
- perfOver20MsCount = 0;
577
- perfDtSamples = [];
578
- mobileDprCap = 2;
579
- mobilePerfGoodWindows = 0;
580
- mobilePerfBadWindows = 0;
581
- static PERF_WINDOW_MS = 1500;
582
- static PERF_BAD_P95_DT_MS = 19;
583
- static PERF_GOOD_P95_DT_MS = 17.5;
584
- static PERF_BAD_SLOW_RATIO = 0.03;
585
- static MOBILE_BAD_WINDOWS_TO_DECREASE_DPR = 2;
586
- static MOBILE_GOOD_WINDOWS_TO_INCREASE_DPR = 4;
587
- static MOBILE_DPR_CAP_MIN = 1.25;
588
- static MOBILE_DPR_CAP_MAX = 1.5;
589
- static MOBILE_DPR_STEP_DOWN = 0.1;
590
- static MOBILE_DPR_STEP_UP = 0.05;
591
- static DPR_QUANT_STEP = 0.25;
592
- static MAX_CANVAS_AREA_PX_COARSE = 900 * 900;
593
- static MAX_CANVAS_AREA_PX_FINE = 1400 * 1400;
594
- static PRE_SPIN_MS = 150;
595
- static WIN_EFFECTS_ENVELOPE_TAU_MS = 120;
596
- static MAX_FRAME_DELTA_MS = 100;
597
- static WIN_BORDER_ALPHA = 0.72;
598
- static WIN_BORDER_INSET_RATIO = 0.08;
599
- static particleSeedsCache = /* @__PURE__ */ new Map();
600
- constructor(t) {
601
- this.canvas = t.canvas, this.container = t.container, this.button = t.button, this.spriteSource = t.sprite, this.spriteCrossOrigin = t.spriteCrossOrigin ?? "anonymous", this.spriteElementsCount = Math.max(1, t.spriteElementsCount ?? 6), this.highlightInitialWinningCells = t.highlightInitialWinningCells !== !1, this.spinQueueController = new j(t.queuedSpinStates);
602
- const i = Y(t.particleColor);
603
- this.particleColorRgb = i.rgb, this.particleColorMode = i.mode, this.symbolScale = $(t.symbolScale, h.DEFAULT_SYMBOL_SCALE), this.motionProfile = S, this.isCoarsePointerDevice = h.detectCoarsePointerDevice(), this.mobileDprCap = this.isCoarsePointerDevice ? h.MOBILE_DPR_CAP_MAX : 2, this.grid = t.initialSegments ? K(t.initialSegments, this.spriteElementsCount) : T(this.spriteElementsCount);
604
- }
605
- async init() {
606
- if (this.bindEvents(), this.resize(), await this.loadSpriteIfProvided(), !this.spriteImage) {
607
- const t = this.spriteLoadError ? ` (${this.spriteLoadError})` : "";
608
- throw new Error(`sprite is required for WebGL renderer${t}`);
609
- }
610
- this.webglRenderer = new rt({
611
- canvas: this.canvas,
612
- spriteImage: this.spriteImage,
613
- spriteElementsCount: this.spriteElementsCount
614
- }), this.webglRenderer.resize(this.width, this.height), this.warmUpOutroPipeline(), this.warmUpGpuPipeline(), this.applyInitialHighlightIfNeeded(), requestAnimationFrame((t) => {
615
- this.render(t), this.startLoop();
616
- });
617
- }
618
- destroy() {
619
- this.unbindEvents(), this.rafLoop.stop(), this.simulationLastNow = 0, this.simulationAccumulatorMs = 0, it(this.runtime), this.webglRenderer?.dispose(), this.webglRenderer = null, this.clearWinningCells();
620
- }
621
- spin() {
622
- if (this.dismissHighlightIfActive(), this.runtime.isSpinning || this.runtime.queueFinished) return;
623
- if (!this.spinQueueController.hasPending()) {
624
- this.runtime.queueFinished = !0, this.button && (this.button.disabled = !0);
625
- return;
626
- }
627
- const t = this.spinQueueController.consume(), i = t?.highlightWin === !0;
628
- this.runtime.isSpinning = !0, this.runtime.phase = "preSpin", this.runtime.preSpinStartedAt = performance.now(), this.runtime.activeSpinState = t, this.runtime.shouldHighlightCurrentSpin = i, this.runtime.hasStartedFirstSpin = !0, this.simulationLastNow = 0, this.simulationAccumulatorMs = 0, this.button && (this.button.disabled = !0), this.startLoop();
629
- }
630
- bindEvents() {
631
- window.addEventListener("resize", this.resize), document.addEventListener("visibilitychange", this.onVisibilityChange), this.button?.addEventListener("click", this.onSpinClick);
632
- }
633
- unbindEvents() {
634
- window.removeEventListener("resize", this.resize), document.removeEventListener("visibilitychange", this.onVisibilityChange), this.button?.removeEventListener("click", this.onSpinClick);
635
- }
636
- onVisibilityChange = () => {
637
- this.lastRafTime = 0, this.simulationLastNow = 0, this.simulationAccumulatorMs = 0, this.resetPerfWindow();
638
- };
639
- onSpinClick = () => {
640
- this.runtime.isSpinning && this.runtime.phase !== "winFlash" || this.spin();
641
- };
642
- getNextGrid(t) {
643
- return t ? C(t, this.spriteElementsCount) : T(this.spriteElementsCount);
644
- }
645
- update(t) {
646
- if (this.runtime.isSpinning) {
647
- if (this.runtime.phase === "preSpin") {
648
- if (t - this.runtime.preSpinStartedAt < h.PRE_SPIN_MS) return;
649
- J(this.runtime, {
650
- activeSpinState: this.runtime.activeSpinState,
651
- shouldHighlightCurrentSpin: this.runtime.shouldHighlightCurrentSpin,
652
- startedAt: t
653
- }), this.runtime.preSpinStartedAt = 0, this.scriptedCascadeQueue = (this.runtime.activeSpinState?.finaleSequenceRows ? this.runtime.activeSpinState.finaleSequenceRows.map((r) => x(r)) : this.runtime.activeSpinState?.finaleSequence ?? []).map((r) => r.map((n) => [...n])), this.clearWinningCells();
654
- const i = this.runtime.activeSpinState?.stopRows ? x(this.runtime.activeSpinState.stopRows) : this.runtime.activeSpinState?.stopGrid, s = this.getNextGrid(i);
655
- this.startOutroTransition(s, t);
656
- }
657
- this.runtime.phase;
658
- }
659
- }
660
- stepScriptedOutro(t) {
661
- if (!this.scriptedOutgoingGrid || !this.scriptedPendingGrid) {
662
- this.finishSpinWithUi();
663
- return;
664
- }
665
- this.outroMotionPlan || (this.outroMotionPlan = this.createOutroMotionPlan()), this.scriptedOutroElapsedMs += t;
666
- const { allOutgoingDone: i, allIncomingDone: s } = D({
667
- elapsedMs: this.scriptedOutroElapsedMs,
668
- scriptedOutgoingOffsets: this.scriptedOutgoingOffsets,
669
- scriptedIncomingOffsets: this.scriptedIncomingOffsets,
670
- scriptedIncomingAlpha: this.scriptedIncomingAlpha,
671
- scriptedIncomingVisibility: this.scriptedIncomingVisibility,
672
- motionPlan: this.outroMotionPlan
673
- });
674
- if (!(!i || !s) && (this.grid = this.scriptedPendingGrid, this.scriptedOutgoingGrid = null, this.scriptedPendingGrid = null, this.outroMotionPlan = null, this.clearWinningCells(), this.resetOutroBuffers(1, "active"), !this.tryStartScriptedCascade(this.scriptedOutroStartedAt + this.scriptedOutroElapsedMs))) {
675
- if (!this.runtime.shouldHighlightCurrentSpin) {
676
- this.finishSpinWithUi();
677
- return;
678
- }
679
- this.setWinningCells(W(this.grid)), F(this.runtime, this.scriptedOutroStartedAt + this.scriptedOutroElapsedMs), this.runtime.activeSpinState?.callback?.(), this.button && this.runtime.shouldHighlightCurrentSpin && this.spinQueueController.hasPending() && (this.button.disabled = !1);
680
- }
681
- }
682
- tryStartScriptedCascade(t) {
683
- if (this.scriptedCascadeQueue.length === 0) return !1;
684
- const i = this.scriptedCascadeQueue.shift();
685
- return i ? (this.startOutroTransition(C(i, this.spriteElementsCount), t), !0) : !1;
686
- }
687
- startOutroTransition(t, i) {
688
- this.scriptedOutgoingGrid = this.grid.map((s) => [...s]), this.scriptedPendingGrid = t.map((s) => [...s]), this.outroMotionPlan = this.createOutroMotionPlan(), this.resetOutroBuffers(0, "hidden"), this.clearWinningCells(), this.runtime.phase = "outro", this.scriptedOutroStartedAt = i, this.scriptedOutroElapsedMs = 0, this.outroInterpolationAlpha = 1;
689
- }
690
- finishSpinWithUi(t = !1) {
691
- const i = this.runtime.activeSpinState, s = t ? void 0 : i?.callback, r = this.spinQueueController.hasPending();
692
- tt(this.runtime, r, performance.now()), this.button && (this.button.disabled = this.runtime.queueFinished), s?.();
693
- }
694
- dismissHighlightIfActive() {
695
- this.runtime.phase === "winFlash" && this.finishSpinWithUi(!0);
696
- }
697
- applyInitialHighlightIfNeeded() {
698
- this.highlightInitialWinningCells && (this.setWinningCells(W(this.grid)), this.initialHighlightRequestedAt = performance.now());
699
- }
700
- warmUpOutroPipeline() {
701
- if (this.isOutroPipelineWarmedUp) return;
702
- const t = this.createOutroMotionPlan(), i = g(), s = g(), r = g(), n = h.createVisibilityGrid("hidden"), o = Math.max(...t.rowStartDelays), l = 2 * t.columnStaggerMs, u = [
703
- 0,
704
- this.motionProfile.fixedStepMs,
705
- t.incomingStartShift + this.motionProfile.fixedStepMs,
706
- t.fallMs + o + l + this.motionProfile.fixedStepMs
707
- ];
708
- for (const c of u) D({
709
- elapsedMs: c,
710
- scriptedOutgoingOffsets: i,
711
- scriptedIncomingOffsets: s,
712
- scriptedIncomingAlpha: r,
713
- scriptedIncomingVisibility: n,
714
- motionPlan: t
715
- });
716
- this.isOutroPipelineWarmedUp = !0;
717
- }
718
- createOutroMotionPlan() {
719
- const t = V({
720
- height: this.height,
721
- boardY: this.boardY,
722
- cellH: this.cellH,
723
- motionProfile: this.motionProfile
724
- });
725
- if (!this.isCoarsePointerDevice) return t;
726
- const i = 1e3 / 60;
727
- return {
728
- ...t,
729
- columnStaggerMs: this.quantizeMs(t.columnStaggerMs, i),
730
- incomingStartShift: this.quantizeMs(t.incomingStartShift, i),
731
- rowStartDelays: [
732
- this.quantizeMs(t.rowStartDelays[0], i),
733
- this.quantizeMs(t.rowStartDelays[1], i),
734
- this.quantizeMs(t.rowStartDelays[2], i)
735
- ]
736
- };
737
- }
738
- quantizeMs(t, i) {
739
- return t <= 0 ? 0 : Math.max(1, Math.round(t / i)) * i;
740
- }
741
- warmUpGpuPipeline() {
742
- if (this.isGpuPipelineWarmedUp) return;
743
- const t = this.webglRenderer;
744
- if (!t) return;
745
- const i = Math.max(8, this.cellW * 0.2), s = Math.max(8, this.cellH * 0.2), r = this.boardX + 2, n = this.boardY + 2;
746
- t.beginFrame(), t.drawSprite(0, r, n, i, s, 1), t.drawSolidRect(r + i + 2, n, i, s, [
747
- 1,
748
- 1,
749
- 1,
750
- 0.35
751
- ]), t.beginAdditiveBlend(), t.drawSoftCircle(r + i * 0.5, n + s * 0.5, Math.max(2, i * 0.25), [
752
- 1,
753
- 0.9,
754
- 0.4,
755
- 0.4
756
- ]), t.endAdditiveBlend(), this.isGpuPipelineWarmedUp = !0;
757
- }
758
- trackOutroPerf(t, i) {
759
- if (this.runtime.phase !== "outro") {
760
- this.resetPerfWindow();
761
- return;
762
- }
763
- this.perfWindowStartedAt <= 0 && (this.perfWindowStartedAt = i), this.perfFrameCount += 1, t > 20 && (this.perfOver20MsCount += 1), this.perfDtSamples.push(t), !(i - this.perfWindowStartedAt < h.PERF_WINDOW_MS) && (this.flushPerfWindow(), this.perfWindowStartedAt = i);
764
- }
765
- resetPerfWindow() {
766
- this.perfWindowStartedAt = 0, this.perfFrameCount = 0, this.perfOver20MsCount = 0, this.perfDtSamples.length = 0;
767
- }
768
- flushPerfWindow() {
769
- if (this.perfFrameCount === 0) return;
770
- const t = [...this.perfDtSamples].sort((s, r) => s - r), i = t[Math.max(0, Math.min(t.length - 1, Math.floor(t.length * 0.95)))];
771
- this.adjustMobileDprCap(i, this.perfOver20MsCount, this.perfFrameCount), this.perfFrameCount = 0, this.perfOver20MsCount = 0, this.perfDtSamples.length = 0;
772
- }
773
- adjustMobileDprCap(t, i, s) {
774
- if (!this.isCoarsePointerDevice || s <= 0) return;
775
- const r = i / s, n = t > h.PERF_BAD_P95_DT_MS || r > h.PERF_BAD_SLOW_RATIO, o = t <= h.PERF_GOOD_P95_DT_MS && i === 0;
776
- let l = this.mobileDprCap;
777
- n ? (this.mobilePerfBadWindows += 1, this.mobilePerfGoodWindows = 0, this.mobilePerfBadWindows >= h.MOBILE_BAD_WINDOWS_TO_DECREASE_DPR && (l = Math.max(h.MOBILE_DPR_CAP_MIN, this.mobileDprCap - h.MOBILE_DPR_STEP_DOWN), this.mobilePerfBadWindows = 0)) : o ? (this.mobilePerfGoodWindows += 1, this.mobilePerfBadWindows = 0, this.mobilePerfGoodWindows >= h.MOBILE_GOOD_WINDOWS_TO_INCREASE_DPR && (l = Math.min(h.MOBILE_DPR_CAP_MAX, this.mobileDprCap + h.MOBILE_DPR_STEP_UP), this.mobilePerfGoodWindows = 0)) : (this.mobilePerfGoodWindows = 0, this.mobilePerfBadWindows = 0), !(Math.abs(l - this.mobileDprCap) < 1e-3) && (this.mobileDprCap = l, this.resize());
778
- }
779
- render(t) {
780
- if (!this.webglRenderer) return;
781
- this.initialHighlightRequestedAt > 0 && t - this.initialHighlightRequestedAt >= 200 && (F(this.runtime, this.initialHighlightRequestedAt), this.runtime.winEffectsEnvelope = 1, this.initialHighlightRequestedAt = 0, this.button && (this.button.disabled = !1));
782
- const i = this.runtime.phase === "preSpin" ? 0 : this.runtime.phase === "winFlash" ? 1 : 0;
783
- if (this.lastRafTime > 0) {
784
- const l = Math.min(t - this.lastRafTime, 50);
785
- this.trackOutroPerf(l, t);
786
- const u = 1 - Math.exp(-l / h.WIN_EFFECTS_ENVELOPE_TAU_MS);
787
- this.runtime.winEffectsEnvelope += (i - this.runtime.winEffectsEnvelope) * u;
788
- } else this.resetPerfWindow();
789
- this.lastRafTime = t, this.webglRenderer.beginFrame();
790
- const s = this.runtime.phase === "winFlash" || this.runtime.phase === "preSpin" || this.initialHighlightRequestedAt > 0 && this.winningCells.length > 0;
791
- this.runtime.phase === "outro" && this.scriptedOutgoingGrid && this.scriptedPendingGrid ? (this.drawGridInterpolated(this.scriptedOutgoingGrid, this.scriptedOutgoingOffsetsPrev, this.scriptedOutgoingOffsets, this.outroInterpolationAlpha, s), this.drawGridInterpolated(this.scriptedPendingGrid, this.scriptedIncomingOffsetsPrev, this.scriptedIncomingOffsets, this.outroInterpolationAlpha, s, this.scriptedIncomingAlphaPrev, this.scriptedIncomingAlpha, this.scriptedIncomingVisibility)) : this.drawGrid(this.grid, null, s);
792
- const r = this.initialHighlightRequestedAt > 0 ? "winFlash" : this.runtime.phase, n = this.initialHighlightRequestedAt > 0 ? this.initialHighlightRequestedAt : this.runtime.winFlashStartedAt, o = this.initialHighlightRequestedAt > 0 ? 1 : this.runtime.winEffectsEnvelope;
793
- this.drawWinningEffects({
794
- now: t,
795
- phase: r,
796
- winFlashStartedAt: n,
797
- winEffectsEnvelope: o
798
- });
799
- }
800
- isWinningCell = (t, i) => this.winningCellKeys.has(`${t}:${i}`);
801
- getRowCompactOffset(t) {
802
- return (U[t] ?? 0) * this.cellH;
803
- }
804
- applyPixelSnapY(t) {
805
- return this.isCoarsePointerDevice ? Math.round(t * 2) / 2 : t;
806
- }
807
- drawGrid(t, i, s) {
808
- this.drawGridWithSampler({
809
- grid: t,
810
- skipWinningCells: s,
811
- sampleOffsetY: (r, n) => i ? i[r][n] : 0,
812
- sampleAlpha: () => 1,
813
- isVisible: () => !0
814
- });
815
- }
816
- drawGridInterpolated(t, i, s, r, n, o, l, u) {
817
- this.drawGridWithSampler({
818
- grid: t,
819
- skipWinningCells: n,
820
- sampleOffsetY: (c, a) => i[c][a] + (s[c][a] - i[c][a]) * r,
821
- sampleAlpha: (c, a) => o && l ? o[c][a] + (l[c][a] - o[c][a]) * r : 1,
822
- isVisible: (c, a) => !u || u[c][a] !== "hidden"
823
- });
824
- }
825
- drawGridWithSampler(t) {
826
- const i = this.webglRenderer;
827
- if (i)
828
- for (let s = 0; s < 3; s += 1) {
829
- const r = this.boardX + s * this.cellW;
830
- for (let n = 0; n < 3; n += 1) {
831
- if (t.skipWinningCells && this.isWinningCell(s, n) || !t.isVisible(s, n)) continue;
832
- const o = t.sampleOffsetY(s, n), l = this.applyPixelSnapY(this.boardY + n * this.cellH + o + this.getRowCompactOffset(n));
833
- if (l > this.height || l + this.cellH < 0) continue;
834
- const u = t.sampleAlpha(s, n);
835
- if (u <= 0) continue;
836
- const c = this.cellW * this.symbolScale, a = this.cellH * this.symbolScale, d = (this.cellW - c) * 0.5, f = (this.cellH - a) * 0.5;
837
- i.drawSprite(t.grid[s][n], r + d, l + f, c, a, u);
838
- }
839
- }
840
- }
841
- drawWinningEffects(t) {
842
- const i = this.webglRenderer;
843
- if (!i || this.winningCells.length === 0 || t.phase !== "winFlash" && t.phase !== "preSpin") return;
844
- const s = Math.max(0, Math.min(1, t.winEffectsEnvelope)), r = Math.max(0, t.now - t.winFlashStartedAt), n = r % v / v, o = 1 + Math.sin(n * Math.PI * 2) * N * s, l = Math.max(1, this.cellW * h.WIN_BORDER_INSET_RATIO), u = Math.max(1, this.cellW * 0.022), c = this.runtime.phase === "winFlash" && this.runtime.shouldHighlightCurrentSpin && this.runtime.hasStartedFirstSpin;
845
- for (const a of this.winningCells) {
846
- const d = this.boardX + a.col * this.cellW, f = this.boardY + a.row * this.cellH + this.getRowCompactOffset(a.row), p = this.grid[a.col][a.row], E = h.WIN_BORDER_ALPHA * s, w = this.particleColorMode === "rainbow" ? h.hslToRgb01((r * 0.2 + a.col * 36 + a.row * 22) % 360, 0.96, 0.64) : [
847
- this.particleColorRgb[0] / 255,
848
- this.particleColorRgb[1] / 255,
849
- this.particleColorRgb[2] / 255
850
- ], _ = this.cellW * this.symbolScale * o, P = this.cellH * this.symbolScale * o, A = (this.cellW - _) * 0.5, B = (this.cellH - P) * 0.5;
851
- i.drawSprite(p, d + A, f + B, _, P, 1);
852
- const G = d + l, H = f + l, O = this.cellW - l * 2, I = this.cellH - l * 2;
853
- O <= u * 2 || I <= u * 2 || (this.drawElectricBorder({
854
- renderer: i,
855
- cell: a,
856
- x: G,
857
- y: H,
858
- w: O,
859
- h: I,
860
- borderThickness: u,
861
- borderColor: w,
862
- alpha: E,
863
- elapsed: r,
864
- envelope: s
865
- }), c && this.drawCellParticleBurst({
866
- renderer: i,
867
- cell: a,
868
- centerX: d + this.cellW * 0.5,
869
- centerY: f + this.cellH * 0.5,
870
- elapsed: r,
871
- envelope: s
872
- }));
873
- }
874
- }
875
- drawCellParticleBurst(t) {
876
- const i = Math.min(this.cellW, this.cellH) * h.PARTICLE_MAX_DISTANCE, s = Math.min(this.cellW, this.cellH) * h.PARTICLE_BASE_RADIUS, r = h.getParticleSeeds(t.cell.col, t.cell.row), n = [
877
- this.particleColorRgb[0] / 255,
878
- this.particleColorRgb[1] / 255,
879
- this.particleColorRgb[2] / 255
880
- ];
881
- t.renderer.beginAdditiveBlend();
882
- for (let o = 0; o < this.particlesPerCell; o += 1) {
883
- const l = r[o], u = l.phaseOffset * 720, c = t.elapsed - u;
884
- if (c < 0) continue;
885
- const a = c % 720 / 720, d = l.seedA * Math.PI * 2, f = i * a * (0.35 + l.seedB * 0.65), p = t.centerX + Math.cos(d) * f, E = t.centerY + Math.sin(d) * f, w = 0.7 + 0.9 * Math.max(0, Math.sin((t.elapsed * 0.012 + l.twinkleSeed * 2) * Math.PI * 2)), _ = Math.max(1, s * (0.55 + l.seedC * 0.6) * (1 - a * 0.5)), P = Math.max(0, Math.min(1, (0.9 + w * 0.2) * h.PARTICLE_GLOBAL_ALPHA * t.envelope));
886
- if (P <= 0) continue;
887
- const A = this.particleColorMode === "rainbow" ? h.getRainbowParticleColor(t.elapsed, t.cell.col, t.cell.row, l.seedA) : n;
888
- t.renderer.drawSoftCircle(p, E, _, [
889
- A[0],
890
- A[1],
891
- A[2],
892
- P
893
- ]);
894
- }
895
- t.renderer.endAdditiveBlend();
896
- }
897
- drawElectricBorder(t) {
898
- const i = Math.max(0, Math.min(1, t.alpha * t.envelope)), s = t.borderThickness * 1.8, r = t.elapsed + t.cell.col * 170 + t.cell.row * 290;
899
- t.renderer.beginAdditiveBlend(), t.renderer.drawElectricBorder({
900
- x: t.x - s,
901
- y: t.y - s,
902
- width: t.w + s * 2,
903
- height: t.h + s * 2,
904
- rgba: [
905
- t.borderColor[0],
906
- t.borderColor[1],
907
- t.borderColor[2],
908
- i * 0.55
909
- ],
910
- timeMs: r,
911
- borderThicknessPx: Math.max(0.8, t.borderThickness * 1.1),
912
- borderInsetPx: s * 0.85,
913
- cornerRadiusPx: Math.max(2, t.borderThickness * 2.5),
914
- noiseAmplitudePx: Math.max(0.15, t.borderThickness * 0.6),
915
- pulseStrength: 0.9
916
- }), t.renderer.drawElectricBorder({
917
- x: t.x - s * 0.5,
918
- y: t.y - s * 0.5,
919
- width: t.w + s,
920
- height: t.h + s,
921
- rgba: [
922
- t.borderColor[0],
923
- t.borderColor[1],
924
- t.borderColor[2],
925
- i * 0.9
926
- ],
927
- timeMs: r * 1.03,
928
- borderThicknessPx: Math.max(0.7, t.borderThickness * 0.8),
929
- borderInsetPx: s * 0.5,
930
- cornerRadiusPx: Math.max(1.5, t.borderThickness * 1.7),
931
- noiseAmplitudePx: Math.max(0.12, t.borderThickness * 0.42),
932
- pulseStrength: 1.25
933
- }), t.renderer.endAdditiveBlend();
934
- }
935
- static hslToRgb01(t, i, s) {
936
- const r = (t % 360 + 360) % 360, n = Math.max(0, Math.min(1, i)), o = Math.max(0, Math.min(1, s)), l = (1 - Math.abs(2 * o - 1)) * n, u = r / 60, c = l * (1 - Math.abs(u % 2 - 1));
937
- let a = 0, d = 0, f = 0;
938
- u >= 0 && u < 1 ? (a = l, d = c) : u < 2 ? (a = c, d = l) : u < 3 ? (d = l, f = c) : u < 4 ? (d = c, f = l) : u < 5 ? (a = c, f = l) : (a = l, f = c);
939
- const p = o - l * 0.5;
940
- return [
941
- a + p,
942
- d + p,
943
- f + p
944
- ];
945
- }
946
- static getRainbowParticleColor(t, i, s, r) {
947
- const n = (r * 360 + t * 0.24 + i * 38 + s * 22) % 360, o = 360 / h.RAINBOW_HUE_BUCKETS, l = Math.floor(n / o) * o;
948
- return h.hslToRgb01(l, 0.98, 0.64);
949
- }
950
- static detectCoarsePointerDevice() {
951
- if (typeof window > "u") return !1;
952
- const t = typeof window.matchMedia == "function" && window.matchMedia("(pointer: coarse)").matches, i = typeof navigator < "u" && typeof navigator.maxTouchPoints == "number" ? navigator.maxTouchPoints : 0;
953
- return t || i > 0;
954
- }
955
- quantizeDprCap(t) {
956
- const i = h.DPR_QUANT_STEP;
957
- return Math.max(1, Math.round(t / i) * i);
958
- }
959
- static hash01(t, i, s, r) {
960
- const n = Math.sin(t * 127.1 + i * 311.7 + s * 74.7 + r * 19.3) * 43758.5453;
961
- return n - Math.floor(n);
962
- }
963
- static getParticleSeeds(t, i) {
964
- const s = `${t},${i}`, r = h.particleSeedsCache.get(s);
965
- if (r) return r;
966
- const n = [];
967
- for (let o = 0; o < 34; o += 1) n.push({
968
- seedA: h.hash01(t, i, o, 1),
969
- seedB: h.hash01(t, i, o, 2),
970
- seedC: h.hash01(t, i, o, 3),
971
- phaseOffset: h.hash01(t, i, o, 4),
972
- twinkleSeed: h.hash01(t, i, o, 5)
973
- });
974
- return h.particleSeedsCache.set(s, n), n;
975
- }
976
- clearWinningCells() {
977
- this.winningCells = [], this.winningCellKeys.clear();
978
- }
979
- setWinningCells(t) {
980
- this.winningCells = t, this.winningCellKeys.clear();
981
- for (const i of t) this.winningCellKeys.add(`${i.col}:${i.row}`);
982
- }
983
- resize = () => {
984
- const t = this.container.getBoundingClientRect(), i = Math.max(300, Math.floor(t.width)), s = Math.max(1, i * i), r = this.isCoarsePointerDevice ? h.MAX_CANVAS_AREA_PX_COARSE : h.MAX_CANVAS_AREA_PX_FINE, n = Math.max(1, Math.sqrt(r / s)), o = this.isCoarsePointerDevice ? this.mobileDprCap : 2, l = this.quantizeDprCap(Math.min(o, n)), u = Math.max(1, Math.min(window.devicePixelRatio || 1, l)), c = Math.max(300, Math.floor(i * u));
985
- this.width = c, this.height = c;
986
- const a = Math.floor(Math.min(this.width / 3, this.height / 3));
987
- this.cellW = a, this.cellH = a, this.boardX = Math.floor((this.width - this.cellW * 3) / 2), this.boardY = Math.floor((this.height - this.cellH * 3) / 2), this.canvas.width = this.width, this.canvas.height = this.height, this.webglRenderer?.resize(this.width, this.height);
988
- };
989
- async loadSpriteIfProvided() {
990
- if (!this.spriteSource) {
991
- this.spriteImage = null, this.spriteLoadError = null;
992
- return;
993
- }
994
- if (typeof this.spriteSource != "string") {
995
- try {
996
- await this.decodeSpriteImage(this.spriteSource), this.spriteImage = this.spriteSource, this.spriteLoadError = null;
997
- } catch (i) {
998
- this.spriteImage = null, this.spriteLoadError = this.toSpriteLoadErrorMessage(i, "[HTMLImageElement]");
999
- }
1000
- return;
1001
- }
1002
- const t = new Image();
1003
- t.decoding = "async", t.crossOrigin = this.spriteCrossOrigin, t.src = this.spriteSource;
1004
- try {
1005
- await this.decodeSpriteImage(t), this.spriteImage = t, this.spriteLoadError = null;
1006
- } catch (i) {
1007
- this.spriteImage = null, this.spriteLoadError = this.toSpriteLoadErrorMessage(i, this.spriteSource);
1008
- }
1009
- }
1010
- async decodeSpriteImage(t) {
1011
- t.complete && t.naturalWidth > 0 && t.naturalHeight > 0 || await t.decode();
1012
- }
1013
- toSpriteLoadErrorMessage(t, i) {
1014
- return `failed to load sprite "${i}" - ${t instanceof Error ? `${t.name}: ${t.message}` : String(t)}`;
1015
- }
1016
- advanceSimulation(t) {
1017
- if (this.simulationLastNow <= 0) {
1018
- this.simulationLastNow = t, this.update(t);
1019
- return;
1020
- }
1021
- const i = Math.max(0, Math.min(t - this.simulationLastNow, h.MAX_FRAME_DELTA_MS));
1022
- if (this.simulationLastNow = t, this.update(t), this.runtime.phase !== "outro") {
1023
- this.outroInterpolationAlpha = 1;
1024
- return;
1025
- }
1026
- this.advanceOutroFixedStep(i);
1027
- }
1028
- advanceOutroFixedStep(t) {
1029
- this.simulationAccumulatorMs += t;
1030
- const i = this.motionProfile.fixedStepMs;
1031
- let s = 0;
1032
- for (; this.simulationAccumulatorMs >= i && s < this.motionProfile.maxCatchUpStepsPerFrame && this.runtime.phase === "outro"; )
1033
- this.snapshotOutroState(), this.stepScriptedOutro(i), this.simulationAccumulatorMs -= i, s += 1;
1034
- if (this.runtime.phase !== "outro") {
1035
- this.outroInterpolationAlpha = 1;
1036
- return;
1037
- }
1038
- this.outroInterpolationAlpha = Math.max(0, Math.min(1, this.simulationAccumulatorMs / i));
1039
- }
1040
- snapshotOutroState() {
1041
- this.copyOffsets(this.scriptedOutgoingOffsetsPrev, this.scriptedOutgoingOffsets), this.copyOffsets(this.scriptedIncomingOffsetsPrev, this.scriptedIncomingOffsets), this.copyOffsets(this.scriptedIncomingAlphaPrev, this.scriptedIncomingAlpha), this.copyVisibility(this.scriptedIncomingVisibilityPrev, this.scriptedIncomingVisibility);
1042
- }
1043
- copyOffsets(t, i) {
1044
- for (let s = 0; s < 3; s += 1) for (let r = 0; r < 3; r += 1) t[s][r] = i[s][r];
1045
- }
1046
- copyVisibility(t, i) {
1047
- for (let s = 0; s < 3; s += 1) for (let r = 0; r < 3; r += 1) t[s][r] = i[s][r];
1048
- }
1049
- resetOutroBuffers(t, i) {
1050
- m(this.scriptedOutgoingOffsets, 0), m(this.scriptedIncomingOffsets, 0), m(this.scriptedOutgoingOffsetsPrev, 0), m(this.scriptedIncomingOffsetsPrev, 0), m(this.scriptedIncomingAlpha, t), m(this.scriptedIncomingAlphaPrev, t), this.fillVisibilityGrid(this.scriptedIncomingVisibility, i), this.fillVisibilityGrid(this.scriptedIncomingVisibilityPrev, i);
1051
- }
1052
- static createVisibilityGrid(t) {
1053
- return Array.from({ length: 3 }, () => Array.from({ length: 3 }, () => t));
1054
- }
1055
- fillVisibilityGrid(t, i) {
1056
- for (let s = 0; s < 3; s += 1) for (let r = 0; r < 3; r += 1) t[s][r] = i;
1057
- }
1058
- startLoop() {
1059
- this.rafLoop.isRunning() || this.rafLoop.start((t) => (this.advanceSimulation(t), this.render(t), this.shouldKeepAnimating()));
1060
- }
1061
- shouldKeepAnimating() {
1062
- return this.runtime.isSpinning || this.initialHighlightRequestedAt > 0 ? !0 : this.runtime.winEffectsEnvelope > 1e-3;
1063
- }
1064
- };
1065
- export {
1066
- ct as CascadingReel
1067
- };
1068
-
1069
- //# sourceMappingURL=index.es.js.map