@elementalfx/dissolve 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/LICENSE +21 -0
- package/README.md +72 -2
- package/dist/dissolve.d.ts +18 -0
- package/dist/dissolve.d.ts.map +1 -0
- package/dist/dissolve.global.js +2 -0
- package/dist/filter.d.ts +29 -0
- package/dist/filter.d.ts.map +1 -0
- package/dist/global.d.ts +2 -0
- package/dist/global.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +764 -0
- package/dist/index.js.map +7 -0
- package/dist/mask.d.ts +34 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/options.d.ts +39 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/particles.d.ts +28 -0
- package/dist/particles.d.ts.map +1 -0
- package/dist/snapshot.d.ts +21 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/package.json +66 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
// packages/dissolve/src/dissolve.ts
|
|
2
|
+
import { createRandom as createRandom2, isBrowser, onFrame as onFrame2, prefersReducedMotion, resolveElement } from "@elementalfx/core";
|
|
3
|
+
|
|
4
|
+
// packages/dissolve/src/filter.ts
|
|
5
|
+
var SVG = "http://www.w3.org/2000/svg";
|
|
6
|
+
var defs = null;
|
|
7
|
+
var count = 0;
|
|
8
|
+
function container() {
|
|
9
|
+
if (defs?.isConnected) return defs;
|
|
10
|
+
const svg = document.createElementNS(SVG, "svg");
|
|
11
|
+
svg.setAttribute("aria-hidden", "true");
|
|
12
|
+
svg.setAttribute("data-fx", "dissolve");
|
|
13
|
+
svg.setAttribute("width", "0");
|
|
14
|
+
svg.setAttribute("height", "0");
|
|
15
|
+
svg.style.cssText = "position:absolute;width:0;height:0;overflow:hidden;pointer-events:none";
|
|
16
|
+
defs = document.createElementNS(SVG, "defs");
|
|
17
|
+
svg.append(defs);
|
|
18
|
+
document.body.append(svg);
|
|
19
|
+
return defs;
|
|
20
|
+
}
|
|
21
|
+
function node(parent, name, attributes) {
|
|
22
|
+
const element = document.createElementNS(SVG, name);
|
|
23
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
24
|
+
element.setAttribute(key, String(value));
|
|
25
|
+
if (key === "href") element.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", String(value));
|
|
26
|
+
}
|
|
27
|
+
parent.append(element);
|
|
28
|
+
return element;
|
|
29
|
+
}
|
|
30
|
+
function threshold(filter, input, result, slope) {
|
|
31
|
+
const transfer = node(filter, "feComponentTransfer", { in: input, result });
|
|
32
|
+
return node(transfer, "feFuncA", { type: "linear", slope, intercept: 0 });
|
|
33
|
+
}
|
|
34
|
+
function createFilter(setup) {
|
|
35
|
+
const { effect, width, height } = setup;
|
|
36
|
+
const id = `fx-dissolve-${++count}`;
|
|
37
|
+
const pad = Math.ceil(Math.max(width, height) * 0.1) + 8;
|
|
38
|
+
const reach = setup.reach ?? 1;
|
|
39
|
+
const filter = node(container(), "filter", {
|
|
40
|
+
id,
|
|
41
|
+
filterUnits: "userSpaceOnUse",
|
|
42
|
+
primitiveUnits: "userSpaceOnUse",
|
|
43
|
+
x: -pad,
|
|
44
|
+
y: -pad,
|
|
45
|
+
width: width + pad * 2,
|
|
46
|
+
height: (effect === "melt" ? height * reach : height) + pad * 2,
|
|
47
|
+
"color-interpolation-filters": "sRGB"
|
|
48
|
+
});
|
|
49
|
+
const softness = effect === "fade" ? 0.12 : 0.02;
|
|
50
|
+
const slope = 1 / softness;
|
|
51
|
+
const band = 0.045;
|
|
52
|
+
const funcs = [];
|
|
53
|
+
let displace = null;
|
|
54
|
+
let fade = null;
|
|
55
|
+
if (effect === "melt") {
|
|
56
|
+
node(filter, "feImage", { href: setup.drip, x: 0, y: 0, width, height: height * reach, preserveAspectRatio: "none", result: "drip" });
|
|
57
|
+
displace = node(filter, "feDisplacementMap", { in: "SourceGraphic", in2: "drip", scale: 0, xChannelSelector: "R", yChannelSelector: "G", result: "moved" });
|
|
58
|
+
node(filter, "feFlood", { "flood-color": "#fff", x: 0, y: 0, width, height: height * reach, result: "columns" });
|
|
59
|
+
node(filter, "feComposite", { in: "moved", in2: "columns", operator: "in", result: "melted" });
|
|
60
|
+
const transfer = node(filter, "feComponentTransfer", { in: "melted" });
|
|
61
|
+
fade = node(transfer, "feFuncA", { type: "linear", slope: 1, intercept: 0 });
|
|
62
|
+
} else {
|
|
63
|
+
node(filter, "feImage", { href: setup.mask, x: 0, y: 0, width, height, preserveAspectRatio: "none", result: "mask" });
|
|
64
|
+
funcs.push({ func: threshold(filter, "mask", "alive", slope), offset: 0 });
|
|
65
|
+
if (effect === "burn") {
|
|
66
|
+
funcs.push({ func: threshold(filter, "mask", "inner", slope), offset: band });
|
|
67
|
+
funcs.push({ func: threshold(filter, "mask", "core", slope), offset: band * 2.5 });
|
|
68
|
+
node(filter, "feComposite", { in: "alive", in2: "inner", operator: "out", result: "hotBand" });
|
|
69
|
+
node(filter, "feComposite", { in: "inner", in2: "core", operator: "out", result: "charBand" });
|
|
70
|
+
node(filter, "feComposite", { in: "SourceGraphic", in2: "core", operator: "in", result: "intact" });
|
|
71
|
+
node(filter, "feColorMatrix", {
|
|
72
|
+
in: "SourceGraphic",
|
|
73
|
+
type: "matrix",
|
|
74
|
+
values: "0.25 0 0 0 0.04 0 0.18 0 0 0.02 0 0 0.14 0 0.01 0 0 0 1 0",
|
|
75
|
+
result: "dark"
|
|
76
|
+
});
|
|
77
|
+
node(filter, "feComposite", { in: "dark", in2: "charBand", operator: "in", result: "charred" });
|
|
78
|
+
node(filter, "feFlood", { "flood-color": setup.color, result: "flame" });
|
|
79
|
+
node(filter, "feComposite", { in: "flame", in2: "hotBand", operator: "in", result: "hot0" });
|
|
80
|
+
node(filter, "feComposite", { in: "hot0", in2: "SourceAlpha", operator: "in", result: "hot" });
|
|
81
|
+
node(filter, "feGaussianBlur", { in: "hot", stdDeviation: 3, result: "glow" });
|
|
82
|
+
const merge = node(filter, "feMerge", {});
|
|
83
|
+
for (const input of ["glow", "intact", "charred", "hot"]) node(merge, "feMergeNode", { in: input });
|
|
84
|
+
} else {
|
|
85
|
+
node(filter, "feComposite", { in: "SourceGraphic", in2: "alive", operator: "in" });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const start = effect === "burn" ? -(band * 2.5 + softness) : -softness;
|
|
89
|
+
const end = 1 + softness;
|
|
90
|
+
return {
|
|
91
|
+
id,
|
|
92
|
+
start,
|
|
93
|
+
end,
|
|
94
|
+
set(edge) {
|
|
95
|
+
for (const { func, offset } of funcs) func.setAttribute("intercept", String(-slope * (edge + offset)));
|
|
96
|
+
},
|
|
97
|
+
melt(distance, opacity) {
|
|
98
|
+
displace?.setAttribute("scale", String(distance * 2));
|
|
99
|
+
fade?.setAttribute("slope", String(opacity));
|
|
100
|
+
},
|
|
101
|
+
destroy() {
|
|
102
|
+
filter.remove();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// packages/dissolve/src/mask.ts
|
|
108
|
+
import { createRandom, noise3 } from "@elementalfx/core";
|
|
109
|
+
function fbm(x, y, z) {
|
|
110
|
+
let sum = 0;
|
|
111
|
+
let amplitude = 0.5;
|
|
112
|
+
let frequency = 1;
|
|
113
|
+
for (let octave = 0; octave < 3; octave++) {
|
|
114
|
+
sum += amplitude * noise3(x * frequency, y * frequency, z + octave * 7.31);
|
|
115
|
+
amplitude *= 0.5;
|
|
116
|
+
frequency *= 2.03;
|
|
117
|
+
}
|
|
118
|
+
return sum;
|
|
119
|
+
}
|
|
120
|
+
function ramp(direction2, u, v) {
|
|
121
|
+
switch (direction2) {
|
|
122
|
+
case "right":
|
|
123
|
+
return u;
|
|
124
|
+
case "left":
|
|
125
|
+
return 1 - u;
|
|
126
|
+
case "down":
|
|
127
|
+
return v;
|
|
128
|
+
case "up":
|
|
129
|
+
return 1 - v;
|
|
130
|
+
default:
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function toPng(columns, rows, fill) {
|
|
135
|
+
const canvas2 = document.createElement("canvas");
|
|
136
|
+
canvas2.width = columns;
|
|
137
|
+
canvas2.height = rows;
|
|
138
|
+
const ctx2 = canvas2.getContext("2d");
|
|
139
|
+
const image = ctx2.createImageData(columns, rows);
|
|
140
|
+
fill(image.data);
|
|
141
|
+
ctx2.putImageData(image, 0, 0);
|
|
142
|
+
return canvas2.toDataURL("image/png");
|
|
143
|
+
}
|
|
144
|
+
function createMask(width, height, options) {
|
|
145
|
+
const step = Math.max(4, Math.ceil(Math.max(width, height) / 256));
|
|
146
|
+
const columns = Math.max(2, Math.ceil(width / step));
|
|
147
|
+
const rows = Math.max(2, Math.ceil(height / step));
|
|
148
|
+
const values = new Float32Array(columns * rows);
|
|
149
|
+
const random = createRandom(options.seed);
|
|
150
|
+
const z = random() * 100;
|
|
151
|
+
const offsetX = random() * 1e3;
|
|
152
|
+
const offsetY = random() * 1e3;
|
|
153
|
+
const noise = options.direction === "none" ? 1 : options.noise;
|
|
154
|
+
const frequency = step / options.scale;
|
|
155
|
+
let min = Infinity;
|
|
156
|
+
let max = -Infinity;
|
|
157
|
+
for (let j = 0; j < rows; j++) {
|
|
158
|
+
for (let i = 0; i < columns; i++) {
|
|
159
|
+
const n = fbm(offsetX + i * frequency, offsetY + j * frequency, z) * 0.9 + 0.5;
|
|
160
|
+
const value = (1 - noise) * ramp(options.direction, i / (columns - 1), j / (rows - 1)) + noise * n;
|
|
161
|
+
values[j * columns + i] = value;
|
|
162
|
+
if (value < min) min = value;
|
|
163
|
+
if (value > max) max = value;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const range = max - min || 1;
|
|
167
|
+
for (let k = 0; k < values.length; k++) values[k] = (values[k] - min) / range;
|
|
168
|
+
const url = toPng(columns, rows, (data) => {
|
|
169
|
+
for (let k = 0; k < values.length; k++) {
|
|
170
|
+
data[k * 4] = 255;
|
|
171
|
+
data[k * 4 + 1] = 255;
|
|
172
|
+
data[k * 4 + 2] = 255;
|
|
173
|
+
data[k * 4 + 3] = Math.round(values[k] * 255);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return {
|
|
177
|
+
width,
|
|
178
|
+
height,
|
|
179
|
+
step,
|
|
180
|
+
columns,
|
|
181
|
+
rows,
|
|
182
|
+
values,
|
|
183
|
+
url,
|
|
184
|
+
at(x, y) {
|
|
185
|
+
const i = Math.min(columns - 1, Math.max(0, Math.floor(x / step)));
|
|
186
|
+
const j = Math.min(rows - 1, Math.max(0, Math.floor(y / step)));
|
|
187
|
+
return values[j * columns + i];
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function createDrip(width, height, reach, seed) {
|
|
192
|
+
const step = Math.max(4, Math.ceil(Math.max(width, height * reach) / 256));
|
|
193
|
+
const columns = Math.max(2, Math.ceil(width / step));
|
|
194
|
+
const rows = Math.max(2, Math.ceil(height * reach / step));
|
|
195
|
+
const random = createRandom(seed);
|
|
196
|
+
const z = random() * 100;
|
|
197
|
+
const drips = new Float32Array(columns);
|
|
198
|
+
for (let i = 0; i < columns; i++) {
|
|
199
|
+
const n = noise3(i * step / 18, z, 0.5) * 0.55 + noise3(i * step / 60, z, 3.1) * 0.45 + 0.5;
|
|
200
|
+
drips[i] = 0.15 + 0.85 * Math.pow(Math.min(1, Math.max(0, n)), 2.4);
|
|
201
|
+
}
|
|
202
|
+
return toPng(columns, rows, (data) => {
|
|
203
|
+
for (let j = 0; j < rows; j++) {
|
|
204
|
+
const depth = Math.min(1, j * step / height);
|
|
205
|
+
for (let i = 0; i < columns; i++) {
|
|
206
|
+
const amount = drips[i] * Math.pow(depth, 1.3);
|
|
207
|
+
const k = (j * columns + i) * 4;
|
|
208
|
+
data[k] = 128;
|
|
209
|
+
data[k + 1] = Math.round(128 - amount * 127);
|
|
210
|
+
data[k + 2] = 128;
|
|
211
|
+
data[k + 3] = 255;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// packages/dissolve/src/options.ts
|
|
218
|
+
var durations = { dust: 1.6, burn: 2.2, melt: 1.8, fade: 1 };
|
|
219
|
+
var directions = { dust: "right", burn: "up", melt: "down", fade: "none" };
|
|
220
|
+
function resolveOptions(options = {}) {
|
|
221
|
+
const effect = options.effect && options.effect in durations ? options.effect : "dust";
|
|
222
|
+
return {
|
|
223
|
+
effect,
|
|
224
|
+
duration: Math.max(0, options.duration ?? durations[effect]),
|
|
225
|
+
direction: options.direction ?? directions[effect],
|
|
226
|
+
noise: Math.min(1, Math.max(0, options.noise ?? 0.4)),
|
|
227
|
+
scale: Math.max(4, options.scale ?? 36),
|
|
228
|
+
color: options.color ?? "#ff7a1a",
|
|
229
|
+
particles: Math.max(0, options.particles ?? 1),
|
|
230
|
+
grain: Math.max(1, options.grain ?? 3),
|
|
231
|
+
after: options.after === "remove" ? "remove" : "hide",
|
|
232
|
+
seed: options.seed ?? Math.floor(Math.random() * 2 ** 31),
|
|
233
|
+
respectReducedMotion: options.respectReducedMotion ?? true
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// packages/dissolve/src/particles.ts
|
|
238
|
+
import { noise3 as noise32, onFrame, pixelRatio } from "@elementalfx/core";
|
|
239
|
+
var MAX_SPECKS = 12e3;
|
|
240
|
+
var specks = [];
|
|
241
|
+
var canvas = null;
|
|
242
|
+
var ctx = null;
|
|
243
|
+
var stop = null;
|
|
244
|
+
var ratio = 1;
|
|
245
|
+
var time = 0;
|
|
246
|
+
function ensureCanvas() {
|
|
247
|
+
if (ctx && canvas?.isConnected) return ctx;
|
|
248
|
+
canvas = document.createElement("canvas");
|
|
249
|
+
canvas.setAttribute("aria-hidden", "true");
|
|
250
|
+
canvas.setAttribute("data-fx", "dissolve");
|
|
251
|
+
canvas.style.cssText = "position:fixed;left:0;top:0;width:100vw;height:100vh;pointer-events:none;z-index:2147483000";
|
|
252
|
+
document.body.append(canvas);
|
|
253
|
+
ctx = canvas.getContext("2d");
|
|
254
|
+
resize();
|
|
255
|
+
return ctx;
|
|
256
|
+
}
|
|
257
|
+
function resize() {
|
|
258
|
+
if (!canvas) return;
|
|
259
|
+
ratio = pixelRatio(2);
|
|
260
|
+
const width = document.documentElement.clientWidth;
|
|
261
|
+
const height = document.documentElement.clientHeight;
|
|
262
|
+
canvas.width = Math.round(width * ratio);
|
|
263
|
+
canvas.height = Math.round(height * ratio);
|
|
264
|
+
}
|
|
265
|
+
function emit(batch) {
|
|
266
|
+
if (batch.length === 0 || typeof document === "undefined") return;
|
|
267
|
+
const room = MAX_SPECKS - specks.length;
|
|
268
|
+
if (room <= 0) return;
|
|
269
|
+
specks.push(...batch.length > room ? batch.slice(0, room) : batch);
|
|
270
|
+
ensureCanvas();
|
|
271
|
+
if (!stop) {
|
|
272
|
+
window.addEventListener("resize", resize);
|
|
273
|
+
stop = onFrame(frame);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function activeSpecks() {
|
|
277
|
+
return specks.length;
|
|
278
|
+
}
|
|
279
|
+
function frame(dt) {
|
|
280
|
+
time += dt;
|
|
281
|
+
const context = ensureCanvas();
|
|
282
|
+
const width = canvas.width;
|
|
283
|
+
const height = canvas.height;
|
|
284
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
285
|
+
context.clearRect(0, 0, width, height);
|
|
286
|
+
context.setTransform(ratio, 0, 0, ratio, -window.scrollX * ratio, -window.scrollY * ratio);
|
|
287
|
+
const alive = [];
|
|
288
|
+
const sparks = [];
|
|
289
|
+
for (const s of specks) {
|
|
290
|
+
if (s.delay && s.delay > 0) {
|
|
291
|
+
s.delay -= dt;
|
|
292
|
+
alive.push(s);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
s.age += dt;
|
|
296
|
+
if (s.age >= s.life) continue;
|
|
297
|
+
alive.push(s);
|
|
298
|
+
const k = s.age / s.life;
|
|
299
|
+
let alpha;
|
|
300
|
+
let size = s.size;
|
|
301
|
+
if (s.kind === "gather") {
|
|
302
|
+
const e = 1 - Math.pow(1 - k, 3);
|
|
303
|
+
s.x = s.x0 + (s.x1 - s.x0) * e;
|
|
304
|
+
s.y = s.y0 + (s.y1 - s.y0) * e + Math.sin(k * Math.PI) * -18;
|
|
305
|
+
alpha = Math.min(1, k * 3);
|
|
306
|
+
} else {
|
|
307
|
+
const swirl = noise32(s.x * 0.01, s.y * 0.01, time * 0.6) * 140;
|
|
308
|
+
s.vx += swirl * dt;
|
|
309
|
+
s.vy += (s.kind === "spark" ? -220 : -40) * dt;
|
|
310
|
+
const drag = Math.exp(-(s.kind === "spark" ? 1.2 : 0.6) * dt);
|
|
311
|
+
s.vx *= drag;
|
|
312
|
+
s.vy *= drag;
|
|
313
|
+
s.x += s.vx * dt;
|
|
314
|
+
s.y += s.vy * dt;
|
|
315
|
+
alpha = 1 - k * k;
|
|
316
|
+
size *= s.kind === "spark" ? 1 - k * 0.7 : 1 - k * 0.5;
|
|
317
|
+
}
|
|
318
|
+
if (s.kind === "spark") {
|
|
319
|
+
sparks.push(s);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
context.globalAlpha = alpha;
|
|
323
|
+
context.fillStyle = s.color;
|
|
324
|
+
context.fillRect(s.x - size / 2, s.y - size / 2, size, size);
|
|
325
|
+
}
|
|
326
|
+
if (sparks.length) {
|
|
327
|
+
context.globalCompositeOperation = "lighter";
|
|
328
|
+
for (const s of sparks) {
|
|
329
|
+
const k = s.age / s.life;
|
|
330
|
+
const size = s.size * (1 - k * 0.7);
|
|
331
|
+
context.fillStyle = s.color;
|
|
332
|
+
context.globalAlpha = (1 - k) * 0.25;
|
|
333
|
+
context.fillRect(s.x - size * 1.5, s.y - size * 1.5, size * 3, size * 3);
|
|
334
|
+
context.globalAlpha = 1 - k;
|
|
335
|
+
context.fillRect(s.x - size / 2, s.y - size / 2, size, size);
|
|
336
|
+
}
|
|
337
|
+
context.globalCompositeOperation = "source-over";
|
|
338
|
+
}
|
|
339
|
+
context.globalAlpha = 1;
|
|
340
|
+
specks = alive;
|
|
341
|
+
if (specks.length === 0) {
|
|
342
|
+
stop?.();
|
|
343
|
+
stop = null;
|
|
344
|
+
window.removeEventListener("resize", resize);
|
|
345
|
+
canvas?.remove();
|
|
346
|
+
canvas = null;
|
|
347
|
+
ctx = null;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// packages/dissolve/src/snapshot.ts
|
|
352
|
+
var XHTML = "http://www.w3.org/1999/xhtml";
|
|
353
|
+
var MAX_ELEMENTS = 800;
|
|
354
|
+
var SKIP = /* @__PURE__ */ new Set(["SCRIPT", "STYLE", "LINK", "TEMPLATE", "NOSCRIPT", "IFRAME", "OBJECT", "EMBED"]);
|
|
355
|
+
function read(source, columns, rows) {
|
|
356
|
+
try {
|
|
357
|
+
const canvas2 = document.createElement("canvas");
|
|
358
|
+
canvas2.width = columns;
|
|
359
|
+
canvas2.height = rows;
|
|
360
|
+
const ctx2 = canvas2.getContext("2d", { willReadFrequently: true });
|
|
361
|
+
ctx2.drawImage(source, 0, 0, columns, rows);
|
|
362
|
+
return { columns, rows, data: ctx2.getImageData(0, 0, columns, rows).data };
|
|
363
|
+
} catch {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function inline(source, width, height) {
|
|
368
|
+
try {
|
|
369
|
+
if (source instanceof HTMLCanvasElement) return source.toDataURL();
|
|
370
|
+
const canvas2 = document.createElement("canvas");
|
|
371
|
+
const scale = Math.min(1, 256 / Math.max(width, height, 1));
|
|
372
|
+
canvas2.width = Math.max(1, Math.round(width * scale));
|
|
373
|
+
canvas2.height = Math.max(1, Math.round(height * scale));
|
|
374
|
+
canvas2.getContext("2d").drawImage(source, 0, 0, canvas2.width, canvas2.height);
|
|
375
|
+
return canvas2.toDataURL();
|
|
376
|
+
} catch {
|
|
377
|
+
return "";
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function styleText(element) {
|
|
381
|
+
const computed = getComputedStyle(element);
|
|
382
|
+
let text = "";
|
|
383
|
+
for (let i = 0; i < computed.length; i++) {
|
|
384
|
+
const name = computed[i];
|
|
385
|
+
text += `${name}:${computed.getPropertyValue(name)};`;
|
|
386
|
+
}
|
|
387
|
+
return text;
|
|
388
|
+
}
|
|
389
|
+
function cloneStyled(source, budget) {
|
|
390
|
+
if (--budget.left < 0) return null;
|
|
391
|
+
const tag = source.tagName.toUpperCase();
|
|
392
|
+
if (source instanceof SVGElement) return source.cloneNode(true);
|
|
393
|
+
if (source instanceof HTMLImageElement || source instanceof HTMLCanvasElement || source instanceof HTMLVideoElement) {
|
|
394
|
+
const image = document.createElementNS(XHTML, "img");
|
|
395
|
+
image.setAttribute("style", styleText(source));
|
|
396
|
+
image.setAttribute("src", inline(source, source.offsetWidth, source.offsetHeight));
|
|
397
|
+
return image;
|
|
398
|
+
}
|
|
399
|
+
const clone = document.createElementNS(XHTML, source.localName);
|
|
400
|
+
clone.setAttribute("style", styleText(source));
|
|
401
|
+
if (source instanceof HTMLInputElement || source instanceof HTMLTextAreaElement) clone.setAttribute("value", source.value);
|
|
402
|
+
for (const child of source.childNodes) {
|
|
403
|
+
if (child.nodeType === 3) clone.append(child.textContent ?? "");
|
|
404
|
+
else if (child.nodeType === 1 && !SKIP.has(child.tagName.toUpperCase())) {
|
|
405
|
+
const copy = cloneStyled(child, budget);
|
|
406
|
+
if (!copy) return null;
|
|
407
|
+
clone.append(copy);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (tag === "TEXTAREA") clone.textContent = source.value;
|
|
411
|
+
return clone;
|
|
412
|
+
}
|
|
413
|
+
function load(url, timeout) {
|
|
414
|
+
return new Promise((resolve) => {
|
|
415
|
+
const image = new Image();
|
|
416
|
+
const timer = setTimeout(() => resolve(null), timeout);
|
|
417
|
+
image.onload = () => {
|
|
418
|
+
clearTimeout(timer);
|
|
419
|
+
resolve(image);
|
|
420
|
+
};
|
|
421
|
+
image.onerror = () => {
|
|
422
|
+
clearTimeout(timer);
|
|
423
|
+
resolve(null);
|
|
424
|
+
};
|
|
425
|
+
image.src = url;
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
async function snapshot(element, width, height, columns, rows) {
|
|
429
|
+
if (element instanceof HTMLImageElement) {
|
|
430
|
+
if (!element.complete || element.naturalWidth === 0) return null;
|
|
431
|
+
return read(element, columns, rows);
|
|
432
|
+
}
|
|
433
|
+
if (element instanceof HTMLCanvasElement || element instanceof HTMLVideoElement) return read(element, columns, rows);
|
|
434
|
+
const clone = cloneStyled(element, { left: MAX_ELEMENTS });
|
|
435
|
+
if (!clone) return null;
|
|
436
|
+
clone.style.setProperty("margin", "0");
|
|
437
|
+
clone.style.setProperty("position", "static");
|
|
438
|
+
clone.style.setProperty("transform", "none");
|
|
439
|
+
clone.style.setProperty("filter", "none");
|
|
440
|
+
clone.style.setProperty("opacity", "1");
|
|
441
|
+
clone.style.setProperty("visibility", "visible");
|
|
442
|
+
clone.style.setProperty("width", `${width}px`);
|
|
443
|
+
clone.style.setProperty("height", `${height}px`);
|
|
444
|
+
clone.style.setProperty("box-sizing", "border-box");
|
|
445
|
+
const markup = new XMLSerializer().serializeToString(clone);
|
|
446
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}"><foreignObject x="0" y="0" width="100%" height="100%">${markup}</foreignObject></svg>`;
|
|
447
|
+
const image = await load(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`, 1500);
|
|
448
|
+
return image ? read(image, columns, rows) : null;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// packages/dissolve/src/dissolve.ts
|
|
452
|
+
var hidden = /* @__PURE__ */ new WeakMap();
|
|
453
|
+
var running = /* @__PURE__ */ new WeakMap();
|
|
454
|
+
var originals = /* @__PURE__ */ new WeakMap();
|
|
455
|
+
function remember(element) {
|
|
456
|
+
if (!originals.has(element)) originals.set(element, { attribute: element.getAttribute("style"), css: element.style.cssText });
|
|
457
|
+
}
|
|
458
|
+
function tidy(element) {
|
|
459
|
+
const original = originals.get(element);
|
|
460
|
+
if (!original || element.style.cssText !== original.css) return;
|
|
461
|
+
originals.delete(element);
|
|
462
|
+
element.getAttribute("style");
|
|
463
|
+
if (original.attribute === null) element.removeAttribute("style");
|
|
464
|
+
else element.setAttribute("style", original.attribute);
|
|
465
|
+
}
|
|
466
|
+
var MELT_REACH = 1.6;
|
|
467
|
+
var SPECK_BUDGET = 6e3;
|
|
468
|
+
function dissolve(target, options = {}) {
|
|
469
|
+
return animate(target, options, false);
|
|
470
|
+
}
|
|
471
|
+
function assemble(target, options = {}) {
|
|
472
|
+
return animate(target, options, true);
|
|
473
|
+
}
|
|
474
|
+
function isDissolved(target) {
|
|
475
|
+
return isBrowser && hidden.has(resolveElement(target));
|
|
476
|
+
}
|
|
477
|
+
function hide(element) {
|
|
478
|
+
remember(element);
|
|
479
|
+
if (!hidden.has(element)) {
|
|
480
|
+
hidden.set(element, [element.style.getPropertyValue("visibility"), element.style.getPropertyPriority("visibility")]);
|
|
481
|
+
}
|
|
482
|
+
element.style.setProperty("visibility", "hidden", "important");
|
|
483
|
+
}
|
|
484
|
+
function unhide(element) {
|
|
485
|
+
const saved = hidden.get(element);
|
|
486
|
+
if (!saved) return;
|
|
487
|
+
hidden.delete(element);
|
|
488
|
+
if (saved[0]) element.style.setProperty("visibility", saved[0], saved[1]);
|
|
489
|
+
else element.style.removeProperty("visibility");
|
|
490
|
+
tidy(element);
|
|
491
|
+
}
|
|
492
|
+
function direction(value) {
|
|
493
|
+
switch (value) {
|
|
494
|
+
case "right":
|
|
495
|
+
return [1, 0];
|
|
496
|
+
case "left":
|
|
497
|
+
return [-1, 0];
|
|
498
|
+
case "up":
|
|
499
|
+
return [0, -1];
|
|
500
|
+
case "down":
|
|
501
|
+
return [0, 1];
|
|
502
|
+
default:
|
|
503
|
+
return [0, 0];
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
function parseColor(value) {
|
|
507
|
+
const match = value.match(/rgba?\(([^)]+)\)/);
|
|
508
|
+
if (!match) return null;
|
|
509
|
+
const parts = match[1].split(/[\s,/]+/).filter(Boolean).map(Number);
|
|
510
|
+
return [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0, parts.length > 3 ? parts[3] : 1];
|
|
511
|
+
}
|
|
512
|
+
async function cells(element, width, height, mask, o) {
|
|
513
|
+
let grain = o.grain;
|
|
514
|
+
while (width / grain * (height / grain) > 6e4) grain *= 1.5;
|
|
515
|
+
const columns = Math.max(1, Math.ceil(width / grain));
|
|
516
|
+
const rows = Math.max(1, Math.ceil(height / grain));
|
|
517
|
+
const picture = await snapshot(element, width, height, columns, rows);
|
|
518
|
+
const random = createRandom2(o.seed ^ 1540483477);
|
|
519
|
+
const result = [];
|
|
520
|
+
if (picture) {
|
|
521
|
+
const { data } = picture;
|
|
522
|
+
for (let j = 0; j < rows; j++) {
|
|
523
|
+
for (let i = 0; i < columns; i++) {
|
|
524
|
+
const k = (j * columns + i) * 4;
|
|
525
|
+
const alpha = data[k + 3];
|
|
526
|
+
if (alpha < 40) continue;
|
|
527
|
+
const r = data[k];
|
|
528
|
+
const g = data[k + 1];
|
|
529
|
+
const b = data[k + 2];
|
|
530
|
+
const x = (i + 0.5) * grain;
|
|
531
|
+
const y = (j + 0.5) * grain;
|
|
532
|
+
result.push({ x, y, at: mask.at(x, y), color: `rgb(${r},${g},${b})` });
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return result;
|
|
536
|
+
}
|
|
537
|
+
const style = getComputedStyle(element);
|
|
538
|
+
const background = parseColor(style.backgroundColor);
|
|
539
|
+
const ink = parseColor(style.color) ?? [40, 40, 40, 1];
|
|
540
|
+
const solid = background && background[3] > 0.1;
|
|
541
|
+
for (let j = 0; j < rows; j++) {
|
|
542
|
+
for (let i = 0; i < columns; i++) {
|
|
543
|
+
if (!solid && random() > 0.3) continue;
|
|
544
|
+
const [r, g, b] = solid && random() > 0.15 ? background : ink;
|
|
545
|
+
const x = (i + 0.5) * grain;
|
|
546
|
+
const y = (j + 0.5) * grain;
|
|
547
|
+
result.push({ x, y, at: mask.at(x, y), color: `rgb(${r},${g},${b})` });
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return result;
|
|
551
|
+
}
|
|
552
|
+
async function animate(target, options, reverse) {
|
|
553
|
+
if (!isBrowser) return;
|
|
554
|
+
const element = resolveElement(target);
|
|
555
|
+
running.get(element)?.();
|
|
556
|
+
const o = resolveOptions(options);
|
|
557
|
+
if (o.duration === 0) {
|
|
558
|
+
if (reverse) unhide(element);
|
|
559
|
+
else finishVanish(element, o);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (o.respectReducedMotion && prefersReducedMotion()) {
|
|
563
|
+
if (reverse) unhide(element);
|
|
564
|
+
const fade = element.animate?.([{ opacity: reverse ? 0 : 1 }, { opacity: reverse ? 1 : 0 }], { duration: 250, easing: "ease" });
|
|
565
|
+
await fade?.finished.catch(() => void 0);
|
|
566
|
+
if (!reverse) finishVanish(element, o);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
const html = element;
|
|
570
|
+
const rect = element.getBoundingClientRect();
|
|
571
|
+
const width = html.offsetWidth || rect.width;
|
|
572
|
+
const height = html.offsetHeight || rect.height;
|
|
573
|
+
if (width < 1 || height < 1) {
|
|
574
|
+
if (reverse) unhide(element);
|
|
575
|
+
else finishVanish(element, o);
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const melting = o.effect === "melt";
|
|
579
|
+
const mask = melting ? null : createMask(width, height, o);
|
|
580
|
+
const filter = createFilter({
|
|
581
|
+
effect: o.effect,
|
|
582
|
+
width,
|
|
583
|
+
height,
|
|
584
|
+
mask: mask?.url ?? "",
|
|
585
|
+
drip: melting ? createDrip(width, height, MELT_REACH, o.seed) : void 0,
|
|
586
|
+
reach: MELT_REACH,
|
|
587
|
+
color: o.color
|
|
588
|
+
});
|
|
589
|
+
const savedFilter = [element.style.getPropertyValue("filter"), element.style.getPropertyPriority("filter")];
|
|
590
|
+
const computedFilter = getComputedStyle(element).filter;
|
|
591
|
+
const place = (p) => {
|
|
592
|
+
if (melting) {
|
|
593
|
+
const k = reverse ? 1 - p : p;
|
|
594
|
+
filter.melt(height * 1.8 * k * k, 1 - smoothstep(0.5, 1, k));
|
|
595
|
+
} else {
|
|
596
|
+
const k = reverse ? 1 - p : p;
|
|
597
|
+
filter.set(filter.start + (filter.end - filter.start) * k);
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
place(0);
|
|
601
|
+
remember(element);
|
|
602
|
+
element.style.setProperty("filter", `url(#${filter.id})${computedFilter && computedFilter !== "none" ? ` ${computedFilter}` : ""}`, "important");
|
|
603
|
+
if (reverse) unhide(element);
|
|
604
|
+
await decode(mask?.url);
|
|
605
|
+
const specks2 = mask && (o.effect === "dust" || o.effect === "burn") && o.particles > 0 ? await cells(element, width, height, mask, o) : [];
|
|
606
|
+
return new Promise((resolve) => {
|
|
607
|
+
let elapsed = 0;
|
|
608
|
+
let stop2 = null;
|
|
609
|
+
const random = createRandom2(o.seed ^ 668265263);
|
|
610
|
+
const [dx, dy] = direction(o.direction);
|
|
611
|
+
const order = specks2.sort((a, b) => a.at - b.at);
|
|
612
|
+
let next = 0;
|
|
613
|
+
const share = order.length ? Math.min(1, SPECK_BUDGET * o.particles / order.length) : 0;
|
|
614
|
+
const finish = () => {
|
|
615
|
+
if (running.get(element) === finish) running.delete(element);
|
|
616
|
+
stop2?.();
|
|
617
|
+
stop2 = null;
|
|
618
|
+
filter.destroy();
|
|
619
|
+
if (savedFilter[0]) element.style.setProperty("filter", savedFilter[0], savedFilter[1]);
|
|
620
|
+
else element.style.removeProperty("filter");
|
|
621
|
+
tidy(element);
|
|
622
|
+
if (!reverse) finishVanish(element, o);
|
|
623
|
+
resolve();
|
|
624
|
+
};
|
|
625
|
+
running.set(element, finish);
|
|
626
|
+
if (reverse && o.effect === "dust") gather(element, order, share, o, dx, dy, filter, random);
|
|
627
|
+
stop2 = onFrame2((dt) => {
|
|
628
|
+
elapsed += dt;
|
|
629
|
+
const p = Math.min(1, elapsed / o.duration);
|
|
630
|
+
place(p);
|
|
631
|
+
if (!reverse && order.length) {
|
|
632
|
+
const edge = filter.start + (filter.end - filter.start) * p;
|
|
633
|
+
const box = element.getBoundingClientRect();
|
|
634
|
+
const sx = box.width / width;
|
|
635
|
+
const sy = box.height / height;
|
|
636
|
+
const batch = [];
|
|
637
|
+
while (next < order.length && order[next].at <= edge) {
|
|
638
|
+
const cell = order[next++];
|
|
639
|
+
const x = box.left + window.scrollX + cell.x * sx;
|
|
640
|
+
const y = box.top + window.scrollY + cell.y * sy;
|
|
641
|
+
if (o.effect === "burn") burnSpecks(batch, x, y, o, random);
|
|
642
|
+
else if (random() < share) batch.push(dust(cell, x, y, o, dx, dy, width, height, random));
|
|
643
|
+
}
|
|
644
|
+
emit(batch);
|
|
645
|
+
}
|
|
646
|
+
if (p >= 1) finish();
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
function dust(cell, x, y, o, dx, dy, width, height, random) {
|
|
651
|
+
let ux = dx;
|
|
652
|
+
let uy = dy;
|
|
653
|
+
if (ux === 0 && uy === 0) {
|
|
654
|
+
const ox = cell.x - width / 2;
|
|
655
|
+
const oy = cell.y - height / 2;
|
|
656
|
+
const length = Math.hypot(ox, oy) || 1;
|
|
657
|
+
ux = ox / length;
|
|
658
|
+
uy = oy / length;
|
|
659
|
+
}
|
|
660
|
+
const speed = 50 + random() * 150;
|
|
661
|
+
return {
|
|
662
|
+
x,
|
|
663
|
+
y,
|
|
664
|
+
vx: ux * speed + (random() - 0.5) * 70,
|
|
665
|
+
vy: uy * speed + (random() - 0.5) * 70 - 30 - random() * 60,
|
|
666
|
+
age: 0,
|
|
667
|
+
life: 0.8 + random() * 1.1,
|
|
668
|
+
size: o.grain * (0.7 + random() * 0.6),
|
|
669
|
+
color: cell.color,
|
|
670
|
+
kind: "dust"
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
var SPARKS = ["#fff3b0", "#ffd166", "#ff9f1c", "#ff6b00"];
|
|
674
|
+
function burnSpecks(batch, x, y, o, random) {
|
|
675
|
+
const roll = random();
|
|
676
|
+
if (roll < 0.03 * o.particles) {
|
|
677
|
+
batch.push({
|
|
678
|
+
x,
|
|
679
|
+
y,
|
|
680
|
+
vx: (random() - 0.5) * 90,
|
|
681
|
+
vy: -60 - random() * 160,
|
|
682
|
+
age: 0,
|
|
683
|
+
life: 0.5 + random() * 0.9,
|
|
684
|
+
size: 1.5 + random() * 2,
|
|
685
|
+
color: SPARKS[Math.floor(random() * SPARKS.length)],
|
|
686
|
+
kind: "spark"
|
|
687
|
+
});
|
|
688
|
+
} else if (roll < 0.03 * o.particles + 0.06 * o.particles) {
|
|
689
|
+
batch.push({
|
|
690
|
+
x,
|
|
691
|
+
y,
|
|
692
|
+
vx: (random() - 0.5) * 40,
|
|
693
|
+
vy: -20 - random() * 50,
|
|
694
|
+
age: 0,
|
|
695
|
+
life: 0.6 + random() * 0.8,
|
|
696
|
+
size: o.grain * (0.6 + random() * 0.5),
|
|
697
|
+
color: "#2b211c",
|
|
698
|
+
kind: "dust"
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function gather(element, order, share, o, dx, dy, filter, random) {
|
|
703
|
+
const box = element.getBoundingClientRect();
|
|
704
|
+
const html = element;
|
|
705
|
+
const sx = box.width / (html.offsetWidth || box.width);
|
|
706
|
+
const sy = box.height / (html.offsetHeight || box.height);
|
|
707
|
+
const span = filter.end - filter.start;
|
|
708
|
+
const batch = [];
|
|
709
|
+
for (const cell of order) {
|
|
710
|
+
if (random() >= share) continue;
|
|
711
|
+
const arrival = (filter.end - cell.at) / span * o.duration;
|
|
712
|
+
const flight = Math.min(arrival, 0.6 + random() * 0.6);
|
|
713
|
+
if (flight < 0.05) continue;
|
|
714
|
+
const x1 = box.left + window.scrollX + cell.x * sx;
|
|
715
|
+
const y1 = box.top + window.scrollY + cell.y * sy;
|
|
716
|
+
let ux = dx;
|
|
717
|
+
let uy = dy;
|
|
718
|
+
if (ux === 0 && uy === 0) {
|
|
719
|
+
const angle = random() * Math.PI * 2;
|
|
720
|
+
ux = Math.cos(angle);
|
|
721
|
+
uy = Math.sin(angle);
|
|
722
|
+
}
|
|
723
|
+
const distance = 80 + random() * 180;
|
|
724
|
+
batch.push({
|
|
725
|
+
x: x1,
|
|
726
|
+
y: y1,
|
|
727
|
+
vx: 0,
|
|
728
|
+
vy: 0,
|
|
729
|
+
age: 0,
|
|
730
|
+
life: flight,
|
|
731
|
+
delay: arrival - flight,
|
|
732
|
+
size: o.grain * (0.7 + random() * 0.6),
|
|
733
|
+
color: cell.color,
|
|
734
|
+
kind: "gather",
|
|
735
|
+
x0: x1 + ux * distance + (random() - 0.5) * 60,
|
|
736
|
+
y0: y1 + uy * distance + (random() - 0.5) * 60 - 30,
|
|
737
|
+
x1,
|
|
738
|
+
y1
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
emit(batch);
|
|
742
|
+
}
|
|
743
|
+
function finishVanish(element, o) {
|
|
744
|
+
if (o.after === "remove") element.remove();
|
|
745
|
+
else hide(element);
|
|
746
|
+
}
|
|
747
|
+
function smoothstep(a, b, x) {
|
|
748
|
+
const t = Math.min(1, Math.max(0, (x - a) / (b - a)));
|
|
749
|
+
return t * t * (3 - 2 * t);
|
|
750
|
+
}
|
|
751
|
+
function decode(url) {
|
|
752
|
+
if (!url) return Promise.resolve();
|
|
753
|
+
const image = new Image();
|
|
754
|
+
image.src = url;
|
|
755
|
+
return image.decode().catch(() => void 0);
|
|
756
|
+
}
|
|
757
|
+
export {
|
|
758
|
+
activeSpecks,
|
|
759
|
+
assemble,
|
|
760
|
+
dissolve,
|
|
761
|
+
isDissolved,
|
|
762
|
+
resolveOptions
|
|
763
|
+
};
|
|
764
|
+
//# sourceMappingURL=index.js.map
|