@algorisys/tinyfly 0.74.0 → 0.75.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.
@@ -1,4 +1,4 @@
1
- import { deserializeTimeline, planNarration } from "@algorisys/tinyfly";
1
+ import { parsePath, createRandom, hashSeed, deserializeTimeline, planNarration } from "@algorisys/tinyfly";
2
2
  import { spawn } from "node:child_process";
3
3
  import { mkdir, writeFile } from "node:fs/promises";
4
4
  import { resolve, join, dirname } from "node:path";
@@ -41,6 +41,233 @@ function shineStops(progress, baseColor, options = {}) {
41
41
  stops.sort((a, b) => a.offset - b.offset);
42
42
  return stops;
43
43
  }
44
+ function pathLength(points) {
45
+ let length = 0;
46
+ for (let i = 1; i < points.length; i++) length += Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y);
47
+ return length;
48
+ }
49
+ function partialPath(points, progress) {
50
+ const t = Math.min(1, Math.max(0, progress));
51
+ if (points.length < 2 || t === 1) return points.slice();
52
+ if (t === 0) return points.slice(0, 1);
53
+ let remaining = pathLength(points) * t;
54
+ const result = [points[0]];
55
+ for (let i = 1; i < points.length; i++) {
56
+ const a = points[i - 1];
57
+ const b = points[i];
58
+ const length = Math.hypot(b.x - a.x, b.y - a.y);
59
+ if (length >= remaining) {
60
+ const f = length === 0 ? 0 : remaining / length;
61
+ result.push({ x: a.x + (b.x - a.x) * f, y: a.y + (b.y - a.y) * f });
62
+ return result;
63
+ }
64
+ result.push(b);
65
+ remaining -= length;
66
+ }
67
+ return result;
68
+ }
69
+ const CURVE_SAMPLES = 12;
70
+ function pathOutline(pathData) {
71
+ const strokes = [];
72
+ let run;
73
+ let runSubpath = -1;
74
+ for (const segment of parsePath(pathData).segments) {
75
+ if (segment.type === "L") {
76
+ if (!run || runSubpath !== segment.subpath) {
77
+ run = { kind: "line", points: [{ x: segment.startX, y: segment.startY }], length: 0 };
78
+ runSubpath = segment.subpath;
79
+ strokes.push(run);
80
+ }
81
+ run.points.push({ x: segment.endX, y: segment.endY });
82
+ run.length += segment.length;
83
+ continue;
84
+ }
85
+ run = void 0;
86
+ const [c1x, c1y, c2x, c2y, ex, ey] = segment.points;
87
+ const points = [];
88
+ for (let i = 0; i <= CURVE_SAMPLES; i++) {
89
+ const t = i / CURVE_SAMPLES;
90
+ const u = 1 - t;
91
+ points.push({
92
+ x: u * u * u * segment.startX + 3 * u * u * t * c1x + 3 * u * t * t * c2x + t * t * t * ex,
93
+ y: u * u * u * segment.startY + 3 * u * u * t * c1y + 3 * u * t * t * c2y + t * t * t * ey
94
+ });
95
+ }
96
+ strokes.push({ kind: "curve", points, length: segment.length });
97
+ }
98
+ return strokes;
99
+ }
100
+ function rectOutline(x, y, width, height, radius = 0) {
101
+ const r = Math.max(0, Math.min(radius, width / 2, height / 2));
102
+ if (r === 0) return pathOutline(`M${x},${y} H${x + width} V${y + height} H${x} Z`);
103
+ const right = x + width;
104
+ const bottom = y + height;
105
+ return pathOutline(
106
+ `M${x + r},${y} H${right - r} A${r},${r} 0 0 1 ${right},${y + r} V${bottom - r} A${r},${r} 0 0 1 ${right - r},${bottom} H${x + r} A${r},${r} 0 0 1 ${x},${bottom - r} V${y + r} A${r},${r} 0 0 1 ${x + r},${y} Z`
107
+ );
108
+ }
109
+ function drawOutline(ctx, strokes, progress, pen) {
110
+ const total = strokes.reduce((sum, stroke) => sum + stroke.length, 0);
111
+ const drawn = total * Math.min(1, Math.max(0, progress));
112
+ let along = 0;
113
+ if (!pen) ctx.beginPath();
114
+ for (const stroke of strokes) {
115
+ const part = stroke.length === 0 ? drawn >= along ? 1 : 0 : Math.min(1, Math.max(0, (drawn - along) / stroke.length));
116
+ along += stroke.length;
117
+ if (pen) {
118
+ if (stroke.kind === "curve") pen.curve(stroke.points, part);
119
+ else pen.line(stroke.points, part);
120
+ continue;
121
+ }
122
+ if (part <= 0) continue;
123
+ const points = partialPath(stroke.points, part);
124
+ ctx.moveTo(points[0].x, points[0].y);
125
+ for (const point of points.slice(1)) ctx.lineTo(point.x, point.y);
126
+ }
127
+ if (!pen) ctx.stroke();
128
+ }
129
+ function boilFrame(time, boil) {
130
+ return boil > 0 ? Math.floor(Math.max(0, time) * boil / 1e3) : 0;
131
+ }
132
+ function sketchPen(ctx, style, time) {
133
+ const roughness = style.roughness ?? 2;
134
+ const passes = Math.max(1, Math.round(style.passes ?? 2));
135
+ const frame = boilFrame(time, style.boil ?? 8);
136
+ let strokes = 0;
137
+ const nextStroke = () => {
138
+ const stroke = strokes++;
139
+ return (pass) => createRandom(hashSeed(`${style.seed ?? 1}:${frame}:${stroke}:${pass}`));
140
+ };
141
+ const spread = (random, amount) => (random.next() * 2 - 1) * amount;
142
+ const eachPass = (draw) => {
143
+ const random = nextStroke();
144
+ const width = ctx.lineWidth;
145
+ const alpha = ctx.globalAlpha;
146
+ for (let pass = 0; pass < passes; pass++) {
147
+ ctx.lineWidth = pass === 0 ? width : width * 0.55;
148
+ ctx.globalAlpha = pass === 0 ? alpha : alpha * 0.6;
149
+ draw(random(pass));
150
+ }
151
+ ctx.lineWidth = width;
152
+ ctx.globalAlpha = alpha;
153
+ };
154
+ const line = (points, progress = 1) => {
155
+ if (points.length < 2) return;
156
+ const lengths = points.slice(1).map((p, i) => Math.hypot(p.x - points[i].x, p.y - points[i].y));
157
+ const drawn = lengths.reduce((sum, l) => sum + l, 0) * Math.min(1, Math.max(0, progress));
158
+ eachPass((random) => {
159
+ const ends = [];
160
+ const bows = [];
161
+ points.forEach((p, i) => {
162
+ ends.push({ x: p.x + spread(random, roughness * 0.5), y: p.y + spread(random, roughness * 0.5) });
163
+ if (i > 0) bows.push([random.next() * 2 - 1, random.next() * 2 - 1]);
164
+ });
165
+ if (drawn <= 0) return;
166
+ ctx.beginPath();
167
+ ctx.moveTo(ends[0].x, ends[0].y);
168
+ let along = 0;
169
+ for (let i = 1; i < ends.length; i++) {
170
+ const cubic = bowedCubic(ends[i - 1], ends[i], roughness, bows[i - 1]);
171
+ const length = lengths[i - 1];
172
+ if (along + length <= drawn) {
173
+ ctx.bezierCurveTo(cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y, cubic[3].x, cubic[3].y);
174
+ along += length;
175
+ continue;
176
+ }
177
+ const part = splitCubic(cubic, length === 0 ? 1 : (drawn - along) / length);
178
+ ctx.bezierCurveTo(part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y);
179
+ break;
180
+ }
181
+ ctx.stroke();
182
+ });
183
+ };
184
+ const ellipse = (cx, cy, rx, ry, progress = 1) => {
185
+ eachPass((random) => {
186
+ const steps = 14;
187
+ const start = random.next() * Math.PI * 2;
188
+ const sweep = Math.PI * 2 + 0.15 + random.next() * 0.3;
189
+ const points = [];
190
+ for (let i = 0; i <= steps; i++) {
191
+ const angle = start + sweep * i / steps;
192
+ const wobble = spread(random, roughness * 0.6);
193
+ points.push({ x: cx + Math.cos(angle) * (rx + wobble), y: cy + Math.sin(angle) * (ry + wobble) });
194
+ }
195
+ const drawn = partialPath(points, progress);
196
+ if (drawn.length < 2) return;
197
+ smoothPath(ctx, drawn);
198
+ ctx.stroke();
199
+ });
200
+ };
201
+ return {
202
+ line,
203
+ curve(points, progress = 1) {
204
+ if (points.length < 2) return;
205
+ const first = points[0];
206
+ const last = points[points.length - 1];
207
+ const chord = Math.hypot(last.x - first.x, last.y - first.y) || 1;
208
+ const nx = -(last.y - first.y) / chord;
209
+ const ny = (last.x - first.x) / chord;
210
+ eachPass((random) => {
211
+ const start = { x: spread(random, roughness * 0.5), y: spread(random, roughness * 0.5) };
212
+ const end = { x: spread(random, roughness * 0.5), y: spread(random, roughness * 0.5) };
213
+ const bow = spread(random, roughness * Math.min(1.5, Math.max(0.3, chord / 80)));
214
+ const wobbled = points.map((p, i) => {
215
+ const t = i / (points.length - 1);
216
+ const off = Math.sin(Math.PI * t) * bow;
217
+ return {
218
+ x: p.x + start.x + (end.x - start.x) * t + nx * off,
219
+ y: p.y + start.y + (end.y - start.y) * t + ny * off
220
+ };
221
+ });
222
+ const drawn = partialPath(wobbled, progress);
223
+ if (drawn.length < 2) return;
224
+ smoothPath(ctx, drawn);
225
+ ctx.stroke();
226
+ });
227
+ },
228
+ circle(cx, cy, r, progress = 1) {
229
+ ellipse(cx, cy, r, r, progress);
230
+ },
231
+ ellipse,
232
+ nudge(scale = 0.5) {
233
+ const random = nextStroke()(0);
234
+ return { x: spread(random, roughness * scale), y: spread(random, roughness * scale) };
235
+ }
236
+ };
237
+ }
238
+ function bowedCubic(a, b, roughness, offsets) {
239
+ const dx = b.x - a.x;
240
+ const dy = b.y - a.y;
241
+ const length = Math.hypot(dx, dy) || 1;
242
+ const bow = roughness * Math.min(1.5, Math.max(0.3, length / 80));
243
+ const nx = -dy / length;
244
+ const ny = dx / length;
245
+ return [
246
+ a,
247
+ { x: a.x + dx / 3 + nx * offsets[0] * bow, y: a.y + dy / 3 + ny * offsets[0] * bow },
248
+ { x: a.x + 2 * dx / 3 + nx * offsets[1] * bow, y: a.y + 2 * dy / 3 + ny * offsets[1] * bow },
249
+ b
250
+ ];
251
+ }
252
+ function splitCubic([p0, p1, p2, p3], t) {
253
+ const lerp = (a2, b2) => ({ x: a2.x + (b2.x - a2.x) * t, y: a2.y + (b2.y - a2.y) * t });
254
+ const a = lerp(p0, p1);
255
+ const b = lerp(p1, p2);
256
+ const c = lerp(p2, p3);
257
+ const d = lerp(a, b);
258
+ const e = lerp(b, c);
259
+ return [p0, a, d, lerp(d, e)];
260
+ }
261
+ function smoothPath(ctx, points) {
262
+ ctx.beginPath();
263
+ ctx.moveTo(points[0].x, points[0].y);
264
+ for (let i = 1; i < points.length - 1; i++) {
265
+ const mid = { x: (points[i].x + points[i + 1].x) / 2, y: (points[i].y + points[i + 1].y) / 2 };
266
+ ctx.quadraticCurveTo(points[i].x, points[i].y, mid.x, mid.y);
267
+ }
268
+ const last = points[points.length - 1];
269
+ ctx.lineTo(last.x, last.y);
270
+ }
44
271
  class CanvasAdapter {
45
272
  targets = /* @__PURE__ */ new Map();
46
273
  targetOrder = [];
@@ -310,10 +537,43 @@ class CanvasAdapter {
310
537
  return { x: target.x, y: target.y, width: 100, height: 100 };
311
538
  }
312
539
  }
