@clypra-studio/engine 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.
Files changed (53) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +677 -0
  3. package/dist/chunk-5JOAEDIQ.js +446 -0
  4. package/dist/chunk-5JOAEDIQ.js.map +1 -0
  5. package/dist/chunk-CJRH4PAV.js +2826 -0
  6. package/dist/chunk-CJRH4PAV.js.map +1 -0
  7. package/dist/chunk-EVQAI3P3.js +3725 -0
  8. package/dist/chunk-EVQAI3P3.js.map +1 -0
  9. package/dist/chunk-PZ5AY32C.js +10 -0
  10. package/dist/chunk-PZ5AY32C.js.map +1 -0
  11. package/dist/chunk-QMRMNH52.js +811 -0
  12. package/dist/chunk-QMRMNH52.js.map +1 -0
  13. package/dist/index-BWo6qV_z.d.cts +139 -0
  14. package/dist/index-BWo6qV_z.d.ts +139 -0
  15. package/dist/index-lNWEWqab.d.cts +633 -0
  16. package/dist/index-lNWEWqab.d.ts +633 -0
  17. package/dist/index.cjs +21232 -0
  18. package/dist/index.cjs.map +1 -0
  19. package/dist/index.d.cts +12254 -0
  20. package/dist/index.d.ts +12254 -0
  21. package/dist/index.js +13320 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/textEffects/index.cjs +3341 -0
  24. package/dist/textEffects/index.cjs.map +1 -0
  25. package/dist/textEffects/index.d.cts +1 -0
  26. package/dist/textEffects/index.d.ts +1 -0
  27. package/dist/textEffects/index.js +30 -0
  28. package/dist/textEffects/index.js.map +1 -0
  29. package/dist/transitions/index.cjs +848 -0
  30. package/dist/transitions/index.cjs.map +1 -0
  31. package/dist/transitions/index.d.cts +170 -0
  32. package/dist/transitions/index.d.ts +170 -0
  33. package/dist/transitions/index.js +32 -0
  34. package/dist/transitions/index.js.map +1 -0
  35. package/dist/v2/backends/index.cjs +480 -0
  36. package/dist/v2/backends/index.cjs.map +1 -0
  37. package/dist/v2/backends/index.d.cts +3 -0
  38. package/dist/v2/backends/index.d.ts +3 -0
  39. package/dist/v2/backends/index.js +8 -0
  40. package/dist/v2/backends/index.js.map +1 -0
  41. package/dist/v2/contract/index.cjs +18 -0
  42. package/dist/v2/contract/index.cjs.map +1 -0
  43. package/dist/v2/contract/index.d.cts +165 -0
  44. package/dist/v2/contract/index.d.ts +165 -0
  45. package/dist/v2/contract/index.js +1 -0
  46. package/dist/v2/contract/index.js.map +1 -0
  47. package/dist/videoEffects/index.cjs +2883 -0
  48. package/dist/videoEffects/index.cjs.map +1 -0
  49. package/dist/videoEffects/index.d.cts +801 -0
  50. package/dist/videoEffects/index.d.ts +801 -0
  51. package/dist/videoEffects/index.js +68 -0
  52. package/dist/videoEffects/index.js.map +1 -0
  53. package/package.json +119 -0
