@clypra-studio/engine 1.10.0 → 1.10.2

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.
@@ -2052,6 +2052,620 @@ function renderTextTemplateToCanvas(ctx, input) {
2052
2052
  };
2053
2053
  }
2054
2054
 
2055
+ // src/rendering/textBubbles.ts
2056
+ function resolveBubbleLayout(textWidth, textHeight, layout) {
2057
+ const p = layout.padding ?? {
2058
+ top: 14,
2059
+ right: 18,
2060
+ bottom: 14,
2061
+ left: 18
2062
+ };
2063
+ const rawOuterW = textWidth + p.left + p.right;
2064
+ const rawOuterH = textHeight + p.top + p.bottom;
2065
+ let outerWidth = Math.max(rawOuterW, layout.minWidth);
2066
+ let outerHeight = Math.max(rawOuterH, layout.minHeight);
2067
+ if (layout.preserveAspectRatio && layout.minWidth > 0 && layout.minHeight > 0) {
2068
+ const aspect = layout.minWidth / layout.minHeight;
2069
+ const currentAspect = outerWidth / outerHeight;
2070
+ if (currentAspect > aspect) {
2071
+ outerHeight = outerWidth / aspect;
2072
+ } else {
2073
+ outerWidth = outerHeight * aspect;
2074
+ }
2075
+ }
2076
+ const textOffsetX = (p.left - p.right) / 2;
2077
+ const textOffsetY = (p.top - p.bottom) / 2;
2078
+ return {
2079
+ outerWidth,
2080
+ outerHeight,
2081
+ textOffsetX,
2082
+ textOffsetY,
2083
+ contentWidth: outerWidth - p.left - p.right,
2084
+ contentHeight: outerHeight - p.top - p.bottom
2085
+ };
2086
+ }
2087
+
2088
+ // src/rendering/bubbleRenderer.ts
2089
+ function pseudoNoise(seed) {
2090
+ const x = Math.sin(seed * 12.9898 + 78.233) * 43758.5453;
2091
+ return x - Math.floor(x);
2092
+ }
2093
+ function drawTornPaper(ctx, x, y, width, height, fillColor = "#faf9f6", shadow = true) {
2094
+ ctx.save();
2095
+ if (shadow) {
2096
+ ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
2097
+ ctx.shadowBlur = 12;
2098
+ ctx.shadowOffsetX = 0;
2099
+ ctx.shadowOffsetY = 4;
2100
+ }
2101
+ ctx.fillStyle = fillColor;
2102
+ ctx.beginPath();
2103
+ const steps = Math.max(16, Math.floor(width / 14));
2104
+ const stepW = width / steps;
2105
+ ctx.moveTo(x, y + 6);
2106
+ for (let i = 0; i <= steps; i++) {
2107
+ const curX = x + i * stepW;
2108
+ const n = pseudoNoise(i * 1.37 + 10);
2109
+ const wave = Math.sin(i / steps * Math.PI * 3) * 3;
2110
+ const curY = y + (n * 5 - 2.5) + wave;
2111
+ if (i === 0) ctx.lineTo(curX, curY);
2112
+ else ctx.lineTo(curX, curY);
2113
+ }
2114
+ ctx.lineTo(x + width, y + height - 6);
2115
+ for (let i = steps; i >= 0; i--) {
2116
+ const curX = x + i * stepW;
2117
+ const n = pseudoNoise(i * 2.19 + 50);
2118
+ const wave = Math.sin(i / steps * Math.PI * 2) * 4;
2119
+ const curY = y + height + (n * 6 - 3) + wave;
2120
+ ctx.lineTo(curX, curY);
2121
+ }
2122
+ ctx.lineTo(x, y + 6);
2123
+ ctx.closePath();
2124
+ ctx.fill();
2125
+ if (shadow) {
2126
+ ctx.shadowColor = "transparent";
2127
+ ctx.shadowBlur = 0;
2128
+ }
2129
+ const paperGrad = ctx.createLinearGradient(x, y, x, y + height);
2130
+ paperGrad.addColorStop(0, "rgba(255, 255, 255, 0.4)");
2131
+ paperGrad.addColorStop(0.5, "rgba(0, 0, 0, 0.02)");
2132
+ paperGrad.addColorStop(1, "rgba(0, 0, 0, 0.06)");
2133
+ ctx.fillStyle = paperGrad;
2134
+ ctx.fill();
2135
+ ctx.restore();
2136
+ }
2137
+ function drawSpeechBubble(ctx, x, y, width, height, fillColor = "#ffffff", strokeColor = "#000000", strokeWidth = 2, radius = 16) {
2138
+ ctx.save();
2139
+ const tailH = Math.min(16, height * 0.25);
2140
+ const bodyH = height - tailH;
2141
+ ctx.fillStyle = fillColor;
2142
+ ctx.strokeStyle = strokeColor;
2143
+ ctx.lineWidth = strokeWidth;
2144
+ ctx.beginPath();
2145
+ if (typeof ctx.roundRect === "function") {
2146
+ ctx.roundRect(x, y, width, bodyH, radius);
2147
+ } else {
2148
+ ctx.rect(x, y, width, bodyH);
2149
+ }
2150
+ ctx.fill();
2151
+ if (strokeWidth > 0) ctx.stroke();
2152
+ const tailBaseX = x + width * 0.25;
2153
+ const tailBaseW = Math.min(24, width * 0.2);
2154
+ const tailTipX = x + width * 0.15;
2155
+ const tailTipY = y + height;
2156
+ ctx.beginPath();
2157
+ ctx.moveTo(tailBaseX, y + bodyH - 2);
2158
+ ctx.quadraticCurveTo(tailBaseX + 2, y + bodyH + tailH * 0.6, tailTipX, tailTipY);
2159
+ ctx.quadraticCurveTo(tailBaseX + tailBaseW * 0.7, y + bodyH + tailH * 0.4, tailBaseX + tailBaseW, y + bodyH - 2);
2160
+ ctx.closePath();
2161
+ ctx.fill();
2162
+ if (strokeWidth > 0) ctx.stroke();
2163
+ ctx.beginPath();
2164
+ if (typeof ctx.roundRect === "function") {
2165
+ ctx.roundRect(x + 1, y + 1, width - 2, bodyH - 2, radius);
2166
+ } else {
2167
+ ctx.rect(x + 1, y + 1, width - 2, bodyH - 2);
2168
+ }
2169
+ ctx.fill();
2170
+ ctx.restore();
2171
+ }
2172
+ function drawConfigurableSpeechBubble(ctx, x, y, width, height, appearance) {
2173
+ const tailHeight = Math.min(appearance.tailSize, height * 0.38);
2174
+ const bodyHeight = height - tailHeight;
2175
+ const radius = Math.min(appearance.cornerRadius, bodyHeight / 2);
2176
+ const tailCenter = appearance.tailPosition === "bottom-left" ? x + width * 0.2 : appearance.tailPosition === "bottom-right" ? x + width * 0.8 : x + width * 0.5;
2177
+ const tailWidth = Math.min(Math.max(18, appearance.tailSize * 1.5), width * 0.28);
2178
+ ctx.save();
2179
+ ctx.globalAlpha = Math.max(0, Math.min(1, appearance.fillOpacity));
2180
+ ctx.fillStyle = appearance.fillColor;
2181
+ ctx.strokeStyle = appearance.strokeColor;
2182
+ ctx.lineWidth = Math.max(0, appearance.strokeWidth);
2183
+ ctx.beginPath();
2184
+ if (typeof ctx.roundRect === "function") {
2185
+ ctx.roundRect(x, y, width, bodyHeight, radius);
2186
+ } else {
2187
+ ctx.rect(x, y, width, bodyHeight);
2188
+ }
2189
+ ctx.fill();
2190
+ if (appearance.strokeWidth > 0) ctx.stroke();
2191
+ ctx.beginPath();
2192
+ ctx.moveTo(tailCenter - tailWidth / 2, y + bodyHeight - 1);
2193
+ ctx.quadraticCurveTo(tailCenter - tailWidth * 0.15, y + bodyHeight + tailHeight * 0.7, tailCenter, y + height);
2194
+ ctx.quadraticCurveTo(tailCenter + tailWidth * 0.18, y + bodyHeight + tailHeight * 0.55, tailCenter + tailWidth / 2, y + bodyHeight - 1);
2195
+ ctx.closePath();
2196
+ ctx.fill();
2197
+ if (appearance.strokeWidth > 0) ctx.stroke();
2198
+ ctx.restore();
2199
+ }
2200
+ function renderBubblePresetBackground(ctx, preset, x, y, width, height) {
2201
+ const appearance = preset.appearance;
2202
+ if (!appearance) return renderBubbleBackground(ctx, preset.id, x, y, width, height);
2203
+ ctx.save();
2204
+ switch (appearance.shape) {
2205
+ case "chalk":
2206
+ drawChalkSpeechBubble(ctx, x, y, width, height);
2207
+ break;
2208
+ case "cloud":
2209
+ drawThoughtCloud(ctx, x, y, width, height, appearance.fillColor, appearance.strokeColor, appearance.strokeWidth);
2210
+ break;
2211
+ case "comic":
2212
+ drawComicBurst(ctx, x, y, width, height, appearance.fillColor, appearance.strokeColor, appearance.strokeWidth);
2213
+ break;
2214
+ case "paper":
2215
+ drawTornPaper(ctx, x, y, width, height, appearance.fillColor, true);
2216
+ break;
2217
+ case "ribbon":
2218
+ drawRibbonBanner(ctx, x, y, width, height, appearance.fillColor, appearance.strokeColor, appearance.strokeWidth);
2219
+ break;
2220
+ case "speech":
2221
+ drawConfigurableSpeechBubble(ctx, x, y, width, height, appearance);
2222
+ break;
2223
+ case "rounded": {
2224
+ ctx.globalAlpha = Math.max(0, Math.min(1, appearance.fillOpacity));
2225
+ ctx.fillStyle = appearance.fillColor;
2226
+ ctx.strokeStyle = appearance.strokeColor;
2227
+ ctx.lineWidth = Math.max(0, appearance.strokeWidth);
2228
+ ctx.beginPath();
2229
+ if (typeof ctx.roundRect === "function") {
2230
+ ctx.roundRect(x, y, width, height, Math.min(appearance.cornerRadius, height / 2));
2231
+ } else {
2232
+ ctx.rect(x, y, width, height);
2233
+ }
2234
+ ctx.fill();
2235
+ if (appearance.strokeWidth > 0) ctx.stroke();
2236
+ break;
2237
+ }
2238
+ }
2239
+ if (appearance.texture === "chalk") {
2240
+ ctx.globalAlpha = 0.55;
2241
+ ctx.strokeStyle = appearance.strokeColor;
2242
+ ctx.setLineDash([1, 3]);
2243
+ ctx.lineWidth = Math.max(1, appearance.strokeWidth * 0.7);
2244
+ ctx.strokeRect(x + 1, y + 1, width - 2, height - 2);
2245
+ } else if (appearance.texture === "hatch") {
2246
+ ctx.globalAlpha = 0.22;
2247
+ ctx.strokeStyle = appearance.strokeColor;
2248
+ ctx.lineWidth = 1;
2249
+ for (let offset = -height; offset < width; offset += 9) {
2250
+ ctx.beginPath();
2251
+ ctx.moveTo(x + offset, y + height);
2252
+ ctx.lineTo(x + offset + height, y);
2253
+ ctx.stroke();
2254
+ }
2255
+ }
2256
+ ctx.restore();
2257
+ return true;
2258
+ }
2259
+ function drawChalkSpeechBubble(ctx, x, y, width, height) {
2260
+ ctx.save();
2261
+ const tailHeight = Math.min(46, height * 0.3);
2262
+ const bodyHeight = height - tailHeight;
2263
+ const radius = Math.min(36, bodyHeight * 0.25);
2264
+ const tailLeft = x + width * 0.1;
2265
+ const tailRight = x + width * 0.19;
2266
+ const tailTipX = x + width * 0.14;
2267
+ const tailTipY = y + height;
2268
+ const balloonPath = () => {
2269
+ ctx.beginPath();
2270
+ ctx.moveTo(x + radius, y);
2271
+ ctx.lineTo(x + width - radius, y);
2272
+ ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
2273
+ ctx.lineTo(x + width, y + bodyHeight - radius);
2274
+ ctx.quadraticCurveTo(x + width, y + bodyHeight, x + width - radius, y + bodyHeight);
2275
+ ctx.lineTo(tailRight, y + bodyHeight);
2276
+ ctx.quadraticCurveTo(tailRight - width * 0.02, y + bodyHeight + tailHeight * 0.35, tailTipX, tailTipY);
2277
+ ctx.quadraticCurveTo(tailLeft + width * 5e-3, y + bodyHeight + tailHeight * 0.32, tailLeft, y + bodyHeight);
2278
+ ctx.lineTo(x + radius, y + bodyHeight);
2279
+ ctx.quadraticCurveTo(x, y + bodyHeight, x, y + bodyHeight - radius);
2280
+ ctx.lineTo(x, y + radius);
2281
+ ctx.quadraticCurveTo(x, y, x + radius, y);
2282
+ ctx.closePath();
2283
+ };
2284
+ ctx.fillStyle = "rgba(184, 122, 132, 0.58)";
2285
+ balloonPath();
2286
+ ctx.fill();
2287
+ ctx.save();
2288
+ ctx.beginPath();
2289
+ ctx.moveTo(x + width * 0.2, y + bodyHeight - tailHeight * 0.13);
2290
+ ctx.quadraticCurveTo(x + width * 0.48, y + bodyHeight + tailHeight * 0.37, x + width * 0.91, y + bodyHeight - tailHeight * 0.02);
2291
+ ctx.quadraticCurveTo(x + width * 0.66, y + bodyHeight + tailHeight * 0.72, x + width * 0.24, y + bodyHeight + tailHeight * 0.44);
2292
+ ctx.closePath();
2293
+ ctx.clip();
2294
+ ctx.shadowColor = "rgba(222, 228, 105, 0.32)";
2295
+ ctx.shadowBlur = 3;
2296
+ ctx.strokeStyle = "rgba(203, 211, 76, 0.52)";
2297
+ ctx.lineWidth = 1.25;
2298
+ for (let offset = -height; offset < width; offset += 9) {
2299
+ ctx.beginPath();
2300
+ ctx.moveTo(x + offset, y + height);
2301
+ ctx.lineTo(x + offset + height, y);
2302
+ ctx.stroke();
2303
+ }
2304
+ ctx.shadowBlur = 0;
2305
+ ctx.strokeStyle = "rgba(255, 250, 196, 0.48)";
2306
+ ctx.lineWidth = 0.8;
2307
+ for (let offset = 0; offset < width + height; offset += 12) {
2308
+ ctx.beginPath();
2309
+ ctx.moveTo(x + offset, y);
2310
+ ctx.lineTo(x + offset - height, y + height);
2311
+ ctx.stroke();
2312
+ }
2313
+ ctx.restore();
2314
+ ctx.strokeStyle = "rgba(255, 253, 246, 0.82)";
2315
+ ctx.lineCap = "round";
2316
+ ctx.lineWidth = 1.35;
2317
+ balloonPath();
2318
+ ctx.stroke();
2319
+ const edgeSegments = 66;
2320
+ for (let index = 0; index < edgeSegments; index++) {
2321
+ const pointX = x + width * (index / edgeSegments);
2322
+ const topNoise = (pseudoNoise(index + 91) - 0.5) * 4;
2323
+ const strokeLength = 0.8 + pseudoNoise(index + 143) * 2.1;
2324
+ ctx.lineWidth = 0.6 + pseudoNoise(index + 211) * 1.1;
2325
+ ctx.beginPath();
2326
+ ctx.moveTo(pointX, y + topNoise);
2327
+ ctx.lineTo(pointX + strokeLength, y + topNoise + (pseudoNoise(index + 19) - 0.5) * 2);
2328
+ ctx.stroke();
2329
+ ctx.beginPath();
2330
+ ctx.moveTo(pointX, y + bodyHeight + topNoise);
2331
+ ctx.lineTo(pointX + strokeLength, y + bodyHeight + topNoise + (pseudoNoise(index + 37) - 0.5) * 2);
2332
+ ctx.stroke();
2333
+ }
2334
+ ctx.restore();
2335
+ }
2336
+ function drawThoughtCloud(ctx, x, y, width, height, fillColor = "#ffffff", strokeColor = "#000000", strokeWidth = 2) {
2337
+ ctx.save();
2338
+ ctx.fillStyle = fillColor;
2339
+ ctx.strokeStyle = strokeColor;
2340
+ ctx.lineWidth = strokeWidth;
2341
+ const cx = x + width / 2;
2342
+ const cy = y + height / 2;
2343
+ const rx = width * 0.44;
2344
+ const ry = height * 0.38;
2345
+ ctx.beginPath();
2346
+ const lobes = 8;
2347
+ for (let i = 0; i < lobes; i++) {
2348
+ const angle = i / lobes * Math.PI * 2;
2349
+ const lx = cx + Math.cos(angle) * rx;
2350
+ const ly = cy + Math.sin(angle) * ry;
2351
+ const lobeR = Math.min(width, height) * 0.28;
2352
+ ctx.moveTo(lx + lobeR, ly);
2353
+ ctx.arc(lx, ly, lobeR, 0, Math.PI * 2);
2354
+ }
2355
+ ctx.fill();
2356
+ if (strokeWidth > 0) ctx.stroke();
2357
+ ctx.restore();
2358
+ }
2359
+ function drawComicBurst(ctx, x, y, width, height, fillColor = "#ffffff", strokeColor = "#000000", strokeWidth = 3) {
2360
+ ctx.save();
2361
+ ctx.fillStyle = fillColor;
2362
+ ctx.strokeStyle = strokeColor;
2363
+ ctx.lineWidth = strokeWidth;
2364
+ const cx = x + width / 2;
2365
+ const cy = y + height / 2;
2366
+ const points = 16;
2367
+ const outerRx = width / 2;
2368
+ const outerRy = height / 2;
2369
+ const innerRx = width * 0.32;
2370
+ const innerRy = height * 0.32;
2371
+ ctx.beginPath();
2372
+ for (let i = 0; i < points * 2; i++) {
2373
+ const angle = i / (points * 2) * Math.PI * 2 - Math.PI / 2;
2374
+ const isOuter = i % 2 === 0;
2375
+ const rx = isOuter ? outerRx : innerRx;
2376
+ const ry = isOuter ? outerRy : innerRy;
2377
+ const px = cx + Math.cos(angle) * rx;
2378
+ const py = cy + Math.sin(angle) * ry;
2379
+ if (i === 0) ctx.moveTo(px, py);
2380
+ else ctx.lineTo(px, py);
2381
+ }
2382
+ ctx.closePath();
2383
+ ctx.fill();
2384
+ if (strokeWidth > 0) ctx.stroke();
2385
+ ctx.restore();
2386
+ }
2387
+ function drawRibbonBanner(ctx, x, y, width, height, fillColor = "#facc15", strokeColor = "#000000", strokeWidth = 2) {
2388
+ ctx.save();
2389
+ ctx.fillStyle = fillColor;
2390
+ ctx.strokeStyle = strokeColor;
2391
+ ctx.lineWidth = strokeWidth;
2392
+ const notch = Math.min(18, width * 0.08);
2393
+ ctx.beginPath();
2394
+ ctx.moveTo(x, y);
2395
+ ctx.lineTo(x + width - notch, y);
2396
+ ctx.lineTo(x + width, y + height / 2);
2397
+ ctx.lineTo(x + width - notch, y + height);
2398
+ ctx.lineTo(x, y + height);
2399
+ ctx.lineTo(x + notch, y + height / 2);
2400
+ ctx.closePath();
2401
+ ctx.fill();
2402
+ if (strokeWidth > 0) ctx.stroke();
2403
+ ctx.restore();
2404
+ }
2405
+ function renderBubbleBackground(ctx, presetId, x, y, width, height, options) {
2406
+ if (!presetId || presetId === "none") return false;
2407
+ const color = options?.color || "#ffffff";
2408
+ const opacity = options?.opacity ?? 1;
2409
+ ctx.save();
2410
+ if (opacity < 1) {
2411
+ ctx.globalAlpha *= opacity;
2412
+ }
2413
+ if (presetId === "bubble-paper-torn" || presetId === "bubble-paper-torn-beige" || presetId === "bubble-paper-kraft" || presetId === "bubble-paper-notebook") {
2414
+ const paperColor = presetId === "bubble-paper-kraft" ? "#d4a373" : presetId === "bubble-paper-torn-beige" ? "#f3eed9" : "#faf9f6";
2415
+ drawTornPaper(ctx, x, y, width, height, color || paperColor, true);
2416
+ ctx.restore();
2417
+ return true;
2418
+ }
2419
+ if (presetId === "bubble-speech-round" || presetId === "bubble-speech-oval" || presetId === "bubble-speech-tall") {
2420
+ drawSpeechBubble(ctx, x, y, width, height, color, "#000000", 2, 16);
2421
+ ctx.restore();
2422
+ return true;
2423
+ }
2424
+ if (presetId === "bubble-speech-chalk-rose") {
2425
+ drawChalkSpeechBubble(ctx, x, y, width, height);
2426
+ ctx.restore();
2427
+ return true;
2428
+ }
2429
+ if (presetId === "bubble-cloud-dotted" || presetId === "bubble-cloud-morning" || presetId === "bubble-cloud-yellow") {
2430
+ drawThoughtCloud(ctx, x, y, width, height, color, "#000000", 2);
2431
+ ctx.restore();
2432
+ return true;
2433
+ }
2434
+ if (presetId === "bubble-comic-bam" || presetId === "bubble-comic-dark-hero") {
2435
+ const burstColor = presetId === "bubble-comic-dark-hero" ? "#18181b" : color;
2436
+ const strokeColor = presetId === "bubble-comic-dark-hero" ? "#ef4444" : "#000000";
2437
+ drawComicBurst(ctx, x, y, width, height, burstColor, strokeColor, 3);
2438
+ ctx.restore();
2439
+ return true;
2440
+ }
2441
+ if (presetId === "bubble-ribbon-sunny" || presetId === "bubble-ribbon-yellow") {
2442
+ drawRibbonBanner(ctx, x, y, width, height, color || "#facc15", "#000000", 2);
2443
+ ctx.restore();
2444
+ return true;
2445
+ }
2446
+ const radius = options?.borderRadius ?? 8;
2447
+ ctx.shadowColor = "rgba(0, 0, 0, 0.2)";
2448
+ ctx.shadowBlur = 8;
2449
+ ctx.shadowOffsetY = 2;
2450
+ ctx.fillStyle = color;
2451
+ ctx.beginPath();
2452
+ if (typeof ctx.roundRect === "function") {
2453
+ ctx.roundRect(x, y, width, height, radius);
2454
+ } else {
2455
+ ctx.rect(x, y, width, height);
2456
+ }
2457
+ ctx.fill();
2458
+ ctx.restore();
2459
+ return true;
2460
+ }
2461
+
2462
+ // src/rendering/bubbleAssets.ts
2463
+ var createBubbleAssetDefinition = () => ({
2464
+ schemaVersion: 1,
2465
+ type: "clypra.textBubble",
2466
+ coordinateSpace: "normalized",
2467
+ geometry: { kind: "parametric-path", body: { cornerRadius: 0.12 }, tail: { enabled: true, side: "bottom", anchor: 0.22, width: 0.18, depth: 0.2, curvature: 0.55 } },
2468
+ layers: { fill: { color: "#7c3aed", opacity: 0.9 }, outline: { color: "#ffffff", width: 0.018, roughness: 0, seed: 1 } },
2469
+ textLayout: { padding: [0.1, 0.1, 0.18, 0.1], alignment: "center", overflow: "grow" }
2470
+ });
2471
+ var createFreeformBubbleAssetDefinition = () => ({
2472
+ ...createBubbleAssetDefinition(),
2473
+ geometry: {
2474
+ kind: "freeform-path",
2475
+ closed: true,
2476
+ commands: [
2477
+ { kind: "move", to: [0.12, 0.04] },
2478
+ { kind: "line", to: [0.88, 0.04] },
2479
+ { kind: "quadratic", control: [0.98, 0.04], to: [0.98, 0.16] },
2480
+ { kind: "line", to: [0.98, 0.7] },
2481
+ { kind: "quadratic", control: [0.98, 0.82], to: [0.86, 0.82] },
2482
+ { kind: "line", to: [0.38, 0.82] },
2483
+ { kind: "line", to: [0.22, 1] },
2484
+ { kind: "line", to: [0.27, 0.82] },
2485
+ { kind: "line", to: [0.12, 0.82] },
2486
+ { kind: "quadratic", control: [0.02, 0.82], to: [0.02, 0.7] },
2487
+ { kind: "line", to: [0.02, 0.16] },
2488
+ { kind: "quadratic", control: [0.02, 0.04], to: [0.12, 0.04] },
2489
+ { kind: "close" }
2490
+ ]
2491
+ }
2492
+ });
2493
+ var clamp = (value) => Math.max(0, Math.min(1, value));
2494
+ function compileBubblePath(definition) {
2495
+ if (definition.geometry.kind === "freeform-path") {
2496
+ return definition.geometry.commands.map((command) => {
2497
+ if (command.kind === "close") return "Z";
2498
+ if (command.kind === "move") return `M ${command.to[0]} ${command.to[1]}`;
2499
+ if (command.kind === "line") return `L ${command.to[0]} ${command.to[1]}`;
2500
+ if (command.kind === "quadratic") return `Q ${command.control[0]} ${command.control[1]} ${command.to[0]} ${command.to[1]}`;
2501
+ if (command.kind === "cubic") return `C ${command.control1[0]} ${command.control1[1]} ${command.control2[0]} ${command.control2[1]} ${command.to[0]} ${command.to[1]}`;
2502
+ return "";
2503
+ }).join(" ");
2504
+ }
2505
+ const { body, tail } = definition.geometry;
2506
+ const r = clamp(body.cornerRadius) * 0.5;
2507
+ const anchor = clamp(tail.anchor);
2508
+ const tailWidth = clamp(tail.width) * 0.5;
2509
+ const depth = clamp(tail.depth);
2510
+ const left = clamp(anchor - tailWidth);
2511
+ const right = clamp(anchor + tailWidth);
2512
+ const bodyPath = `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
2513
+ if (!tail.enabled) return bodyPath;
2514
+ if (tail.side === "bottom") {
2515
+ return `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${right} Q ${anchor + tailWidth * 0.35} ${1 + depth * tail.curvature} ${anchor} ${1 + depth} Q ${anchor - tailWidth * 0.35} ${1 + depth * tail.curvature} ${left} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
2516
+ }
2517
+ if (tail.side === "top") {
2518
+ return `M ${r} 0 H ${left} Q ${anchor - tailWidth * 0.35} ${-depth * tail.curvature} ${anchor} ${-depth} Q ${anchor + tailWidth * 0.35} ${-depth * tail.curvature} ${right} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
2519
+ }
2520
+ if (tail.side === "left") {
2521
+ return `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${right} Q ${-depth * tail.curvature} ${anchor + tailWidth * 0.35} ${-depth} ${anchor} Q ${-depth * tail.curvature} ${anchor - tailWidth * 0.35} 0 ${left} V ${r} Q 0 0 ${r} 0 Z`;
2522
+ }
2523
+ return `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${left} Q ${1 + depth * tail.curvature} ${anchor - tailWidth * 0.35} ${1 + depth} ${anchor} Q ${1 + depth * tail.curvature} ${anchor + tailWidth * 0.35} 1 ${right} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
2524
+ }
2525
+ function compileBubbleSvg(definition, width = 1e3, height = 700) {
2526
+ const { fill, outline, accent } = definition.layers;
2527
+ const texture = accent?.texture === "hatch" ? `<pattern id="hatch" width="24" height="24" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"><line x1="0" y1="0" x2="0" y2="24" stroke="${accent.color}" stroke-opacity="${accent.opacity}" stroke-width="3" /></pattern>` : "";
2528
+ const textureOverlay = accent?.texture === "hatch" ? `<path d="${compileBubblePath(definition)}" fill="url(#hatch)" />` : "";
2529
+ return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="-0.25 -0.25 1.5 1.5"><defs>${texture}</defs><path d="${compileBubblePath(definition)}" fill="${fill.color}" fill-opacity="${fill.opacity}" stroke="${outline.color}" stroke-width="${outline.width}" vector-effect="non-scaling-stroke" />${textureOverlay}</svg>`;
2530
+ }
2531
+ function traceBubbleSilhouette(ctx, x, y, width, height, radius, tail, tailDepth) {
2532
+ const r = Math.min(radius, width / 2, height / 2);
2533
+ const anchor = clamp(tail.anchor);
2534
+ const halfTail = clamp(tail.width) * (tail.side === "left" || tail.side === "right" ? height : width) / 2;
2535
+ const along = tail.side === "left" || tail.side === "right" ? y + height * anchor : x + width * anchor;
2536
+ ctx.beginPath();
2537
+ ctx.moveTo(x + r, y);
2538
+ if (tail.enabled && tail.side === "top") {
2539
+ ctx.lineTo(along - halfTail, y);
2540
+ ctx.quadraticCurveTo(along - halfTail * 0.35, y - tailDepth * tail.curvature, along, y - tailDepth);
2541
+ ctx.quadraticCurveTo(along + halfTail * 0.35, y - tailDepth * tail.curvature, along + halfTail, y);
2542
+ }
2543
+ ctx.lineTo(x + width - r, y);
2544
+ ctx.quadraticCurveTo(x + width, y, x + width, y + r);
2545
+ if (tail.enabled && tail.side === "right") {
2546
+ ctx.lineTo(x + width, along - halfTail);
2547
+ ctx.quadraticCurveTo(x + width + tailDepth * tail.curvature, along - halfTail * 0.35, x + width + tailDepth, along);
2548
+ ctx.quadraticCurveTo(x + width + tailDepth * tail.curvature, along + halfTail * 0.35, x + width, along + halfTail);
2549
+ }
2550
+ ctx.lineTo(x + width, y + height - r);
2551
+ ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
2552
+ if (tail.enabled && tail.side === "bottom") {
2553
+ ctx.lineTo(along + halfTail, y + height);
2554
+ ctx.quadraticCurveTo(along + halfTail * 0.35, y + height + tailDepth * tail.curvature, along, y + height + tailDepth);
2555
+ ctx.quadraticCurveTo(along - halfTail * 0.35, y + height + tailDepth * tail.curvature, along - halfTail, y + height);
2556
+ }
2557
+ ctx.lineTo(x + r, y + height);
2558
+ ctx.quadraticCurveTo(x, y + height, x, y + height - r);
2559
+ if (tail.enabled && tail.side === "left") {
2560
+ ctx.lineTo(x, along + halfTail);
2561
+ ctx.quadraticCurveTo(x - tailDepth * tail.curvature, along + halfTail * 0.35, x - tailDepth, along);
2562
+ ctx.quadraticCurveTo(x - tailDepth * tail.curvature, along - halfTail * 0.35, x, along - halfTail);
2563
+ }
2564
+ ctx.lineTo(x, y + r);
2565
+ ctx.quadraticCurveTo(x, y, x + r, y);
2566
+ ctx.closePath();
2567
+ }
2568
+ function renderBubbleAssetDefinition(ctx, definition, x, y, width, height) {
2569
+ if (definition.geometry.kind === "freeform-path") {
2570
+ renderFreeformBubbleAssetDefinition(ctx, definition, x, y, width, height);
2571
+ return;
2572
+ }
2573
+ const { tail, body } = definition.geometry;
2574
+ const tailDepth = tail.enabled ? clamp(tail.depth) * (tail.side === "left" || tail.side === "right" ? width : height) : 0;
2575
+ const bodyX = x + (tail.side === "left" ? tailDepth : 0);
2576
+ const bodyY = y + (tail.side === "top" ? tailDepth : 0);
2577
+ const bodyWidth = width - (tail.side === "left" || tail.side === "right" ? tailDepth : 0);
2578
+ const bodyHeight = height - (tail.side === "top" || tail.side === "bottom" ? tailDepth : 0);
2579
+ const radius = clamp(body.cornerRadius) * Math.min(bodyWidth, bodyHeight) / 2;
2580
+ ctx.save();
2581
+ traceBubbleSilhouette(ctx, bodyX, bodyY, bodyWidth, bodyHeight, radius, tail, tailDepth);
2582
+ ctx.globalAlpha = definition.layers.fill.opacity;
2583
+ ctx.fillStyle = definition.layers.fill.color;
2584
+ ctx.fill();
2585
+ const accent = definition.layers.accent;
2586
+ if (accent && accent.texture !== "none") {
2587
+ ctx.save();
2588
+ ctx.clip();
2589
+ ctx.globalAlpha = accent.opacity;
2590
+ ctx.strokeStyle = accent.color;
2591
+ ctx.lineWidth = Math.max(1, Math.min(width, height) * 6e-3);
2592
+ if (accent.texture === "hatch") {
2593
+ const spacing = Math.max(5, Math.min(width, height) * 0.09);
2594
+ for (let offset = -height; offset < width + height; offset += spacing) {
2595
+ ctx.beginPath();
2596
+ ctx.moveTo(x + offset, y + height);
2597
+ ctx.lineTo(x + offset + height, y);
2598
+ ctx.stroke();
2599
+ }
2600
+ } else {
2601
+ let state = definition.layers.outline.seed >>> 0;
2602
+ const nextRandom = () => {
2603
+ state = state * 1664525 + 1013904223 >>> 0;
2604
+ return state / 4294967296;
2605
+ };
2606
+ const count = Math.max(18, Math.round(width * height / 700));
2607
+ for (let index = 0; index < count; index += 1) {
2608
+ const px = x + nextRandom() * width;
2609
+ const py = y + nextRandom() * height;
2610
+ const length = 1 + nextRandom() * Math.min(width, height) * 0.025;
2611
+ ctx.beginPath();
2612
+ ctx.moveTo(px, py);
2613
+ ctx.lineTo(px + length, py);
2614
+ ctx.stroke();
2615
+ }
2616
+ }
2617
+ ctx.restore();
2618
+ }
2619
+ if (definition.layers.outline.width > 0) {
2620
+ ctx.globalAlpha = 1;
2621
+ ctx.strokeStyle = definition.layers.outline.color;
2622
+ ctx.lineWidth = definition.layers.outline.width * Math.min(width, height);
2623
+ ctx.stroke();
2624
+ }
2625
+ ctx.restore();
2626
+ }
2627
+ function traceFreeformPath(ctx, definition, x, y, width, height) {
2628
+ ctx.beginPath();
2629
+ for (const command of definition.geometry.commands) {
2630
+ if (command.kind === "close") ctx.closePath();
2631
+ else if (command.kind === "move") ctx.moveTo(x + command.to[0] * width, y + command.to[1] * height);
2632
+ else if (command.kind === "line") ctx.lineTo(x + command.to[0] * width, y + command.to[1] * height);
2633
+ else if (command.kind === "quadratic") ctx.quadraticCurveTo(x + command.control[0] * width, y + command.control[1] * height, x + command.to[0] * width, y + command.to[1] * height);
2634
+ else if (command.kind === "cubic") ctx.bezierCurveTo(x + command.control1[0] * width, y + command.control1[1] * height, x + command.control2[0] * width, y + command.control2[1] * height, x + command.to[0] * width, y + command.to[1] * height);
2635
+ }
2636
+ }
2637
+ function renderFreeformBubbleAssetDefinition(ctx, definition, x, y, width, height) {
2638
+ ctx.save();
2639
+ traceFreeformPath(ctx, definition, x, y, width, height);
2640
+ ctx.globalAlpha = definition.layers.fill.opacity;
2641
+ ctx.fillStyle = definition.layers.fill.color;
2642
+ ctx.fill();
2643
+ if (definition.layers.outline.width > 0) {
2644
+ ctx.globalAlpha = 1;
2645
+ ctx.strokeStyle = definition.layers.outline.color;
2646
+ ctx.lineWidth = definition.layers.outline.width * Math.min(width, height);
2647
+ ctx.stroke();
2648
+ }
2649
+ ctx.restore();
2650
+ }
2651
+ function validateBubbleAssetDefinition(definition) {
2652
+ const errors = [];
2653
+ if (definition.schemaVersion !== 1 || definition.type !== "clypra.textBubble") errors.push("Unsupported bubble asset schema.");
2654
+ if (!/^#[0-9a-f]{6}$/i.test(definition.layers.fill.color)) errors.push("Fill color must be a six-digit hex value.");
2655
+ if (definition.layers.fill.opacity < 0 || definition.layers.fill.opacity > 1) errors.push("Fill opacity must be between 0 and 1.");
2656
+ if (!/^#[0-9a-f]{6}$/i.test(definition.layers.outline.color)) errors.push("Outline color must be a six-digit hex value.");
2657
+ if (definition.layers.outline.width < 0 || definition.layers.outline.width > 0.1) errors.push("Outline width must be normalized between 0 and 0.1.");
2658
+ if (definition.geometry.kind === "parametric-path") {
2659
+ if (definition.geometry.body.cornerRadius < 0 || definition.geometry.body.cornerRadius > 1) errors.push("Corner radius must be normalized.");
2660
+ if (definition.geometry.tail.anchor < 0 || definition.geometry.tail.anchor > 1) errors.push("Tail anchor must be normalized.");
2661
+ if (definition.geometry.tail.width < 0 || definition.geometry.tail.width > 1 || definition.geometry.tail.depth < 0 || definition.geometry.tail.depth > 1) errors.push("Tail width and depth must be normalized.");
2662
+ } else if (definition.geometry.commands.length < 4 || !definition.geometry.commands.some((command) => command.kind === "close")) {
2663
+ errors.push("Freeform bubble paths must contain a closed silhouette.");
2664
+ }
2665
+ if (definition.textLayout.padding.some((value) => value < 0 || value > 0.5)) errors.push("Text padding must be normalized between 0 and 0.5.");
2666
+ return errors;
2667
+ }
2668
+
2055
2669
  // src/rendering/index.ts
2056
2670
  function renderCanonicalToCanvas(ctx, input) {
2057
2671
  if (input.capability === "text-template") {
@@ -2106,6 +2720,22 @@ export {
2106
2720
  getTextTemplateControls,
2107
2721
  resolveTemplateFontFamily,
2108
2722
  renderTextTemplateToCanvas,
2723
+ resolveBubbleLayout,
2724
+ pseudoNoise,
2725
+ drawTornPaper,
2726
+ drawSpeechBubble,
2727
+ renderBubblePresetBackground,
2728
+ drawChalkSpeechBubble,
2729
+ drawThoughtCloud,
2730
+ drawComicBurst,
2731
+ drawRibbonBanner,
2732
+ renderBubbleBackground,
2733
+ createBubbleAssetDefinition,
2734
+ createFreeformBubbleAssetDefinition,
2735
+ compileBubblePath,
2736
+ compileBubbleSvg,
2737
+ renderBubbleAssetDefinition,
2738
+ validateBubbleAssetDefinition,
2109
2739
  renderCanonicalToCanvas
2110
2740
  };
2111
- //# sourceMappingURL=chunk-JP44RJUI.js.map
2741
+ //# sourceMappingURL=chunk-ZV3RQAUJ.js.map