540
+ /** Whether a shape is drawn stroke by stroke: sketched, or only partly drawn on. */
541
+ drawsOutline(target) {
542
+ return target.sketch !== void 0 || target.drawOn !== void 0 && target.drawOn < 1;
543
+ }
544
+ /**
545
+ * Draw a shape outline-first: the fill (clean) once the outline is complete,
546
+ * then the outline up to `drawOn`, in pencil strokes when sketched.
547
+ */
548
+ renderOutlined(ctx, target, fill, outline) {
549
+ const progress = Math.min(1, Math.max(0, target.drawOn ?? 1));
550
+ if (fill && target.fillStyle && progress >= 1) fill();
551
+ if (!target.strokeStyle || progress <= 0) return;
552
+ const pen = target.sketch ? sketchPen(ctx, target.sketch, this.time) : void 0;
553
+ if (pen) {
554
+ ctx.lineCap = target.lineCap ?? "round";
555
+ ctx.lineJoin = target.lineJoin ?? "round";
556
+ }
557
+ outline(progress, pen);
558
+ }
313
559
  /**
314
560
  * Render a rectangle.
315
561
  */
316
562
  renderRect(ctx, target) {
563
+ if (this.drawsOutline(target)) {
564
+ const { x, y, width, height } = target;
565
+ return this.renderOutlined(
566
+ ctx,
567
+ target,
568
+ () => {
569
+ ctx.beginPath();
570
+ if (target.borderRadius && target.borderRadius > 0) this.roundRect(ctx, x, y, width, height, target.borderRadius);
571
+ else ctx.rect(x, y, width, height);
572
+ ctx.fill();
573
+ },
574
+ (progress, pen) => drawOutline(ctx, rectOutline(x, y, width, height, target.borderRadius), progress, pen)
575
+ );
576
+ }
317
577
  ctx.beginPath();
318
578
  if (target.borderRadius && target.borderRadius > 0) {
319
579
  this.roundRect(ctx, target.x, target.y, target.width, target.height, target.borderRadius);
@@ -347,6 +607,24 @@ class CanvasAdapter {
347
607
  * Render a circle.
348
608
  */
349
609
  renderCircle(ctx, target) {
610
+ if (this.drawsOutline(target)) {
611
+ const { x, y, radius } = target;
612
+ return this.renderOutlined(
613
+ ctx,
614
+ target,
615
+ () => {
616
+ ctx.beginPath();
617
+ ctx.arc(x, y, radius, 0, Math.PI * 2);
618
+ ctx.fill();
619
+ },
620
+ (progress, pen) => {
621
+ if (pen) return pen.circle(x, y, radius, progress);
622
+ ctx.beginPath();
623
+ ctx.arc(x, y, radius, -Math.PI / 2, -Math.PI / 2 + Math.PI * 2 * progress);
624
+ ctx.stroke();
625
+ }
626
+ );
627
+ }
350
628
  ctx.beginPath();
351
629
  ctx.arc(target.x, target.y, target.radius, 0, Math.PI * 2);
352
630
  if (target.fillStyle) {
@@ -391,6 +669,19 @@ class CanvasAdapter {
391
669
  if (target.lineCap) {
392
670
  ctx.lineCap = target.lineCap;
393
671
  }
672
+ if (this.drawsOutline(target)) {
673
+ const points = [
674
+ { x: target.x, y: target.y },
675
+ { x: target.x2, y: target.y2 }
676
+ ];
677
+ const length = Math.hypot(target.x2 - target.x, target.y2 - target.y);
678
+ return this.renderOutlined(
679
+ ctx,
680
+ target,
681
+ void 0,
682
+ (progress, pen) => drawOutline(ctx, [{ kind: "line", points, length }], progress, pen)
683
+ );
684
+ }
394
685
  ctx.beginPath();
395
686
  ctx.moveTo(target.x, target.y);
396
687
  ctx.lineTo(target.x2, target.y2);
@@ -411,6 +702,14 @@ class CanvasAdapter {
411
702
  }
412
703
  const path = new Path2D(target.d);
413
704
  ctx.translate(target.x, target.y);
705
+ if (this.drawsOutline(target)) {
706
+ return this.renderOutlined(
707
+ ctx,
708
+ target,
709
+ () => ctx.fill(path),
710
+ (progress, pen) => drawOutline(ctx, pathOutline(target.d), progress, pen)
711
+ );
712
+ }
414
713
  if (target.fillStyle) {
415
714
  ctx.fill(path);
416
715
  }