@@ -0,0 +1,3341 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/textEffects/index.ts
20
+ var textEffects_exports = {};
21
+ __export(textEffects_exports, {
22
+ FontLoader: () => FontLoader,
23
+ _buildConfig: () => _buildConfig,
24
+ builtInPresets: () => builtInPresets,
25
+ defaultConfig: () => defaultConfig,
26
+ ensureFontsLoaded: () => ensureFontsLoaded,
27
+ evaluateScene: () => evaluateScene,
28
+ getFontLoader: () => getFontLoader,
29
+ renderTextEffectCore: () => renderTextEffectCore,
30
+ resetFontLoader: () => resetFontLoader,
31
+ resolveFontFamilyName: () => resolveFontFamilyName,
32
+ sceneToConfig: () => sceneToConfig,
33
+ textEffectConfigToScene: () => textEffectConfigToScene
34
+ });
35
+ module.exports = __toCommonJS(textEffects_exports);
36
+
37
+ // src/engine/schema.ts
38
+ var DEFAULT_CANVAS_WIDTH = 800;
39
+ var DEFAULT_CANVAS_HEIGHT = 200;
40
+ var DEFAULT_FONT_SIZE = 80;
41
+ var LEGACY_RENDERER_MAP = {
42
+ InkBrushEngine: "ink"
43
+ };
44
+ var ENGINE_ID_TO_LEGACY = {
45
+ ink: "InkBrushEngine"
46
+ };
47
+ function newLayerId() {
48
+ return `layer-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`;
49
+ }
50
+
51
+ // src/engine/textLayout.ts
52
+ function measureLine(ctx, line, letterSpacing) {
53
+ const prev = ctx.letterSpacing;
54
+ if (letterSpacing !== 0) {
55
+ ctx.letterSpacing = `${letterSpacing}px`;
56
+ }
57
+ const w = ctx.measureText(line || " ").width;
58
+ ctx.letterSpacing = prev ?? "0px";
59
+ return w;
60
+ }
61
+ function getSafeRect(cfg) {
62
+ const w = cfg.canvasWidth || DEFAULT_CANVAS_WIDTH;
63
+ const h = cfg.canvasHeight || DEFAULT_CANVAS_HEIGHT;
64
+ const marginX = cfg.panelEnabled ? (cfg.panelPaddingX ?? 40) + 16 : Math.min(48, w * 0.06);
65
+ const marginY = cfg.panelEnabled ? (cfg.panelPaddingY ?? 20) + 16 : Math.min(40, h * 0.1);
66
+ return {
67
+ x: marginX,
68
+ y: marginY,
69
+ width: Math.max(40, w - marginX * 2),
70
+ height: Math.max(24, h - marginY * 2)
71
+ };
72
+ }
73
+ function wrapTextToWidth(ctx, text, maxWidth, letterSpacing) {
74
+ const paragraphs = text.split("\n");
75
+ const lines = [];
76
+ for (const para of paragraphs) {
77
+ if (para === "") {
78
+ lines.push("");
79
+ continue;
80
+ }
81
+ let current = "";
82
+ for (let i = 0; i < para.length; i++) {
83
+ const char = para[i];
84
+ const tryLine = current + char;
85
+ if (measureLine(ctx, tryLine, letterSpacing) <= maxWidth) {
86
+ current = tryLine;
87
+ } else {
88
+ if (current) {
89
+ lines.push(current);
90
+ }
91
+ current = char;
92
+ }
93
+ }
94
+ if (current) {
95
+ lines.push(current);
96
+ }
97
+ }
98
+ return lines.length > 0 ? lines : [""];
99
+ }
100
+ function layoutWithFontSize(ctx, cfg, fontSize, lines) {
101
+ const cWidth = cfg.canvasWidth || DEFAULT_CANVAS_WIDTH;
102
+ const cHeight = cfg.canvasHeight || DEFAULT_CANVAS_HEIGHT;
103
+ const safe = getSafeRect(cfg);
104
+ const lineHeight = cfg.lineHeight ?? 1.2;
105
+ const letterSpacing = cfg.letterSpacing ?? 0;
106
+ const fontStr = `${cfg.fontStyle} ${cfg.fontWeight} ${fontSize}px "${cfg.fontFamily}"`;
107
+ ctx.font = fontStr;
108
+ const lineWidths = lines.map((line) => measureLine(ctx, line, letterSpacing));
109
+ const maxLineWidth = Math.max(...lineWidths, 1);
110
+ const textBlockHeight = lines.length === 1 ? fontSize : (lines.length - 1) * fontSize * lineHeight + fontSize;
111
+ let align = "center";
112
+ let startX = cWidth / 2;
113
+ if (cfg.textPosX === "left") {
114
+ align = "left";
115
+ startX = safe.x;
116
+ } else if (cfg.textPosX === "right") {
117
+ align = "right";
118
+ startX = safe.x + safe.width;
119
+ } else {
120
+ startX = safe.x + safe.width / 2;
121
+ }
122
+ let startY = safe.y + (safe.height - textBlockHeight) / 2 + fontSize * 0.85;
123
+ if (cfg.textPosY === "top") {
124
+ startY = safe.y + fontSize * 0.85;
125
+ } else if (cfg.textPosY === "bottom") {
126
+ startY = safe.y + safe.height - textBlockHeight + fontSize * 0.85;
127
+ }
128
+ let xMin = startX;
129
+ if (align === "center") xMin = startX - maxLineWidth / 2;
130
+ else if (align === "right") xMin = startX - maxLineWidth;
131
+ const yMin = startY - fontSize * 0.85;
132
+ const yMax = startY + (lines.length - 1) * fontSize * lineHeight + fontSize * 0.15;
133
+ return {
134
+ lines,
135
+ fontSize,
136
+ startX,
137
+ startY,
138
+ align,
139
+ lineWidths,
140
+ bounds: {
141
+ xMin,
142
+ yMin,
143
+ xMax: xMin + maxLineWidth,
144
+ yMax,
145
+ maxLineWidth,
146
+ textBlockHeight
147
+ },
148
+ safeRect: safe
149
+ };
150
+ }
151
+ function measureTextFits(ctx, cfg, fontSize, lines) {
152
+ const safe = getSafeRect(cfg);
153
+ const lineHeight = cfg.lineHeight ?? 1.2;
154
+ const layout = layoutWithFontSize(ctx, cfg, fontSize, lines);
155
+ return layout.bounds.maxLineWidth <= safe.width + 1 && layout.bounds.textBlockHeight <= safe.height + 1;
156
+ }
157
+ function computeAutoFitFontSize(ctx, cfg, wrappedLines) {
158
+ const max = Math.min(cfg.fontSize || DEFAULT_FONT_SIZE, 200);
159
+ let lo = 12;
160
+ let hi = max;
161
+ let best = 12;
162
+ while (lo <= hi) {
163
+ const mid = Math.floor((lo + hi) / 2);
164
+ if (measureTextFits(ctx, cfg, mid, wrappedLines)) {
165
+ best = mid;
166
+ lo = mid + 1;
167
+ } else {
168
+ hi = mid - 1;
169
+ }
170
+ }
171
+ return best;
172
+ }
173
+ function computeTextLayout(ctx, cfg, options) {
174
+ const safe = getSafeRect(cfg);
175
+ const wrap = options?.wrap ?? true;
176
+ const autoFit = options?.autoFit ?? !!cfg.autoFitText;
177
+ let lines = cfg.text.split("\n");
178
+ if (wrap) {
179
+ lines = wrapTextToWidth(ctx, cfg.text, safe.width, cfg.letterSpacing ?? 0);
180
+ }
181
+ let fontSize = cfg.fontSize;
182
+ if (autoFit) {
183
+ fontSize = computeAutoFitFontSize(ctx, cfg, lines);
184
+ }
185
+ return layoutWithFontSize(ctx, cfg, fontSize, lines);
186
+ }
187
+
188
+ // src/engine/perCharFill.ts
189
+ function measureLineWidth(ctx, line, letterSpacing) {
190
+ if (!line) return 0;
191
+ let w = 0;
192
+ for (let i = 0; i < line.length; i++) {
193
+ w += ctx.measureText(line[i]).width;
194
+ if (i < line.length - 1) w += letterSpacing;
195
+ }
196
+ return w;
197
+ }
198
+ function drawPerCharText(ctx, opts) {
199
+ const {
200
+ lines,
201
+ startX,
202
+ startY,
203
+ lineAdvance,
204
+ align,
205
+ letterSpacing,
206
+ charFillColors,
207
+ defaultColor,
208
+ mode,
209
+ offsetX = 0,
210
+ offsetY = 0
211
+ } = opts;
212
+ ctx.save();
213
+ const prevSpacing = ctx.letterSpacing;
214
+ ctx.letterSpacing = "0px";
215
+ ctx.textBaseline = "alphabetic";
216
+ let glyphIndex = 0;
217
+ lines.forEach((line, lineIndex) => {
218
+ const py = startY + lineIndex * lineAdvance;
219
+ const lineWidth = measureLineWidth(ctx, line, letterSpacing);
220
+ let x = startX;
221
+ if (align === "center") x = startX - lineWidth / 2;
222
+ else if (align === "right") x = startX - lineWidth;
223
+ for (let i = 0; i < line.length; i++) {
224
+ const ch = line[i];
225
+ const color = charFillColors[glyphIndex] ?? defaultColor;
226
+ if (mode === "fill") ctx.fillStyle = color;
227
+ else ctx.strokeStyle = color;
228
+ const drawX = x + offsetX;
229
+ const drawY = py + offsetY;
230
+ if (mode === "fill") ctx.fillText(ch, drawX, drawY);
231
+ else ctx.strokeText(ch, drawX, drawY);
232
+ x += ctx.measureText(ch).width + letterSpacing;
233
+ glyphIndex++;
234
+ }
235
+ });
236
+ ctx.letterSpacing = prevSpacing ?? "0px";
237
+ ctx.restore();
238
+ }
239
+ function shouldUsePerCharFill(cfg) {
240
+ return !!cfg.perCharFillEnabled && cfg.fillType === "solid" && !cfg.customRenderer && (cfg.charFillColors?.length ?? 0) > 0;
241
+ }
242
+
243
+ // src/platform.ts
244
+ var _roundRect = null;
245
+ var _offscreenCanvas = null;
246
+ function probe2d() {
247
+ if (typeof document === "undefined") return null;
248
+ try {
249
+ return document.createElement("canvas").getContext("2d");
250
+ } catch {
251
+ return null;
252
+ }
253
+ }
254
+ function supportsRoundRect() {
255
+ if (_roundRect !== null) return _roundRect;
256
+ const ctx = probe2d();
257
+ _roundRect = !!ctx && typeof ctx.roundRect === "function";
258
+ return _roundRect;
259
+ }
260
+ function supportsOffscreenCanvas() {
261
+ if (_offscreenCanvas !== null) return _offscreenCanvas;
262
+ _offscreenCanvas = typeof globalThis !== "undefined" && typeof globalThis.OffscreenCanvas === "function";
263
+ return _offscreenCanvas;
264
+ }
265
+ function createCanvas(width, height) {
266
+ if (supportsOffscreenCanvas()) {
267
+ return new OffscreenCanvas(width, height);
268
+ }
269
+ if (typeof document !== "undefined") {
270
+ const canvas = document.createElement("canvas");
271
+ canvas.width = width;
272
+ canvas.height = height;
273
+ return canvas;
274
+ }
275
+ const factory = globalThis.__clypraCreateCanvas;
276
+ if (factory) return factory(width, height);
277
+ try {
278
+ const nodeRequire = (0, eval)("require");
279
+ const nodeCanvas = nodeRequire("@napi-rs/canvas");
280
+ return nodeCanvas.createCanvas(width, height);
281
+ } catch {
282
+ throw new Error("[clypra/engine] No canvas implementation available in this environment. Set globalThis.__clypraCreateCanvas or install @napi-rs/canvas.");
283
+ }
284
+ }
285
+ function releaseCanvas(canvas) {
286
+ if (canvas instanceof OffscreenCanvas) return;
287
+ canvas.width = 0;
288
+ canvas.height = 0;
289
+ }
290
+ var CanvasDevice = class {
291
+ static canvases = [];
292
+ static maxPoolSize = 10;
293
+ /**
294
+ * Acquire a Canvas context from the pool or create a new one.
295
+ * If a canvas is pulled from the pool, it is resized to the target dimensions.
296
+ */
297
+ static acquire(width, height) {
298
+ let canvas;
299
+ if (this.canvases.length > 0) {
300
+ canvas = this.canvases.pop();
301
+ if (canvas.width !== width || canvas.height !== height) {
302
+ canvas.width = width;
303
+ canvas.height = height;
304
+ }
305
+ } else {
306
+ canvas = createCanvas(width, height);
307
+ }
308
+ return canvas;
309
+ }
310
+ /**
311
+ * Release a canvas back to the pool, or free its resources immediately if pool is full.
312
+ */
313
+ static release(canvas) {
314
+ if (this.canvases.length < this.maxPoolSize) {
315
+ this.canvases.push(canvas);
316
+ } else {
317
+ releaseCanvas(canvas);
318
+ }
319
+ }
320
+ /**
321
+ * Disposes all pooled canvases to release GPU/memory backing stores.
322
+ */
323
+ static clearPool() {
324
+ while (this.canvases.length > 0) {
325
+ const c = this.canvases.pop();
326
+ releaseCanvas(c);
327
+ }
328
+ }
329
+ };
330
+
331
+ // src/engine/procedural/utils.ts
332
+ function getCanvas2DContext(canvas) {
333
+ return canvas.getContext("2d");
334
+ }
335
+ function seededRandom(seed) {
336
+ let s = seed;
337
+ return function() {
338
+ s = s * 16807 % 2147483647;
339
+ return (s - 1) / 2147483646;
340
+ };
341
+ }
342
+ function textSeed(text) {
343
+ return text.split("").reduce((acc, c) => acc + c.charCodeAt(0), 0) * 9301 % 49297;
344
+ }
345
+ function hexToRgb(hex) {
346
+ const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
347
+ const fullHex = hex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);
348
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(fullHex);
349
+ return result ? {
350
+ r: parseInt(result[1], 16),
351
+ g: parseInt(result[2], 16),
352
+ b: parseInt(result[3], 16)
353
+ } : { r: 255, g: 255, b: 255 };
354
+ }
355
+
356
+ // src/engine/procedural/InkBrushEngine.ts
357
+ var InkBrushEngine = class {
358
+ cfg;
359
+ bristleLines = [];
360
+ grainDots = [];
361
+ drips = [];
362
+ constructor(config) {
363
+ const defaults = {
364
+ text: "INK",
365
+ effectName: "InkBrush",
366
+ fontFamily: "Bebas Neue",
367
+ fontWeight: 900,
368
+ fontStyle: "italic",
369
+ fontSize: 96,
370
+ letterSpacing: 2,
371
+ lineHeight: 1.1,
372
+ wrapText: true,
373
+ autoFitText: false,
374
+ perCharFillEnabled: false,
375
+ charFillColors: [],
376
+ fillType: "solid",
377
+ fillColor: "#FFFFFF",
378
+ fillGradientAngle: 90,
379
+ fillGradientStops: [],
380
+ patternType: "chalk",
381
+ strokeEnabled: false,
382
+ strokeColor: "#000000",
383
+ strokeWidth: 0,
384
+ strokePosition: "outside",
385
+ strokeOpacity: 100,
386
+ strokeLineJoin: "round",
387
+ strokeBlur: 0,
388
+ strokeType: "single",
389
+ strokeColorSecondary: "#FFFFFF",
390
+ strokeWidthSecondary: 4,
391
+ strokeFadeRange: 0,
392
+ glowLayers: [],
393
+ shadowEnabled: false,
394
+ shadowColor: "#000000",
395
+ shadowBlur: 10,
396
+ shadowOffsetX: 5,
397
+ shadowOffsetY: 5,
398
+ shadowOpacity: 80,
399
+ shadowType: "drop",
400
+ bevelEnabled: false,
401
+ bevelDepth: 5,
402
+ bevelHighlight: "#FFFFFF",
403
+ bevelShadow: "#000000",
404
+ bevelDirection: "bottom-right",
405
+ bevelCoreColor: "#000000",
406
+ bevelEdgeColor: "#2A2A38",
407
+ bevelEdgeWidth: 0,
408
+ bevelBlur: 0,
409
+ bevelBlurColor: "#000000",
410
+ bevelPerspectiveEnabled: false,
411
+ bevelVanishingPointX: 40,
412
+ bevelVanishingPointY: 80,
413
+ bevelFocalLength: 400,
414
+ stackEnabled: false,
415
+ stackCount: 3,
416
+ stackOffsetX: 10,
417
+ stackOffsetY: -10,
418
+ stackOpacityDecay: 20,
419
+ stackColor1: "#FF7C00",
420
+ stackColor2: "#00FFDD",
421
+ stackColor3: "#FF00AA",
422
+ stackColor4: "#AA00FF",
423
+ panelEnabled: false,
424
+ panelColor: "#1E1E26",
425
+ panelOpacity: 80,
426
+ panelRadius: 12,
427
+ panelPaddingX: 40,
428
+ panelPaddingY: 20,
429
+ panelStrokeEnabled: false,
430
+ panelStrokeColor: "#2A2A38",
431
+ panelStrokeWidth: 2,
432
+ canvasWidth: DEFAULT_CANVAS_WIDTH,
433
+ canvasHeight: DEFAULT_CANVAS_HEIGHT,
434
+ textPosX: "center",
435
+ textPosY: "middle",
436
+ inkColor: "#FFFFFF",
437
+ bristleDensity: 0.8,
438
+ bristleSkipRate: 0.2,
439
+ dripRate: 0.3,
440
+ dripMaxLength: 40,
441
+ grainDensity: 0.15,
442
+ skewX: -0.2,
443
+ customRenderer: "InkBrushEngine"
444
+ };
445
+ this.cfg = {
446
+ ...defaults,
447
+ ...config
448
+ };
449
+ this.precomputeData();
450
+ }
451
+ precomputeData() {
452
+ const { text, fontFamily, fontWeight, fontStyle, fontSize, letterSpacing, lineHeight, canvasWidth, canvasHeight, textPosX, textPosY, bristleDensity, bristleSkipRate, dripRate, dripMaxLength, grainDensity, skewX } = this.cfg;
453
+ const width = canvasWidth;
454
+ const height = canvasHeight;
455
+ const rand = seededRandom(textSeed(text));
456
+ let canvas = createCanvas(width, height);
457
+ canvas.width = width;
458
+ canvas.height = height;
459
+ const ctx = getCanvas2DContext(canvas);
460
+ if (!ctx) return;
461
+ const lines = text.split("\n");
462
+ const numLines = lines.length;
463
+ const textBlockHeight = fontSize + (numLines - 1) * fontSize * lineHeight;
464
+ let startX = width / 2;
465
+ let align = "center";
466
+ if (textPosX === "left") {
467
+ startX = 50;
468
+ align = "left";
469
+ } else if (textPosX === "right") {
470
+ startX = width - 50;
471
+ align = "right";
472
+ }
473
+ ctx.textAlign = align;
474
+ ctx.textBaseline = "alphabetic";
475
+ const fontStr = `${fontStyle} ${fontWeight} ${fontSize}px "${fontFamily}"`;
476
+ ctx.font = fontStr;
477
+ if (letterSpacing !== 0) {
478
+ ctx.letterSpacing = `${letterSpacing}px`;
479
+ }
480
+ let startY = (height - textBlockHeight) / 2 + fontSize * 0.85;
481
+ if (textPosY === "top") {
482
+ startY = 40 + fontSize * 0.85;
483
+ } else if (textPosY === "bottom") {
484
+ startY = height - 40 - textBlockHeight + fontSize * 0.85;
485
+ }
486
+ ctx.save();
487
+ if (skewX !== 0) {
488
+ ctx.transform(1, 0, skewX, 1, 0, 0);
489
+ }
490
+ ctx.fillStyle = "#FFFFFF";
491
+ lines.forEach((line, index) => {
492
+ const py = startY + index * fontSize * lineHeight;
493
+ ctx.fillText(line, startX, py);
494
+ });
495
+ ctx.restore();
496
+ const imgData = ctx.getImageData(0, 0, width, height);
497
+ const pixels = imgData.data;
498
+ let minY = height;
499
+ let maxY = 0;
500
+ for (let y = 0; y < height; y++) {
501
+ for (let x = 0; x < width; x++) {
502
+ const idx = (y * width + x) * 4;
503
+ if (pixels[idx + 3] > 128) {
504
+ if (y < minY) minY = y;
505
+ if (y > maxY) maxY = y;
506
+ }
507
+ }
508
+ }
509
+ if (minY >= maxY) return;
510
+ const step = Math.max(1, Math.min(3, 1 / bristleDensity));
511
+ for (let y = minY; y <= maxY; y += step) {
512
+ const segments = [];
513
+ let inSegment = false;
514
+ let xStart = 0;
515
+ const targetY = Math.floor(y);
516
+ for (let x = 0; x < width; x++) {
517
+ const idx = (targetY * width + x) * 4;
518
+ const isInside = pixels[idx + 3] > 128;
519
+ if (isInside && !inSegment) {
520
+ inSegment = true;
521
+ xStart = x;
522
+ } else if (!isInside && inSegment) {
523
+ inSegment = false;
524
+ segments.push({ xStart, xEnd: x - 1 });
525
+ }
526
+ }
527
+ if (inSegment) {
528
+ segments.push({ xStart, xEnd: width - 1 });
529
+ }
530
+ for (const seg of segments) {
531
+ if (rand() < bristleSkipRate) continue;
532
+ const segW = seg.xEnd - seg.xStart;
533
+ const startLimit = seg.xStart + rand() * Math.min(15, segW * 0.2);
534
+ const endLimit = seg.xEnd - rand() * Math.min(15, segW * 0.2);
535
+ if (startLimit >= endLimit) continue;
536
+ const opacity = 0.4 + rand() * 0.6;
537
+ const lw = 0.5 + rand() * 1.5;
538
+ const hasGap = rand() < 0.1;
539
+ if (hasGap) {
540
+ const gapPct = 0.3 + rand() * 0.4;
541
+ const gapLen = 5 + rand() * 15;
542
+ const midX = startLimit + (endLimit - startLimit) * gapPct;
543
+ const p1Start = startLimit;
544
+ const p1End = Math.max(startLimit, midX - gapLen / 2);
545
+ const p2Start = Math.min(endLimit, midX + gapLen / 2);
546
+ const p2End = endLimit;
547
+ if (p1End > p1Start) {
548
+ this.bristleLines.push({ y: targetY, x0: p1Start, x1: p1End, opacity, lineWidth: lw });
549
+ }
550
+ if (p2End > p2Start) {
551
+ this.bristleLines.push({ y: targetY, x0: p2Start, x1: p2End, opacity, lineWidth: lw });
552
+ }
553
+ } else {
554
+ this.bristleLines.push({ y: targetY, x0: startLimit, x1: endLimit, opacity, lineWidth: lw });
555
+ }
556
+ }
557
+ }
558
+ for (let y = minY; y <= maxY; y++) {
559
+ for (let x = 0; x < width; x++) {
560
+ const idx = (y * width + x) * 4;
561
+ if (pixels[idx + 3] > 128) {
562
+ if (rand() < grainDensity) {
563
+ this.grainDots.push({
564
+ x,
565
+ y,
566
+ opacity: 0.2 + rand() * 0.3
567
+ });
568
+ }
569
+ }
570
+ }
571
+ }
572
+ const bottomRowForCol = new Array(width).fill(-1);
573
+ for (let x = 0; x < width; x++) {
574
+ for (let y = height - 1; y >= 0; y--) {
575
+ const idx = (y * width + x) * 4;
576
+ if (pixels[idx + 3] > 128) {
577
+ bottomRowForCol[x] = y;
578
+ break;
579
+ }
580
+ }
581
+ }
582
+ for (let x = 0; x < width; x++) {
583
+ const sY = bottomRowForCol[x];
584
+ if (sY === -1) continue;
585
+ if (rand() < dripRate) {
586
+ const length = 8 + rand() * (dripMaxLength - 8);
587
+ const startWidth = 1 + rand() * 1;
588
+ const endWidth = rand() < 0.5 ? startWidth : 0.5;
589
+ const startOpacity = 0.6 + rand() * 0.3;
590
+ const hasBlob = rand() < 0.6;
591
+ const blobRadius = 1.5 + rand() * 1.5;
592
+ this.drips.push({
593
+ x,
594
+ startY: sY,
595
+ length,
596
+ startWidth,
597
+ endWidth,
598
+ startOpacity,
599
+ hasBlob,
600
+ blobRadius
601
+ });
602
+ }
603
+ }
604
+ }
605
+ advanceSteps(steps) {
606
+ }
607
+ drawFrame(ctx) {
608
+ const { text, fontFamily, fontWeight, fontStyle, fontSize, letterSpacing, lineHeight, canvasWidth, canvasHeight, textPosX, textPosY, skewX, inkColor } = this.cfg;
609
+ const width = canvasWidth;
610
+ const height = canvasHeight;
611
+ ctx.clearRect(0, 0, width, height);
612
+ ctx.imageSmoothingEnabled = true;
613
+ const lines = text.split("\n");
614
+ const numLines = lines.length;
615
+ const textBlockHeight = fontSize + (numLines - 1) * fontSize * lineHeight;
616
+ let startX = width / 2;
617
+ let align = "center";
618
+ if (textPosX === "left") {
619
+ startX = 50;
620
+ align = "left";
621
+ } else if (textPosX === "right") {
622
+ startX = width - 50;
623
+ align = "right";
624
+ }
625
+ ctx.textAlign = align;
626
+ ctx.textBaseline = "alphabetic";
627
+ const fontStr = `${fontStyle} ${fontWeight} ${fontSize}px "${fontFamily}"`;
628
+ ctx.font = fontStr;
629
+ if (letterSpacing !== 0) {
630
+ ctx.letterSpacing = `${letterSpacing}px`;
631
+ }
632
+ let startY = (height - textBlockHeight) / 2 + fontSize * 0.85;
633
+ if (textPosY === "top") {
634
+ startY = 40 + fontSize * 0.85;
635
+ } else if (textPosY === "bottom") {
636
+ startY = height - 40 - textBlockHeight + fontSize * 0.85;
637
+ }
638
+ ctx.save();
639
+ if (skewX !== 0) {
640
+ ctx.transform(1, 0, skewX, 1, 0, 0);
641
+ }
642
+ ctx.fillStyle = inkColor;
643
+ lines.forEach((line, index) => {
644
+ const py = startY + index * fontSize * lineHeight;
645
+ ctx.fillText(line, startX, py);
646
+ });
647
+ ctx.restore();
648
+ ctx.save();
649
+ ctx.globalCompositeOperation = "source-in";
650
+ ctx.strokeStyle = inkColor;
651
+ this.bristleLines.forEach((line) => {
652
+ ctx.globalAlpha = line.opacity;
653
+ ctx.lineWidth = line.lineWidth;
654
+ ctx.beginPath();
655
+ ctx.moveTo(line.x0, line.y);
656
+ ctx.lineTo(line.x1, line.y);
657
+ ctx.stroke();
658
+ });
659
+ ctx.fillStyle = inkColor;
660
+ this.grainDots.forEach((dot) => {
661
+ ctx.globalAlpha = dot.opacity;
662
+ ctx.fillRect(dot.x, dot.y, 1, 1);
663
+ });
664
+ ctx.restore();
665
+ const inkRgb = hexToRgb(inkColor);
666
+ ctx.save();
667
+ this.drips.forEach((drip) => {
668
+ const grad = ctx.createLinearGradient(drip.x, drip.startY, drip.x, drip.startY + drip.length);
669
+ grad.addColorStop(0, `rgba(${inkRgb.r}, ${inkRgb.g}, ${inkRgb.b}, ${drip.startOpacity})`);
670
+ grad.addColorStop(1, `rgba(${inkRgb.r}, ${inkRgb.g}, ${inkRgb.b}, 0)`);
671
+ ctx.strokeStyle = grad;
672
+ ctx.lineWidth = drip.startWidth;
673
+ ctx.beginPath();
674
+ ctx.moveTo(drip.x, drip.startY);
675
+ ctx.lineTo(drip.x, drip.startY + drip.length);
676
+ ctx.stroke();
677
+ if (drip.hasBlob) {
678
+ ctx.fillStyle = `rgba(${inkRgb.r}, ${inkRgb.g}, ${inkRgb.b}, ${drip.startOpacity * 0.5})`;
679
+ ctx.beginPath();
680
+ ctx.arc(drip.x, drip.startY + drip.length, drip.blobRadius, 0, Math.PI * 2);
681
+ ctx.fill();
682
+ }
683
+ });
684
+ ctx.restore();
685
+ }
686
+ };
687
+
688
+ // src/canvas-utils.ts
689
+ function drawRoundedRect(ctx, x, y, w, h, r) {
690
+ const radius = Math.max(0, Math.min(r, Math.min(w, h) / 2));
691
+ ctx.beginPath();
692
+ if (supportsRoundRect()) {
693
+ ctx.roundRect(x, y, w, h, radius);
694
+ } else {
695
+ ctx.moveTo(x + radius, y);
696
+ ctx.lineTo(x + w - radius, y);
697
+ ctx.quadraticCurveTo(x + w, y, x + w, y + radius);
698
+ ctx.lineTo(x + w, y + h - radius);
699
+ ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
700
+ ctx.lineTo(x + radius, y + h);
701
+ ctx.quadraticCurveTo(x, y + h, x, y + h - radius);
702
+ ctx.lineTo(x, y + radius);
703
+ ctx.quadraticCurveTo(x, y, x + radius, y);
704
+ }
705
+ }
706
+
707
+ // src/renderer.ts
708
+ function getCanvas2DContext2(canvas) {
709
+ return canvas.getContext("2d");
710
+ }
711
+ function ctxSupportsFilter(ctx) {
712
+ try {
713
+ const prev = ctx.filter;
714
+ ctx.filter = "blur(4px)";
715
+ const ok = typeof ctx.filter === "string" && ctx.filter.includes("blur");
716
+ ctx.filter = prev;
717
+ return ok;
718
+ } catch {
719
+ return false;
720
+ }
721
+ }
722
+ function renderTextEffectCore(ctx, cfg) {
723
+ if (cfg.customRenderer === "InkBrushEngine") {
724
+ const engine = new InkBrushEngine(cfg);
725
+ engine.drawFrame(ctx);
726
+ return;
727
+ }
728
+ const { text, fontFamily, fontWeight, fontStyle, fontSize, letterSpacing, lineHeight, fillType, fillColor, fillGradientAngle, fillGradientStops, patternType, strokeEnabled, strokeColor, strokeWidth, strokePosition, strokeOpacity, strokeLineJoin, strokeBlur, strokeType, strokeColorSecondary, strokeWidthSecondary, strokeFadeRange, glowLayers, shadowEnabled, shadowColor, shadowBlur, shadowOffsetX, shadowOffsetY, shadowOpacity, shadowType, bevelEnabled, bevelDepth, bevelHighlight, bevelShadow, bevelDirection, bevelCoreColor, bevelEdgeColor, bevelEdgeWidth, bevelBlur, bevelBlurColor, bevelPerspectiveEnabled, bevelVanishingPointX, bevelVanishingPointY, bevelFocalLength, stackEnabled, stackCount, stackOffsetX, stackOffsetY, stackOpacityDecay, stackColor1, stackColor2, stackColor3, stackColor4, panelEnabled, panelColor, panelOpacity, panelRadius, panelPaddingX, panelPaddingY, panelStrokeEnabled, panelStrokeColor, panelStrokeWidth, canvasWidth, canvasHeight, textPosX, textPosY } = cfg;
729
+ ctx.imageSmoothingEnabled = true;
730
+ ctx.lineJoin = strokeLineJoin;
731
+ const cWidth = canvasWidth || DEFAULT_CANVAS_WIDTH;
732
+ const cHeight = canvasHeight || DEFAULT_CANVAS_HEIGHT;
733
+ const layout = computeTextLayout(ctx, cfg, {
734
+ wrap: cfg.wrapText !== false,
735
+ autoFit: !!cfg.autoFitText
736
+ });
737
+ const lines = layout.lines;
738
+ const numLines = lines.length;
739
+ const effectiveFontSize = layout.fontSize;
740
+ const lineAdvance = effectiveFontSize * lineHeight;
741
+ const textBlockHeight = layout.bounds.textBlockHeight;
742
+ let startX = layout.startX;
743
+ let startY = layout.startY;
744
+ const align = layout.align;
745
+ const maxLineWidth = layout.bounds.maxLineWidth;
746
+ const lineWidths = layout.lineWidths;
747
+ const fontStr = `${fontStyle} ${fontWeight} ${effectiveFontSize}px "${fontFamily}"`;
748
+ ctx.font = fontStr;
749
+ ctx.textAlign = align;
750
+ if (letterSpacing !== 0) {
751
+ ctx.letterSpacing = `${letterSpacing}px`;
752
+ }
753
+ let xMin = layout.bounds.xMin;
754
+ let xMax = layout.bounds.xMax;
755
+ let yMin = layout.bounds.yMin;
756
+ let yMax = layout.bounds.yMax;
757
+ let shiftX_half = 0;
758
+ let shiftY_half = 0;
759
+ if (bevelEnabled && bevelDepth > 0) {
760
+ if (bevelPerspectiveEnabled) {
761
+ const vpx = cWidth / 2 + (bevelVanishingPointX !== void 0 ? bevelVanishingPointX : 40) / 100 * (cWidth / 2);
762
+ const vpy = cHeight / 2 + (bevelVanishingPointY !== void 0 ? bevelVanishingPointY : 80) / 100 * (cHeight / 2);
763
+ const fl = Math.max(100, bevelFocalLength !== void 0 ? bevelFocalLength : 400);
764
+ const scale = fl / (fl + bevelDepth);
765
+ const shiftX_back = (vpx - startX) * (1 - scale);
766
+ const shiftY_back = (vpy - startY) * (1 - scale);
767
+ shiftX_half = shiftX_back / 2;
768
+ shiftY_half = shiftY_back / 2;
769
+ } else {
770
+ let dx = 0;
771
+ let dy = 0;
772
+ if (bevelDirection === "bottom-right") {
773
+ dx = bevelDepth;
774
+ dy = bevelDepth;
775
+ } else if (bevelDirection === "bottom") {
776
+ dy = bevelDepth;
777
+ } else if (bevelDirection === "right") {
778
+ dx = bevelDepth;
779
+ }
780
+ shiftX_half = dx / 2;
781
+ shiftY_half = dy / 2;
782
+ }
783
+ startX -= shiftX_half;
784
+ startY -= shiftY_half;
785
+ xMin -= shiftX_half;
786
+ xMax -= shiftX_half;
787
+ yMin -= shiftY_half;
788
+ yMax -= shiftY_half;
789
+ }
790
+ let total_xMin = xMin;
791
+ let total_xMax = xMax;
792
+ let total_yMin = yMin;
793
+ let total_yMax = yMax;
794
+ if (bevelEnabled && bevelDepth > 0) {
795
+ if (bevelPerspectiveEnabled) {
796
+ const vpx = cWidth / 2 + (bevelVanishingPointX !== void 0 ? bevelVanishingPointX : 40) / 100 * (cWidth / 2);
797
+ const vpy = cHeight / 2 + (bevelVanishingPointY !== void 0 ? bevelVanishingPointY : 80) / 100 * (cHeight / 2);
798
+ const fl = Math.max(100, bevelFocalLength !== void 0 ? bevelFocalLength : 400);
799
+ const scale = fl / (fl + bevelDepth);
800
+ const x_new_left = vpx + (xMin - vpx) * scale;
801
+ const x_new_right = vpx + (xMax - vpx) * scale;
802
+ const y_new_top = vpy + (yMin - vpy) * scale;
803
+ const y_new_bottom = vpy + (yMax - vpy) * scale;
804
+ total_xMin = Math.min(xMin, x_new_left, x_new_right);
805
+ total_xMax = Math.max(xMax, x_new_left, x_new_right);
806
+ total_yMin = Math.min(yMin, y_new_top, y_new_bottom);
807
+ total_yMax = Math.max(yMax, y_new_top, y_new_bottom);
808
+ } else {
809
+ let dx = 0;
810
+ let dy = 0;
811
+ if (bevelDirection === "bottom-right") {
812
+ dx = bevelDepth;
813
+ dy = bevelDepth;
814
+ } else if (bevelDirection === "bottom") {
815
+ dy = bevelDepth;
816
+ } else if (bevelDirection === "right") {
817
+ dx = bevelDepth;
818
+ }
819
+ total_xMin = Math.min(xMin, xMin + dx);
820
+ total_xMax = Math.max(xMax, xMax + dx);
821
+ total_yMin = Math.min(yMin, yMin + dy);
822
+ total_yMax = Math.max(yMax, yMax + dy);
823
+ }
824
+ }
825
+ const usePerCharFill = shouldUsePerCharFill(cfg);
826
+ const perCharColors = cfg.charFillColors ?? [];
827
+ const renderLines = (mode, overrideStyle, offsetX = 0, offsetY = 0, options) => {
828
+ if (options?.perCharFill && usePerCharFill && mode === "fill" && !overrideStyle) {
829
+ drawPerCharText(ctx, {
830
+ lines,
831
+ startX,
832
+ startY,
833
+ lineAdvance,
834
+ align,
835
+ letterSpacing,
836
+ charFillColors: perCharColors,
837
+ defaultColor: fillColor,
838
+ mode: "fill",
839
+ offsetX,
840
+ offsetY
841
+ });
842
+ return;
843
+ }
844
+ const savedLetterSpacing = ctx.letterSpacing || "normal";
845
+ if (letterSpacing !== 0) {
846
+ ctx.letterSpacing = `${letterSpacing}px`;
847
+ }
848
+ if (overrideStyle) {
849
+ if (mode === "fill") {
850
+ ctx.fillStyle = overrideStyle;
851
+ } else {
852
+ ctx.strokeStyle = overrideStyle;
853
+ }
854
+ }
855
+ lines.forEach((line, index) => {
856
+ const py = startY + index * lineAdvance;
857
+ if (mode === "fill") {
858
+ ctx.fillText(line, startX + offsetX, py + offsetY);
859
+ } else {
860
+ ctx.strokeText(line, startX + offsetX, py + offsetY);
861
+ }
862
+ });
863
+ ctx.letterSpacing = savedLetterSpacing;
864
+ };
865
+ const renderWithShadowTrick = (mode, sColor, sBlur, sOffsetX, sOffsetY, opacity, overrideStyle = "#000", spread = 0) => {
866
+ ctx.save();
867
+ ctx.globalAlpha = opacity / 100;
868
+ const shiftX = 1e4;
869
+ ctx.shadowColor = sColor;
870
+ ctx.shadowBlur = sBlur;
871
+ ctx.shadowOffsetX = shiftX + sOffsetX;
872
+ ctx.shadowOffsetY = sOffsetY;
873
+ const savedLetterSpacing = ctx.letterSpacing || "normal";
874
+ if (letterSpacing !== 0) {
875
+ ctx.letterSpacing = `${letterSpacing}px`;
876
+ }
877
+ const prevStyle = mode === "fill" ? ctx.fillStyle : ctx.strokeStyle;
878
+ if (mode === "fill") {
879
+ ctx.fillStyle = overrideStyle;
880
+ } else {
881
+ ctx.strokeStyle = overrideStyle;
882
+ }
883
+ const prevStrokeStyle = ctx.strokeStyle;
884
+ const prevLineWidth = ctx.lineWidth;
885
+ if (spread > 0) {
886
+ ctx.strokeStyle = overrideStyle;
887
+ ctx.lineWidth = spread * 2;
888
+ ctx.lineJoin = strokeLineJoin;
889
+ }
890
+ lines.forEach((line, index) => {
891
+ const py = startY + index * lineAdvance;
892
+ if (mode === "fill") {
893
+ if (spread > 0) {
894
+ ctx.strokeText(line, startX - shiftX, py);
895
+ }
896
+ ctx.fillText(line, startX - shiftX, py);
897
+ } else {
898
+ ctx.strokeText(line, startX - shiftX, py);
899
+ }
900
+ });
901
+ ctx.letterSpacing = savedLetterSpacing;
902
+ if (mode === "fill") {
903
+ ctx.fillStyle = prevStyle;
904
+ } else {
905
+ ctx.strokeStyle = prevStyle;
906
+ }
907
+ if (spread > 0) {
908
+ ctx.strokeStyle = prevStrokeStyle;
909
+ ctx.lineWidth = prevLineWidth;
910
+ }
911
+ ctx.restore();
912
+ };
913
+ if (panelEnabled) {
914
+ ctx.save();
915
+ ctx.globalAlpha = panelOpacity / 100;
916
+ ctx.fillStyle = panelColor;
917
+ const px = total_xMin - panelPaddingX;
918
+ const py = total_yMin - panelPaddingY;
919
+ const pw = total_xMax - total_xMin + 2 * panelPaddingX;
920
+ const ph = total_yMax - total_yMin + 2 * panelPaddingY;
921
+ const cx = px + pw / 2;
922
+ const cy = py + ph / 2;
923
+ const isComicBurst = cfg.effectName.toLowerCase().includes("comic") || cfg.panelStyle === "burst";
924
+ ctx.beginPath();
925
+ if (isComicBurst) {
926
+ const numPoints = 36;
927
+ const rx = pw * 0.56;
928
+ const ry = ph * 0.68;
929
+ for (let i = 0; i < numPoints; i++) {
930
+ const angle = i / numPoints * Math.PI * 2;
931
+ const wave1 = Math.sin(angle * 8) * 0.12;
932
+ const wave3 = Math.cos(angle * 14) * 0.05;
933
+ const isSpikePeak = i % 2 === 0;
934
+ const depthFactor = isSpikePeak ? 1.12 + wave1 + wave3 : 0.76 - wave3;
935
+ const sx = cx + Math.cos(angle) * rx * depthFactor;
936
+ const sy = cy + Math.sin(angle) * ry * depthFactor;
937
+ if (i === 0) {
938
+ ctx.moveTo(sx, sy);
939
+ } else {
940
+ ctx.lineTo(sx, sy);
941
+ }
942
+ }
943
+ } else {
944
+ drawRoundedRect(ctx, px, py, pw, ph, panelRadius);
945
+ }
946
+ ctx.closePath();
947
+ ctx.fill();
948
+ if (panelStrokeEnabled) {
949
+ ctx.strokeStyle = panelStrokeColor;
950
+ ctx.lineWidth = panelStrokeWidth;
951
+ const prevLineJoin = ctx.lineJoin;
952
+ if (isComicBurst) {
953
+ ctx.lineJoin = "miter";
954
+ }
955
+ ctx.stroke();
956
+ ctx.lineJoin = prevLineJoin;
957
+ }
958
+ ctx.restore();
959
+ }
960
+ glowLayers.forEach((layer2) => {
961
+ if (layer2.enabled && layer2.type === "outer" && layer2.opacity > 0) {
962
+ const renderCount = Math.max(1, Math.min(20, layer2.strength ?? 1));
963
+ for (let i = 0; i < renderCount; i++) {
964
+ renderWithShadowTrick("fill", layer2.color, layer2.blur, 0, 0, layer2.opacity, "#000", layer2.spread ?? 0);
965
+ }
966
+ }
967
+ });
968
+ if (shadowEnabled && shadowType === "drop" && shadowOpacity > 0) {
969
+ if (bevelEnabled && bevelDepth > 0) {
970
+ if (bevelPerspectiveEnabled) {
971
+ const vpx = cWidth / 2 + (bevelVanishingPointX !== void 0 ? bevelVanishingPointX : 40) / 100 * (cWidth / 2);
972
+ const vpy = cHeight / 2 + (bevelVanishingPointY !== void 0 ? bevelVanishingPointY : 80) / 100 * (cHeight / 2);
973
+ const fl = Math.max(100, bevelFocalLength !== void 0 ? bevelFocalLength : 400);
974
+ const scale = fl / (fl + bevelDepth);
975
+ ctx.save();
976
+ ctx.translate(vpx, vpy);
977
+ ctx.scale(scale, scale);
978
+ ctx.translate(-vpx, -vpy);
979
+ renderWithShadowTrick("fill", shadowColor, shadowBlur, shadowOffsetX, shadowOffsetY, shadowOpacity);
980
+ ctx.restore();
981
+ } else {
982
+ let dx = 0;
983
+ let dy = 0;
984
+ if (bevelDirection === "bottom-right") {
985
+ dx = bevelDepth;
986
+ dy = bevelDepth;
987
+ } else if (bevelDirection === "bottom") {
988
+ dy = bevelDepth;
989
+ } else if (bevelDirection === "right") {
990
+ dx = bevelDepth;
991
+ }
992
+ renderWithShadowTrick("fill", shadowColor, shadowBlur, shadowOffsetX + dx, shadowOffsetY + dy, shadowOpacity);
993
+ }
994
+ } else {
995
+ renderWithShadowTrick("fill", shadowColor, shadowBlur, shadowOffsetX, shadowOffsetY, shadowOpacity);
996
+ }
997
+ }
998
+ const isGlitch = cfg.effectName.toLowerCase().includes("glitch") || cfg.text === "SYSTEM ERR";
999
+ if (isGlitch) {
1000
+ ctx.save();
1001
+ ctx.globalAlpha = 0.8;
1002
+ renderLines("fill", "#00FFFF", -4, -2);
1003
+ renderLines("fill", "#FF00FF", 4, 2);
1004
+ ctx.restore();
1005
+ }
1006
+ if (bevelEnabled && bevelDepth > 0) {
1007
+ const shadowRgb = hexToRgb(bevelShadow || "#1A0A00");
1008
+ const coreRgb = hexToRgb(bevelCoreColor || bevelShadow || "#3A1A00");
1009
+ const highlightRgb = hexToRgb(bevelHighlight || "#FFFFFF");
1010
+ const shadeForDepth = (t) => {
1011
+ const eased = t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
1012
+ if (eased <= 0.5) {
1013
+ const u = eased * 2;
1014
+ const r = Math.round(shadowRgb.r + (coreRgb.r - shadowRgb.r) * u);
1015
+ const g = Math.round(shadowRgb.g + (coreRgb.g - shadowRgb.g) * u);
1016
+ const b = Math.round(shadowRgb.b + (coreRgb.b - shadowRgb.b) * u);
1017
+ return `rgb(${r},${g},${b})`;
1018
+ } else {
1019
+ const u = (eased - 0.5) * 2;
1020
+ const r = Math.round(coreRgb.r + (highlightRgb.r - coreRgb.r) * u);
1021
+ const g = Math.round(coreRgb.g + (highlightRgb.g - coreRgb.g) * u);
1022
+ const b = Math.round(coreRgb.b + (highlightRgb.b - coreRgb.b) * u);
1023
+ return `rgb(${r},${g},${b})`;
1024
+ }
1025
+ };
1026
+ if (bevelPerspectiveEnabled) {
1027
+ const vpx = cWidth / 2 + (bevelVanishingPointX !== void 0 ? bevelVanishingPointX : 40) / 100 * (cWidth / 2);
1028
+ const vpy = cHeight / 2 + (bevelVanishingPointY !== void 0 ? bevelVanishingPointY : 80) / 100 * (cHeight / 2);
1029
+ const fl = Math.max(100, bevelFocalLength !== void 0 ? bevelFocalLength : 400);
1030
+ if (bevelBlur && bevelBlur > 0) {
1031
+ const blurColor = bevelBlurColor || bevelShadow || "#000000";
1032
+ if (ctxSupportsFilter(ctx)) {
1033
+ ctx.save();
1034
+ ctx.filter = `blur(${bevelBlur}px)`;
1035
+ for (let i = bevelDepth; i > 0; i -= Math.max(1, Math.floor(bevelDepth / 4))) {
1036
+ const scale = fl / (fl + i);
1037
+ ctx.save();
1038
+ ctx.translate(vpx, vpy);
1039
+ ctx.scale(scale, scale);
1040
+ ctx.translate(-vpx, -vpy);
1041
+ renderLines("fill", blurColor);
1042
+ ctx.restore();
1043
+ }
1044
+ ctx.restore();
1045
+ } else {
1046
+ for (let i = bevelDepth; i > 0; i -= Math.max(1, Math.floor(bevelDepth / 4))) {
1047
+ const scale = fl / (fl + i);
1048
+ ctx.save();
1049
+ ctx.translate(vpx, vpy);
1050
+ ctx.scale(scale, scale);
1051
+ ctx.translate(-vpx, -vpy);
1052
+ renderWithShadowTrick("fill", blurColor, bevelBlur, 0, 0, 100);
1053
+ ctx.restore();
1054
+ }
1055
+ }
1056
+ }
1057
+ ctx.save();
1058
+ for (let i = bevelDepth; i > 0; i--) {
1059
+ const t = 1 - (i - 1) / Math.max(1, bevelDepth - 1);
1060
+ const aoFactor = 0.35 + 0.65 * (1 - (i - 1) / Math.max(1, bevelDepth));
1061
+ const baseColor = shadeForDepth(t);
1062
+ const bRgb = hexToRgb(baseColor.startsWith("#") ? baseColor : "#000000");
1063
+ const baseRgbParsed = (() => {
1064
+ const m = baseColor.match(/rgb\((\d+),(\d+),(\d+)\)/);
1065
+ return m ? { r: +m[1], g: +m[2], b: +m[3] } : bRgb;
1066
+ })();
1067
+ const aoColor = `rgb(${Math.round(baseRgbParsed.r * aoFactor)},${Math.round(baseRgbParsed.g * aoFactor)},${Math.round(baseRgbParsed.b * aoFactor)})`;
1068
+ const scale = fl / (fl + i);
1069
+ ctx.save();
1070
+ ctx.translate(vpx, vpy);
1071
+ ctx.scale(scale, scale);
1072
+ ctx.translate(-vpx, -vpy);
1073
+ renderLines("fill", aoColor);
1074
+ if (bevelEdgeWidth && bevelEdgeWidth > 0) {
1075
+ ctx.save();
1076
+ ctx.strokeStyle = bevelEdgeColor || "#000000";
1077
+ ctx.lineWidth = bevelEdgeWidth;
1078
+ ctx.lineJoin = strokeLineJoin || "round";
1079
+ renderLines("stroke");
1080
+ ctx.restore();
1081
+ }
1082
+ ctx.restore();
1083
+ }
1084
+ ctx.restore();
1085
+ if (bevelEdgeWidth && bevelEdgeWidth > 0) {
1086
+ ctx.save();
1087
+ ctx.globalAlpha = 0.6;
1088
+ ctx.strokeStyle = bevelHighlight;
1089
+ ctx.lineWidth = Math.max(0.5, (bevelEdgeWidth || 1) * 0.5);
1090
+ ctx.lineJoin = strokeLineJoin || "round";
1091
+ renderLines("stroke");
1092
+ ctx.restore();
1093
+ }
1094
+ } else {
1095
+ const getDirOffset = (i) => {
1096
+ if (bevelDirection === "bottom-right") return { dx: i, dy: i };
1097
+ if (bevelDirection === "bottom") return { dx: 0, dy: i };
1098
+ if (bevelDirection === "right") return { dx: i, dy: 0 };
1099
+ return { dx: i, dy: i };
1100
+ };
1101
+ if (bevelBlur && bevelBlur > 0) {
1102
+ const blurColor = bevelBlurColor || bevelShadow || "#000000";
1103
+ if (ctxSupportsFilter(ctx)) {
1104
+ ctx.save();
1105
+ ctx.filter = `blur(${bevelBlur}px)`;
1106
+ for (let i = bevelDepth; i > 0; i -= Math.max(1, Math.floor(bevelDepth / 4))) {
1107
+ const { dx, dy } = getDirOffset(i);
1108
+ renderLines("fill", blurColor, dx, dy);
1109
+ }
1110
+ ctx.restore();
1111
+ } else {
1112
+ for (let i = bevelDepth; i > 0; i -= Math.max(1, Math.floor(bevelDepth / 4))) {
1113
+ const { dx, dy } = getDirOffset(i);
1114
+ renderWithShadowTrick("fill", blurColor, bevelBlur, dx, dy, 100);
1115
+ }
1116
+ }
1117
+ }
1118
+ ctx.save();
1119
+ for (let i = bevelDepth; i > 0; i--) {
1120
+ const { dx, dy } = getDirOffset(i);
1121
+ const t = 1 - (i - 1) / Math.max(1, bevelDepth - 1);
1122
+ const depthRatio = (i - 1) / Math.max(1, bevelDepth - 1);
1123
+ const aoFactor = 0.25 + 0.75 * (1 - depthRatio * 0.8);
1124
+ const baseColor = shadeForDepth(t);
1125
+ const baseRgbParsed = (() => {
1126
+ const m = baseColor.match(/rgb\((\d+),(\d+),(\d+)\)/);
1127
+ return m ? { r: +m[1], g: +m[2], b: +m[3] } : shadowRgb;
1128
+ })();
1129
+ const shadedColor = `rgb(${Math.round(baseRgbParsed.r * aoFactor)},${Math.round(baseRgbParsed.g * aoFactor)},${Math.round(baseRgbParsed.b * aoFactor)})`;
1130
+ renderLines("fill", shadedColor, dx, dy);
1131
+ if (bevelEdgeWidth && bevelEdgeWidth > 0) {
1132
+ ctx.save();
1133
+ ctx.globalAlpha = 0.4 + 0.6 * (1 - depthRatio);
1134
+ ctx.strokeStyle = bevelEdgeColor || "#000000";
1135
+ ctx.lineWidth = bevelEdgeWidth;
1136
+ ctx.lineJoin = strokeLineJoin || "round";
1137
+ renderLines("stroke", void 0, dx, dy);
1138
+ ctx.restore();
1139
+ }
1140
+ }
1141
+ ctx.restore();
1142
+ ctx.save();
1143
+ ctx.globalAlpha = 0.5;
1144
+ ctx.strokeStyle = bevelHighlight;
1145
+ ctx.lineWidth = Math.max(0.5, bevelEdgeWidth || 1.5);
1146
+ ctx.lineJoin = "round";
1147
+ renderLines("stroke", bevelHighlight, 0, 0);
1148
+ ctx.restore();
1149
+ }
1150
+ }
1151
+ const applyStroke = () => {
1152
+ ctx.save();
1153
+ const sType = strokeType || "single";
1154
+ const sBlur = strokeBlur || 0;
1155
+ const sColorSecondary = strokeColorSecondary || "#FFFFFF";
1156
+ const sWidthSecondary = strokeWidthSecondary !== void 0 ? strokeWidthSecondary : 4;
1157
+ const sFadeRange = strokeFadeRange || 0;
1158
+ ctx.lineJoin = strokeLineJoin || "round";
1159
+ let customStrokeStyle = strokeColor;
1160
+ if (sFadeRange > 0) {
1161
+ const grad = ctx.createLinearGradient(0, yMin, 0, yMax);
1162
+ const rgb = hexToRgb(strokeColor);
1163
+ grad.addColorStop(0, `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${strokeOpacity / 100})`);
1164
+ const fadeLimit = Math.min(1, sFadeRange / 100);
1165
+ grad.addColorStop(fadeLimit, `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0)`);
1166
+ grad.addColorStop(1, `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0)`);
1167
+ customStrokeStyle = grad;
1168
+ }
1169
+ const drawStrokeLayer = (color, width, blurAmount, opacity, position) => {
1170
+ if (blurAmount > 0 && ctxSupportsFilter(ctx)) {
1171
+ ctx.save();
1172
+ ctx.globalAlpha = opacity / 100;
1173
+ ctx.strokeStyle = color;
1174
+ ctx.filter = `blur(${blurAmount}px)`;
1175
+ if (position === "outside") {
1176
+ ctx.lineWidth = width * 2;
1177
+ renderLines("stroke");
1178
+ } else if (position === "center") {
1179
+ ctx.lineWidth = width;
1180
+ renderLines("stroke");
1181
+ } else if (position === "inside") {
1182
+ ctx.globalCompositeOperation = "source-atop";
1183
+ ctx.lineWidth = width * 2;
1184
+ renderLines("stroke");
1185
+ }
1186
+ ctx.restore();
1187
+ } else if (blurAmount > 0) {
1188
+ const colorStr = typeof color === "string" ? color : strokeColor;
1189
+ const spread = position === "center" ? width / 2 : width;
1190
+ if (position === "inside") {
1191
+ ctx.save();
1192
+ ctx.globalCompositeOperation = "source-atop";
1193
+ renderWithShadowTrick("stroke", colorStr, blurAmount, 0, 0, opacity, void 0, spread);
1194
+ ctx.restore();
1195
+ } else {
1196
+ renderWithShadowTrick("stroke", colorStr, blurAmount, 0, 0, opacity, void 0, spread);
1197
+ }
1198
+ } else {
1199
+ ctx.save();
1200
+ ctx.globalAlpha = opacity / 100;
1201
+ ctx.strokeStyle = color;
1202
+ if (position === "outside") {
1203
+ ctx.lineWidth = width * 2;
1204
+ renderLines("stroke");
1205
+ } else if (position === "center") {
1206
+ ctx.lineWidth = width;
1207
+ renderLines("stroke");
1208
+ } else if (position === "inside") {
1209
+ ctx.globalCompositeOperation = "source-atop";
1210
+ ctx.lineWidth = width * 2;
1211
+ renderLines("stroke");
1212
+ }
1213
+ ctx.restore();
1214
+ }
1215
+ };
1216
+ if (sType === "double") {
1217
+ const outerWidth = strokeWidth + sWidthSecondary;
1218
+ drawStrokeLayer(sColorSecondary, outerWidth, sBlur, strokeOpacity, strokePosition);
1219
+ drawStrokeLayer(
1220
+ customStrokeStyle,
1221
+ strokeWidth,
1222
+ 0,
1223
+ // inner remains crisp
1224
+ strokeOpacity,
1225
+ strokePosition
1226
+ );
1227
+ } else if (sType === "neon") {
1228
+ drawStrokeLayer(
1229
+ strokeColor,
1230
+ strokeWidth * 1.8,
1231
+ sBlur || 8,
1232
+ // fallback blur to look gorgeous
1233
+ strokeOpacity * 0.7,
1234
+ strokePosition
1235
+ );
1236
+ drawStrokeLayer("#FFFFFF", strokeWidth * 0.5, 0, 95, strokePosition);
1237
+ } else {
1238
+ drawStrokeLayer(customStrokeStyle, strokeWidth, sBlur, strokeOpacity, strokePosition);
1239
+ }
1240
+ ctx.restore();
1241
+ };
1242
+ if (stackEnabled && (stackCount ?? 0) >= 1) {
1243
+ const cnt = stackCount ?? 3;
1244
+ const offX = stackOffsetX ?? 10;
1245
+ const offY = stackOffsetY ?? -10;
1246
+ const decay = (stackOpacityDecay ?? 20) / 100;
1247
+ const stackColors = [stackColor1 || "#FF7C00", stackColor2 || "#00FFDD", stackColor3 || "#FF00AA", stackColor4 || "#AA00FF"];
1248
+ for (let s = cnt; s >= 1; s--) {
1249
+ ctx.save();
1250
+ const dx = s * offX;
1251
+ const dy = s * offY;
1252
+ const layerOpacity = Math.max(0.01, 1 - s * decay);
1253
+ ctx.globalAlpha = layerOpacity;
1254
+ const layerColor = stackColors[(s - 1) % stackColors.length] || "#FFFFFF";
1255
+ if (strokeEnabled && strokeWidth > 0 && strokePosition !== "inside") {
1256
+ ctx.save();
1257
+ ctx.strokeStyle = layerColor;
1258
+ ctx.lineWidth = strokeWidth;
1259
+ ctx.lineJoin = strokeLineJoin;
1260
+ ctx.globalAlpha = strokeOpacity / 100 * layerOpacity;
1261
+ renderLines("stroke", layerColor, dx, dy);
1262
+ ctx.restore();
1263
+ }
1264
+ renderLines("fill", layerColor, dx, dy);
1265
+ ctx.restore();
1266
+ }
1267
+ }
1268
+ if (strokeEnabled && strokeWidth > 0 && strokePosition !== "inside") {
1269
+ applyStroke();
1270
+ }
1271
+ ctx.save();
1272
+ let textFill = fillColor;
1273
+ if (fillType === "linear" && fillGradientStops.length >= 2) {
1274
+ const angleRad = fillGradientAngle * Math.PI / 180;
1275
+ const cx = (xMin + xMax) / 2;
1276
+ const cy = (yMin + yMax) / 2;
1277
+ const r = Math.max(xMax - xMin, yMax - yMin) / 2;
1278
+ const x0 = cx - Math.cos(angleRad) * r;
1279
+ const y0 = cy - Math.sin(angleRad) * r;
1280
+ const x1 = cx + Math.cos(angleRad) * r;
1281
+ const y1 = cy + Math.sin(angleRad) * r;
1282
+ const grad = ctx.createLinearGradient(x0, y0, x1, y1);
1283
+ fillGradientStops.forEach((stop) => {
1284
+ grad.addColorStop(stop.offset / 100, stop.color);
1285
+ });
1286
+ textFill = grad;
1287
+ } else if (fillType === "radial" && fillGradientStops.length >= 2) {
1288
+ const cx = (xMin + xMax) / 2;
1289
+ const cy = (yMin + yMax) / 2;
1290
+ const r = Math.max(xMax - xMin, yMax - yMin) / 1.5;
1291
+ const grad = ctx.createRadialGradient(cx, cy, 2, cx, cy, r);
1292
+ fillGradientStops.forEach((stop) => {
1293
+ grad.addColorStop(stop.offset / 100, stop.color);
1294
+ });
1295
+ textFill = grad;
1296
+ } else if (fillType === "pattern") {
1297
+ const pType = patternType || "chalk";
1298
+ const patColor = fillColor || "#ffffff";
1299
+ const patCanvas = createCanvas(128, 128);
1300
+ if (pType === "carbon") {
1301
+ patCanvas.width = 8;
1302
+ patCanvas.height = 8;
1303
+ } else if (pType === "stripes") {
1304
+ patCanvas.width = 16;
1305
+ patCanvas.height = 16;
1306
+ } else if (pType === "halftone") {
1307
+ patCanvas.width = 24;
1308
+ patCanvas.height = 24;
1309
+ } else if (pType === "noise") {
1310
+ patCanvas.width = 96;
1311
+ patCanvas.height = 96;
1312
+ } else if (pType === "film" || pType === "brushed" || pType === "paper") {
1313
+ patCanvas.width = 128;
1314
+ patCanvas.height = 128;
1315
+ } else if (pType === "marble") {
1316
+ patCanvas.width = 256;
1317
+ patCanvas.height = 256;
1318
+ } else {
1319
+ patCanvas.width = 120;
1320
+ patCanvas.height = 120;
1321
+ }
1322
+ const patCtx = getCanvas2DContext2(patCanvas);
1323
+ const seedRandom = (initSeed) => {
1324
+ let currentSeed = initSeed;
1325
+ return () => {
1326
+ currentSeed = (currentSeed * 1664525 + 1013904223) % 4294967296;
1327
+ return currentSeed / 4294967296;
1328
+ };
1329
+ };
1330
+ const rand = seedRandom(42);
1331
+ if (pType === "chalk") {
1332
+ patCtx.fillStyle = "rgba(0,0,0,0)";
1333
+ patCtx.fillRect(0, 0, 120, 120);
1334
+ patCtx.fillStyle = patColor;
1335
+ for (let i = 0; i < 3500; i++) {
1336
+ const px = Math.floor(rand() * 120);
1337
+ const py = Math.floor(rand() * 120);
1338
+ patCtx.globalAlpha = 0.08 + rand() * 0.18;
1339
+ patCtx.fillRect(px, py, 1.2, 1.2);
1340
+ }
1341
+ patCtx.strokeStyle = patColor;
1342
+ for (let s = 0; s < 4; s++) {
1343
+ const angle = s * Math.PI / 4 + (rand() - 0.5) * 0.15;
1344
+ patCtx.lineWidth = 0.5 + rand() * 0.9;
1345
+ for (let i = 0; i < 40; i++) {
1346
+ patCtx.globalAlpha = 0.05 + rand() * 0.16;
1347
+ patCtx.beginPath();
1348
+ const startX2 = rand() * 120;
1349
+ const startY2 = rand() * 120;
1350
+ const len = 15 + rand() * 30;
1351
+ patCtx.moveTo(startX2, startY2);
1352
+ patCtx.lineTo(startX2 + Math.cos(angle) * len, startY2 + Math.sin(angle) * len);
1353
+ patCtx.stroke();
1354
+ }
1355
+ }
1356
+ for (let i = 0; i < 220; i++) {
1357
+ const cx = rand() * 120;
1358
+ const cy = rand() * 120;
1359
+ const r = 1 + rand() * 3;
1360
+ patCtx.globalAlpha = 0.03 + rand() * 0.08;
1361
+ patCtx.fillStyle = patColor;
1362
+ patCtx.beginPath();
1363
+ patCtx.arc(cx, cy, r, 0, Math.PI * 2);
1364
+ patCtx.fill();
1365
+ }
1366
+ } else if (pType === "noise") {
1367
+ patCtx.fillStyle = "rgba(0,0,0,0)";
1368
+ patCtx.fillRect(0, 0, 96, 96);
1369
+ patCtx.fillStyle = patColor;
1370
+ for (let i = 0; i < 4500; i++) {
1371
+ const px = Math.floor(rand() * 96);
1372
+ const py = Math.floor(rand() * 96);
1373
+ patCtx.globalAlpha = 0.12 + rand() * 0.38;
1374
+ patCtx.fillRect(px, py, rand() > 0.85 ? 1.5 : 1, rand() > 0.85 ? 1.5 : 1);
1375
+ }
1376
+ for (let i = 0; i < 150; i++) {
1377
+ const px = Math.floor(rand() * 96);
1378
+ const py = Math.floor(rand() * 96);
1379
+ const size = 1.6 + rand() * 1.5;
1380
+ patCtx.globalAlpha = 0.05 + rand() * 0.12;
1381
+ patCtx.fillRect(px, py, size, size);
1382
+ }
1383
+ } else if (pType === "grunge") {
1384
+ patCtx.fillStyle = "rgba(0,0,0,0)";
1385
+ patCtx.fillRect(0, 0, 128, 128);
1386
+ patCtx.fillStyle = patColor;
1387
+ for (let i = 0; i < 60; i++) {
1388
+ const cx = rand() * 128;
1389
+ const cy = rand() * 128;
1390
+ const r = 3 + rand() * 18;
1391
+ patCtx.globalAlpha = 0.06 + rand() * 0.15;
1392
+ patCtx.beginPath();
1393
+ patCtx.arc(cx, cy, r, 0, Math.PI * 2);
1394
+ patCtx.fill();
1395
+ }
1396
+ patCtx.strokeStyle = patColor;
1397
+ for (let i = 0; i < 22; i++) {
1398
+ const sx = rand() * 128;
1399
+ const sy = rand() * 128;
1400
+ const angle = rand() * Math.PI / 3 - Math.PI / 6;
1401
+ const len = 12 + rand() * 25;
1402
+ patCtx.lineWidth = 0.5 + rand() * 1.5;
1403
+ patCtx.globalAlpha = 0.15 + rand() * 0.25;
1404
+ patCtx.beginPath();
1405
+ patCtx.moveTo(sx, sy);
1406
+ patCtx.lineTo(sx + Math.cos(angle) * len, sy + Math.sin(angle) * len);
1407
+ patCtx.stroke();
1408
+ }
1409
+ for (let i = 0; i < 1800; i++) {
1410
+ const px = Math.floor(rand() * 128);
1411
+ const py = Math.floor(rand() * 128);
1412
+ patCtx.globalAlpha = 0.08 + rand() * 0.22;
1413
+ patCtx.fillRect(px, py, 1.2, 1.2);
1414
+ }
1415
+ } else if (pType === "carbon") {
1416
+ patCtx.fillStyle = "rgba(0,0,0,0.15)";
1417
+ patCtx.fillRect(0, 0, 8, 8);
1418
+ patCtx.fillStyle = patColor;
1419
+ patCtx.globalAlpha = 0.65;
1420
+ patCtx.fillRect(0, 0, 4, 4);
1421
+ patCtx.fillRect(4, 4, 4, 4);
1422
+ patCtx.fillStyle = "#FFFFFF";
1423
+ patCtx.globalAlpha = 0.22;
1424
+ patCtx.fillRect(0, 0, 4, 1);
1425
+ patCtx.fillRect(4, 4, 4, 1);
1426
+ patCtx.fillStyle = "#000000";
1427
+ patCtx.globalAlpha = 0.35;
1428
+ patCtx.fillRect(0, 3, 4, 1);
1429
+ patCtx.fillRect(4, 7, 4, 1);
1430
+ } else if (pType === "stripes") {
1431
+ patCtx.fillStyle = "rgba(0,0,0,0)";
1432
+ patCtx.fillRect(0, 0, 16, 16);
1433
+ patCtx.strokeStyle = patColor;
1434
+ patCtx.lineWidth = 3.5;
1435
+ patCtx.globalAlpha = 0.65;
1436
+ patCtx.beginPath();
1437
+ patCtx.moveTo(-4, 12);
1438
+ patCtx.lineTo(12, -4);
1439
+ patCtx.moveTo(0, 16);
1440
+ patCtx.lineTo(16, 0);
1441
+ patCtx.moveTo(4, 20);
1442
+ patCtx.lineTo(20, 4);
1443
+ patCtx.stroke();
1444
+ } else if (pType === "film") {
1445
+ patCtx.fillStyle = patColor;
1446
+ patCtx.globalAlpha = 0.94;
1447
+ patCtx.fillRect(0, 0, 128, 128);
1448
+ for (let i = 0; i < 4800; i++) {
1449
+ const px = Math.floor(rand() * 128);
1450
+ const py = Math.floor(rand() * 128);
1451
+ const isDark = rand() > 0.45;
1452
+ patCtx.fillStyle = isDark ? "#000000" : "#FFFFFF";
1453
+ patCtx.globalAlpha = isDark ? 0.13 + rand() * 0.22 : 0.15 + rand() * 0.28;
1454
+ patCtx.fillRect(px, py, rand() > 0.9 ? 1.5 : 1, rand() > 0.9 ? 1.5 : 1);
1455
+ }
1456
+ patCtx.strokeStyle = "rgba(255, 255, 255, 0.48)";
1457
+ for (let i = 0; i < 10; i++) {
1458
+ const sx = rand() * 128;
1459
+ const sy = rand() * 128;
1460
+ const len = 12 + rand() * 45;
1461
+ const angle = -Math.PI / 2 + (rand() - 0.5) * 0.18;
1462
+ patCtx.lineWidth = 0.35 + rand() * 0.55;
1463
+ patCtx.globalAlpha = 0.22 + rand() * 0.38;
1464
+ patCtx.beginPath();
1465
+ patCtx.moveTo(sx, sy);
1466
+ patCtx.lineTo(sx + Math.cos(angle) * len, sy + Math.sin(angle) * len);
1467
+ patCtx.stroke();
1468
+ }
1469
+ patCtx.strokeStyle = "rgba(0, 0, 0, 0.32)";
1470
+ for (let i = 0; i < 5; i++) {
1471
+ const sx = rand() * 128;
1472
+ const sy = rand() * 128;
1473
+ const len = 15 + rand() * 50;
1474
+ const angle = -Math.PI / 2 + (rand() - 0.5) * 0.12;
1475
+ patCtx.lineWidth = 0.3 + rand() * 0.5;
1476
+ patCtx.globalAlpha = 0.18 + rand() * 0.25;
1477
+ patCtx.beginPath();
1478
+ patCtx.moveTo(sx, sy);
1479
+ patCtx.lineTo(sx + Math.cos(angle) * len, sy + Math.sin(angle) * len);
1480
+ patCtx.stroke();
1481
+ }
1482
+ patCtx.strokeStyle = "rgba(0, 0, 0, 0.48)";
1483
+ for (let i = 0; i < 4; i++) {
1484
+ const sx = rand() * 128;
1485
+ const sy = rand() * 128;
1486
+ patCtx.lineWidth = 0.55 + rand() * 0.65;
1487
+ patCtx.globalAlpha = 0.35 + rand() * 0.3;
1488
+ patCtx.beginPath();
1489
+ patCtx.moveTo(sx, sy);
1490
+ patCtx.quadraticCurveTo(sx + (rand() - 0.5) * 16, sy + (rand() - 0.5) * 16, sx + (rand() - 0.5) * 28, sy + (rand() - 0.5) * 28);
1491
+ patCtx.stroke();
1492
+ }
1493
+ patCtx.fillStyle = "#FFFFFF";
1494
+ for (let i = 0; i < 30; i++) {
1495
+ const cx = rand() * 128;
1496
+ const cy = rand() * 128;
1497
+ const r = 0.75 + rand() * 2.4;
1498
+ patCtx.globalAlpha = 0.25 + rand() * 0.5;
1499
+ patCtx.beginPath();
1500
+ patCtx.arc(cx, cy, r, 0, Math.PI * 2);
1501
+ patCtx.fill();
1502
+ }
1503
+ patCtx.fillStyle = "#000000";
1504
+ for (let i = 0; i < 20; i++) {
1505
+ const cx = rand() * 128;
1506
+ const cy = rand() * 128;
1507
+ const r = 0.65 + rand() * 2;
1508
+ patCtx.globalAlpha = 0.2 + rand() * 0.4;
1509
+ patCtx.beginPath();
1510
+ patCtx.arc(cx, cy, r, 0, Math.PI * 2);
1511
+ patCtx.fill();
1512
+ }
1513
+ } else if (pType === "brushed") {
1514
+ patCtx.fillStyle = patColor;
1515
+ patCtx.fillRect(0, 0, 128, 128);
1516
+ for (let i = 0; i < 350; i++) {
1517
+ const y = rand() * 128;
1518
+ const x = rand() * 128;
1519
+ const len = 30 + rand() * 80;
1520
+ const thickness = 0.5 + rand() * 1.5;
1521
+ const isLight = rand() > 0.45;
1522
+ patCtx.strokeStyle = isLight ? "#FFFFFF" : "#000000";
1523
+ patCtx.globalAlpha = isLight ? 0.04 + rand() * 0.12 : 0.03 + rand() * 0.08;
1524
+ patCtx.lineWidth = thickness;
1525
+ patCtx.beginPath();
1526
+ patCtx.moveTo(x, y);
1527
+ patCtx.lineTo(x + len, y);
1528
+ patCtx.stroke();
1529
+ if (x + len > 128) {
1530
+ patCtx.beginPath();
1531
+ patCtx.moveTo(x - 128, y);
1532
+ patCtx.lineTo(x + len - 128, y);
1533
+ patCtx.stroke();
1534
+ }
1535
+ }
1536
+ for (let i = 0; i < 8; i++) {
1537
+ const x = rand() * 128;
1538
+ const w = 10 + rand() * 30;
1539
+ const isLight = rand() > 0.5;
1540
+ const grad = patCtx.createLinearGradient(x, 0, x + w, 0);
1541
+ const baseColor = isLight ? "255,255,255" : "0,0,0";
1542
+ const alpha = 0.01 + rand() * 0.04;
1543
+ grad.addColorStop(0, `rgba(${baseColor}, 0)`);
1544
+ grad.addColorStop(0.5, `rgba(${baseColor}, ${alpha})`);
1545
+ grad.addColorStop(1, `rgba(${baseColor}, 0)`);
1546
+ patCtx.fillStyle = grad;
1547
+ patCtx.globalAlpha = 1;
1548
+ patCtx.fillRect(x, 0, w, 128);
1549
+ if (x + w > 128) {
1550
+ patCtx.fillRect(x - 128, 0, w, 128);
1551
+ }
1552
+ }
1553
+ } else if (pType === "marble") {
1554
+ patCtx.fillStyle = patColor;
1555
+ patCtx.fillRect(0, 0, 256, 256);
1556
+ for (let i = 0; i < 8; i++) {
1557
+ const cx = rand() * 256;
1558
+ const cy = rand() * 256;
1559
+ const r = 40 + rand() * 70;
1560
+ const grad = patCtx.createRadialGradient(cx, cy, 0, cx, cy, r);
1561
+ const isLight = rand() > 0.45;
1562
+ const alpha = 0.06 + rand() * 0.12;
1563
+ const cStr = isLight ? "255,255,255" : "0,0,0";
1564
+ grad.addColorStop(0, `rgba(${cStr}, ${alpha})`);
1565
+ grad.addColorStop(0.5, `rgba(${cStr}, ${alpha * 0.4})`);
1566
+ grad.addColorStop(1, `rgba(${cStr}, 0)`);
1567
+ patCtx.fillStyle = grad;
1568
+ patCtx.globalAlpha = 1;
1569
+ patCtx.beginPath();
1570
+ patCtx.arc(cx, cy, r, 0, Math.PI * 2);
1571
+ patCtx.fill();
1572
+ }
1573
+ const drawMarbleVein = (color, width, opac) => {
1574
+ patCtx.strokeStyle = color;
1575
+ patCtx.lineWidth = width;
1576
+ patCtx.globalAlpha = opac;
1577
+ let px = rand() * 256;
1578
+ let py = 0;
1579
+ patCtx.beginPath();
1580
+ patCtx.moveTo(px, py);
1581
+ const steps = 18;
1582
+ for (let s = 1; s <= steps; s++) {
1583
+ const progress = s / steps;
1584
+ const targetY = progress * 256;
1585
+ const frequency = 4;
1586
+ const amp = 35;
1587
+ const noise = Math.sin(progress * Math.PI * frequency + rand() * 2) * amp;
1588
+ const targetX = (px + (rand() - 0.5) * 50 + noise + 256) % 256;
1589
+ patCtx.lineTo(targetX, targetY);
1590
+ }
1591
+ patCtx.stroke();
1592
+ };
1593
+ for (let i = 0; i < 4; i++) {
1594
+ drawMarbleVein("#000000", 1.2 + rand() * 1.5, 0.15 + rand() * 0.15);
1595
+ }
1596
+ for (let i = 0; i < 3; i++) {
1597
+ const isGold = rand() > 0.4;
1598
+ const vColor = isGold ? "#EAB308" : "#FFFFFF";
1599
+ drawMarbleVein(vColor, 0.7 + rand() * 0.8, 0.2 + rand() * 0.2);
1600
+ }
1601
+ for (let i = 0; i < 5; i++) {
1602
+ drawMarbleVein("#000000", 0.4, 0.08 + rand() * 0.06);
1603
+ }
1604
+ } else if (pType === "halftone") {
1605
+ patCtx.fillStyle = "rgba(0,0,0,0)";
1606
+ patCtx.fillRect(0, 0, 24, 24);
1607
+ const dotColor = fillColor || "#ffffff";
1608
+ patCtx.fillStyle = "#000000";
1609
+ patCtx.globalAlpha = 0.35;
1610
+ patCtx.beginPath();
1611
+ patCtx.arc(12, 12, 5.5, 0, Math.PI * 2);
1612
+ patCtx.arc(0, 0, 3.5, 0, Math.PI * 2);
1613
+ patCtx.arc(24, 0, 3.5, 0, Math.PI * 2);
1614
+ patCtx.arc(0, 24, 3.5, 0, Math.PI * 2);
1615
+ patCtx.arc(24, 24, 3.5, 0, Math.PI * 2);
1616
+ patCtx.fill();
1617
+ patCtx.fillStyle = dotColor;
1618
+ patCtx.globalAlpha = 0.95;
1619
+ patCtx.beginPath();
1620
+ patCtx.arc(11, 11, 5, 0, Math.PI * 2);
1621
+ patCtx.arc(0, 0, 3, 0, Math.PI * 2);
1622
+ patCtx.arc(24, 0, 3, 0, Math.PI * 2);
1623
+ patCtx.arc(0, 24, 3, 0, Math.PI * 2);
1624
+ patCtx.arc(24, 24, 3, 0, Math.PI * 2);
1625
+ patCtx.fill();
1626
+ patCtx.fillStyle = dotColor === "#FFFFFF" || dotColor === "#ffffff" ? "#7C6FFF" : "#FFFFFF";
1627
+ patCtx.globalAlpha = 0.55;
1628
+ patCtx.beginPath();
1629
+ patCtx.arc(12, 0, 1.5, 0, Math.PI * 2);
1630
+ patCtx.arc(12, 24, 1.5, 0, Math.PI * 2);
1631
+ patCtx.arc(0, 12, 1.5, 0, Math.PI * 2);
1632
+ patCtx.arc(24, 12, 1.5, 0, Math.PI * 2);
1633
+ patCtx.fill();
1634
+ } else if (pType === "paper") {
1635
+ patCtx.fillStyle = patColor;
1636
+ patCtx.fillRect(0, 0, 128, 128);
1637
+ for (let i = 0; i < 350; i++) {
1638
+ const fx = rand() * 128;
1639
+ const fy = rand() * 128;
1640
+ const flen = 3 + rand() * 12;
1641
+ const fangle = rand() * Math.PI * 2;
1642
+ const isDark = rand() > 0.4;
1643
+ patCtx.strokeStyle = isDark ? "#000000" : "#FFFFFF";
1644
+ patCtx.globalAlpha = isDark ? 0.03 + rand() * 0.08 : 0.05 + rand() * 0.12;
1645
+ patCtx.lineWidth = 0.4 + rand() * 0.7;
1646
+ patCtx.beginPath();
1647
+ patCtx.moveTo(fx, fy);
1648
+ patCtx.quadraticCurveTo(fx + Math.cos(fangle) * flen * 0.5 + (rand() - 0.5) * 4, fy + Math.sin(fangle) * flen * 0.5 + (rand() - 0.5) * 4, fx + Math.cos(fangle) * flen, fy + Math.sin(fangle) * flen);
1649
+ patCtx.stroke();
1650
+ }
1651
+ for (let i = 0; i < 5e3; i++) {
1652
+ const gx = Math.floor(rand() * 128);
1653
+ const gy = Math.floor(rand() * 128);
1654
+ const isDark = rand() > 0.5;
1655
+ patCtx.fillStyle = isDark ? "#000000" : "#FFFFFF";
1656
+ patCtx.globalAlpha = isDark ? 0.04 : 0.06;
1657
+ patCtx.fillRect(gx, gy, 1, 1);
1658
+ }
1659
+ const points = [];
1660
+ for (let i = 0; i < 6; i++) {
1661
+ points.push([rand() * 128, rand() * 128]);
1662
+ }
1663
+ points.push([0, 0], [128, 0], [128, 128], [0, 128]);
1664
+ for (let i = 0; i < 15; i++) {
1665
+ const p1 = points[Math.floor(rand() * points.length)];
1666
+ const p2 = points[Math.floor(rand() * points.length)];
1667
+ const p3 = points[Math.floor(rand() * points.length)];
1668
+ if (p1 !== p2 && p2 !== p3) {
1669
+ const grad = patCtx.createLinearGradient(p1[0], p1[1], p2[0], p2[1]);
1670
+ const alpha = 0.01 + rand() * 0.06;
1671
+ const isDark = rand() > 0.5;
1672
+ const cStr = isDark ? "0,0,0" : "255,255,255";
1673
+ grad.addColorStop(0, `rgba(${cStr}, ${alpha})`);
1674
+ grad.addColorStop(1, `rgba(${cStr}, 0)`);
1675
+ patCtx.fillStyle = grad;
1676
+ patCtx.globalAlpha = 1;
1677
+ patCtx.beginPath();
1678
+ patCtx.moveTo(p1[0], p1[1]);
1679
+ patCtx.lineTo(p2[0], p2[1]);
1680
+ patCtx.lineTo(p3[0], p3[1]);
1681
+ patCtx.closePath();
1682
+ patCtx.fill();
1683
+ }
1684
+ }
1685
+ for (let i = 0; i < 12; i++) {
1686
+ const sx = rand() * 128;
1687
+ const sy = rand() * 128;
1688
+ const ex = rand() * 128;
1689
+ const ey = rand() * 128;
1690
+ const dx = ex - sx;
1691
+ const dy = ey - sy;
1692
+ const len = Math.sqrt(dx * dx + dy * dy);
1693
+ if (len === 0) continue;
1694
+ const nx = -dy / len;
1695
+ const ny = dx / len;
1696
+ patCtx.strokeStyle = "#000000";
1697
+ patCtx.lineWidth = 0.5 + rand() * 1.2;
1698
+ patCtx.globalAlpha = 0.05 + rand() * 0.12;
1699
+ patCtx.beginPath();
1700
+ patCtx.moveTo(sx + nx * 0.8, sy + ny * 0.8);
1701
+ patCtx.lineTo(ex + nx * 0.8, ey + ny * 0.8);
1702
+ patCtx.stroke();
1703
+ patCtx.strokeStyle = "#000000";
1704
+ patCtx.lineWidth = 0.3 + rand() * 0.4;
1705
+ patCtx.globalAlpha = 0.08 + rand() * 0.15;
1706
+ patCtx.beginPath();
1707
+ patCtx.moveTo(sx, sy);
1708
+ patCtx.lineTo(ex, ey);
1709
+ patCtx.stroke();
1710
+ patCtx.strokeStyle = "#FFFFFF";
1711
+ patCtx.lineWidth = 0.6 + rand() * 1.5;
1712
+ patCtx.globalAlpha = 0.08 + rand() * 0.22;
1713
+ patCtx.beginPath();
1714
+ patCtx.moveTo(sx - nx * 0.8, sy - ny * 0.8);
1715
+ patCtx.lineTo(ex - nx * 0.8, ey - ny * 0.8);
1716
+ patCtx.stroke();
1717
+ }
1718
+ }
1719
+ const pat = ctx.createPattern(patCanvas, "repeat");
1720
+ if (pat) {
1721
+ textFill = pat;
1722
+ }
1723
+ }
1724
+ const createSeededRand = (seed) => {
1725
+ let val = seed;
1726
+ return () => {
1727
+ val = (val * 9301 + 49297) % 233280;
1728
+ return val / 233280;
1729
+ };
1730
+ };
1731
+ const isInkStyle = cfg.effectName.toLowerCase().includes("ink") || cfg.fontFamily.toLowerCase().includes("brush") || cfg.effectName.toLowerCase().includes("grunge") || cfg.effectName.toLowerCase().includes("scratch") || cfg.fontFamily === "Permanent Marker";
1732
+ if (isInkStyle) {
1733
+ const tCanvas = createCanvas(cWidth, cHeight);
1734
+ tCanvas.width = cWidth;
1735
+ tCanvas.height = cHeight;
1736
+ const tCtx = getCanvas2DContext2(tCanvas);
1737
+ if (tCtx) {
1738
+ tCtx.font = fontStr;
1739
+ tCtx.textAlign = align;
1740
+ tCtx.textBaseline = "alphabetic";
1741
+ const rand = createSeededRand(text.length * 171 + fontSize * 11 + (fillColor ? fillColor.length : 1));
1742
+ tCtx.fillStyle = "#FFFFFF";
1743
+ const originalLSpacing = tCtx.letterSpacing || "normal";
1744
+ if (letterSpacing !== 0) {
1745
+ tCtx.letterSpacing = `${letterSpacing}px`;
1746
+ }
1747
+ lines.forEach((line, index) => {
1748
+ const py = startY + index * lineAdvance;
1749
+ tCtx.fillText(line, startX, py);
1750
+ });
1751
+ const mxMin = Math.max(0, Math.floor(xMin - 20));
1752
+ const mxMax = Math.min(cWidth - 1, Math.ceil(xMax + 20));
1753
+ const myMin = Math.max(0, Math.floor(yMin - 20));
1754
+ const myMax = Math.min(cHeight - 1, Math.ceil(yMax + 20));
1755
+ const scanW = mxMax - mxMin;
1756
+ const scanH = myMax - myMin;
1757
+ if (scanW > 0 && scanH > 0) {
1758
+ const imgData = tCtx.getImageData(mxMin, myMin, scanW, scanH);
1759
+ const data = imgData.data;
1760
+ const isFilled = (coordX, coordY) => {
1761
+ if (coordX < 0 || coordX >= scanW || coordY < 0 || coordY >= scanH) return false;
1762
+ const idx = (coordY * scanW + coordX) * 4;
1763
+ return data[idx + 3] > 70;
1764
+ };
1765
+ const topEdges = [];
1766
+ const bottomEdges = [];
1767
+ const boundaryEdges = [];
1768
+ const insidePixels = [];
1769
+ for (let y = 1; y < scanH - 1; y += 1) {
1770
+ for (let x = 1; x < scanW - 1; x += 1) {
1771
+ if (isFilled(x, y)) {
1772
+ const emptyLeft = !isFilled(x - 1, y);
1773
+ const emptyRight = !isFilled(x + 1, y);
1774
+ const emptyTop = !isFilled(x, y - 1);
1775
+ const emptyBottom = !isFilled(x, y + 1);
1776
+ if (emptyTop) {
1777
+ topEdges.push({ x: mxMin + x, y: myMin + y });
1778
+ } else if (emptyBottom) {
1779
+ bottomEdges.push({ x: mxMin + x, y: myMin + y });
1780
+ } else if (emptyLeft || emptyRight) {
1781
+ boundaryEdges.push({ x: mxMin + x, y: myMin + y });
1782
+ } else {
1783
+ if (rand() < 0.15) {
1784
+ insidePixels.push({ x: mxMin + x, y: myMin + y });
1785
+ }
1786
+ }
1787
+ }
1788
+ }
1789
+ }
1790
+ tCtx.strokeStyle = "#FFFFFF";
1791
+ tCtx.fillStyle = "#FFFFFF";
1792
+ const topSampleRate = 5;
1793
+ for (let i = 0; i < topEdges.length; i += topSampleRate) {
1794
+ const pt = topEdges[i];
1795
+ const h = fontSize * (0.06 + rand() * 0.16);
1796
+ const w = 1.8 + rand() * 2.5;
1797
+ const slantOffset = h * 0.22;
1798
+ tCtx.beginPath();
1799
+ tCtx.moveTo(pt.x - w / 2, pt.y);
1800
+ tCtx.lineTo(pt.x + w / 2, pt.y);
1801
+ tCtx.lineTo(pt.x + slantOffset, pt.y - h);
1802
+ tCtx.closePath();
1803
+ tCtx.fill();
1804
+ }
1805
+ const bottomSampleRate = 5;
1806
+ for (let i = 0; i < bottomEdges.length; i += bottomSampleRate) {
1807
+ const pt = bottomEdges[i];
1808
+ const h = fontSize * (0.07 + rand() * 0.2);
1809
+ const w = 1.8 + rand() * 2.5;
1810
+ const slantOffset = -h * 0.22;
1811
+ tCtx.beginPath();
1812
+ tCtx.moveTo(pt.x - w / 2, pt.y);
1813
+ tCtx.lineTo(pt.x + w / 2, pt.y);
1814
+ tCtx.lineTo(pt.x + slantOffset, pt.y + h);
1815
+ tCtx.closePath();
1816
+ tCtx.fill();
1817
+ }
1818
+ const boundarySampleRate = 3;
1819
+ for (let i = 0; i < boundaryEdges.length; i += boundarySampleRate) {
1820
+ const pt = boundaryEdges[i];
1821
+ const r = 0.5 + rand() * 1.5;
1822
+ tCtx.beginPath();
1823
+ tCtx.arc(pt.x + (rand() - 0.5) * 1.2, pt.y + (rand() - 0.5) * 1.2, r, 0, Math.PI * 2);
1824
+ tCtx.fill();
1825
+ }
1826
+ const splatterCount = Math.floor(fontSize * 0.5);
1827
+ for (let s = 0; s < splatterCount; s++) {
1828
+ const pt = boundaryEdges[Math.floor(rand() * boundaryEdges.length)] || topEdges[Math.floor(rand() * topEdges.length)];
1829
+ if (pt) {
1830
+ const dist = 3 + rand() * (fontSize * 0.24);
1831
+ const angle = rand() * Math.PI * 2;
1832
+ const sx = pt.x + Math.cos(angle) * dist;
1833
+ const sy = pt.y + Math.sin(angle) * dist;
1834
+ const size = 0.4 + rand() * 1.6;
1835
+ tCtx.globalAlpha = 0.25 + rand() * 0.65;
1836
+ tCtx.beginPath();
1837
+ tCtx.arc(sx, sy, size, 0, Math.PI * 2);
1838
+ tCtx.fill();
1839
+ tCtx.globalAlpha = 1;
1840
+ }
1841
+ }
1842
+ tCtx.save();
1843
+ tCtx.globalCompositeOperation = "destination-out";
1844
+ const scratchCount = Math.floor(fontSize * 0.42);
1845
+ for (let i = 0; i < scratchCount; i++) {
1846
+ const pt = insidePixels[Math.floor(rand() * insidePixels.length)];
1847
+ if (pt) {
1848
+ const len = fontSize * (0.06 + rand() * 0.14);
1849
+ const angle = -Math.PI / 4.2 + (rand() - 0.5) * 0.1;
1850
+ tCtx.lineWidth = 0.5 + rand() * 0.8;
1851
+ tCtx.globalAlpha = 0.2 + rand() * 0.4;
1852
+ tCtx.beginPath();
1853
+ tCtx.moveTo(pt.x - Math.cos(angle) * (len / 2), pt.y - Math.sin(angle) * (len / 2));
1854
+ tCtx.lineTo(pt.x + Math.cos(angle) * (len / 2), pt.y + Math.sin(angle) * (len / 2));
1855
+ tCtx.stroke();
1856
+ }
1857
+ }
1858
+ tCtx.restore();
1859
+ }
1860
+ tCtx.letterSpacing = originalLSpacing;
1861
+ ctx.save();
1862
+ const tintCanvas = createCanvas(cWidth, cHeight);
1863
+ tintCanvas.width = cWidth;
1864
+ tintCanvas.height = cHeight;
1865
+ const tintCtx = getCanvas2DContext2(tintCanvas);
1866
+ if (tintCtx) {
1867
+ tintCtx.drawImage(tCanvas, 0, 0);
1868
+ tintCtx.globalCompositeOperation = "source-in";
1869
+ tintCtx.fillStyle = textFill;
1870
+ tintCtx.fillRect(0, 0, cWidth, cHeight);
1871
+ ctx.drawImage(tintCanvas, 0, 0);
1872
+ }
1873
+ ctx.restore();
1874
+ }
1875
+ } else {
1876
+ ctx.save();
1877
+ if (fillType !== "none") {
1878
+ if (usePerCharFill && fillType === "solid") {
1879
+ renderLines("fill", void 0, 0, 0, { perCharFill: true });
1880
+ } else {
1881
+ renderLines("fill", textFill);
1882
+ }
1883
+ }
1884
+ ctx.restore();
1885
+ }
1886
+ if (strokeEnabled && strokeWidth > 0 && strokePosition === "inside") {
1887
+ applyStroke();
1888
+ }
1889
+ glowLayers.forEach((layer2) => {
1890
+ if (layer2.enabled && layer2.type === "inner" && layer2.opacity > 0) {
1891
+ ctx.save();
1892
+ ctx.globalCompositeOperation = "source-atop";
1893
+ const renderCount = Math.max(1, Math.min(20, layer2.strength ?? 1));
1894
+ for (let i = 0; i < renderCount; i++) {
1895
+ renderWithShadowTrick("fill", layer2.color, layer2.blur, 0, 0, layer2.opacity, "#000000", layer2.spread ?? 0);
1896
+ }
1897
+ ctx.restore();
1898
+ }
1899
+ });
1900
+ if (shadowEnabled && shadowType === "inner" && shadowOpacity > 0) {
1901
+ ctx.save();
1902
+ ctx.globalCompositeOperation = "source-atop";
1903
+ renderWithShadowTrick("fill", shadowColor, shadowBlur, shadowOffsetX, shadowOffsetY, shadowOpacity, "#000000");
1904
+ ctx.restore();
1905
+ }
1906
+ if (isGlitch) {
1907
+ ctx.save();
1908
+ ctx.globalCompositeOperation = "source-atop";
1909
+ ctx.strokeStyle = "rgba(0,0,0,0.18)";
1910
+ ctx.lineWidth = 1.5;
1911
+ for (let ly = yMin; ly < yMax; ly += 4) {
1912
+ ctx.beginPath();
1913
+ ctx.moveTo(xMin - 50, ly);
1914
+ ctx.lineTo(xMax + 50, ly);
1915
+ ctx.stroke();
1916
+ }
1917
+ ctx.restore();
1918
+ }
1919
+ }
1920
+
1921
+ // src/presets.ts
1922
+ var defaultConfig = {
1923
+ text: "CLYPRA STUDIO",
1924
+ effectName: "My Effect",
1925
+ fontFamily: "Poppins",
1926
+ fontWeight: 700,
1927
+ fontStyle: "normal",
1928
+ fontSize: DEFAULT_FONT_SIZE,
1929
+ letterSpacing: 4,
1930
+ lineHeight: 1.2,
1931
+ fillType: "solid",
1932
+ fillColor: "#FFFFFF",
1933
+ fillGradientAngle: 90,
1934
+ fillGradientStops: [
1935
+ { color: "#FFFFFF", offset: 0 },
1936
+ { color: "#E0E0E0", offset: 100 }
1937
+ ],
1938
+ strokeEnabled: false,
1939
+ strokeColor: "#7C6FFF",
1940
+ strokeWidth: 4,
1941
+ strokePosition: "outside",
1942
+ strokeOpacity: 100,
1943
+ strokeLineJoin: "round",
1944
+ glowLayers: [
1945
+ { enabled: false, color: "#7C6FFF", blur: 20, opacity: 80, type: "outer" },
1946
+ { enabled: false, color: "#FF007C", blur: 40, opacity: 60, type: "outer" },
1947
+ { enabled: false, color: "#00F0FF", blur: 60, opacity: 40, type: "outer" }
1948
+ ],
1949
+ shadowEnabled: false,
1950
+ shadowColor: "#000000",
1951
+ shadowBlur: 10,
1952
+ shadowOffsetX: 5,
1953
+ shadowOffsetY: 5,
1954
+ shadowOpacity: 80,
1955
+ shadowType: "drop",
1956
+ bevelEnabled: false,
1957
+ bevelDepth: 5,
1958
+ bevelHighlight: "#FFFFFF",
1959
+ bevelShadow: "#000000",
1960
+ bevelDirection: "bottom-right",
1961
+ bevelCoreColor: "#000000",
1962
+ bevelEdgeColor: "#2A2A38",
1963
+ bevelEdgeWidth: 0,
1964
+ bevelBlur: 0,
1965
+ bevelBlurColor: "#000000",
1966
+ bevelPerspectiveEnabled: false,
1967
+ bevelVanishingPointX: 40,
1968
+ bevelVanishingPointY: 80,
1969
+ bevelFocalLength: 400,
1970
+ stackEnabled: false,
1971
+ stackCount: 3,
1972
+ stackOffsetX: 10,
1973
+ stackOffsetY: -10,
1974
+ stackOpacityDecay: 20,
1975
+ stackColor1: "#FF7C00",
1976
+ stackColor2: "#00FFDD",
1977
+ stackColor3: "#FF00AA",
1978
+ stackColor4: "#AA00FF",
1979
+ panelEnabled: false,
1980
+ panelColor: "#1E1E26",
1981
+ panelOpacity: 80,
1982
+ panelRadius: 12,
1983
+ panelPaddingX: 40,
1984
+ panelPaddingY: 20,
1985
+ panelStrokeEnabled: false,
1986
+ panelStrokeColor: "#2A2A38",
1987
+ panelStrokeWidth: 2,
1988
+ canvasWidth: DEFAULT_CANVAS_WIDTH,
1989
+ canvasHeight: DEFAULT_CANVAS_HEIGHT,
1990
+ textPosX: "center",
1991
+ textPosY: "middle",
1992
+ wrapText: true,
1993
+ autoFitText: false,
1994
+ // Ink Brush custom properties
1995
+ inkColor: "#FFFFFF",
1996
+ bristleDensity: 0.8,
1997
+ bristleSkipRate: 0.2,
1998
+ dripRate: 0.3,
1999
+ dripMaxLength: 40,
2000
+ grainDensity: 0.15,
2001
+ skewX: -0.2
2002
+ };
2003
+ var builtInPresets = [
2004
+ {
2005
+ id: "premium-sticker",
2006
+ name: "STICKER",
2007
+ category: "Classic",
2008
+ config: {
2009
+ ...defaultConfig,
2010
+ text: "TEXT",
2011
+ effectName: "StickerEffect",
2012
+ fontFamily: "Arial Rounded MT Bold",
2013
+ fontWeight: 900,
2014
+ fontStyle: "normal",
2015
+ fontSize: 88,
2016
+ letterSpacing: 2,
2017
+ lineHeight: 1.2,
2018
+ fillType: "linear",
2019
+ fillGradientAngle: 90,
2020
+ fillGradientStops: [
2021
+ { color: "#FFCE00", offset: 0 },
2022
+ { color: "#E05300", offset: 100 }
2023
+ ],
2024
+ strokeEnabled: true,
2025
+ strokeColor: "#FFFFFF",
2026
+ strokeWidth: 15,
2027
+ strokePosition: "outside",
2028
+ strokeType: "single",
2029
+ strokeLineJoin: "round",
2030
+ strokeOpacity: 100,
2031
+ bevelEnabled: true,
2032
+ bevelDepth: 8,
2033
+ bevelDirection: "bottom-right",
2034
+ bevelHighlight: "#FFCE00",
2035
+ bevelCoreColor: "#4E1901",
2036
+ bevelShadow: "#1D0A00",
2037
+ shadowEnabled: true,
2038
+ shadowColor: "#000000",
2039
+ shadowBlur: 14,
2040
+ shadowOffsetX: 6,
2041
+ shadowOffsetY: 8,
2042
+ shadowOpacity: 65,
2043
+ shadowType: "drop"
2044
+ }
2045
+ },
2046
+ {
2047
+ id: "premium-holo",
2048
+ name: "HOLOGRAPHIC",
2049
+ category: "Classic",
2050
+ config: {
2051
+ ...defaultConfig,
2052
+ text: "TEXT",
2053
+ effectName: "HolographicEffect",
2054
+ fontFamily: "Impact",
2055
+ fontWeight: 900,
2056
+ fontStyle: "normal",
2057
+ fontSize: 100,
2058
+ letterSpacing: 2,
2059
+ lineHeight: 1.2,
2060
+ fillType: "linear",
2061
+ fillGradientAngle: 125,
2062
+ fillGradientStops: [
2063
+ { color: "#6FE3FF", offset: 0 },
2064
+ { color: "#D196FF", offset: 25 },
2065
+ { color: "#FF9CEE", offset: 50 },
2066
+ { color: "#FFE49E", offset: 75 },
2067
+ { color: "#D4FFF1", offset: 100 }
2068
+ ],
2069
+ strokeEnabled: true,
2070
+ strokeColor: "#FFFFFF",
2071
+ strokeWidth: 2,
2072
+ strokePosition: "outside",
2073
+ strokeType: "single",
2074
+ strokeOpacity: 100,
2075
+ glowLayers: [{ enabled: true, color: "#D196FF", blur: 15, opacity: 50, type: "outer" }],
2076
+ bevelEnabled: true,
2077
+ bevelDepth: 4,
2078
+ bevelDirection: "bottom-right",
2079
+ bevelHighlight: "#FFFFFF",
2080
+ bevelCoreColor: "#2A3540",
2081
+ bevelShadow: "#0E151D",
2082
+ shadowEnabled: true,
2083
+ shadowColor: "#000000",
2084
+ shadowBlur: 20,
2085
+ shadowOffsetX: 6,
2086
+ shadowOffsetY: 10,
2087
+ shadowOpacity: 90,
2088
+ shadowType: "drop"
2089
+ }
2090
+ },
2091
+ {
2092
+ id: "classic-ink",
2093
+ name: "Classic Ink",
2094
+ category: "Classic",
2095
+ config: {
2096
+ ...defaultConfig,
2097
+ text: "Classic Ink",
2098
+ effectName: "ClassicInk",
2099
+ fontFamily: "Playfair Display",
2100
+ fontWeight: 700,
2101
+ fontStyle: "normal",
2102
+ fontSize: 85,
2103
+ letterSpacing: 1,
2104
+ lineHeight: 1.2,
2105
+ fillType: "linear",
2106
+ fillGradientAngle: 90,
2107
+ fillGradientStops: [
2108
+ { color: "#FDFBF7", offset: 0 },
2109
+ { color: "#EAE5D9", offset: 100 }
2110
+ ],
2111
+ strokeEnabled: true,
2112
+ strokeColor: "#1E1E26",
2113
+ strokeWidth: 2,
2114
+ strokePosition: "outside",
2115
+ strokeOpacity: 100,
2116
+ strokeLineJoin: "round",
2117
+ shadowEnabled: true,
2118
+ shadowColor: "#3D2B1F",
2119
+ shadowBlur: 14,
2120
+ shadowOffsetX: 6,
2121
+ shadowOffsetY: 6,
2122
+ shadowOpacity: 90,
2123
+ shadowType: "drop"
2124
+ }
2125
+ },
2126
+ {
2127
+ id: "neon-crimson",
2128
+ name: "Neon Crimson",
2129
+ category: "Neon",
2130
+ config: {
2131
+ ...defaultConfig,
2132
+ text: "NEON",
2133
+ effectName: "NeonCrimson",
2134
+ fontFamily: "Bebas Neue",
2135
+ fontWeight: 700,
2136
+ fontStyle: "normal",
2137
+ fontSize: 100,
2138
+ letterSpacing: 6,
2139
+ lineHeight: 1.2,
2140
+ fillType: "none",
2141
+ strokeEnabled: true,
2142
+ strokeColor: "#FFFFFF",
2143
+ strokeWidth: 3,
2144
+ strokePosition: "center",
2145
+ strokeOpacity: 100,
2146
+ strokeLineJoin: "round",
2147
+ glowLayers: [
2148
+ { enabled: true, color: "#FF003C", blur: 12, opacity: 100, type: "outer" },
2149
+ { enabled: true, color: "#FF003C", blur: 35, opacity: 80, type: "outer" },
2150
+ { enabled: true, color: "#FF003C", blur: 80, opacity: 60, type: "outer" }
2151
+ ],
2152
+ shadowEnabled: false
2153
+ }
2154
+ },
2155
+ {
2156
+ id: "cyberpunk-yellow",
2157
+ name: "Cyberpunk Yellow",
2158
+ category: "Neon",
2159
+ config: {
2160
+ ...defaultConfig,
2161
+ text: "CYBER",
2162
+ effectName: "CyberpunkYellow",
2163
+ fontFamily: "Anton",
2164
+ fontWeight: 900,
2165
+ fontStyle: "normal",
2166
+ fontSize: 90,
2167
+ letterSpacing: 2,
2168
+ lineHeight: 1.2,
2169
+ fillType: "solid",
2170
+ fillColor: "#FFE600",
2171
+ strokeEnabled: true,
2172
+ strokeColor: "#000000",
2173
+ strokeWidth: 6,
2174
+ strokePosition: "outside",
2175
+ strokeOpacity: 100,
2176
+ strokeLineJoin: "round",
2177
+ shadowEnabled: true,
2178
+ shadowColor: "#FFE600",
2179
+ shadowBlur: 15,
2180
+ shadowOffsetX: 4,
2181
+ shadowOffsetY: 4,
2182
+ shadowOpacity: 95,
2183
+ shadowType: "drop"
2184
+ }
2185
+ },
2186
+ {
2187
+ id: "retro-comic",
2188
+ name: "Retro Comic",
2189
+ category: "Experimental",
2190
+ config: {
2191
+ ...defaultConfig,
2192
+ text: "BOOM!",
2193
+ effectName: "RetroComic",
2194
+ fontFamily: "Arial Black",
2195
+ fontWeight: 900,
2196
+ fontStyle: "normal",
2197
+ fontSize: 85,
2198
+ letterSpacing: 3,
2199
+ lineHeight: 1.1,
2200
+ fillType: "solid",
2201
+ fillColor: "#000000",
2202
+ strokeEnabled: true,
2203
+ strokeColor: "#FFFFFF",
2204
+ strokeWidth: 6,
2205
+ strokePosition: "outside",
2206
+ strokeOpacity: 100,
2207
+ strokeLineJoin: "round",
2208
+ glowLayers: [
2209
+ { enabled: true, color: "#000000", blur: 0, opacity: 100, type: "outer", strength: 1, spread: 3 },
2210
+ { enabled: false, color: "#FF007C", blur: 40, opacity: 60, type: "outer" },
2211
+ { enabled: false, color: "#00F0FF", blur: 60, opacity: 40, type: "outer" }
2212
+ ],
2213
+ // panelEnabled: true,
2214
+ // panelColor: "#FFE100",
2215
+ // panelOpacity: 100,
2216
+ // panelRadius: 10,
2217
+ // panelPaddingX: 55,
2218
+ // panelPaddingY: 35,
2219
+ // panelStrokeEnabled: true,
2220
+ // panelStrokeColor: "#000000",
2221
+ // panelStrokeWidth: 4,
2222
+ shadowEnabled: true,
2223
+ shadowColor: "#000000",
2224
+ shadowBlur: 0,
2225
+ shadowOffsetX: 8,
2226
+ shadowOffsetY: 8,
2227
+ shadowOpacity: 100,
2228
+ shadowType: "drop"
2229
+ }
2230
+ }
2231
+ ];
2232
+
2233
+ // src/engine/timelineDefaults.ts
2234
+ function ensureDefaultTimeline(doc) {
2235
+ if (doc.timeline.tracks.length > 0) return doc;
2236
+ const shadowLayer = doc.effectLayers.find((l) => l.type === "shadow");
2237
+ const maskLayer = doc.effectLayers.find((l) => l.type === "mask");
2238
+ const tracks = [];
2239
+ if (shadowLayer) {
2240
+ tracks.push({
2241
+ layerId: shadowLayer.id,
2242
+ paramPath: "shadowOffsetY",
2243
+ keyframes: [
2244
+ { time: 0, value: shadowLayer.params.shadowOffsetY ?? 5, easing: "easeInOut" },
2245
+ {
2246
+ time: doc.timeline.duration / 2,
2247
+ value: (shadowLayer.params.shadowOffsetY ?? 5) + 8,
2248
+ easing: "easeInOut"
2249
+ },
2250
+ {
2251
+ time: doc.timeline.duration,
2252
+ value: shadowLayer.params.shadowOffsetY ?? 5,
2253
+ easing: "easeInOut"
2254
+ }
2255
+ ]
2256
+ });
2257
+ }
2258
+ if (maskLayer) {
2259
+ tracks.push({
2260
+ layerId: maskLayer.id,
2261
+ paramPath: "revealProgress",
2262
+ keyframes: [
2263
+ { time: 0, value: 0, easing: "easeOut" },
2264
+ { time: doc.timeline.duration * 0.85, value: 1, easing: "easeOut" }
2265
+ ]
2266
+ });
2267
+ maskLayer.enabled = false;
2268
+ }
2269
+ return {
2270
+ ...doc,
2271
+ effectLayers: doc.effectLayers.map((l) => ({ ...l })),
2272
+ timeline: { ...doc.timeline, tracks }
2273
+ };
2274
+ }
2275
+
2276
+ // src/engine/migrate.ts
2277
+ function layer(type, name, params, extra) {
2278
+ return {
2279
+ id: newLayerId(),
2280
+ type,
2281
+ name,
2282
+ enabled: true,
2283
+ opacity: 1,
2284
+ blendMode: "source-over",
2285
+ target: type === "panel" ? "scene" : "text",
2286
+ params,
2287
+ ...extra
2288
+ };
2289
+ }
2290
+ function resolveCustomEngineId(cfg) {
2291
+ if (!cfg.customRenderer) return null;
2292
+ return LEGACY_RENDERER_MAP[cfg.customRenderer] ?? null;
2293
+ }
2294
+ function textEffectConfigToScene(cfg) {
2295
+ const engineId = resolveCustomEngineId(cfg);
2296
+ const layers = [];
2297
+ if (engineId) {
2298
+ layers.push(
2299
+ layer(
2300
+ "customEngine",
2301
+ "Custom Engine",
2302
+ { engineId },
2303
+ {
2304
+ enabled: true,
2305
+ target: "scene"
2306
+ }
2307
+ )
2308
+ );
2309
+ layers.push(
2310
+ layer(
2311
+ "customEngine",
2312
+ "Engine Params",
2313
+ { ...collectEngineParams(cfg, engineId) },
2314
+ {
2315
+ enabled: true,
2316
+ target: "text"
2317
+ }
2318
+ )
2319
+ );
2320
+ }
2321
+ layers.push(
2322
+ layer(
2323
+ "panel",
2324
+ "Background Panel",
2325
+ {
2326
+ panelEnabled: cfg.panelEnabled,
2327
+ panelColor: cfg.panelColor,
2328
+ panelOpacity: cfg.panelOpacity,
2329
+ panelRadius: cfg.panelRadius,
2330
+ panelPaddingX: cfg.panelPaddingX,
2331
+ panelPaddingY: cfg.panelPaddingY,
2332
+ panelStrokeEnabled: cfg.panelStrokeEnabled,
2333
+ panelStrokeColor: cfg.panelStrokeColor,
2334
+ panelStrokeWidth: cfg.panelStrokeWidth
2335
+ },
2336
+ { enabled: cfg.panelEnabled }
2337
+ )
2338
+ );
2339
+ (cfg.glowLayers || []).forEach((g, i) => {
2340
+ layers.push(layer("glow", `Glow ${i + 1}`, { ...g }, { enabled: g.enabled, opacity: (g.opacity ?? 100) / 100 }));
2341
+ });
2342
+ layers.push(
2343
+ layer(
2344
+ "shadow",
2345
+ "Shadow",
2346
+ {
2347
+ shadowEnabled: cfg.shadowEnabled,
2348
+ shadowColor: cfg.shadowColor,
2349
+ shadowBlur: cfg.shadowBlur,
2350
+ shadowOffsetX: cfg.shadowOffsetX,
2351
+ shadowOffsetY: cfg.shadowOffsetY,
2352
+ shadowOpacity: cfg.shadowOpacity,
2353
+ shadowType: cfg.shadowType
2354
+ },
2355
+ { enabled: cfg.shadowEnabled }
2356
+ )
2357
+ );
2358
+ layers.push(
2359
+ layer(
2360
+ "extrusion",
2361
+ "Bevel / Extrusion",
2362
+ {
2363
+ bevelEnabled: cfg.bevelEnabled,
2364
+ bevelDepth: cfg.bevelDepth,
2365
+ bevelHighlight: cfg.bevelHighlight,
2366
+ bevelShadow: cfg.bevelShadow,
2367
+ bevelDirection: cfg.bevelDirection,
2368
+ bevelCoreColor: cfg.bevelCoreColor,
2369
+ bevelEdgeColor: cfg.bevelEdgeColor,
2370
+ bevelEdgeWidth: cfg.bevelEdgeWidth,
2371
+ bevelBlur: cfg.bevelBlur,
2372
+ bevelBlurColor: cfg.bevelBlurColor,
2373
+ bevelPerspectiveEnabled: cfg.bevelPerspectiveEnabled,
2374
+ bevelVanishingPointX: cfg.bevelVanishingPointX,
2375
+ bevelVanishingPointY: cfg.bevelVanishingPointY,
2376
+ bevelFocalLength: cfg.bevelFocalLength
2377
+ },
2378
+ { enabled: cfg.bevelEnabled }
2379
+ )
2380
+ );
2381
+ layers.push(
2382
+ layer(
2383
+ "duplicateStack",
2384
+ "Stack Extrusion",
2385
+ {
2386
+ stackEnabled: cfg.stackEnabled,
2387
+ stackCount: cfg.stackCount,
2388
+ stackOffsetX: cfg.stackOffsetX,
2389
+ stackOffsetY: cfg.stackOffsetY,
2390
+ stackOpacityDecay: cfg.stackOpacityDecay,
2391
+ stackColor1: cfg.stackColor1,
2392
+ stackColor2: cfg.stackColor2,
2393
+ stackColor3: cfg.stackColor3,
2394
+ stackColor4: cfg.stackColor4
2395
+ },
2396
+ { enabled: !!cfg.stackEnabled }
2397
+ )
2398
+ );
2399
+ layers.push(
2400
+ layer(
2401
+ "stroke",
2402
+ "Stroke",
2403
+ {
2404
+ strokeEnabled: cfg.strokeEnabled,
2405
+ strokeColor: cfg.strokeColor,
2406
+ strokeWidth: cfg.strokeWidth,
2407
+ strokePosition: cfg.strokePosition,
2408
+ strokeOpacity: cfg.strokeOpacity,
2409
+ strokeLineJoin: cfg.strokeLineJoin,
2410
+ strokeBlur: cfg.strokeBlur,
2411
+ strokeType: cfg.strokeType,
2412
+ strokeColorSecondary: cfg.strokeColorSecondary,
2413
+ strokeWidthSecondary: cfg.strokeWidthSecondary,
2414
+ strokeFadeRange: cfg.strokeFadeRange
2415
+ },
2416
+ { enabled: cfg.strokeEnabled }
2417
+ )
2418
+ );
2419
+ layers.push(
2420
+ layer(
2421
+ "fill",
2422
+ "Fill",
2423
+ {
2424
+ fillType: cfg.fillType,
2425
+ fillColor: cfg.fillColor,
2426
+ fillGradientAngle: cfg.fillGradientAngle,
2427
+ fillGradientStops: cfg.fillGradientStops,
2428
+ patternType: cfg.patternType,
2429
+ perCharFillEnabled: cfg.perCharFillEnabled,
2430
+ charFillColors: cfg.charFillColors
2431
+ },
2432
+ { enabled: cfg.fillType !== "none" }
2433
+ )
2434
+ );
2435
+ layers.push(layer("mask", "Text Alpha Mask", { maskType: "alphaText", revealProgress: 1 }, { enabled: false }));
2436
+ layers.push(layer("filter", "Compositor FX", { blur: 0, bloom: 0 }, { enabled: false, target: "previous" }));
2437
+ const doc = {
2438
+ version: 1,
2439
+ effectName: cfg.effectName,
2440
+ canvas: {
2441
+ width: cfg.canvasWidth,
2442
+ height: cfg.canvasHeight,
2443
+ background: "transparent"
2444
+ },
2445
+ text: {
2446
+ content: cfg.text,
2447
+ fontFamily: cfg.fontFamily,
2448
+ fontWeight: cfg.fontWeight,
2449
+ fontStyle: cfg.fontStyle,
2450
+ fontSize: cfg.fontSize,
2451
+ letterSpacing: cfg.letterSpacing,
2452
+ lineHeight: cfg.lineHeight,
2453
+ textPosX: cfg.textPosX,
2454
+ textPosY: cfg.textPosY,
2455
+ wrapText: cfg.wrapText !== false,
2456
+ autoFitText: !!cfg.autoFitText,
2457
+ perCharFillEnabled: cfg.perCharFillEnabled,
2458
+ charFillColors: cfg.charFillColors
2459
+ },
2460
+ effectLayers: layers,
2461
+ customEngineId: engineId,
2462
+ engineParams: engineId ? collectEngineParams(cfg, engineId) : void 0,
2463
+ compositor: { blur: 0, bloom: 0, bloomThreshold: 0.6 },
2464
+ timeline: { duration: 2, fps: 30, loop: true, tracks: [] },
2465
+ legacyConfig: { ...cfg }
2466
+ };
2467
+ return ensureDefaultTimeline(doc);
2468
+ }
2469
+ function collectEngineParams(cfg, id) {
2470
+ switch (id) {
2471
+ case "ink":
2472
+ return {
2473
+ inkColor: cfg.inkColor,
2474
+ bristleDensity: cfg.bristleDensity,
2475
+ bristleSkipRate: cfg.bristleSkipRate,
2476
+ dripRate: cfg.dripRate,
2477
+ dripMaxLength: cfg.dripMaxLength,
2478
+ grainDensity: cfg.grainDensity,
2479
+ skewX: cfg.skewX
2480
+ };
2481
+ default:
2482
+ return {};
2483
+ }
2484
+ }
2485
+ function getLayerParams(doc, type, index = 0) {
2486
+ const found = doc.effectLayers.filter((l) => l.type === type);
2487
+ return found[index]?.params;
2488
+ }
2489
+ function sceneToConfig(doc) {
2490
+ const base = doc.legacyConfig ? { ...doc.legacyConfig } : {
2491
+ ...createDefaultFromScene(doc)
2492
+ };
2493
+ base.effectName = doc.effectName;
2494
+ base.text = doc.text.content;
2495
+ base.fontFamily = doc.text.fontFamily;
2496
+ base.fontWeight = doc.text.fontWeight;
2497
+ base.fontStyle = doc.text.fontStyle;
2498
+ base.fontSize = doc.text.fontSize;
2499
+ base.letterSpacing = doc.text.letterSpacing;
2500
+ base.lineHeight = doc.text.lineHeight;
2501
+ base.textPosX = doc.text.textPosX;
2502
+ base.textPosY = doc.text.textPosY;
2503
+ base.wrapText = doc.text.wrapText !== false;
2504
+ base.autoFitText = !!doc.text.autoFitText;
2505
+ base.canvasWidth = doc.canvas.width;
2506
+ base.canvasHeight = doc.canvas.height;
2507
+ const panel = getLayerParams(doc, "panel");
2508
+ if (panel) {
2509
+ Object.assign(base, {
2510
+ panelEnabled: panel.panelEnabled,
2511
+ panelColor: panel.panelColor,
2512
+ panelOpacity: panel.panelOpacity,
2513
+ panelRadius: panel.panelRadius,
2514
+ panelPaddingX: panel.panelPaddingX,
2515
+ panelPaddingY: panel.panelPaddingY,
2516
+ panelStrokeEnabled: panel.panelStrokeEnabled,
2517
+ panelStrokeColor: panel.panelStrokeColor,
2518
+ panelStrokeWidth: panel.panelStrokeWidth
2519
+ });
2520
+ }
2521
+ const glows = doc.effectLayers.filter((l) => l.type === "glow");
2522
+ base.glowLayers = glows.map((l) => {
2523
+ const p = l.params;
2524
+ return {
2525
+ enabled: l.enabled && (p.enabled ?? true),
2526
+ color: p.color ?? "#7C6FFF",
2527
+ blur: p.blur ?? 20,
2528
+ opacity: Math.round((p.opacity ?? 80) * (l.opacity ?? 1)),
2529
+ type: p.type ?? "outer",
2530
+ strength: p.strength,
2531
+ spread: p.spread
2532
+ };
2533
+ });
2534
+ while (base.glowLayers.length < 3) {
2535
+ base.glowLayers.push({
2536
+ enabled: false,
2537
+ color: "#7C6FFF",
2538
+ blur: 20,
2539
+ opacity: 80,
2540
+ type: "outer"
2541
+ });
2542
+ }
2543
+ const shadow = getLayerParams(doc, "shadow");
2544
+ if (shadow) Object.assign(base, shadow);
2545
+ const extrusion = getLayerParams(doc, "extrusion");
2546
+ if (extrusion) Object.assign(base, extrusion);
2547
+ const stack = getLayerParams(doc, "duplicateStack");
2548
+ if (stack) Object.assign(base, stack);
2549
+ const stroke = getLayerParams(doc, "stroke");
2550
+ if (stroke) Object.assign(base, stroke);
2551
+ const fill = getLayerParams(doc, "fill");
2552
+ if (fill) Object.assign(base, fill);
2553
+ base.perCharFillEnabled = doc.text.perCharFillEnabled ?? fill?.perCharFillEnabled;
2554
+ base.charFillColors = doc.text.charFillColors ?? fill?.charFillColors;
2555
+ if (doc.customEngineId) {
2556
+ base.customRenderer = ENGINE_ID_TO_LEGACY[doc.customEngineId];
2557
+ Object.assign(base, doc.engineParams || {});
2558
+ } else {
2559
+ base.customRenderer = void 0;
2560
+ }
2561
+ base.glowLayers = base.glowLayers.slice(0, 6);
2562
+ const filterLayers = doc.effectLayers.filter((l) => l.type === "filter" && l.enabled);
2563
+ const filter = filterLayers[filterLayers.length - 1]?.params;
2564
+ const compositor = {
2565
+ blur: filter?.blur ?? doc.compositor.blur ?? 0,
2566
+ bloom: filter?.bloom ?? doc.compositor.bloom ?? 0,
2567
+ bloomThreshold: doc.compositor.bloomThreshold ?? 0.6
2568
+ };
2569
+ return base;
2570
+ }
2571
+ function createDefaultFromScene(doc) {
2572
+ const cfg = { ...defaultConfig };
2573
+ cfg.text = doc.text.content;
2574
+ return cfg;
2575
+ }
2576
+ function resolveFontFamilyName(fontFamily) {
2577
+ const f = fontFamily?.toLowerCase() || "";
2578
+ if (f.includes("inter")) return "Inter Variable";
2579
+ if (f.includes("montserrat")) return "Montserrat Variable";
2580
+ if (f.includes("geist")) return "Geist Variable";
2581
+ if (f.includes("space grotesk") || f.includes("grotesk")) return "Space Grotesk Variable";
2582
+ if (f.includes("outfit")) return "Outfit Variable";
2583
+ if (f.includes("roboto variable")) return "Roboto Variable";
2584
+ if (f.includes("roboto condensed")) return "Roboto Condensed";
2585
+ if (f === "roboto") return "Roboto Variable";
2586
+ if (f.includes("open sans")) return "Open Sans Variable";
2587
+ if (f.includes("raleway")) return "Raleway Variable";
2588
+ if (f.includes("oswald")) return "Oswald Variable";
2589
+ if (f.includes("playfair display")) return "Playfair Display Variable";
2590
+ if (f.includes("nunito")) return "Nunito Variable";
2591
+ if (f.includes("dancing script")) return "Dancing Script Variable";
2592
+ if (f === "lato") return "Lato";
2593
+ if (f === "anton") return "Anton";
2594
+ if (f === "bebas neue") return "Bebas Neue";
2595
+ if (f === "poppins") return "Poppins";
2596
+ if (f === "permanent marker") return "Permanent Marker";
2597
+ if (f === "bangers") return "Bangers";
2598
+ if (f === "press start 2p") return "Press Start 2P";
2599
+ if (f === "pacifico") return "Pacifico";
2600
+ return fontFamily;
2601
+ }
2602
+ function _buildConfig(effect, text, fontSize, canvasWidth, canvasHeight, time, clipStartTime, clipDuration) {
2603
+ const fill = effect.fills?.[0];
2604
+ const stroke = effect.strokes?.[0];
2605
+ const shadow = effect.shadows?.[0];
2606
+ const bevel = effect.bevel;
2607
+ const panel = effect.panel;
2608
+ const ratio = fontSize / 100;
2609
+ const config = {
2610
+ // Canvas / text
2611
+ width: canvasWidth,
2612
+ height: canvasHeight,
2613
+ canvasWidth,
2614
+ canvasHeight,
2615
+ text,
2616
+ time: time ?? 0,
2617
+ clipStartTime: clipStartTime ?? 0,
2618
+ clipDuration: clipDuration ?? 5,
2619
+ // Font
2620
+ fontFamily: resolveFontFamilyName(effect.font.family),
2621
+ fontWeight: effect.font.weight,
2622
+ fontStyle: effect.font.style,
2623
+ fontSize,
2624
+ letterSpacing: effect.font.letterSpacing,
2625
+ lineHeight: effect.font.lineHeight
2626
+ };
2627
+ if (effect.animation) {
2628
+ config.animation = effect.animation;
2629
+ }
2630
+ if (fill) {
2631
+ if (fill.type !== void 0) config.fillType = fill.type;
2632
+ if (fill.color !== void 0) config.fillColor = fill.color;
2633
+ if (fill.gradient?.angle !== void 0) config.fillGradientAngle = fill.gradient.angle;
2634
+ if (fill.gradient?.stops !== void 0) config.fillGradientStops = fill.gradient.stops;
2635
+ if (fill.patternType !== void 0) config.patternType = fill.patternType;
2636
+ if (fill.perCharFillEnabled !== void 0) config.perCharFillEnabled = fill.perCharFillEnabled;
2637
+ if (fill.charFillColors !== void 0) config.charFillColors = fill.charFillColors;
2638
+ } else {
2639
+ config.fillType = "none";
2640
+ }
2641
+ config.strokeEnabled = !!stroke;
2642
+ if (stroke) {
2643
+ if (stroke.color !== void 0) config.strokeColor = stroke.color;
2644
+ if (stroke.width !== void 0) config.strokeWidth = stroke.width * ratio;
2645
+ if (stroke.position !== void 0) config.strokePosition = stroke.position;
2646
+ if (stroke.opacity !== void 0) config.strokeOpacity = stroke.opacity;
2647
+ if (stroke.lineJoin !== void 0) config.strokeLineJoin = stroke.lineJoin;
2648
+ if (stroke.blur !== void 0) config.strokeBlur = stroke.blur * ratio;
2649
+ if (stroke.type !== void 0) config.strokeType = stroke.type;
2650
+ if (stroke.colorSecondary !== void 0) config.strokeColorSecondary = stroke.colorSecondary;
2651
+ if (stroke.widthSecondary !== void 0) config.strokeWidthSecondary = stroke.widthSecondary * ratio;
2652
+ if (stroke.fadeRange !== void 0) config.strokeFadeRange = stroke.fadeRange;
2653
+ }
2654
+ config.shadowEnabled = !!shadow;
2655
+ if (shadow) {
2656
+ if (shadow.color !== void 0) config.shadowColor = shadow.color;
2657
+ if (shadow.blur !== void 0) config.shadowBlur = shadow.blur * ratio;
2658
+ if (shadow.offset?.x !== void 0) config.shadowOffsetX = shadow.offset.x * ratio;
2659
+ else if (shadow.offsetX !== void 0) config.shadowOffsetX = shadow.offsetX * ratio;
2660
+ if (shadow.offset?.y !== void 0) config.shadowOffsetY = shadow.offset.y * ratio;
2661
+ else if (shadow.offsetY !== void 0) config.shadowOffsetY = shadow.offsetY * ratio;
2662
+ if (shadow.opacity !== void 0) config.shadowOpacity = shadow.opacity;
2663
+ if (shadow.type !== void 0) config.shadowType = shadow.type;
2664
+ }
2665
+ config.bevelEnabled = !!bevel;
2666
+ if (bevel) {
2667
+ if (bevel.depth !== void 0) config.bevelDepth = Math.round(bevel.depth * ratio);
2668
+ if (bevel.highlight !== void 0) config.bevelHighlight = bevel.highlight;
2669
+ else if (bevel.highlightColor !== void 0) config.bevelHighlight = bevel.highlightColor;
2670
+ if (bevel.shadow !== void 0) config.bevelShadow = bevel.shadow;
2671
+ else if (bevel.shadowColor !== void 0) config.bevelShadow = bevel.shadowColor;
2672
+ if (bevel.direction !== void 0) config.bevelDirection = bevel.direction;
2673
+ if (bevel.coreColor !== void 0) config.bevelCoreColor = bevel.coreColor;
2674
+ if (bevel.edgeColor !== void 0) config.bevelEdgeColor = bevel.edgeColor;
2675
+ if (bevel.edgeWidth !== void 0) config.bevelEdgeWidth = bevel.edgeWidth * ratio;
2676
+ if (bevel.blur !== void 0) config.bevelBlur = bevel.blur * ratio;
2677
+ if (bevel.blurColor !== void 0) config.bevelBlurColor = bevel.blurColor;
2678
+ if (bevel.perspectiveEnabled !== void 0) config.bevelPerspectiveEnabled = bevel.perspectiveEnabled;
2679
+ if (bevel.vanishingPointX !== void 0) config.bevelVanishingPointX = bevel.vanishingPointX;
2680
+ if (bevel.vanishingPointY !== void 0) config.bevelVanishingPointY = bevel.vanishingPointY;
2681
+ if (bevel.focalLength !== void 0) config.bevelFocalLength = bevel.focalLength;
2682
+ }
2683
+ if (effect.stack) {
2684
+ config.stackEnabled = !!effect.stack.count;
2685
+ if (effect.stack.count !== void 0) config.stackCount = effect.stack.count;
2686
+ if (effect.stack.offsetX !== void 0) config.stackOffsetX = effect.stack.offsetX * ratio;
2687
+ if (effect.stack.offsetY !== void 0) config.stackOffsetY = effect.stack.offsetY * ratio;
2688
+ if (effect.stack.opacityDecay !== void 0) config.stackOpacityDecay = effect.stack.opacityDecay;
2689
+ if (effect.stack.color1 !== void 0) config.stackColor1 = effect.stack.color1;
2690
+ if (effect.stack.color2 !== void 0) config.stackColor2 = effect.stack.color2;
2691
+ if (effect.stack.color3 !== void 0) config.stackColor3 = effect.stack.color3;
2692
+ if (effect.stack.color4 !== void 0) config.stackColor4 = effect.stack.color4;
2693
+ }
2694
+ config.panelEnabled = !!panel;
2695
+ if (panel) {
2696
+ if (panel.color !== void 0) config.panelColor = panel.color;
2697
+ if (panel.opacity !== void 0) config.panelOpacity = panel.opacity;
2698
+ if (panel.radius !== void 0) config.panelRadius = panel.radius;
2699
+ if (panel.padding?.x !== void 0) config.panelPaddingX = panel.padding.x * ratio;
2700
+ else if (panel.paddingX !== void 0) config.panelPaddingX = panel.paddingX * ratio;
2701
+ if (panel.padding?.y !== void 0) config.panelPaddingY = panel.padding.y * ratio;
2702
+ else if (panel.paddingY !== void 0) config.panelPaddingY = panel.paddingY * ratio;
2703
+ if (panel.stroke !== void 0) {
2704
+ config.panelStrokeEnabled = !!panel.stroke;
2705
+ if (panel.stroke.color !== void 0) config.panelStrokeColor = panel.stroke.color;
2706
+ if (panel.stroke.width !== void 0) config.panelStrokeWidth = panel.stroke.width * ratio;
2707
+ }
2708
+ }
2709
+ if (effect.glows) {
2710
+ config.glowLayers = effect.glows.map((g) => {
2711
+ const mappedGlow = {
2712
+ enabled: true,
2713
+ color: g.color,
2714
+ blur: typeof g.blur === "number" ? g.blur * ratio : g.blur ?? 0,
2715
+ opacity: g.opacity,
2716
+ type: g.type ?? "outer"
2717
+ };
2718
+ if (g.strength !== void 0) mappedGlow.strength = g.strength;
2719
+ if (g.spread !== void 0) mappedGlow.spread = g.spread * ratio;
2720
+ return mappedGlow;
2721
+ });
2722
+ }
2723
+ const rawEffect = effect;
2724
+ if (rawEffect.customRenderer !== void 0) config.customRenderer = rawEffect.customRenderer;
2725
+ if (rawEffect.inkColor !== void 0) config.inkColor = rawEffect.inkColor;
2726
+ if (rawEffect.bristleDensity !== void 0) config.bristleDensity = rawEffect.bristleDensity;
2727
+ if (rawEffect.bristleSkipRate !== void 0) config.bristleSkipRate = rawEffect.bristleSkipRate;
2728
+ if (rawEffect.dripRate !== void 0) config.dripRate = rawEffect.dripRate;
2729
+ if (rawEffect.dripMaxLength !== void 0) config.dripMaxLength = rawEffect.dripMaxLength;
2730
+ if (rawEffect.grainDensity !== void 0) config.grainDensity = rawEffect.grainDensity;
2731
+ if (rawEffect.skewX !== void 0) config.skewX = rawEffect.skewX;
2732
+ const standardKeys = /* @__PURE__ */ new Set([
2733
+ "id",
2734
+ "name",
2735
+ "category",
2736
+ "description",
2737
+ "tags",
2738
+ "font",
2739
+ "fills",
2740
+ "strokes",
2741
+ "shadows",
2742
+ "glows",
2743
+ "glow",
2744
+ "bevel",
2745
+ "panel",
2746
+ "background",
2747
+ "text",
2748
+ "animation",
2749
+ "stack",
2750
+ "boundingBox",
2751
+ "version",
2752
+ "width",
2753
+ "height",
2754
+ "canvasWidth",
2755
+ "canvasHeight",
2756
+ "effectName",
2757
+ "fontFamily",
2758
+ "fontWeight",
2759
+ "fontStyle",
2760
+ "fontSize",
2761
+ "letterSpacing",
2762
+ "lineHeight",
2763
+ "fillType",
2764
+ "fillColor",
2765
+ "fillGradientAngle",
2766
+ "fillGradientStops",
2767
+ "patternType",
2768
+ "perCharFillEnabled",
2769
+ "charFillColors",
2770
+ "strokeEnabled",
2771
+ "strokeColor",
2772
+ "strokeWidth",
2773
+ "strokePosition",
2774
+ "strokeOpacity",
2775
+ "strokeLineJoin",
2776
+ "strokeBlur",
2777
+ "strokeType",
2778
+ "strokeColorSecondary",
2779
+ "strokeWidthSecondary",
2780
+ "strokeFadeRange",
2781
+ "glowLayers",
2782
+ "shadowEnabled",
2783
+ "shadowColor",
2784
+ "shadowBlur",
2785
+ "shadowOffsetX",
2786
+ "shadowOffsetY",
2787
+ "shadowOpacity",
2788
+ "shadowType",
2789
+ "bevelEnabled",
2790
+ "bevelDepth",
2791
+ "bevelHighlight",
2792
+ "bevelShadow",
2793
+ "bevelDirection",
2794
+ "bevelCoreColor",
2795
+ "bevelEdgeColor",
2796
+ "bevelEdgeWidth",
2797
+ "bevelBlur",
2798
+ "bevelBlurColor",
2799
+ "bevelPerspectiveEnabled",
2800
+ "bevelVanishingPointX",
2801
+ "bevelVanishingPointY",
2802
+ "bevelFocalLength",
2803
+ "stackEnabled",
2804
+ "stackCount",
2805
+ "stackOffsetX",
2806
+ "stackOffsetY",
2807
+ "stackOpacityDecay",
2808
+ "stackColor1",
2809
+ "stackColor2",
2810
+ "stackColor3",
2811
+ "stackColor4",
2812
+ "panelEnabled",
2813
+ "panelColor",
2814
+ "panelOpacity",
2815
+ "panelRadius",
2816
+ "panelPaddingX",
2817
+ "panelPaddingY",
2818
+ "panelStrokeEnabled",
2819
+ "panelStrokeColor",
2820
+ "panelStrokeWidth",
2821
+ "textPosX",
2822
+ "textPosY",
2823
+ "autoFitText",
2824
+ "wrapText",
2825
+ "customRenderer",
2826
+ "inkColor",
2827
+ "bristleDensity",
2828
+ "bristleSkipRate",
2829
+ "dripRate",
2830
+ "dripMaxLength",
2831
+ "grainDensity",
2832
+ "skewX"
2833
+ ]);
2834
+ for (const key of Object.keys(effect)) {
2835
+ if (!standardKeys.has(key)) {
2836
+ config[key] = effect[key];
2837
+ }
2838
+ }
2839
+ return config;
2840
+ }
2841
+
2842
+ // src/engine/animation.ts
2843
+ function ease(t, kind = "linear") {
2844
+ switch (kind) {
2845
+ case "easeIn":
2846
+ return t * t;
2847
+ case "easeOut":
2848
+ return t * (2 - t);
2849
+ case "easeInOut":
2850
+ return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
2851
+ default:
2852
+ return t;
2853
+ }
2854
+ }
2855
+ function interpolateKeyframes(keyframes, time) {
2856
+ if (keyframes.length === 0) return 0;
2857
+ const sorted = [...keyframes].sort((a, b) => a.time - b.time);
2858
+ if (time <= sorted[0].time) return sorted[0].value;
2859
+ if (time >= sorted[sorted.length - 1].time) return sorted[sorted.length - 1].value;
2860
+ for (let i = 0; i < sorted.length - 1; i++) {
2861
+ const a = sorted[i];
2862
+ const b = sorted[i + 1];
2863
+ if (time >= a.time && time <= b.time) {
2864
+ const span = b.time - a.time || 1;
2865
+ const localT = (time - a.time) / span;
2866
+ const eased = ease(localT, b.easing ?? "linear");
2867
+ return a.value + (b.value - a.value) * eased;
2868
+ }
2869
+ }
2870
+ return sorted[sorted.length - 1].value;
2871
+ }
2872
+ function resolveAnimatedScalar(doc, layerId, paramPath, baseValue, time) {
2873
+ const track = doc.timeline.tracks.find((t) => t.layerId === layerId && t.paramPath === paramPath);
2874
+ if (!track || track.keyframes.length === 0) return baseValue;
2875
+ return interpolateKeyframes(track.keyframes, time);
2876
+ }
2877
+ function applyTimelineAtTime(doc, time) {
2878
+ const looped = doc.timeline.loop && doc.timeline.duration > 0 ? time % doc.timeline.duration : Math.min(time, doc.timeline.duration);
2879
+ const layers = doc.effectLayers.map((layer2) => {
2880
+ let opacity = layer2.opacity;
2881
+ const params = { ...layer2.params };
2882
+ for (const track of doc.timeline.tracks) {
2883
+ if (track.layerId !== layer2.id) continue;
2884
+ if (track.paramPath === "layerOpacity") {
2885
+ opacity = resolveAnimatedScalar(doc, layer2.id, track.paramPath, opacity, looped);
2886
+ continue;
2887
+ }
2888
+ const parts = track.paramPath.split(".");
2889
+ let target = params;
2890
+ for (let i = 0; i < parts.length - 1; i++) {
2891
+ const key = parts[i];
2892
+ if (typeof target[key] !== "object" || target[key] === null) {
2893
+ target[key] = {};
2894
+ }
2895
+ target = target[key];
2896
+ }
2897
+ const leaf = parts[parts.length - 1];
2898
+ const current = typeof target[leaf] === "number" ? target[leaf] : 0;
2899
+ target[leaf] = resolveAnimatedScalar(doc, layer2.id, track.paramPath, current, looped);
2900
+ }
2901
+ return { ...layer2, opacity, params };
2902
+ });
2903
+ return { ...doc, effectLayers: layers };
2904
+ }
2905
+
2906
+ // src/compositor/index.ts
2907
+ var VERTEX_SRC = `#version 300 es
2908
+ in vec2 a_pos;
2909
+ in vec2 a_uv;
2910
+ out vec2 v_uv;
2911
+ void main() {
2912
+ v_uv = a_uv;
2913
+ gl_Position = vec4(a_pos, 0.0, 1.0);
2914
+ }
2915
+ `;
2916
+ var FRAGMENT_SRC = `#version 300 es
2917
+ precision highp float;
2918
+ in vec2 v_uv;
2919
+ uniform sampler2D u_tex;
2920
+ uniform vec2 u_texel;
2921
+ uniform float u_blur;
2922
+ uniform float u_bloom;
2923
+ uniform float u_bloomThreshold;
2924
+ out vec4 outColor;
2925
+
2926
+ void main() {
2927
+ vec4 c = texture(u_tex, v_uv);
2928
+ if (u_blur < 0.5 && u_bloom < 0.01) {
2929
+ outColor = c;
2930
+ return;
2931
+ }
2932
+ vec4 sum = c;
2933
+ float w = 0.0;
2934
+ if (u_blur >= 0.5) {
2935
+ sum = vec4(0.0);
2936
+ w = 0.0;
2937
+ for (int x = -2; x <= 2; x++) {
2938
+ for (int y = -2; y <= 2; y++) {
2939
+ vec2 off = vec2(float(x), float(y)) * u_texel * u_blur;
2940
+ vec4 s = texture(u_tex, v_uv + off);
2941
+ float weight = 1.0 / (1.0 + float(x*x + y*y));
2942
+ sum += s * weight;
2943
+ w += weight;
2944
+ }
2945
+ }
2946
+ sum /= w;
2947
+ }
2948
+ vec3 bloom = vec3(0.0);
2949
+ if (u_bloom > 0.01) {
2950
+ float lum = dot(sum.rgb, vec3(0.299, 0.587, 0.114));
2951
+ if (lum > u_bloomThreshold) {
2952
+ bloom = sum.rgb * u_bloom * (lum - u_bloomThreshold);
2953
+ }
2954
+ }
2955
+ outColor = vec4(clamp(sum.rgb + bloom, 0.0, 1.0), c.a);
2956
+ }
2957
+ `;
2958
+ var WebGLCompositor = class {
2959
+ gl = null;
2960
+ program = null;
2961
+ vao = null;
2962
+ texture = null;
2963
+ isSupported;
2964
+ constructor() {
2965
+ if (typeof document === "undefined") {
2966
+ this.isSupported = false;
2967
+ return;
2968
+ }
2969
+ const canvas = document.createElement("canvas");
2970
+ canvas.width = 2;
2971
+ canvas.height = 2;
2972
+ const gl = canvas.getContext("webgl2", { premultipliedAlpha: true, alpha: true });
2973
+ if (!gl) {
2974
+ this.isSupported = false;
2975
+ return;
2976
+ }
2977
+ this.gl = gl;
2978
+ this.isSupported = this.initProgram(gl);
2979
+ }
2980
+ initProgram(gl) {
2981
+ const vs = this.compileShader(gl, gl.VERTEX_SHADER, VERTEX_SRC);
2982
+ const fs = this.compileShader(gl, gl.FRAGMENT_SHADER, FRAGMENT_SRC);
2983
+ if (!vs || !fs) return false;
2984
+ const program = gl.createProgram();
2985
+ if (!program) return false;
2986
+ gl.attachShader(program, vs);
2987
+ gl.attachShader(program, fs);
2988
+ gl.linkProgram(program);
2989
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
2990
+ console.warn("Compositor program link failed", gl.getProgramInfoLog(program));
2991
+ return false;
2992
+ }
2993
+ this.program = program;
2994
+ const positions = new Float32Array([-1, -1, 0, 1, 1, -1, 1, 1, -1, 1, 0, 0, 1, 1, 1, 0]);
2995
+ const vao = gl.createVertexArray();
2996
+ const vbo = gl.createBuffer();
2997
+ gl.bindVertexArray(vao);
2998
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
2999
+ gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
3000
+ const locPos = gl.getAttribLocation(program, "a_pos");
3001
+ const locUv = gl.getAttribLocation(program, "a_uv");
3002
+ gl.enableVertexAttribArray(locPos);
3003
+ gl.vertexAttribPointer(locPos, 2, gl.FLOAT, false, 16, 0);
3004
+ gl.enableVertexAttribArray(locUv);
3005
+ gl.vertexAttribPointer(locUv, 2, gl.FLOAT, false, 16, 8);
3006
+ gl.bindVertexArray(null);
3007
+ this.vao = vao;
3008
+ this.texture = gl.createTexture();
3009
+ return true;
3010
+ }
3011
+ compileShader(gl, type, source) {
3012
+ const shader = gl.createShader(type);
3013
+ if (!shader) return null;
3014
+ gl.shaderSource(shader, source);
3015
+ gl.compileShader(shader);
3016
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
3017
+ console.warn(gl.getShaderInfoLog(shader));
3018
+ return null;
3019
+ }
3020
+ return shader;
3021
+ }
3022
+ renderToContext(targetCtx, source, settings) {
3023
+ const gl = this.gl;
3024
+ const program = this.program;
3025
+ if (!gl || !program || !this.isSupported) {
3026
+ targetCtx.clearRect(0, 0, source.width, source.height);
3027
+ targetCtx.drawImage(source, 0, 0);
3028
+ return;
3029
+ }
3030
+ const w = source.width;
3031
+ const h = source.height;
3032
+ const canvas = gl.canvas;
3033
+ if (canvas.width !== w || canvas.height !== h) {
3034
+ canvas.width = w;
3035
+ canvas.height = h;
3036
+ }
3037
+ gl.viewport(0, 0, w, h);
3038
+ gl.clearColor(0, 0, 0, 0);
3039
+ gl.clear(gl.COLOR_BUFFER_BIT);
3040
+ gl.useProgram(program);
3041
+ gl.activeTexture(gl.TEXTURE0);
3042
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
3043
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
3044
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
3045
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
3046
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
3047
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
3048
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
3049
+ gl.uniform1i(gl.getUniformLocation(program, "u_tex"), 0);
3050
+ gl.uniform2f(gl.getUniformLocation(program, "u_texel"), 1 / w, 1 / h);
3051
+ gl.uniform1f(gl.getUniformLocation(program, "u_blur"), settings.blur ?? 0);
3052
+ gl.uniform1f(gl.getUniformLocation(program, "u_bloom"), settings.bloom ?? 0);
3053
+ gl.uniform1f(gl.getUniformLocation(program, "u_bloomThreshold"), settings.bloomThreshold ?? 0.6);
3054
+ gl.bindVertexArray(this.vao);
3055
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
3056
+ gl.bindVertexArray(null);
3057
+ targetCtx.clearRect(0, 0, w, h);
3058
+ targetCtx.drawImage(canvas, 0, 0);
3059
+ }
3060
+ dispose() {
3061
+ const gl = this.gl;
3062
+ if (!gl) return;
3063
+ if (this.texture) gl.deleteTexture(this.texture);
3064
+ if (this.program) gl.deleteProgram(this.program);
3065
+ if (this.vao) gl.deleteVertexArray(this.vao);
3066
+ this.gl = null;
3067
+ }
3068
+ };
3069
+
3070
+ // src/engine/mask.ts
3071
+ function applyMaskReveal(ctx, doc, width, height) {
3072
+ const maskLayer = doc.effectLayers.find((l) => l.type === "mask" && l.enabled);
3073
+ if (!maskLayer) return;
3074
+ const maskType = maskLayer.params.maskType || "alphaText";
3075
+ const progress = Math.max(0, Math.min(1, maskLayer.params.revealProgress ?? 1));
3076
+ if (progress >= 1) return;
3077
+ ctx.save();
3078
+ ctx.globalCompositeOperation = "destination-in";
3079
+ if (maskType === "rectReveal") {
3080
+ const w = width * progress;
3081
+ ctx.fillStyle = "#ffffff";
3082
+ ctx.fillRect(0, 0, w, height);
3083
+ } else {
3084
+ const w = width * progress;
3085
+ const grad = ctx.createLinearGradient(0, 0, width, 0);
3086
+ grad.addColorStop(0, "rgba(255,255,255,1)");
3087
+ grad.addColorStop(Math.max(0, progress - 0.02), "rgba(255,255,255,1)");
3088
+ grad.addColorStop(Math.min(1, progress + 0.02), "rgba(255,255,255,0)");
3089
+ grad.addColorStop(1, "rgba(255,255,255,0)");
3090
+ ctx.fillStyle = grad;
3091
+ ctx.fillRect(0, 0, width, height);
3092
+ }
3093
+ ctx.restore();
3094
+ }
3095
+
3096
+ // src/engine/evaluate.ts
3097
+ var sharedCompositor = null;
3098
+ function getCompositor() {
3099
+ if (typeof document === "undefined") return null;
3100
+ if (!sharedCompositor) {
3101
+ sharedCompositor = new WebGLCompositor();
3102
+ }
3103
+ return sharedCompositor;
3104
+ }
3105
+ function evaluateScene(doc, time, ctx, options = {}) {
3106
+ const animated = applyTimelineAtTime(doc, time);
3107
+ const cfg = sceneToConfig(animated);
3108
+ const w = cfg.canvasWidth || DEFAULT_CANVAS_WIDTH;
3109
+ const h = cfg.canvasHeight || DEFAULT_CANVAS_HEIGHT;
3110
+ const filterLayers = animated.effectLayers.filter((l) => l.type === "filter" && l.enabled);
3111
+ const lastFilter = filterLayers[filterLayers.length - 1]?.params;
3112
+ const comp = {
3113
+ blur: lastFilter?.blur ?? animated.compositor.blur ?? 0,
3114
+ bloom: lastFilter?.bloom ?? animated.compositor.bloom ?? 0,
3115
+ bloomThreshold: animated.compositor.bloomThreshold ?? 0.6
3116
+ };
3117
+ const usePostFx = !options.skipPostFx && (comp.blur > 0 || comp.bloom > 0);
3118
+ const finishFrame = () => applyMaskReveal(ctx, animated, w, h);
3119
+ if (!usePostFx) {
3120
+ ctx.clearRect(0, 0, w, h);
3121
+ renderTextEffectCore(ctx, cfg);
3122
+ finishFrame();
3123
+ return;
3124
+ }
3125
+ const temp = CanvasDevice.acquire(w, h);
3126
+ const tctx = temp.getContext("2d");
3127
+ if (tctx) {
3128
+ tctx.clearRect(0, 0, w, h);
3129
+ renderTextEffectCore(tctx, cfg);
3130
+ applyMaskReveal(tctx, animated, w, h);
3131
+ const compositor = options.compositor ?? getCompositor();
3132
+ if (compositor?.isSupported) {
3133
+ compositor.renderToContext(ctx, temp, comp);
3134
+ } else {
3135
+ ctx.clearRect(0, 0, w, h);
3136
+ ctx.drawImage(temp, 0, 0);
3137
+ }
3138
+ CanvasDevice.release(temp);
3139
+ return;
3140
+ }
3141
+ CanvasDevice.release(temp);
3142
+ renderTextEffectCore(ctx, cfg);
3143
+ finishFrame();
3144
+ }
3145
+
3146
+ // src/fontLoader.ts
3147
+ var FontLoader = class {
3148
+ state = {
3149
+ loading: /* @__PURE__ */ new Set(),
3150
+ loaded: /* @__PURE__ */ new Set(),
3151
+ failed: /* @__PURE__ */ new Map(),
3152
+ promises: /* @__PURE__ */ new Map()
3153
+ };
3154
+ async ensureFont(descriptor) {
3155
+ const resolvedFamily = resolveFontFamilyName(descriptor.family);
3156
+ const resolvedDescriptor = { ...descriptor, family: resolvedFamily };
3157
+ const key = this.getFontKey(resolvedDescriptor);
3158
+ if (this.state.loaded.has(key)) {
3159
+ return {
3160
+ font: descriptor,
3161
+ loaded: true,
3162
+ loadTimeMs: 0
3163
+ };
3164
+ }
3165
+ if (this.state.failed.has(key)) {
3166
+ return {
3167
+ font: descriptor,
3168
+ loaded: false,
3169
+ error: this.state.failed.get(key),
3170
+ loadTimeMs: 0
3171
+ };
3172
+ }
3173
+ if (this.state.promises.has(key)) {
3174
+ return this.state.promises.get(key);
3175
+ }
3176
+ const promise = this.loadFont(resolvedDescriptor).then((res) => ({
3177
+ ...res,
3178
+ font: descriptor
3179
+ }));
3180
+ this.state.promises.set(key, promise);
3181
+ return promise;
3182
+ }
3183
+ async ensureFonts(descriptors) {
3184
+ return Promise.all(descriptors.map((desc) => this.ensureFont(desc)));
3185
+ }
3186
+ async waitForFontsReady() {
3187
+ if (typeof document === "undefined" || !document.fonts) {
3188
+ return;
3189
+ }
3190
+ await document.fonts.ready;
3191
+ }
3192
+ isLoaded(descriptor) {
3193
+ const key = this.getFontKey(descriptor);
3194
+ return this.state.loaded.has(key);
3195
+ }
3196
+ getStats() {
3197
+ return {
3198
+ loaded: this.state.loaded.size,
3199
+ loading: this.state.loading.size,
3200
+ failed: this.state.failed.size
3201
+ };
3202
+ }
3203
+ clear() {
3204
+ this.state.loading.clear();
3205
+ this.state.loaded.clear();
3206
+ this.state.failed.clear();
3207
+ this.state.promises.clear();
3208
+ }
3209
+ async loadFont(descriptor) {
3210
+ const key = this.getFontKey(descriptor);
3211
+ const startTime = performance.now();
3212
+ this.state.loading.add(key);
3213
+ const SYSTEM_FONTS = /* @__PURE__ */ new Set([
3214
+ "arial",
3215
+ "georgia",
3216
+ "times new roman",
3217
+ "courier new",
3218
+ "arial rounded mt bold",
3219
+ "impact",
3220
+ "helvetica",
3221
+ "sans-serif",
3222
+ "serif",
3223
+ "monospace"
3224
+ ]);
3225
+ const isGoogleFont = (family) => {
3226
+ return !SYSTEM_FONTS.has(family.toLowerCase());
3227
+ };
3228
+ const injectGoogleFontLink = (family) => {
3229
+ if (typeof document === "undefined") return;
3230
+ const fontId = `gfont-${family.replace(/\s+/g, "-").toLowerCase()}`;
3231
+ if (document.getElementById(fontId)) return;
3232
+ const link = document.createElement("link");
3233
+ link.id = fontId;
3234
+ link.rel = "stylesheet";
3235
+ link.href = `https://fonts.googleapis.com/css2?family=${family.replace(/\s+/g, "+")}:wght@400;500;600;700;800;900&display=swap`;
3236
+ document.head.appendChild(link);
3237
+ console.log(`[FontLoader] Dynamically injected Google Fonts stylesheet for: ${family}`);
3238
+ };
3239
+ try {
3240
+ if (typeof document === "undefined" || !document.fonts) {
3241
+ throw new Error("Font API not available");
3242
+ }
3243
+ const weight = this.normalizeFontWeight(descriptor.weight);
3244
+ const style = descriptor.style || "normal";
3245
+ const fontFace = `${style} ${weight} 16px "${descriptor.family}"`;
3246
+ if (isGoogleFont(descriptor.family)) {
3247
+ injectGoogleFontLink(descriptor.family);
3248
+ }
3249
+ if (document.fonts.check(fontFace)) {
3250
+ this.state.loaded.add(key);
3251
+ this.state.loading.delete(key);
3252
+ this.state.promises.delete(key);
3253
+ return {
3254
+ font: descriptor,
3255
+ loaded: true,
3256
+ loadTimeMs: performance.now() - startTime
3257
+ };
3258
+ }
3259
+ await document.fonts.load(fontFace);
3260
+ if (!document.fonts.check(fontFace)) {
3261
+ const fallbackFontFace = `${style} 400 16px "${descriptor.family}"`;
3262
+ if (weight !== 400 && document.fonts.check(fallbackFontFace)) {
3263
+ console.log(`[FontLoader] Strict check failed for "${fontFace}", but weight 400 is loaded. Accepting fallback.`);
3264
+ } else {
3265
+ throw new Error(`Font "${descriptor.family}" failed to load`);
3266
+ }
3267
+ }
3268
+ this.state.loaded.add(key);
3269
+ this.state.loading.delete(key);
3270
+ this.state.promises.delete(key);
3271
+ return {
3272
+ font: descriptor,
3273
+ loaded: true,
3274
+ loadTimeMs: performance.now() - startTime
3275
+ };
3276
+ } catch (error) {
3277
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
3278
+ this.state.failed.set(key, errorMessage);
3279
+ this.state.loading.delete(key);
3280
+ this.state.promises.delete(key);
3281
+ return {
3282
+ font: descriptor,
3283
+ loaded: false,
3284
+ error: errorMessage,
3285
+ loadTimeMs: performance.now() - startTime
3286
+ };
3287
+ }
3288
+ }
3289
+ getFontKey(descriptor) {
3290
+ const weight = this.normalizeFontWeight(descriptor.weight);
3291
+ const style = descriptor.style || "normal";
3292
+ return `${descriptor.family}|${weight}|${style}`;
3293
+ }
3294
+ normalizeFontWeight(weight) {
3295
+ if (typeof weight === "number") {
3296
+ return weight;
3297
+ }
3298
+ if (!weight) return 400;
3299
+ const asNum = parseInt(weight, 10);
3300
+ if (!isNaN(asNum) && asNum >= 100 && asNum <= 900) {
3301
+ return asNum;
3302
+ }
3303
+ const weightMap = {
3304
+ normal: 400,
3305
+ bold: 700,
3306
+ lighter: 300,
3307
+ bolder: 700
3308
+ };
3309
+ return weightMap[weight] ?? 400;
3310
+ }
3311
+ };
3312
+ var globalFontLoader = null;
3313
+ function getFontLoader() {
3314
+ if (!globalFontLoader) {
3315
+ globalFontLoader = new FontLoader();
3316
+ }
3317
+ return globalFontLoader;
3318
+ }
3319
+ function resetFontLoader() {
3320
+ globalFontLoader = null;
3321
+ }
3322
+ async function ensureFontsLoaded(descriptors) {
3323
+ const loader = getFontLoader();
3324
+ return loader.ensureFonts(descriptors);
3325
+ }
3326
+ // Annotate the CommonJS export names for ESM import in node:
3327
+ 0 && (module.exports = {
3328
+ FontLoader,
3329
+ _buildConfig,
3330
+ builtInPresets,
3331
+ defaultConfig,
3332
+ ensureFontsLoaded,
3333
+ evaluateScene,
3334
+ getFontLoader,
3335
+ renderTextEffectCore,
3336
+ resetFontLoader,
3337
+ resolveFontFamilyName,
3338
+ sceneToConfig,
3339
+ textEffectConfigToScene
3340
+ });
3341
+ //# sourceMappingURL=index.cjs.map