thanosfx 0.0.0-stage → 0.1.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/AGENTS.md +61 -0
- package/LICENSE +21 -0
- package/README.md +259 -2
- package/dist/chunk-CKJFWVQU.js +858 -0
- package/dist/chunk-CKJFWVQU.js.map +1 -0
- package/dist/index.cjs +877 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +887 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +17 -0
- package/dist/react.d.ts +17 -0
- package/dist/react.js +31 -0
- package/dist/react.js.map +1 -0
- package/dist/types-BoMZtjIa.d.cts +57 -0
- package/dist/types-BoMZtjIa.d.ts +57 -0
- package/llms.txt +311 -0
- package/package.json +108 -4
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
|
|
5
|
+
// src/core/options.ts
|
|
6
|
+
var BASE_DEFAULTS = {
|
|
7
|
+
duration: 1e3,
|
|
8
|
+
removeTarget: true,
|
|
9
|
+
dpr: 0,
|
|
10
|
+
reducedMotion: "auto",
|
|
11
|
+
burnMs: 200,
|
|
12
|
+
density: 0.9,
|
|
13
|
+
gravity: 3200,
|
|
14
|
+
bounce: 0.32,
|
|
15
|
+
spread: 210,
|
|
16
|
+
life: 1,
|
|
17
|
+
friction: 0.66,
|
|
18
|
+
lift: 190,
|
|
19
|
+
particleScale: 1,
|
|
20
|
+
embers: true,
|
|
21
|
+
collision: true,
|
|
22
|
+
mode: "sweep",
|
|
23
|
+
collideWith: [],
|
|
24
|
+
attractTo: null,
|
|
25
|
+
attractStrength: 0.7,
|
|
26
|
+
attractSwirl: 0.35
|
|
27
|
+
};
|
|
28
|
+
var effectDefaults = {
|
|
29
|
+
burn: { ...BASE_DEFAULTS, duration: 1700 },
|
|
30
|
+
assemble: { ...BASE_DEFAULTS, duration: 1800, removeTarget: false }
|
|
31
|
+
};
|
|
32
|
+
function resolveEffectOptions(effect, options) {
|
|
33
|
+
return { ...effectDefaults[effect], ...options };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/core/capture.ts
|
|
37
|
+
function inlineStyles(source, target) {
|
|
38
|
+
const computed = getComputedStyle(source);
|
|
39
|
+
const targetStyle = target.style;
|
|
40
|
+
for (let index = 0; index < computed.length; index += 1) {
|
|
41
|
+
const property = computed[index];
|
|
42
|
+
if (!property) continue;
|
|
43
|
+
targetStyle.setProperty(
|
|
44
|
+
property,
|
|
45
|
+
computed.getPropertyValue(property),
|
|
46
|
+
computed.getPropertyPriority(property)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function inlineStylesRecursive(source, target) {
|
|
51
|
+
inlineStyles(source, target);
|
|
52
|
+
const sourceChildren = source.children;
|
|
53
|
+
const targetChildren = target.children;
|
|
54
|
+
const count = Math.min(sourceChildren.length, targetChildren.length);
|
|
55
|
+
for (let index = 0; index < count; index += 1) {
|
|
56
|
+
const sourceChild = sourceChildren[index];
|
|
57
|
+
const targetChild = targetChildren[index];
|
|
58
|
+
if (sourceChild && targetChild) inlineStylesRecursive(sourceChild, targetChild);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function fallbackSnapshot(element, rect, dpr) {
|
|
62
|
+
const canvas = document.createElement("canvas");
|
|
63
|
+
canvas.width = Math.max(1, Math.round(rect.width * dpr));
|
|
64
|
+
canvas.height = Math.max(1, Math.round(rect.height * dpr));
|
|
65
|
+
const ctx = canvas.getContext("2d");
|
|
66
|
+
if (!ctx) return canvas;
|
|
67
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
68
|
+
const computed = getComputedStyle(element);
|
|
69
|
+
const radius = Number.parseFloat(computed.borderRadius) || 0;
|
|
70
|
+
const drawRounded = () => {
|
|
71
|
+
ctx.beginPath();
|
|
72
|
+
ctx.moveTo(radius, 0);
|
|
73
|
+
ctx.arcTo(rect.width, 0, rect.width, rect.height, radius);
|
|
74
|
+
ctx.arcTo(rect.width, rect.height, 0, rect.height, radius);
|
|
75
|
+
ctx.arcTo(0, rect.height, 0, 0, radius);
|
|
76
|
+
ctx.arcTo(0, 0, rect.width, 0, radius);
|
|
77
|
+
ctx.closePath();
|
|
78
|
+
};
|
|
79
|
+
drawRounded();
|
|
80
|
+
ctx.fillStyle = computed.backgroundColor === "rgba(0, 0, 0, 0)" ? "#1b1a1f" : computed.backgroundColor;
|
|
81
|
+
ctx.fill();
|
|
82
|
+
ctx.fillStyle = computed.color || "#f4f4f1";
|
|
83
|
+
ctx.font = `${computed.fontWeight} ${computed.fontSize} ${computed.fontFamily}`;
|
|
84
|
+
const text = element.textContent?.trim().slice(0, 160) ?? "";
|
|
85
|
+
ctx.fillText(text, 18, 30, Math.max(1, rect.width - 36));
|
|
86
|
+
return canvas;
|
|
87
|
+
}
|
|
88
|
+
async function captureElement(element, dpr) {
|
|
89
|
+
const rect = element.getBoundingClientRect();
|
|
90
|
+
const width = Math.max(1, Math.round(rect.width));
|
|
91
|
+
const height = Math.max(1, Math.round(rect.height));
|
|
92
|
+
if (typeof window !== "undefined" && typeof window.html2canvas === "function") {
|
|
93
|
+
const canvas2 = await window.html2canvas(element, {
|
|
94
|
+
backgroundColor: null,
|
|
95
|
+
scale: dpr,
|
|
96
|
+
logging: false
|
|
97
|
+
});
|
|
98
|
+
return { canvas: canvas2, rect: { x: rect.left, y: rect.top, width: rect.width, height: rect.height } };
|
|
99
|
+
}
|
|
100
|
+
const clone = element.cloneNode(true);
|
|
101
|
+
inlineStylesRecursive(element, clone);
|
|
102
|
+
clone.style.transform = "none";
|
|
103
|
+
clone.style.transformOrigin = "0 0";
|
|
104
|
+
clone.style.direction = "ltr";
|
|
105
|
+
clone.style.writingMode = "horizontal-tb";
|
|
106
|
+
clone.style.width = `${rect.width}px`;
|
|
107
|
+
clone.style.height = `${rect.height}px`;
|
|
108
|
+
clone.style.margin = "0";
|
|
109
|
+
clone.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
|
|
110
|
+
const svg = `<?xml version="1.0" encoding="UTF-8"?>
|
|
111
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" preserveAspectRatio="none">
|
|
112
|
+
<foreignObject width="100%" height="100%">${new XMLSerializer().serializeToString(clone)}</foreignObject>
|
|
113
|
+
</svg>`;
|
|
114
|
+
await (document.fonts ? document.fonts.ready : Promise.resolve());
|
|
115
|
+
const image = new Image();
|
|
116
|
+
const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
|
|
117
|
+
const loaded = await new Promise((resolve) => {
|
|
118
|
+
image.onload = () => resolve(true);
|
|
119
|
+
image.onerror = () => resolve(false);
|
|
120
|
+
image.src = url;
|
|
121
|
+
});
|
|
122
|
+
if (!loaded) {
|
|
123
|
+
return {
|
|
124
|
+
canvas: fallbackSnapshot(element, { x: rect.left, y: rect.top, width, height }, dpr),
|
|
125
|
+
rect: { x: rect.left, y: rect.top, width: rect.width, height: rect.height }
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const canvas = document.createElement("canvas");
|
|
129
|
+
canvas.width = Math.max(1, Math.round(width * dpr));
|
|
130
|
+
canvas.height = Math.max(1, Math.round(height * dpr));
|
|
131
|
+
const ctx = canvas.getContext("2d");
|
|
132
|
+
if (ctx) {
|
|
133
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
134
|
+
ctx.drawImage(image, 0, 0, width, height);
|
|
135
|
+
}
|
|
136
|
+
return { canvas, rect: { x: rect.left, y: rect.top, width: rect.width, height: rect.height } };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/core/attractor.ts
|
|
140
|
+
var MAX_ARRIVAL_RADIUS = 48;
|
|
141
|
+
function isElement(value) {
|
|
142
|
+
return typeof Element !== "undefined" && value instanceof Element;
|
|
143
|
+
}
|
|
144
|
+
function fromRect(x, y, width, height) {
|
|
145
|
+
const size = Math.min(Math.abs(width), Math.abs(height));
|
|
146
|
+
return {
|
|
147
|
+
x: x + width / 2,
|
|
148
|
+
y: y + height / 2,
|
|
149
|
+
radius: Math.min(MAX_ARRIVAL_RADIUS, size * 0.5)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function resolveAttractor(value) {
|
|
153
|
+
if (!value) return null;
|
|
154
|
+
if (isElement(value)) {
|
|
155
|
+
const rect = value.getBoundingClientRect();
|
|
156
|
+
return fromRect(rect.left, rect.top, rect.width, rect.height);
|
|
157
|
+
}
|
|
158
|
+
if (Array.isArray(value)) {
|
|
159
|
+
const tuple = value;
|
|
160
|
+
if (tuple.length >= 4) {
|
|
161
|
+
return fromRect(tuple[0] ?? 0, tuple[1] ?? 0, tuple[2] ?? 0, tuple[3] ?? 0);
|
|
162
|
+
}
|
|
163
|
+
return { x: tuple[0] ?? 0, y: tuple[1] ?? 0, radius: 0 };
|
|
164
|
+
}
|
|
165
|
+
if ("left" in value) {
|
|
166
|
+
return fromRect(value.left, value.top, value.width, value.height);
|
|
167
|
+
}
|
|
168
|
+
if ("width" in value && "height" in value) {
|
|
169
|
+
return fromRect(value.x, value.y, value.width, value.height);
|
|
170
|
+
}
|
|
171
|
+
return { x: value.x, y: value.y, radius: 0 };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/core/gl.ts
|
|
175
|
+
var QUAD_VERTEX = `#version 300 es
|
|
176
|
+
precision highp float;
|
|
177
|
+
in vec2 a_corner;
|
|
178
|
+
uniform vec2 u_res;
|
|
179
|
+
uniform vec4 u_row;
|
|
180
|
+
out vec2 v_uv;
|
|
181
|
+
void main() {
|
|
182
|
+
vec2 pos = u_row.xy + a_corner * u_row.zw;
|
|
183
|
+
v_uv = a_corner;
|
|
184
|
+
vec2 clip = vec2(pos.x / u_res.x * 2.0 - 1.0, 1.0 - pos.y / u_res.y * 2.0);
|
|
185
|
+
gl_Position = vec4(clip, 0.0, 1.0);
|
|
186
|
+
}`;
|
|
187
|
+
function compile(gl, type, source) {
|
|
188
|
+
const shader = gl.createShader(type);
|
|
189
|
+
if (!shader) throw new Error("Could not create shader");
|
|
190
|
+
gl.shaderSource(shader, source);
|
|
191
|
+
gl.compileShader(shader);
|
|
192
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
193
|
+
const log = gl.getShaderInfoLog(shader) ?? "unknown shader error";
|
|
194
|
+
gl.deleteShader(shader);
|
|
195
|
+
throw new Error(`Shader compile error:
|
|
196
|
+
${log}`);
|
|
197
|
+
}
|
|
198
|
+
return shader;
|
|
199
|
+
}
|
|
200
|
+
function linkProgram(gl, vertexSource, fragmentSource) {
|
|
201
|
+
const vertex = compile(gl, gl.VERTEX_SHADER, vertexSource);
|
|
202
|
+
const fragment = compile(gl, gl.FRAGMENT_SHADER, fragmentSource);
|
|
203
|
+
const program = gl.createProgram();
|
|
204
|
+
if (!program) throw new Error("Could not create program");
|
|
205
|
+
gl.attachShader(program, vertex);
|
|
206
|
+
gl.attachShader(program, fragment);
|
|
207
|
+
gl.linkProgram(program);
|
|
208
|
+
gl.deleteShader(vertex);
|
|
209
|
+
gl.deleteShader(fragment);
|
|
210
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
211
|
+
const log = gl.getProgramInfoLog(program) ?? "unknown link error";
|
|
212
|
+
gl.deleteProgram(program);
|
|
213
|
+
throw new Error(`Program link error:
|
|
214
|
+
${log}`);
|
|
215
|
+
}
|
|
216
|
+
return program;
|
|
217
|
+
}
|
|
218
|
+
function createTexture(gl, source) {
|
|
219
|
+
const texture = gl.createTexture();
|
|
220
|
+
if (!texture) throw new Error("Could not create texture");
|
|
221
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
222
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
223
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
224
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
225
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
226
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
|
|
227
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
|
|
228
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
229
|
+
return texture;
|
|
230
|
+
}
|
|
231
|
+
var QuadProgram = class {
|
|
232
|
+
constructor(gl, fragmentSource) {
|
|
233
|
+
__publicField(this, "program");
|
|
234
|
+
__publicField(this, "gl");
|
|
235
|
+
__publicField(this, "buffer");
|
|
236
|
+
__publicField(this, "corner");
|
|
237
|
+
__publicField(this, "uniforms", /* @__PURE__ */ new Map());
|
|
238
|
+
this.gl = gl;
|
|
239
|
+
this.program = linkProgram(gl, QUAD_VERTEX, fragmentSource);
|
|
240
|
+
const buffer = gl.createBuffer();
|
|
241
|
+
if (!buffer) throw new Error("Could not create quad buffer");
|
|
242
|
+
this.buffer = buffer;
|
|
243
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
244
|
+
gl.bufferData(
|
|
245
|
+
gl.ARRAY_BUFFER,
|
|
246
|
+
new Float32Array([0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]),
|
|
247
|
+
gl.STATIC_DRAW
|
|
248
|
+
);
|
|
249
|
+
this.corner = gl.getAttribLocation(this.program, "a_corner");
|
|
250
|
+
gl.useProgram(this.program);
|
|
251
|
+
for (const name of ["u_res", "u_row", "u_tex"]) this.location(name);
|
|
252
|
+
}
|
|
253
|
+
location(name) {
|
|
254
|
+
if (!this.uniforms.has(name)) {
|
|
255
|
+
this.uniforms.set(name, this.gl.getUniformLocation(this.program, name));
|
|
256
|
+
}
|
|
257
|
+
return this.uniforms.get(name) ?? null;
|
|
258
|
+
}
|
|
259
|
+
use(frame) {
|
|
260
|
+
const gl = this.gl;
|
|
261
|
+
gl.useProgram(this.program);
|
|
262
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
|
|
263
|
+
gl.enableVertexAttribArray(this.corner);
|
|
264
|
+
gl.vertexAttribPointer(this.corner, 2, gl.FLOAT, false, 0, 0);
|
|
265
|
+
gl.uniform2f(this.location("u_res"), frame.canvas.width, frame.canvas.height);
|
|
266
|
+
gl.uniform4f(
|
|
267
|
+
this.location("u_row"),
|
|
268
|
+
frame.rect.x * frame.dpr,
|
|
269
|
+
frame.rect.y * frame.dpr,
|
|
270
|
+
frame.rect.width * frame.dpr,
|
|
271
|
+
frame.rect.height * frame.dpr
|
|
272
|
+
);
|
|
273
|
+
gl.uniform1i(this.location("u_tex"), 0);
|
|
274
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
275
|
+
gl.bindTexture(gl.TEXTURE_2D, frame.texture);
|
|
276
|
+
}
|
|
277
|
+
set1f(name, value) {
|
|
278
|
+
this.gl.uniform1f(this.location(name), value);
|
|
279
|
+
}
|
|
280
|
+
set2f(name, x, y) {
|
|
281
|
+
this.gl.uniform2f(this.location(name), x, y);
|
|
282
|
+
}
|
|
283
|
+
set4f(name, x, y, z, w) {
|
|
284
|
+
this.gl.uniform4f(this.location(name), x, y, z, w);
|
|
285
|
+
}
|
|
286
|
+
draw() {
|
|
287
|
+
this.gl.drawArrays(this.gl.TRIANGLES, 0, 6);
|
|
288
|
+
}
|
|
289
|
+
dispose() {
|
|
290
|
+
this.gl.deleteProgram(this.program);
|
|
291
|
+
this.gl.deleteBuffer(this.buffer);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// src/core/math.ts
|
|
296
|
+
function clamp(value, min, max) {
|
|
297
|
+
return Math.min(max, Math.max(min, value));
|
|
298
|
+
}
|
|
299
|
+
function clamp01(value) {
|
|
300
|
+
return clamp(value, 0, 1);
|
|
301
|
+
}
|
|
302
|
+
function prefersReducedMotion() {
|
|
303
|
+
if (typeof window === "undefined" || !window.matchMedia) return false;
|
|
304
|
+
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// src/core/effects/burn.ts
|
|
308
|
+
var BURN_FRAGMENT = `#version 300 es
|
|
309
|
+
precision highp float;
|
|
310
|
+
in vec2 v_uv;
|
|
311
|
+
out vec4 o;
|
|
312
|
+
uniform sampler2D u_tex;
|
|
313
|
+
uniform float u_time;
|
|
314
|
+
uniform float u_sweep;
|
|
315
|
+
uniform float u_gray;
|
|
316
|
+
uniform float u_black;
|
|
317
|
+
uniform float u_mode;
|
|
318
|
+
uniform float u_embers;
|
|
319
|
+
float hash21(vec2 p) {
|
|
320
|
+
p = fract(p * vec2(123.34, 456.21));
|
|
321
|
+
p += dot(p, p + 45.32);
|
|
322
|
+
return fract(p.x * p.y);
|
|
323
|
+
}
|
|
324
|
+
float frontFn(vec2 uv) {
|
|
325
|
+
float n = hash21(uv * 7.0);
|
|
326
|
+
return clamp(uv.x * 0.78 + uv.y * 0.22 + (n - 0.5) * 0.10, 0.0, 1.0);
|
|
327
|
+
}
|
|
328
|
+
void main() {
|
|
329
|
+
vec4 tex = texture(u_tex, vec2(v_uv.x, 1.0 - v_uv.y));
|
|
330
|
+
if (tex.a < 0.01) discard;
|
|
331
|
+
float startT = (u_mode > 0.5) ? frontFn(v_uv) * u_sweep : 0.0;
|
|
332
|
+
float burn = u_gray + u_black;
|
|
333
|
+
float blackDone = startT + burn;
|
|
334
|
+
float vanishBand = 0.10 * max(burn, 0.0001);
|
|
335
|
+
float ft = clamp((u_time - startT) / max(burn, 0.0001), 0.0, 1.0);
|
|
336
|
+
float e = ft * ft;
|
|
337
|
+
vec3 carbon = vec3(0.03);
|
|
338
|
+
vec3 col = mix(tex.rgb, carbon, e);
|
|
339
|
+
float vanish = smoothstep(blackDone, blackDone + vanishBand, u_time);
|
|
340
|
+
float alpha = tex.a * (1.0 - vanish);
|
|
341
|
+
if (alpha <= 0.003) discard;
|
|
342
|
+
float ember = exp(-pow((u_time - blackDone) / (0.09 * max(u_sweep, burn)), 2.0));
|
|
343
|
+
col += vec3(1.0, 0.40, 0.11) * ember * 0.85 * u_embers * step(0.6, e);
|
|
344
|
+
o = vec4(col, alpha);
|
|
345
|
+
}`;
|
|
346
|
+
var PARTICLE_VERTEX = `#version 300 es
|
|
347
|
+
precision highp float;
|
|
348
|
+
in vec2 a_uv;
|
|
349
|
+
uniform sampler2D u_tex;
|
|
350
|
+
uniform vec2 u_res;
|
|
351
|
+
uniform vec4 u_row;
|
|
352
|
+
uniform float u_time;
|
|
353
|
+
uniform float u_sweep;
|
|
354
|
+
uniform float u_gray;
|
|
355
|
+
uniform float u_black;
|
|
356
|
+
uniform float u_mode;
|
|
357
|
+
uniform float u_g;
|
|
358
|
+
uniform float u_radius;
|
|
359
|
+
uniform float u_rectCount;
|
|
360
|
+
uniform float u_collOn;
|
|
361
|
+
uniform float u_rest;
|
|
362
|
+
uniform float u_friction;
|
|
363
|
+
uniform float u_life;
|
|
364
|
+
uniform float u_dpr;
|
|
365
|
+
uniform float u_cell;
|
|
366
|
+
uniform float u_pscale;
|
|
367
|
+
uniform float u_spread;
|
|
368
|
+
uniform float u_lift;
|
|
369
|
+
uniform float u_attractOn;
|
|
370
|
+
uniform vec2 u_attract;
|
|
371
|
+
uniform float u_attractRadius;
|
|
372
|
+
uniform float u_attractStrength;
|
|
373
|
+
uniform float u_attractSwirl;
|
|
374
|
+
const int MAX_RECTS = 6;
|
|
375
|
+
uniform vec4 u_rects[MAX_RECTS];
|
|
376
|
+
out float v_alpha;
|
|
377
|
+
out vec3 v_col;
|
|
378
|
+
out float v_glow;
|
|
379
|
+
float hash21(vec2 p) {
|
|
380
|
+
p = fract(p * vec2(123.34, 456.21));
|
|
381
|
+
p += dot(p, p + 45.32);
|
|
382
|
+
return fract(p.x * p.y);
|
|
383
|
+
}
|
|
384
|
+
float frontFn(vec2 uv) {
|
|
385
|
+
float n = hash21(uv * 7.0);
|
|
386
|
+
return clamp(uv.x * 0.78 + uv.y * 0.22 + (n - 0.5) * 0.10, 0.0, 1.0);
|
|
387
|
+
}
|
|
388
|
+
float crossTime(vec2 p, vec2 v, float g, float plane, float tr) {
|
|
389
|
+
float a = 0.5 * g, b = v.y, c = p.y - plane;
|
|
390
|
+
float disc = b * b - 4.0 * a * c;
|
|
391
|
+
if (disc < 0.0) return 1e9;
|
|
392
|
+
float sq = sqrt(disc);
|
|
393
|
+
float r1 = (-b - sq) / (2.0 * a);
|
|
394
|
+
float r2 = (-b + sq) / (2.0 * a);
|
|
395
|
+
float tt = 1e9;
|
|
396
|
+
if (r1 > 1e-4) tt = r1;
|
|
397
|
+
if (r2 > 1e-4 && r2 < tt) tt = r2;
|
|
398
|
+
return (tt <= tr) ? tt : 1e9;
|
|
399
|
+
}
|
|
400
|
+
float hitRect(vec2 p, vec2 v, float g, float tr, vec4 rect, out vec2 normal) {
|
|
401
|
+
float left = rect.x, top = rect.y, right = rect.x + rect.z, r = u_radius;
|
|
402
|
+
float t0 = crossTime(p, v, g, top, tr);
|
|
403
|
+
if (t0 >= 1e9) return 1e9;
|
|
404
|
+
float x0 = p.x + v.x * t0;
|
|
405
|
+
if (x0 < left || x0 > right) return 1e9;
|
|
406
|
+
bool leftC = x0 < left + r, rightC = x0 > right - r;
|
|
407
|
+
if (!leftC && !rightC) { normal = vec2(0.0, -1.0); return t0; }
|
|
408
|
+
float cx = leftC ? left + r : right - r, cy = top + r;
|
|
409
|
+
float dx = x0 - cx;
|
|
410
|
+
float inside = r * r - dx * dx;
|
|
411
|
+
if (inside <= 0.0) return 1e9;
|
|
412
|
+
float sY = cy - sqrt(inside);
|
|
413
|
+
float t1 = crossTime(p, v, g, sY, tr);
|
|
414
|
+
if (t1 >= 1e9) return 1e9;
|
|
415
|
+
float x1 = p.x + v.x * t1;
|
|
416
|
+
normal = normalize(vec2(x1, sY) - vec2(cx, cy));
|
|
417
|
+
return t1;
|
|
418
|
+
}
|
|
419
|
+
vec2 simulate(vec2 p0, vec2 v0, float t) {
|
|
420
|
+
vec2 pos = p0, vel = v0;
|
|
421
|
+
float tr = t, g = u_g;
|
|
422
|
+
for (int i = 0; i < 4; i++) {
|
|
423
|
+
float bestT = 1e9;
|
|
424
|
+
vec2 bestN = vec2(0.0, -1.0);
|
|
425
|
+
if (u_collOn > 0.5) {
|
|
426
|
+
for (int k = 0; k < MAX_RECTS; k++) {
|
|
427
|
+
if (k >= int(u_rectCount)) break;
|
|
428
|
+
vec2 n;
|
|
429
|
+
float th = hitRect(pos, vel, g, tr, u_rects[k], n);
|
|
430
|
+
if (th < bestT) { bestT = th; bestN = n; }
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (bestT > tr) {
|
|
434
|
+
pos.x += vel.x * tr;
|
|
435
|
+
pos.y += vel.y * tr + 0.5 * g * tr * tr;
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
pos.x += vel.x * bestT;
|
|
439
|
+
pos.y += vel.y * bestT + 0.5 * g * bestT * bestT;
|
|
440
|
+
vel.y += g * bestT;
|
|
441
|
+
vec2 vNorm = dot(vel, bestN) * bestN;
|
|
442
|
+
vec2 vTan = vel - vNorm;
|
|
443
|
+
vel = vTan * u_friction - vNorm * u_rest;
|
|
444
|
+
tr -= bestT;
|
|
445
|
+
}
|
|
446
|
+
return pos;
|
|
447
|
+
}
|
|
448
|
+
void main() {
|
|
449
|
+
vec4 tx = texture(u_tex, vec2(a_uv.x, 1.0 - a_uv.y));
|
|
450
|
+
float startT = (u_mode > 0.5) ? frontFn(a_uv) * u_sweep : 0.0;
|
|
451
|
+
float td = startT + u_gray + u_black;
|
|
452
|
+
float lt = u_time - td;
|
|
453
|
+
if (tx.a < 0.4 || lt < 0.0) {
|
|
454
|
+
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
|
|
455
|
+
gl_PointSize = 0.0;
|
|
456
|
+
v_alpha = 0.0;
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
float r1 = hash21(a_uv * vec2(12.9, 78.2));
|
|
460
|
+
float r2 = hash21(a_uv * vec2(39.3, 11.1));
|
|
461
|
+
float r3 = hash21(a_uv * vec2(63.7, 21.4));
|
|
462
|
+
float r4 = hash21(a_uv * vec2(91.2, 47.6));
|
|
463
|
+
vec2 p0 = vec2(u_row.x + a_uv.x * u_row.z, u_row.y + a_uv.y * u_row.w);
|
|
464
|
+
float vx = (a_uv.x - 0.5) * u_spread + (r1 - 0.5) * u_spread * 1.5;
|
|
465
|
+
float vy = -u_lift * (0.35 + 0.75 * r2);
|
|
466
|
+
vec2 pos = simulate(p0, vec2(vx, vy), lt);
|
|
467
|
+
float life = u_life * (0.55 + 0.6 * r3);
|
|
468
|
+
if (u_attractOn > 0.5) {
|
|
469
|
+
// The magnet completes before the particle dies, so the dust still gathers
|
|
470
|
+
// in the target. The shared fade below keeps the sprites as fine and airy
|
|
471
|
+
// as the collision/none modes instead of holding them at full size.
|
|
472
|
+
float absorbStart = life * 0.58;
|
|
473
|
+
float pullStart = min(
|
|
474
|
+
mix(0.72, 0.04, clamp(u_attractStrength, 0.0, 1.0)) * life,
|
|
475
|
+
absorbStart * 0.82
|
|
476
|
+
);
|
|
477
|
+
float magnet = smoothstep(pullStart, absorbStart, lt);
|
|
478
|
+
vec2 rel = u_attract - pos;
|
|
479
|
+
float dist = max(length(rel), 0.0001);
|
|
480
|
+
vec2 dir = rel / dist;
|
|
481
|
+
vec2 perp = vec2(-dir.y, dir.x);
|
|
482
|
+
float arcT = clamp(magnet / 0.7, 0.0, 1.0);
|
|
483
|
+
float arc = sin(arcT * 3.14159265) * min(dist, 140.0) * u_attractSwirl;
|
|
484
|
+
float jitter = u_attractRadius * magnet * (0.15 + 0.85 * r4);
|
|
485
|
+
vec2 finalOffset = vec2(cos(r1 * 6.2831853), sin(r1 * 6.2831853)) * jitter;
|
|
486
|
+
float converge = 1.0 - pow(1.0 - magnet, 2.0);
|
|
487
|
+
pos = mix(pos, u_attract, converge) + perp * arc + finalOffset;
|
|
488
|
+
}
|
|
489
|
+
float fade = clamp(1.0 - lt / life, 0.0, 1.0);
|
|
490
|
+
fade *= smoothstep(0.0, 0.12 * life, lt);
|
|
491
|
+
fade = fade * fade;
|
|
492
|
+
vec2 clip = vec2(pos.x / u_res.x * 2.0 - 1.0, 1.0 - pos.y / u_res.y * 2.0);
|
|
493
|
+
gl_Position = vec4(clip, 0.0, 1.0);
|
|
494
|
+
gl_PointSize = max(1.0, u_cell * u_dpr * u_pscale * (0.5 + 0.5 * fade));
|
|
495
|
+
float emberMask = step(0.84, r4);
|
|
496
|
+
float glow = emberMask * clamp(1.0 - lt / 0.22, 0.0, 1.0);
|
|
497
|
+
v_col = mix(vec3(0.56), vec3(1.0, 0.45, 0.12), glow);
|
|
498
|
+
v_glow = glow;
|
|
499
|
+
v_alpha = fade * tx.a;
|
|
500
|
+
}`;
|
|
501
|
+
var PARTICLE_FRAGMENT = `#version 300 es
|
|
502
|
+
precision highp float;
|
|
503
|
+
in float v_alpha;
|
|
504
|
+
in vec3 v_col;
|
|
505
|
+
in float v_glow;
|
|
506
|
+
out vec4 o;
|
|
507
|
+
void main() {
|
|
508
|
+
vec2 d = gl_PointCoord - 0.5;
|
|
509
|
+
float r = length(d);
|
|
510
|
+
float a = v_alpha * (1.0 - smoothstep(0.40, 0.5, r));
|
|
511
|
+
if (a <= 0.003) discard;
|
|
512
|
+
o = vec4(v_col + v_glow * 0.4, a);
|
|
513
|
+
}`;
|
|
514
|
+
function rectTuple(item) {
|
|
515
|
+
if (Array.isArray(item)) return [item[0], item[1], item[2], item[3]];
|
|
516
|
+
if (typeof Element !== "undefined" && item instanceof Element) {
|
|
517
|
+
const rect2 = item.getBoundingClientRect();
|
|
518
|
+
return [rect2.left, rect2.top, rect2.width, rect2.height];
|
|
519
|
+
}
|
|
520
|
+
if ("left" in item) return [item.left, item.top, item.width, item.height];
|
|
521
|
+
const rect = item;
|
|
522
|
+
return [rect.x, rect.y, rect.width, rect.height];
|
|
523
|
+
}
|
|
524
|
+
function resolveRects(frame) {
|
|
525
|
+
const rects = frame.options.collideWith;
|
|
526
|
+
const out = new Float32Array(24);
|
|
527
|
+
rects.slice(0, 6).forEach((item, index) => {
|
|
528
|
+
const [x, y, width, height] = rectTuple(item);
|
|
529
|
+
out[index * 4 + 0] = x * frame.dpr;
|
|
530
|
+
out[index * 4 + 1] = y * frame.dpr;
|
|
531
|
+
out[index * 4 + 2] = width * frame.dpr;
|
|
532
|
+
out[index * 4 + 3] = height * frame.dpr;
|
|
533
|
+
});
|
|
534
|
+
return out;
|
|
535
|
+
}
|
|
536
|
+
function makeUVBuffer(gl, width, height, density) {
|
|
537
|
+
let cols = Math.max(8, Math.round(width * density));
|
|
538
|
+
let rows = Math.max(4, Math.round(height * density));
|
|
539
|
+
while (cols * rows > 14e4) {
|
|
540
|
+
cols = Math.round(cols * 0.92);
|
|
541
|
+
rows = Math.round(rows * 0.92);
|
|
542
|
+
}
|
|
543
|
+
const data = new Float32Array(2 * cols * rows);
|
|
544
|
+
let offset = 0;
|
|
545
|
+
for (let y = 0; y < rows; y += 1) {
|
|
546
|
+
for (let x = 0; x < cols; x += 1) {
|
|
547
|
+
data[offset++] = (x + 0.5) / cols;
|
|
548
|
+
data[offset++] = (y + 0.5) / rows;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
const buffer = gl.createBuffer();
|
|
552
|
+
if (!buffer) throw new Error("Could not create particle buffer");
|
|
553
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
554
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
|
|
555
|
+
return { buffer, count: cols * rows };
|
|
556
|
+
}
|
|
557
|
+
function createBurnEffect(frame, reverse = false) {
|
|
558
|
+
const { gl, options } = frame;
|
|
559
|
+
const quad = new QuadProgram(gl, BURN_FRAGMENT);
|
|
560
|
+
const particleProgram = linkProgram(gl, PARTICLE_VERTEX, PARTICLE_FRAGMENT);
|
|
561
|
+
const particleUv = gl.getAttribLocation(particleProgram, "a_uv");
|
|
562
|
+
const particleRects = resolveRects(frame);
|
|
563
|
+
const attractor = resolveAttractor(options.attractTo);
|
|
564
|
+
const attractStrength = clamp01(options.attractStrength);
|
|
565
|
+
const uvBuffer = makeUVBuffer(
|
|
566
|
+
gl,
|
|
567
|
+
frame.capture.width / frame.dpr,
|
|
568
|
+
frame.capture.height / frame.dpr,
|
|
569
|
+
options.density
|
|
570
|
+
);
|
|
571
|
+
const grayS = options.burnMs / 1e3 * 0.55;
|
|
572
|
+
const blackS = options.burnMs / 1e3 * 0.45;
|
|
573
|
+
const sweepLead = options.mode === "sweep" ? 1.25 * (options.burnMs / 1e3) : 0;
|
|
574
|
+
const baseTotal = sweepLead + grayS + blackS + 1.2 * options.life + 0.05;
|
|
575
|
+
const total = Math.max(0.05, options.duration / 1e3);
|
|
576
|
+
const timeScale = total / baseTotal;
|
|
577
|
+
function setParticleUniforms(time) {
|
|
578
|
+
gl.useProgram(particleProgram);
|
|
579
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer.buffer);
|
|
580
|
+
gl.enableVertexAttribArray(particleUv);
|
|
581
|
+
gl.vertexAttribPointer(particleUv, 2, gl.FLOAT, false, 0, 0);
|
|
582
|
+
gl.uniform2f(
|
|
583
|
+
gl.getUniformLocation(particleProgram, "u_res"),
|
|
584
|
+
frame.canvas.width,
|
|
585
|
+
frame.canvas.height
|
|
586
|
+
);
|
|
587
|
+
gl.uniform4f(
|
|
588
|
+
gl.getUniformLocation(particleProgram, "u_row"),
|
|
589
|
+
frame.rect.x * frame.dpr,
|
|
590
|
+
frame.rect.y * frame.dpr,
|
|
591
|
+
frame.rect.width * frame.dpr,
|
|
592
|
+
frame.rect.height * frame.dpr
|
|
593
|
+
);
|
|
594
|
+
gl.uniform1i(gl.getUniformLocation(particleProgram, "u_tex"), 0);
|
|
595
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_time"), time);
|
|
596
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_sweep"), sweepLead);
|
|
597
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_gray"), grayS);
|
|
598
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_black"), blackS);
|
|
599
|
+
gl.uniform1f(
|
|
600
|
+
gl.getUniformLocation(particleProgram, "u_mode"),
|
|
601
|
+
options.mode === "sweep" ? 1 : 0
|
|
602
|
+
);
|
|
603
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_g"), options.gravity);
|
|
604
|
+
gl.uniform4fv(gl.getUniformLocation(particleProgram, "u_rects[0]"), particleRects);
|
|
605
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_radius"), 8);
|
|
606
|
+
gl.uniform1f(
|
|
607
|
+
gl.getUniformLocation(particleProgram, "u_rectCount"),
|
|
608
|
+
Math.min(options.collideWith.length, 6)
|
|
609
|
+
);
|
|
610
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_collOn"), options.collision ? 1 : 0);
|
|
611
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_rest"), options.bounce);
|
|
612
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_friction"), options.friction);
|
|
613
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_life"), options.life);
|
|
614
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_dpr"), frame.dpr);
|
|
615
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_cell"), 1 / options.density);
|
|
616
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_pscale"), options.particleScale);
|
|
617
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_spread"), options.spread);
|
|
618
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_lift"), options.lift);
|
|
619
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_attractOn"), attractor ? 1 : 0);
|
|
620
|
+
gl.uniform2f(
|
|
621
|
+
gl.getUniformLocation(particleProgram, "u_attract"),
|
|
622
|
+
(attractor?.x ?? 0) * frame.dpr,
|
|
623
|
+
(attractor?.y ?? 0) * frame.dpr
|
|
624
|
+
);
|
|
625
|
+
gl.uniform1f(
|
|
626
|
+
gl.getUniformLocation(particleProgram, "u_attractRadius"),
|
|
627
|
+
(attractor?.radius ?? 0) * frame.dpr
|
|
628
|
+
);
|
|
629
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_attractStrength"), attractStrength);
|
|
630
|
+
gl.uniform1f(gl.getUniformLocation(particleProgram, "u_attractSwirl"), options.attractSwirl);
|
|
631
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
632
|
+
gl.bindTexture(gl.TEXTURE_2D, frame.texture);
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
duration: total,
|
|
636
|
+
render(elapsed) {
|
|
637
|
+
const raw = reverse ? Math.max(0, total - elapsed) : elapsed;
|
|
638
|
+
const time = raw * timeScale;
|
|
639
|
+
gl.clearColor(0, 0, 0, 0);
|
|
640
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
641
|
+
gl.enable(gl.BLEND);
|
|
642
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
643
|
+
quad.use(frame);
|
|
644
|
+
quad.set1f("u_time", time);
|
|
645
|
+
quad.set1f("u_sweep", sweepLead);
|
|
646
|
+
quad.set1f("u_gray", grayS);
|
|
647
|
+
quad.set1f("u_black", blackS);
|
|
648
|
+
quad.set1f("u_mode", options.mode === "sweep" ? 1 : 0);
|
|
649
|
+
quad.set1f("u_embers", options.embers ? 1 : 0);
|
|
650
|
+
quad.draw();
|
|
651
|
+
setParticleUniforms(time);
|
|
652
|
+
gl.drawArrays(gl.POINTS, 0, uvBuffer.count);
|
|
653
|
+
},
|
|
654
|
+
dispose() {
|
|
655
|
+
quad.dispose();
|
|
656
|
+
gl.deleteProgram(particleProgram);
|
|
657
|
+
gl.deleteBuffer(uvBuffer.buffer);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// src/core/effects/assemble.ts
|
|
663
|
+
function createAssembleEffect(frame) {
|
|
664
|
+
return createBurnEffect(frame, true);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/core/effects/index.ts
|
|
668
|
+
var effectFactories = {
|
|
669
|
+
burn: (frame) => createBurnEffect(frame),
|
|
670
|
+
assemble: createAssembleEffect
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
// src/core/run.ts
|
|
674
|
+
function toRect(input) {
|
|
675
|
+
if (typeof Element !== "undefined" && input instanceof Element) {
|
|
676
|
+
const rect = input.getBoundingClientRect();
|
|
677
|
+
return { x: rect.left, y: rect.top, width: rect.width, height: rect.height };
|
|
678
|
+
}
|
|
679
|
+
if (Array.isArray(input)) {
|
|
680
|
+
return { x: input[0] ?? 0, y: input[1] ?? 0, width: input[2] ?? 0, height: input[3] ?? 0 };
|
|
681
|
+
}
|
|
682
|
+
if ("left" in input) {
|
|
683
|
+
return { x: input.left, y: input.top, width: input.width, height: input.height };
|
|
684
|
+
}
|
|
685
|
+
if ("x" in input) {
|
|
686
|
+
return { x: input.x, y: input.y, width: input.width, height: input.height };
|
|
687
|
+
}
|
|
688
|
+
return null;
|
|
689
|
+
}
|
|
690
|
+
function createOverlay(dpr) {
|
|
691
|
+
const canvas = document.createElement("canvas");
|
|
692
|
+
canvas.style.cssText = "position:fixed;inset:0;width:100%;height:100%;pointer-events:none;z-index:70;display:block;";
|
|
693
|
+
document.body.appendChild(canvas);
|
|
694
|
+
const gl = canvas.getContext("webgl2", {
|
|
695
|
+
premultipliedAlpha: false,
|
|
696
|
+
antialias: true,
|
|
697
|
+
alpha: true
|
|
698
|
+
});
|
|
699
|
+
if (!gl) {
|
|
700
|
+
canvas.remove();
|
|
701
|
+
throw new Error("ThanosFx requires WebGL2");
|
|
702
|
+
}
|
|
703
|
+
const resize = () => {
|
|
704
|
+
canvas.width = Math.max(1, Math.round(window.innerWidth * dpr));
|
|
705
|
+
canvas.height = Math.max(1, Math.round(window.innerHeight * dpr));
|
|
706
|
+
canvas.style.width = `${window.innerWidth}px`;
|
|
707
|
+
canvas.style.height = `${window.innerHeight}px`;
|
|
708
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
709
|
+
};
|
|
710
|
+
resize();
|
|
711
|
+
window.addEventListener("resize", resize);
|
|
712
|
+
return {
|
|
713
|
+
canvas,
|
|
714
|
+
gl,
|
|
715
|
+
dispose() {
|
|
716
|
+
window.removeEventListener("resize", resize);
|
|
717
|
+
canvas.remove();
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
function fadeOut(target, remove, cancelled) {
|
|
722
|
+
return new Promise((resolve) => {
|
|
723
|
+
const previous = target.style.transition;
|
|
724
|
+
const previousOpacity = target.style.opacity;
|
|
725
|
+
target.style.transition = "opacity 160ms ease";
|
|
726
|
+
target.style.opacity = "0";
|
|
727
|
+
window.setTimeout(() => {
|
|
728
|
+
if (cancelled()) {
|
|
729
|
+
target.style.transition = previous;
|
|
730
|
+
target.style.opacity = previousOpacity;
|
|
731
|
+
resolve();
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
if (remove && target.parentNode) target.parentNode.removeChild(target);
|
|
735
|
+
else target.style.opacity = previousOpacity;
|
|
736
|
+
target.style.transition = previous;
|
|
737
|
+
resolve();
|
|
738
|
+
}, 180);
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
function playEffect(target, effect, options = {}) {
|
|
742
|
+
if (!(target instanceof HTMLElement)) {
|
|
743
|
+
throw new TypeError("playEffect(target, effect): target must be an HTMLElement");
|
|
744
|
+
}
|
|
745
|
+
const opts = resolveEffectOptions(effect, options);
|
|
746
|
+
const dpr = Math.max(1, opts.dpr || Math.min(window.devicePixelRatio || 1, 2));
|
|
747
|
+
const collisionRects = opts.collideWith.map(toRect).filter((rect) => rect !== null);
|
|
748
|
+
let cancelled = false;
|
|
749
|
+
let rafId = 0;
|
|
750
|
+
let overlay = null;
|
|
751
|
+
let texture = null;
|
|
752
|
+
let instance = null;
|
|
753
|
+
let originalVisibility = target.style.visibility;
|
|
754
|
+
let settled = false;
|
|
755
|
+
const cleanup = () => {
|
|
756
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
757
|
+
rafId = 0;
|
|
758
|
+
if (instance) {
|
|
759
|
+
try {
|
|
760
|
+
instance.dispose();
|
|
761
|
+
} catch {
|
|
762
|
+
}
|
|
763
|
+
instance = null;
|
|
764
|
+
}
|
|
765
|
+
if (overlay) {
|
|
766
|
+
if (texture) overlay.gl.deleteTexture(texture);
|
|
767
|
+
texture = null;
|
|
768
|
+
overlay.dispose();
|
|
769
|
+
overlay = null;
|
|
770
|
+
}
|
|
771
|
+
target.style.visibility = originalVisibility;
|
|
772
|
+
};
|
|
773
|
+
const finish = (remove) => {
|
|
774
|
+
if (settled) return;
|
|
775
|
+
settled = true;
|
|
776
|
+
cleanup();
|
|
777
|
+
if (remove && target.parentNode) target.parentNode.removeChild(target);
|
|
778
|
+
};
|
|
779
|
+
const promise = (async () => {
|
|
780
|
+
if (opts.reducedMotion !== "allow" && opts.reducedMotion !== "reduce" && prefersReducedMotion()) {
|
|
781
|
+
await fadeOut(target, opts.removeTarget, () => cancelled);
|
|
782
|
+
if (!cancelled) {
|
|
783
|
+
target.style.visibility = originalVisibility;
|
|
784
|
+
if (!opts.removeTarget) target.style.opacity = "1";
|
|
785
|
+
}
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
const captured = await captureElement(target, dpr);
|
|
789
|
+
if (cancelled) {
|
|
790
|
+
target.style.visibility = originalVisibility;
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
overlay = createOverlay(dpr);
|
|
794
|
+
texture = createTexture(overlay.gl, captured.canvas);
|
|
795
|
+
originalVisibility = target.style.visibility;
|
|
796
|
+
target.style.visibility = "hidden";
|
|
797
|
+
const frame = {
|
|
798
|
+
gl: overlay.gl,
|
|
799
|
+
canvas: overlay.canvas,
|
|
800
|
+
texture,
|
|
801
|
+
capture: captured.canvas,
|
|
802
|
+
rect: captured.rect,
|
|
803
|
+
dpr,
|
|
804
|
+
target,
|
|
805
|
+
options: { ...opts, collideWith: collisionRects }
|
|
806
|
+
};
|
|
807
|
+
try {
|
|
808
|
+
instance = effectFactories[effect](frame);
|
|
809
|
+
const effectInstance = instance;
|
|
810
|
+
const duration = Math.max(0.05, effectInstance.duration);
|
|
811
|
+
const start = performance.now();
|
|
812
|
+
await new Promise((resolve) => {
|
|
813
|
+
const tick = (now) => {
|
|
814
|
+
if (cancelled) {
|
|
815
|
+
resolve();
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
const elapsed = (now - start) / 1e3;
|
|
819
|
+
if (elapsed >= duration) {
|
|
820
|
+
resolve();
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
try {
|
|
824
|
+
effectInstance.render(elapsed);
|
|
825
|
+
} catch (error) {
|
|
826
|
+
console.error("ThanosFx effect failed:", error);
|
|
827
|
+
resolve();
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
rafId = requestAnimationFrame(tick);
|
|
831
|
+
};
|
|
832
|
+
rafId = requestAnimationFrame(tick);
|
|
833
|
+
});
|
|
834
|
+
} catch (error) {
|
|
835
|
+
cleanup();
|
|
836
|
+
throw error;
|
|
837
|
+
}
|
|
838
|
+
if (cancelled) {
|
|
839
|
+
target.style.visibility = originalVisibility;
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
finish(opts.removeTarget);
|
|
843
|
+
})();
|
|
844
|
+
return {
|
|
845
|
+
promise: promise.finally(() => {
|
|
846
|
+
if (cancelled) cleanup();
|
|
847
|
+
}),
|
|
848
|
+
cancel() {
|
|
849
|
+
cancelled = true;
|
|
850
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
851
|
+
cleanup();
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
export { effectDefaults, playEffect, resolveEffectOptions };
|
|
857
|
+
//# sourceMappingURL=chunk-CKJFWVQU.js.map
|
|
858
|
+
//# sourceMappingURL=chunk-CKJFWVQU.js.map
|