@slidevideo/cli 0.3.0 → 0.3.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.
Files changed (61) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +3 -3
  3. package/addons/slidevideo/geometry.ts +334 -1
  4. package/addons/slidevideo/global-top.vue +57 -6
  5. package/addons/slidevideo/preview.ts +242 -25
  6. package/assets/fonts/OFL-LICENSES.txt +95 -0
  7. package/assets/fonts/instrument-serif-latin-2.woff2 +0 -0
  8. package/assets/fonts/instrument-serif-latin-ext-1.woff2 +0 -0
  9. package/assets/fonts/jetbrains-mono-latin-4.woff2 +0 -0
  10. package/assets/fonts/jetbrains-mono-latin-ext-3.woff2 +0 -0
  11. package/assets/fonts/plus-jakarta-sans-latin-6.woff2 +0 -0
  12. package/assets/fonts/plus-jakarta-sans-latin-ext-5.woff2 +0 -0
  13. package/dist/adapters/edge.d.ts +1 -0
  14. package/dist/adapters/edge.js +4 -3
  15. package/dist/adapters/elevenlabs.d.ts +1 -0
  16. package/dist/adapters/elevenlabs.js +4 -3
  17. package/dist/adapters/powerpoint-preview.d.ts +1 -1
  18. package/dist/adapters/powerpoint-preview.js +13 -2
  19. package/dist/adapters/powerpoint.js +91 -14
  20. package/dist/adapters/slidev-inspection.js +7 -2
  21. package/dist/annotation-paint.d.ts +195 -5
  22. package/dist/annotation-paint.js +1188 -2
  23. package/dist/annotations.d.ts +26 -2
  24. package/dist/annotations.js +1117 -29
  25. package/dist/assembly.js +143 -37
  26. package/dist/captions.d.ts +6 -0
  27. package/dist/captions.js +320 -0
  28. package/dist/cli.js +59 -19
  29. package/dist/compositor.d.ts +17 -2
  30. package/dist/compositor.js +108 -26
  31. package/dist/contracts.d.ts +254 -14
  32. package/dist/media.js +24 -1
  33. package/dist/pipeline.d.ts +6 -1
  34. package/dist/pipeline.js +68 -20
  35. package/dist/preview/contracts.d.ts +15 -0
  36. package/dist/preview/player.js +527 -34
  37. package/dist/preview/server.d.ts +1 -1
  38. package/dist/preview/server.js +125 -4
  39. package/dist/preview/view.d.ts +1 -1
  40. package/dist/preview/view.js +26 -7
  41. package/dist/run.d.ts +1 -0
  42. package/dist/run.js +20 -0
  43. package/dist/speech.d.ts +13 -3
  44. package/dist/speech.js +164 -45
  45. package/dist/stamp-fonts.d.ts +17 -0
  46. package/dist/stamp-fonts.js +202 -0
  47. package/dist/storyboard.js +60 -3
  48. package/dist/timing.d.ts +4 -1
  49. package/dist/timing.js +322 -14
  50. package/dist/update-check-worker.js +43 -11
  51. package/dist/update-check.js +14 -10
  52. package/package.json +12 -1
  53. package/schemas/annotation-geometry.v1.json +111 -0
  54. package/schemas/schedule.v1.json +1672 -84
  55. package/schemas/storyboard.v1.json +3725 -85
  56. package/skills/slidevideo/SKILL.md +5 -5
  57. package/skills/slidevideo/references/decks.md +6 -4
  58. package/skills/slidevideo/references/production.md +12 -11
  59. package/skills/slidevideo/references/storyboard.md +268 -5
  60. package/tools/powerpoint/powerpoint.ps1 +155 -6
  61. package/tools/powerpoint/preview.ps1 +68 -1
@@ -1,3 +1,12 @@
1
+ const DEFAULT_SPOTLIGHT_COLOR = '#000000';
2
+ const DEFAULT_SPOTLIGHT_RADIUS = 0.012;
3
+ const DEFAULT_ZOOM_PADDING = 0.04;
4
+ const DEFAULT_ZOOM_MAX_SCALE = 2.5;
5
+ const DEFAULT_LASER_COLOR = '#ff2d2d';
6
+ const DEFAULT_LASER_OPACITY = 0.95;
7
+ const DEFAULT_LASER_RADIUS = 0.008;
8
+ const DEFAULT_LASER_GLOW = 0.024;
9
+ const DEFAULT_LASER_TRAIL_MS = 240;
1
10
  export function targetKey(target) {
2
11
  switch (target.type) {
3
12
  case 'slide':
@@ -8,13 +17,44 @@ export function targetKey(target) {
8
17
  return `element:${target.selector}`;
9
18
  case 'shape':
10
19
  return `shape:${target.id}`;
20
+ case 'text': {
21
+ const scope = target.container
22
+ ? target.container.type === 'element'
23
+ ? `element:${target.container.selector}`
24
+ : `shape:${target.container.id}`
25
+ : 'slide';
26
+ const occ = target.occurrence ?? 1;
27
+ const slice = target.slice ? `@${target.slice.start}:${target.slice.end}` : '';
28
+ return `text:${scope}#${occ}:${JSON.stringify(target.text)}${slice}`;
29
+ }
11
30
  }
12
31
  }
13
32
  export function annotationTargets(annotation) {
14
33
  switch (annotation.effect) {
15
34
  case 'highlight':
16
35
  case 'underline':
36
+ case 'spotlight':
37
+ case 'zoom':
17
38
  return [annotation.target];
39
+ case 'stamp':
40
+ return [annotation.target.type === 'point' ? { type: 'slide' } : annotation.target];
41
+ case 'laser': {
42
+ const seen = new Set();
43
+ const targets = [];
44
+ const push = (t) => {
45
+ const key = targetKey(t);
46
+ if (!seen.has(key)) {
47
+ seen.add(key);
48
+ targets.push(t);
49
+ }
50
+ };
51
+ push({ type: 'slide' });
52
+ for (const point of annotation.target.points) {
53
+ if ('target' in point)
54
+ push(point.target);
55
+ }
56
+ return targets;
57
+ }
18
58
  }
19
59
  }
20
60
  export function geometryRequests(schedule) {
@@ -153,17 +193,1119 @@ export function underlinePaint(annotation, { content, fragments, viewport }) {
153
193
  fragments: painted,
154
194
  };
155
195
  }
196
+ export function spotlightPaint(annotation, { content, fragments, viewport }) {
197
+ const { appearance, motion } = annotation;
198
+ const paddingX = appearance.padding?.x ?? 0;
199
+ const paddingY = appearance.padding?.y ?? 0;
200
+ const clipLeft = Math.max(0, Math.round(content.x));
201
+ const clipTop = Math.max(0, Math.round(content.y));
202
+ const clipRight = Math.min(viewport.width, Math.round(content.x + content.width));
203
+ const clipBottom = Math.min(viewport.height, Math.round(content.y + content.height));
204
+ const slide_bounds = {
205
+ x: clipLeft,
206
+ y: clipTop,
207
+ width: Math.max(0, clipRight - clipLeft),
208
+ height: Math.max(0, clipBottom - clipTop),
209
+ };
210
+ const softness_px = Math.max(0, Math.round((appearance.softness ?? 0) * content.height));
211
+ const shape = appearance.shape ?? 'rounded-rect';
212
+ const rawRadius = shape === 'rounded-rect'
213
+ ? (('radius' in appearance ? appearance.radius : undefined) ?? DEFAULT_SPOTLIGHT_RADIUS) * content.height
214
+ : 0;
215
+ const apertures = fragments.map((bounds) => {
216
+ const x = Math.floor(content.x + (bounds.x - paddingX) * content.width);
217
+ const y = Math.floor(content.y + (bounds.y - paddingY) * content.height);
218
+ const right = Math.ceil(content.x + (bounds.x + bounds.width + paddingX) * content.width);
219
+ const bottom = Math.ceil(content.y + (bounds.y + bounds.height + paddingY) * content.height);
220
+ const width = Math.max(0, right - x);
221
+ const height = Math.max(0, bottom - y);
222
+ if (shape === 'ellipse') {
223
+ return { shape: 'ellipse', cx: x + width / 2, cy: y + height / 2, rx: width / 2, ry: height / 2 };
224
+ }
225
+ const radius = Math.max(0, Math.min(width / 2, height / 2, Math.round(rawRadius)));
226
+ return { shape: 'rounded-rect', x, y, width, height, radius };
227
+ });
228
+ return {
229
+ effect: 'spotlight',
230
+ start_ms: annotation.start_ms,
231
+ end_ms: annotation.end_ms,
232
+ in_ms: motion?.in_ms ?? 0,
233
+ out_ms: motion?.out_ms ?? 0,
234
+ color: (appearance.color ?? DEFAULT_SPOTLIGHT_COLOR).toLowerCase(),
235
+ opacity: appearance.opacity,
236
+ softness_px,
237
+ slide_bounds,
238
+ apertures,
239
+ };
240
+ }
241
+ export function zoomPaint(annotation, { content, fragments, viewport }) {
242
+ const { appearance, motion } = annotation;
243
+ const clipLeft = Math.max(0, Math.round(content.x));
244
+ const clipTop = Math.max(0, Math.round(content.y));
245
+ const clipRight = Math.min(viewport.width, Math.round(content.x + content.width));
246
+ const clipBottom = Math.min(viewport.height, Math.round(content.y + content.height));
247
+ const slide_bounds = {
248
+ x: clipLeft,
249
+ y: clipTop,
250
+ width: Math.max(0, clipRight - clipLeft),
251
+ height: Math.max(0, clipBottom - clipTop),
252
+ };
253
+ let minX = Infinity;
254
+ let minY = Infinity;
255
+ let maxX = -Infinity;
256
+ let maxY = -Infinity;
257
+ for (const fragment of fragments) {
258
+ if (fragment.x < minX)
259
+ minX = fragment.x;
260
+ if (fragment.y < minY)
261
+ minY = fragment.y;
262
+ if (fragment.x + fragment.width > maxX)
263
+ maxX = fragment.x + fragment.width;
264
+ if (fragment.y + fragment.height > maxY)
265
+ maxY = fragment.y + fragment.height;
266
+ }
267
+ const padX = appearance?.padding?.x ?? DEFAULT_ZOOM_PADDING;
268
+ const padY = appearance?.padding?.y ?? DEFAULT_ZOOM_PADDING;
269
+ const paddedW = Math.min(slide_bounds.width, (maxX - minX + 2 * padX) * slide_bounds.width);
270
+ const paddedH = Math.min(slide_bounds.height, (maxY - minY + 2 * padY) * slide_bounds.height);
271
+ const fit = appearance?.fit ?? 'contain';
272
+ const fitScale = paddedW > 0 && paddedH > 0
273
+ ? (fit === 'cover'
274
+ ? Math.max(slide_bounds.width / paddedW, slide_bounds.height / paddedH)
275
+ : Math.min(slide_bounds.width / paddedW, slide_bounds.height / paddedH))
276
+ : 1;
277
+ const target_scale = appearance?.scale !== undefined
278
+ ? Math.max(1, Math.min(appearance.max_scale ?? appearance.scale, appearance.scale))
279
+ : Math.max(1, Math.min(appearance?.max_scale ?? DEFAULT_ZOOM_MAX_SCALE, fitScale));
280
+ const sw = slide_bounds.width / target_scale;
281
+ const sh = slide_bounds.height / target_scale;
282
+ const centerX = slide_bounds.x + ((minX + maxX) / 2) * slide_bounds.width;
283
+ const centerY = slide_bounds.y + ((minY + maxY) / 2) * slide_bounds.height;
284
+ const sx = Math.max(slide_bounds.x, Math.min(slide_bounds.x + slide_bounds.width - sw, centerX - sw / 2));
285
+ const sy = Math.max(slide_bounds.y, Math.min(slide_bounds.y + slide_bounds.height - sh, centerY - sh / 2));
286
+ return {
287
+ effect: 'zoom',
288
+ start_ms: annotation.start_ms,
289
+ end_ms: annotation.end_ms,
290
+ in_ms: motion?.in_ms ?? 0,
291
+ out_ms: motion?.out_ms ?? 0,
292
+ easing: motion?.easing ?? 'ease-in-out',
293
+ target_scale,
294
+ slide_bounds,
295
+ target_source: { x: sx, y: sy, width: sw, height: sh },
296
+ };
297
+ }
298
+ export function zoomTransformVisible(effect) {
299
+ return effect.slide_bounds.width > 0 && effect.slide_bounds.height > 0 && effect.target_scale > 1.0001;
300
+ }
301
+ export function zoomProgress(effect, timeMs) {
302
+ const easeIn = effect.in_ms > 0 ? (timeMs - effect.start_ms) / effect.in_ms : 1;
303
+ const easeOut = effect.out_ms > 0 ? (effect.end_ms - timeMs) / effect.out_ms : 1;
304
+ const t = Math.max(0, Math.min(1, easeIn, easeOut));
305
+ if (effect.easing === 'linear')
306
+ return t;
307
+ return t * t * (3 - 2 * t);
308
+ }
309
+ export function evaluateViewTransform(effect, timeMs) {
310
+ if (!zoomTransformVisible(effect))
311
+ return undefined;
312
+ const progress = zoomProgress(effect, timeMs);
313
+ if (progress <= 1e-6)
314
+ return undefined;
315
+ const sb = effect.slide_bounds;
316
+ const ts = effect.target_source;
317
+ const width = sb.width + (ts.width - sb.width) * progress;
318
+ const height = sb.height + (ts.height - sb.height) * progress;
319
+ if (!(width > 0 && height > 0))
320
+ return undefined;
321
+ const source = {
322
+ x: sb.x + (ts.x - sb.x) * progress,
323
+ y: sb.y + (ts.y - sb.y) * progress,
324
+ width,
325
+ height,
326
+ };
327
+ return {
328
+ content: sb,
329
+ source,
330
+ scale: sb.width / source.width,
331
+ progress,
332
+ };
333
+ }
334
+ export function activeViewTransform(effects, timeMs) {
335
+ for (const effect of effects) {
336
+ if (effect.effect !== 'zoom')
337
+ continue;
338
+ const view = evaluateViewTransform(effect, timeMs);
339
+ if (view)
340
+ return view;
341
+ }
342
+ return undefined;
343
+ }
344
+ export function forwardTransformRect(view, rect) {
345
+ const { content: c, source: s, scale } = view;
346
+ const left = Math.max(c.x, c.x + (rect.x - s.x) * scale);
347
+ const top = Math.max(c.y, c.y + (rect.y - s.y) * scale);
348
+ const right = Math.min(c.x + c.width, c.x + (rect.x + rect.width - s.x) * scale);
349
+ const bottom = Math.min(c.y + c.height, c.y + (rect.y + rect.height - s.y) * scale);
350
+ return { x: left, y: top, width: Math.max(0, right - left), height: Math.max(0, bottom - top) };
351
+ }
352
+ export function inverseTransformPoint(view, px, py) {
353
+ return {
354
+ x: view.source.x + (px - view.content.x) / view.scale,
355
+ y: view.source.y + (py - view.content.y) / view.scale,
356
+ };
357
+ }
358
+ function anchorPoint(fragments, anchor, sb) {
359
+ let minX = Infinity;
360
+ let minY = Infinity;
361
+ let maxX = -Infinity;
362
+ let maxY = -Infinity;
363
+ for (const f of fragments) {
364
+ if (f.x < minX)
365
+ minX = f.x;
366
+ if (f.y < minY)
367
+ minY = f.y;
368
+ if (f.x + f.width > maxX)
369
+ maxX = f.x + f.width;
370
+ if (f.y + f.height > maxY)
371
+ maxY = f.y + f.height;
372
+ }
373
+ const nx = anchor.includes('left') ? minX : anchor.includes('right') ? maxX : (minX + maxX) / 2;
374
+ const ny = anchor.includes('top') ? minY : anchor.includes('bottom') ? maxY : (minY + maxY) / 2;
375
+ return {
376
+ x: Math.max(sb.x, Math.min(sb.x + sb.width, sb.x + nx * sb.width)),
377
+ y: Math.max(sb.y, Math.min(sb.y + sb.height, sb.y + ny * sb.height)),
378
+ };
379
+ }
380
+ function catmullRomPoint(p0, p1, p2, p3, t) {
381
+ const t2 = t * t;
382
+ const t3 = t2 * t;
383
+ return {
384
+ x: 0.5 * ((2 * p1.x) + (-p0.x + p2.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (-p0.x + 3 * p1.x - 3 * p2.x + p3.x) * t3),
385
+ y: 0.5 * ((2 * p1.y) + (-p0.y + p2.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (-p0.y + 3 * p1.y - 3 * p2.y + p3.y) * t3),
386
+ };
387
+ }
388
+ export function laserPaint(annotation, slideGeometry, resolveTarget) {
389
+ const { appearance, motion } = annotation;
390
+ const { content, viewport } = slideGeometry;
391
+ const clipLeft = Math.max(0, Math.round(content.x));
392
+ const clipTop = Math.max(0, Math.round(content.y));
393
+ const clipRight = Math.min(viewport.width, Math.round(content.x + content.width));
394
+ const clipBottom = Math.min(viewport.height, Math.round(content.y + content.height));
395
+ const slide_bounds = {
396
+ x: clipLeft,
397
+ y: clipTop,
398
+ width: Math.max(0, clipRight - clipLeft),
399
+ height: Math.max(0, clipBottom - clipTop),
400
+ };
401
+ const waypoints = annotation.target.points.map(point => {
402
+ const dwell_ms = point.dwell_ms ?? 0;
403
+ if ('target' in point) {
404
+ const fragments = resolveTarget(point.target);
405
+ if (!fragments || fragments.length === 0) {
406
+ throw new Error(`Missing geometry for laser waypoint target ${targetKey(point.target)}`);
407
+ }
408
+ const pt = anchorPoint(fragments, point.anchor ?? 'center', slide_bounds);
409
+ return { x: pt.x, y: pt.y, dwell_ms };
410
+ }
411
+ return {
412
+ x: Math.max(slide_bounds.x, Math.min(slide_bounds.x + slide_bounds.width, slide_bounds.x + point.x * slide_bounds.width)),
413
+ y: Math.max(slide_bounds.y, Math.min(slide_bounds.y + slide_bounds.height, slide_bounds.y + point.y * slide_bounds.height)),
414
+ dwell_ms,
415
+ };
416
+ });
417
+ const interpolation = appearance?.interpolation ?? 'smooth';
418
+ const rawSegments = [];
419
+ for (let i = 0; i < waypoints.length - 1; i++) {
420
+ const p1 = waypoints[i];
421
+ const p2 = waypoints[i + 1];
422
+ const samples = [];
423
+ if (interpolation === 'smooth' && waypoints.length >= 3) {
424
+ const p0 = waypoints[Math.max(0, i - 1)];
425
+ const p3 = waypoints[Math.min(waypoints.length - 1, i + 2)];
426
+ const steps = 24;
427
+ for (let s = 0; s <= steps; s++) {
428
+ const pt = catmullRomPoint(p0, p1, p2, p3, s / steps);
429
+ samples.push({
430
+ x: Math.max(slide_bounds.x, Math.min(slide_bounds.x + slide_bounds.width, pt.x)),
431
+ y: Math.max(slide_bounds.y, Math.min(slide_bounds.y + slide_bounds.height, pt.y)),
432
+ });
433
+ }
434
+ }
435
+ else {
436
+ samples.push({ x: p1.x, y: p1.y }, { x: p2.x, y: p2.y });
437
+ }
438
+ const cumulative = [0];
439
+ let length = 0;
440
+ for (let s = 1; s < samples.length; s++) {
441
+ length += Math.hypot(samples[s].x - samples[s - 1].x, samples[s].y - samples[s - 1].y);
442
+ cumulative.push(length);
443
+ }
444
+ rawSegments.push({ samples, cumulative, length });
445
+ }
446
+ const total_length = rawSegments.reduce((sum, seg) => sum + seg.length, 0);
447
+ const totalDwell = waypoints.reduce((sum, wp) => sum + wp.dwell_ms, 0);
448
+ const durationMs = Math.max(0, annotation.end_ms - annotation.start_ms);
449
+ const travelMs = Math.max(0, durationMs - totalDwell);
450
+ const segments = [];
451
+ let cursor = annotation.start_ms;
452
+ for (let i = 0; i < rawSegments.length; i++) {
453
+ cursor += waypoints[i].dwell_ms;
454
+ const segDur = total_length > 1e-6
455
+ ? travelMs * (rawSegments[i].length / total_length)
456
+ : rawSegments.length > 0 ? travelMs / rawSegments.length : 0;
457
+ segments.push({
458
+ start_ms: cursor,
459
+ end_ms: cursor + segDur,
460
+ ...rawSegments[i],
461
+ });
462
+ cursor += segDur;
463
+ }
464
+ const radius_px = Math.max(1, (appearance?.radius ?? DEFAULT_LASER_RADIUS) * slide_bounds.height);
465
+ const glow_px = Math.max(0, (appearance?.glow ?? DEFAULT_LASER_GLOW) * slide_bounds.height);
466
+ return {
467
+ effect: 'laser',
468
+ start_ms: annotation.start_ms,
469
+ end_ms: annotation.end_ms,
470
+ in_ms: motion?.in_ms ?? 0,
471
+ out_ms: motion?.out_ms ?? 0,
472
+ color: (appearance?.color ?? DEFAULT_LASER_COLOR).toLowerCase(),
473
+ opacity: appearance?.opacity ?? DEFAULT_LASER_OPACITY,
474
+ radius_px,
475
+ glow_px,
476
+ trail_ms: appearance?.trail_ms ?? DEFAULT_LASER_TRAIL_MS,
477
+ interpolation,
478
+ slide_bounds,
479
+ waypoints,
480
+ segments,
481
+ total_length,
482
+ };
483
+ }
484
+ export function laserVisible(effect) {
485
+ return effect.slide_bounds.width > 0 && effect.slide_bounds.height > 0 && effect.waypoints.length > 0 && effect.radius_px > 0;
486
+ }
487
+ export function laserFadeFraction(effect, timeMs) {
488
+ const fadeIn = effect.in_ms > 0 ? (timeMs - effect.start_ms) / effect.in_ms : 1;
489
+ const fadeOut = effect.out_ms > 0 ? (effect.end_ms - timeMs) / effect.out_ms : 1;
490
+ return Math.max(0, Math.min(1, fadeIn, fadeOut));
491
+ }
492
+ export function evaluateLaserPosition(effect, timeMs) {
493
+ if (effect.waypoints.length === 1 || effect.segments.length === 0) {
494
+ return { x: effect.waypoints[0].x, y: effect.waypoints[0].y };
495
+ }
496
+ const clamped = Math.max(effect.start_ms, Math.min(effect.end_ms, timeMs));
497
+ for (let i = 0; i < effect.segments.length; i++) {
498
+ const seg = effect.segments[i];
499
+ if (clamped <= seg.start_ms) {
500
+ return { x: effect.waypoints[i].x, y: effect.waypoints[i].y };
501
+ }
502
+ if (clamped < seg.end_ms && seg.end_ms > seg.start_ms) {
503
+ const frac = (clamped - seg.start_ms) / (seg.end_ms - seg.start_ms);
504
+ if (seg.length <= 1e-6)
505
+ return { x: seg.samples[0].x, y: seg.samples[0].y };
506
+ const targetDist = frac * seg.length;
507
+ let lo = 0;
508
+ let hi = seg.cumulative.length - 1;
509
+ while (lo + 1 < hi) {
510
+ const mid = (lo + hi) >> 1;
511
+ if (seg.cumulative[mid] <= targetDist)
512
+ lo = mid;
513
+ else
514
+ hi = mid;
515
+ }
516
+ const d0 = seg.cumulative[lo];
517
+ const d1 = seg.cumulative[hi];
518
+ const alpha = d1 > d0 ? (targetDist - d0) / (d1 - d0) : 0;
519
+ return {
520
+ x: seg.samples[lo].x + (seg.samples[hi].x - seg.samples[lo].x) * alpha,
521
+ y: seg.samples[lo].y + (seg.samples[hi].y - seg.samples[lo].y) * alpha,
522
+ };
523
+ }
524
+ }
525
+ const last = effect.waypoints[effect.waypoints.length - 1];
526
+ return { x: last.x, y: last.y };
527
+ }
528
+ export function evaluateLaserTrail(effect, timeMs) {
529
+ if (effect.trail_ms <= 0 || effect.total_length <= 1e-6)
530
+ return [];
531
+ const startT = Math.max(effect.start_ms, timeMs - effect.trail_ms);
532
+ const span = timeMs - startT;
533
+ if (span <= 1e-3)
534
+ return [];
535
+ const steps = Math.min(28, Math.max(4, Math.ceil(span / 12)));
536
+ const points = [];
537
+ for (let i = 0; i < steps; i++) {
538
+ const w = (i + 1) / (steps + 1);
539
+ const pt = evaluateLaserPosition(effect, startT + span * w);
540
+ points.push({ x: pt.x, y: pt.y, weight: w });
541
+ }
542
+ return points;
543
+ }
544
+ export function stampFontCss(font, sizePx) {
545
+ const px = Number(sizePx.toFixed(2));
546
+ if (font === 'mono')
547
+ return `700 ${px}px "SlideVideoMono"`;
548
+ if (font === 'serif')
549
+ return `italic 400 ${px}px "SlideVideoSerif"`;
550
+ return `700 ${px}px "SlideVideoSans"`;
551
+ }
552
+ function wrapStampText(text, fontCss, maxTextWidth, overflow, measureText) {
553
+ const paragraphs = text.replace(/\r\n/g, '\n').split('\n');
554
+ const lines = [];
555
+ let overflowError;
556
+ let clipped = false;
557
+ if (maxTextWidth !== undefined && maxTextWidth <= 1) {
558
+ if (overflow === 'error') {
559
+ return { lines: paragraphs, maxLineWidth: 0, overflowError: 'Stamp max_width leaves no room for text after padding and symbol', clipped: false };
560
+ }
561
+ clipped = true;
562
+ }
563
+ for (const paragraph of paragraphs) {
564
+ if (maxTextWidth === undefined || maxTextWidth <= 1) {
565
+ lines.push(paragraph);
566
+ continue;
567
+ }
568
+ const words = paragraph.trim().length > 0 ? paragraph.trim().split(/\s+/) : [''];
569
+ let current = '';
570
+ for (const word of words) {
571
+ const candidate = current ? `${current} ${word}` : word;
572
+ if (measureText(fontCss, candidate) <= maxTextWidth + 0.5) {
573
+ current = candidate;
574
+ }
575
+ else if (!current) {
576
+ if (overflow === 'error' && !overflowError) {
577
+ overflowError = `Stamp word ${JSON.stringify(word)} exceeds max_width`;
578
+ }
579
+ else {
580
+ clipped = true;
581
+ }
582
+ lines.push(word);
583
+ }
584
+ else {
585
+ lines.push(current);
586
+ if (measureText(fontCss, word) > maxTextWidth + 0.5) {
587
+ if (overflow === 'error' && !overflowError) {
588
+ overflowError = `Stamp word ${JSON.stringify(word)} exceeds max_width`;
589
+ }
590
+ else {
591
+ clipped = true;
592
+ }
593
+ lines.push(word);
594
+ current = '';
595
+ }
596
+ else {
597
+ current = word;
598
+ }
599
+ }
600
+ }
601
+ if (current || words.length === 1 && words[0] === '')
602
+ lines.push(current);
603
+ }
604
+ let maxLineWidth = 0;
605
+ for (const line of lines) {
606
+ const w = measureText(fontCss, line);
607
+ if (w > maxLineWidth)
608
+ maxLineWidth = w;
609
+ }
610
+ return { lines, maxLineWidth, overflowError, clipped };
611
+ }
612
+ function rotatedBoxBounds(cx, cy, width, height, rotationRad) {
613
+ const hw = width / 2;
614
+ const hh = height / 2;
615
+ const c = Math.abs(Math.cos(rotationRad));
616
+ const s = Math.abs(Math.sin(rotationRad));
617
+ const bw = hw * c + hh * s;
618
+ const bh = hw * s + hh * c;
619
+ return { x: cx - bw, y: cy - bh, width: bw * 2, height: bh * 2 };
620
+ }
621
+ export function stampPaint(annotation, geometry, measureText) {
622
+ const { content } = geometry;
623
+ const app = annotation.appearance;
624
+ const anchor = app.anchor ?? 'center';
625
+ let anchorX;
626
+ let anchorY;
627
+ if (annotation.target.type === 'point') {
628
+ anchorX = content.x + annotation.target.x * content.width;
629
+ anchorY = content.y + annotation.target.y * content.height;
630
+ }
631
+ else {
632
+ const pt = anchorPoint(geometry.fragments, anchor, content);
633
+ anchorX = pt.x;
634
+ anchorY = pt.y;
635
+ }
636
+ if (app.offset) {
637
+ anchorX += app.offset.x * content.width;
638
+ anchorY += app.offset.y * content.height;
639
+ }
640
+ const fontKey = app.font ?? 'sans';
641
+ const fontSizePx = Math.max(8, (app.size ?? 0.036) * content.height);
642
+ const lineHeightPx = fontSizePx * 1.24;
643
+ const fontCss = stampFontCss(fontKey, fontSizePx);
644
+ const padX = (app.padding?.x ?? 0.018) * content.width;
645
+ const padY = (app.padding?.y ?? 0.012) * content.height;
646
+ const symbolSizePx = app.symbol ? fontSizePx * 0.96 : 0;
647
+ const symbolGapPx = app.symbol && app.text ? fontSizePx * 0.42 : 0;
648
+ const overflow = app.overflow ?? 'error';
649
+ const maxBoxWidth = app.max_width !== undefined ? app.max_width * content.width : undefined;
650
+ const maxTextWidth = maxBoxWidth !== undefined ? maxBoxWidth - 2 * padX - symbolSizePx - symbolGapPx : undefined;
651
+ let lines = [];
652
+ let textWidth = 0;
653
+ let overflowError;
654
+ let clipped = false;
655
+ if (app.text !== undefined) {
656
+ const wrapped = wrapStampText(app.text, fontCss, maxTextWidth, overflow, measureText);
657
+ lines = wrapped.lines;
658
+ textWidth = wrapped.maxLineWidth;
659
+ overflowError = wrapped.overflowError;
660
+ clipped = wrapped.clipped;
661
+ }
662
+ const innerWidth = symbolSizePx + symbolGapPx + textWidth;
663
+ const innerHeight = Math.max(symbolSizePx, lines.length > 0 ? lines.length * lineHeightPx : 0);
664
+ const width = maxBoxWidth !== undefined && overflow === 'clip'
665
+ ? Math.max(2, Math.min(maxBoxWidth, innerWidth + 2 * padX))
666
+ : Math.max(2, innerWidth + 2 * padX);
667
+ const height = Math.max(2, innerHeight + 2 * padY);
668
+ const motionType = annotation.motion?.type ?? 'none';
669
+ const startScale = annotation.motion?.type === 'pop' ? (annotation.motion.start_scale ?? 1.35) : 1;
670
+ const maxScale = motionType === 'pop' && (annotation.motion?.in_ms ?? 0) > 0 ? Math.max(1, startScale) : 1;
671
+ const radiusPx = Math.min(width / 2, height / 2, (app.radius ?? 0.012) * content.height);
672
+ const rotationRad = ((app.rotation_deg ?? 0) * Math.PI) / 180;
673
+ const borderWidthPx = app.border ? Math.max(1, (app.border.width ?? 0.004) * content.height) : 0;
674
+ const outer = rotatedBoxBounds(anchorX, anchorY, (width + borderWidthPx) * maxScale, (height + borderWidthPx) * maxScale, rotationRad);
675
+ if (!overflowError && overflow === 'error') {
676
+ const tol = 1;
677
+ if (outer.x < content.x - tol
678
+ || outer.y < content.y - tol
679
+ || outer.x + outer.width > content.x + content.width + tol
680
+ || outer.y + outer.height > content.y + content.height + tol) {
681
+ overflowError = 'Stamp extends outside the slide-content rectangle; reduce size, padding, or max_width, adjust anchor/offset, or set appearance.overflow: clip';
682
+ }
683
+ }
684
+ return {
685
+ effect: 'stamp',
686
+ start_ms: annotation.start_ms,
687
+ end_ms: annotation.end_ms,
688
+ motion_type: motionType,
689
+ in_ms: annotation.motion?.in_ms ?? 0,
690
+ out_ms: annotation.motion?.out_ms ?? 0,
691
+ start_scale: startScale,
692
+ anchor_x: anchorX,
693
+ anchor_y: anchorY,
694
+ width,
695
+ height,
696
+ padding_x: padX,
697
+ padding_y: padY,
698
+ radius_px: radiusPx,
699
+ rotation_rad: rotationRad,
700
+ color: app.color ?? '#ffffff',
701
+ opacity: app.opacity ?? 1,
702
+ ...(app.background ? { background: app.background } : {}),
703
+ background_opacity: app.background ? (app.background_opacity ?? 0.95) : 0,
704
+ ...(app.border ? { border: { color: app.border.color, opacity: app.border.opacity ?? 1, width_px: borderWidthPx } } : {}),
705
+ font_css: fontCss,
706
+ font_size_px: fontSizePx,
707
+ line_height_px: lineHeightPx,
708
+ lines,
709
+ align: app.align ?? 'center',
710
+ ...(app.symbol ? { symbol: app.symbol } : {}),
711
+ symbol_size_px: symbolSizePx,
712
+ symbol_gap_px: symbolGapPx,
713
+ clip_content: clipped || overflow === 'clip',
714
+ slide_bounds: content,
715
+ ...(overflowError ? { overflow_error: overflowError } : {}),
716
+ };
717
+ }
718
+ export function stampMotionState(effect, timeMs) {
719
+ if (timeMs < effect.start_ms || timeMs >= effect.end_ms)
720
+ return { opacity: 0, scale: 1 };
721
+ if (effect.motion_type === 'none')
722
+ return { opacity: effect.opacity, scale: 1 };
723
+ const elapsed = timeMs - effect.start_ms;
724
+ const remaining = effect.end_ms - timeMs;
725
+ const fadeOut = effect.out_ms > 0 ? Math.max(0, Math.min(1, remaining / effect.out_ms)) : 1;
726
+ if (effect.motion_type === 'fade') {
727
+ const fadeIn = effect.in_ms > 0 ? Math.max(0, Math.min(1, elapsed / effect.in_ms)) : 1;
728
+ return { opacity: effect.opacity * Math.min(fadeIn, fadeOut), scale: 1 };
729
+ }
730
+ // pop: ease-out cubic scale from start_scale to 1.0 over in_ms
731
+ if (effect.in_ms > 0 && elapsed < effect.in_ms) {
732
+ const raw = Math.max(0, Math.min(1, elapsed / effect.in_ms));
733
+ const eased = 1 - (1 - raw) ** 3;
734
+ const scale = effect.start_scale + (1 - effect.start_scale) * eased;
735
+ const fadeIn = Math.min(1, raw * 1.4);
736
+ return { opacity: effect.opacity * Math.min(fadeIn, fadeOut), scale };
737
+ }
738
+ return { opacity: effect.opacity * fadeOut, scale: 1 };
739
+ }
740
+ export function stampVisible(effect) {
741
+ const sb = effect.slide_bounds;
742
+ if (sb.width <= 0 || sb.height <= 0 || effect.width < 2 || effect.height < 2)
743
+ return false;
744
+ const border = effect.border?.width_px ?? 0;
745
+ const box = rotatedBoxBounds(effect.anchor_x, effect.anchor_y, effect.width + border, effect.height + border, effect.rotation_rad);
746
+ const left = Math.max(sb.x, box.x);
747
+ const top = Math.max(sb.y, box.y);
748
+ const right = Math.min(sb.x + sb.width, box.x + box.width);
749
+ const bottom = Math.min(sb.y + sb.height, box.y + box.height);
750
+ return right - left >= 2 && bottom - top >= 2;
751
+ }
752
+ export function stampDirtyBounds(effect, timeMs) {
753
+ const { opacity, scale } = stampMotionState(effect, timeMs);
754
+ if (opacity <= 1e-6 || !stampVisible(effect))
755
+ return undefined;
756
+ const border = effect.border?.width_px ?? 0;
757
+ const w = (effect.width + border) * scale + 4;
758
+ const h = (effect.height + border) * scale + 4;
759
+ const box = rotatedBoxBounds(effect.anchor_x, effect.anchor_y, w, h, effect.rotation_rad);
760
+ const sb = effect.slide_bounds;
761
+ const x = Math.max(sb.x, Math.floor(box.x));
762
+ const y = Math.max(sb.y, Math.floor(box.y));
763
+ const right = Math.min(sb.x + sb.width, Math.ceil(box.x + box.width));
764
+ const bottom = Math.min(sb.y + sb.height, Math.ceil(box.y + box.height));
765
+ return right > x && bottom > y ? { x, y, width: right - x, height: bottom - y } : undefined;
766
+ }
767
+ function addRoundedRectPath(context, x, y, w, h, r) {
768
+ const rad = Math.max(0, Math.min(w / 2, h / 2, r));
769
+ context.beginPath();
770
+ if (rad <= 0) {
771
+ context.rect(x, y, w, h);
772
+ return;
773
+ }
774
+ context.moveTo(x + rad, y);
775
+ context.lineTo(x + w - rad, y);
776
+ context.arcTo(x + w, y, x + w, y + rad, rad);
777
+ context.lineTo(x + w, y + h - rad);
778
+ context.arcTo(x + w, y + h, x + w - rad, y + h, rad);
779
+ context.lineTo(x + rad, y + h);
780
+ context.arcTo(x, y + h, x, y + h - rad, rad);
781
+ context.lineTo(x, y + rad);
782
+ context.arcTo(x, y, x + rad, y, rad);
783
+ context.closePath();
784
+ }
785
+ function paintStampSymbol(context, symbol, cx, cy, size, color, alpha) {
786
+ const strokeW = Math.max(2, size * 0.16);
787
+ context.save();
788
+ context.globalAlpha = alpha;
789
+ context.strokeStyle = color;
790
+ context.fillStyle = color;
791
+ context.lineWidth = strokeW;
792
+ context.lineCap = 'round';
793
+ context.lineJoin = 'round';
794
+ if (symbol === 'check') {
795
+ context.beginPath();
796
+ context.moveTo(cx - size * 0.36, cy + size * 0.02);
797
+ context.lineTo(cx - size * 0.10, cy + size * 0.28);
798
+ context.lineTo(cx + size * 0.38, cy - size * 0.28);
799
+ context.stroke();
800
+ }
801
+ else if (symbol === 'cross') {
802
+ const d = size * 0.30;
803
+ context.beginPath();
804
+ context.moveTo(cx - d, cy - d);
805
+ context.lineTo(cx + d, cy + d);
806
+ context.moveTo(cx + d, cy - d);
807
+ context.lineTo(cx - d, cy + d);
808
+ context.stroke();
809
+ }
810
+ else if (symbol === 'warning') {
811
+ context.lineWidth = Math.max(1.8, size * 0.11);
812
+ context.beginPath();
813
+ context.moveTo(cx, cy - size * 0.40);
814
+ context.lineTo(cx + size * 0.42, cy + size * 0.34);
815
+ context.lineTo(cx - size * 0.42, cy + size * 0.34);
816
+ context.closePath();
817
+ context.stroke();
818
+ context.beginPath();
819
+ context.moveTo(cx, cy - size * 0.14);
820
+ context.lineTo(cx, cy + size * 0.10);
821
+ context.stroke();
822
+ context.beginPath();
823
+ context.ellipse(cx, cy + size * 0.23, size * 0.055, size * 0.055, 0, 0, Math.PI * 2);
824
+ context.fill();
825
+ }
826
+ else if (symbol === 'star') {
827
+ const outer = size * 0.44;
828
+ const inner = size * 0.19;
829
+ context.beginPath();
830
+ for (let i = 0; i < 10; i++) {
831
+ const r = i % 2 === 0 ? outer : inner;
832
+ const angle = -Math.PI / 2 + (i * Math.PI) / 5;
833
+ const px = cx + Math.cos(angle) * r;
834
+ const py = cy + Math.sin(angle) * r;
835
+ if (i === 0)
836
+ context.moveTo(px, py);
837
+ else
838
+ context.lineTo(px, py);
839
+ }
840
+ context.closePath();
841
+ context.fill();
842
+ }
843
+ else if (symbol === 'info') {
844
+ context.lineWidth = Math.max(1.8, size * 0.11);
845
+ context.beginPath();
846
+ context.ellipse(cx, cy, size * 0.40, size * 0.40, 0, 0, Math.PI * 2);
847
+ context.stroke();
848
+ context.beginPath();
849
+ context.ellipse(cx, cy - size * 0.19, size * 0.055, size * 0.055, 0, 0, Math.PI * 2);
850
+ context.fill();
851
+ context.beginPath();
852
+ context.moveTo(cx, cy - size * 0.04);
853
+ context.lineTo(cx, cy + size * 0.22);
854
+ context.stroke();
855
+ }
856
+ else if (symbol === 'arrow-right') {
857
+ context.beginPath();
858
+ context.moveTo(cx - size * 0.36, cy);
859
+ context.lineTo(cx + size * 0.34, cy);
860
+ context.moveTo(cx + size * 0.08, cy - size * 0.26);
861
+ context.lineTo(cx + size * 0.36, cy);
862
+ context.lineTo(cx + size * 0.08, cy + size * 0.26);
863
+ context.stroke();
864
+ }
865
+ context.restore();
866
+ }
867
+ export function paintStamp(context, effect, timeMs) {
868
+ const { opacity, scale } = stampMotionState(effect, timeMs);
869
+ if (opacity <= 1e-6 || !stampVisible(effect))
870
+ return;
871
+ const sb = effect.slide_bounds;
872
+ context.save();
873
+ context.beginPath();
874
+ context.rect(sb.x, sb.y, sb.width, sb.height);
875
+ context.clip();
876
+ context.globalCompositeOperation = 'source-over';
877
+ context.translate(effect.anchor_x, effect.anchor_y);
878
+ if (effect.rotation_rad !== 0)
879
+ context.rotate(effect.rotation_rad);
880
+ if (scale !== 1)
881
+ context.scale(scale, scale);
882
+ const halfW = effect.width / 2;
883
+ const halfH = effect.height / 2;
884
+ if (effect.background) {
885
+ addRoundedRectPath(context, -halfW, -halfH, effect.width, effect.height, effect.radius_px);
886
+ context.fillStyle = effect.background;
887
+ context.globalAlpha = opacity * effect.background_opacity;
888
+ context.fill();
889
+ }
890
+ if (effect.border) {
891
+ addRoundedRectPath(context, -halfW, -halfH, effect.width, effect.height, effect.radius_px);
892
+ context.strokeStyle = effect.border.color;
893
+ context.lineWidth = effect.border.width_px;
894
+ context.lineJoin = 'round';
895
+ context.globalAlpha = opacity * effect.border.opacity;
896
+ context.stroke();
897
+ }
898
+ if (effect.clip_content) {
899
+ addRoundedRectPath(context, -halfW, -halfH, effect.width, effect.height, effect.radius_px);
900
+ context.clip();
901
+ }
902
+ const innerLeft = -halfW + effect.padding_x;
903
+ const innerWidth = Math.max(0, effect.width - 2 * effect.padding_x);
904
+ if (effect.symbol) {
905
+ const symCx = effect.lines.length > 0 ? innerLeft + effect.symbol_size_px / 2 : 0;
906
+ paintStampSymbol(context, effect.symbol, symCx, 0, effect.symbol_size_px, effect.color, opacity);
907
+ }
908
+ if (effect.lines.length > 0) {
909
+ const textAreaLeft = innerLeft + effect.symbol_size_px + effect.symbol_gap_px;
910
+ const textAreaWidth = Math.max(0, innerWidth - effect.symbol_size_px - effect.symbol_gap_px);
911
+ const totalTextHeight = effect.lines.length * effect.line_height_px;
912
+ const firstLineY = -totalTextHeight / 2 + effect.line_height_px / 2;
913
+ let textX;
914
+ if (effect.align === 'left')
915
+ textX = textAreaLeft;
916
+ else if (effect.align === 'right')
917
+ textX = textAreaLeft + textAreaWidth;
918
+ else
919
+ textX = textAreaLeft + textAreaWidth / 2;
920
+ context.font = effect.font_css;
921
+ context.fillStyle = effect.color;
922
+ context.textAlign = effect.align;
923
+ context.textBaseline = 'middle';
924
+ context.globalAlpha = opacity;
925
+ for (let i = 0; i < effect.lines.length; i++) {
926
+ context.fillText(effect.lines[i], textX, firstLineY + i * effect.line_height_px);
927
+ }
928
+ }
929
+ context.restore();
930
+ }
931
+ export function hasActiveStamp(effects, timeMs) {
932
+ return effects.some(effect => effect.effect === 'stamp' && stampMotionState(effect, timeMs).opacity > 1e-6 && stampVisible(effect));
933
+ }
934
+ export function paintStamps(context, effects, timeMs) {
935
+ for (const effect of effects) {
936
+ if (effect.effect === 'stamp')
937
+ paintStamp(context, effect, timeMs);
938
+ }
939
+ }
940
+ export function laserDirtyBounds(effect, timeMs) {
941
+ const fade = laserFadeFraction(effect, timeMs);
942
+ if (fade <= 1e-6 || !laserVisible(effect))
943
+ return undefined;
944
+ const reach = effect.radius_px + effect.glow_px + 2;
945
+ const head = evaluateLaserPosition(effect, timeMs);
946
+ let minX = head.x - reach;
947
+ let minY = head.y - reach;
948
+ let maxX = head.x + reach;
949
+ let maxY = head.y + reach;
950
+ for (const pt of evaluateLaserTrail(effect, timeMs)) {
951
+ const r = (effect.radius_px + effect.glow_px * 0.5) * pt.weight + 2;
952
+ if (pt.x - r < minX)
953
+ minX = pt.x - r;
954
+ if (pt.y - r < minY)
955
+ minY = pt.y - r;
956
+ if (pt.x + r > maxX)
957
+ maxX = pt.x + r;
958
+ if (pt.y + r > maxY)
959
+ maxY = pt.y + r;
960
+ }
961
+ const sb = effect.slide_bounds;
962
+ const x = Math.max(sb.x, Math.floor(minX));
963
+ const y = Math.max(sb.y, Math.floor(minY));
964
+ const right = Math.min(sb.x + sb.width, Math.ceil(maxX));
965
+ const bottom = Math.min(sb.y + sb.height, Math.ceil(maxY));
966
+ return right > x && bottom > y ? { x, y, width: right - x, height: bottom - y } : undefined;
967
+ }
968
+ export function hasActiveLaser(effects, timeMs) {
969
+ return effects.some(effect => effect.effect === 'laser' && laserFadeFraction(effect, timeMs) > 1e-6 && laserVisible(effect));
970
+ }
971
+ export function paintLaser(context, effect, timeMs) {
972
+ const fade = laserFadeFraction(effect, timeMs);
973
+ if (fade <= 1e-6 || !laserVisible(effect))
974
+ return;
975
+ const alpha = effect.opacity * fade;
976
+ const sb = effect.slide_bounds;
977
+ context.save();
978
+ context.beginPath();
979
+ context.rect(sb.x, sb.y, sb.width, sb.height);
980
+ context.clip();
981
+ context.globalCompositeOperation = 'source-over';
982
+ const trail = evaluateLaserTrail(effect, timeMs);
983
+ context.fillStyle = effect.color;
984
+ for (const pt of trail) {
985
+ const r = Math.max(1, effect.radius_px * (0.35 + 0.65 * pt.weight));
986
+ context.globalAlpha = alpha * 0.32 * pt.weight * pt.weight;
987
+ context.beginPath();
988
+ context.ellipse(pt.x, pt.y, r, r, 0, 0, Math.PI * 2);
989
+ context.fill();
990
+ }
991
+ const head = evaluateLaserPosition(effect, timeMs);
992
+ if (effect.glow_px > 0) {
993
+ const rings = 6;
994
+ for (let k = rings; k >= 1; k--) {
995
+ const frac = k / rings;
996
+ const r = effect.radius_px + effect.glow_px * frac;
997
+ const ringAlpha = alpha * 0.14 * (1 - frac * 0.72) * (1 - frac * 0.72);
998
+ context.globalAlpha = ringAlpha;
999
+ context.beginPath();
1000
+ context.ellipse(head.x, head.y, r, r, 0, 0, Math.PI * 2);
1001
+ context.fill();
1002
+ }
1003
+ }
1004
+ context.fillStyle = effect.color;
1005
+ context.globalAlpha = alpha;
1006
+ context.beginPath();
1007
+ context.ellipse(head.x, head.y, effect.radius_px, effect.radius_px, 0, 0, Math.PI * 2);
1008
+ context.fill();
1009
+ const coreRadius = Math.max(1, effect.radius_px * 0.45);
1010
+ context.fillStyle = '#ffffff';
1011
+ context.globalAlpha = alpha * 0.85;
1012
+ context.beginPath();
1013
+ context.ellipse(head.x, head.y, coreRadius, coreRadius, 0, 0, Math.PI * 2);
1014
+ context.fill();
1015
+ context.restore();
1016
+ }
1017
+ export function paintLasers(context, effects, timeMs) {
1018
+ for (const effect of effects) {
1019
+ if (effect.effect === 'laser')
1020
+ paintLaser(context, effect, timeMs);
1021
+ }
1022
+ }
1023
+ export function laserDotSample(effect, timeMs, px, py) {
1024
+ const fade = laserFadeFraction(effect, timeMs);
1025
+ if (fade <= 1e-6 || !laserVisible(effect))
1026
+ return { alpha: 0, rgb: [0, 0, 0] };
1027
+ const head = evaluateLaserPosition(effect, timeMs);
1028
+ const dist = Math.hypot(px - head.x, py - head.y);
1029
+ const alpha = effect.opacity * fade;
1030
+ const colorRgb = parseHexRgb(effect.color);
1031
+ const coreRadius = Math.max(1, effect.radius_px * 0.45);
1032
+ if (dist <= coreRadius) {
1033
+ const whiteAlpha = alpha * 0.85;
1034
+ // Composite color dot then white hot core over base
1035
+ const r = colorRgb[0] * (1 - whiteAlpha) + 255 * whiteAlpha;
1036
+ const g = colorRgb[1] * (1 - whiteAlpha) + 255 * whiteAlpha;
1037
+ const b = colorRgb[2] * (1 - whiteAlpha) + 255 * whiteAlpha;
1038
+ const combinedAlpha = 1 - (1 - alpha) * (1 - whiteAlpha);
1039
+ return {
1040
+ alpha: combinedAlpha,
1041
+ rgb: [r, g, b],
1042
+ };
1043
+ }
1044
+ if (dist <= effect.radius_px) {
1045
+ return { alpha, rgb: colorRgb };
1046
+ }
1047
+ return { alpha: 0, rgb: colorRgb };
1048
+ }
156
1049
  export function annotationPaint(annotation, geometry) {
157
1050
  switch (annotation.effect) {
158
1051
  case 'highlight':
159
1052
  return highlightPaint(annotation, geometry);
160
1053
  case 'underline':
161
1054
  return underlinePaint(annotation, geometry);
1055
+ case 'spotlight':
1056
+ return spotlightPaint(annotation, geometry);
1057
+ case 'zoom':
1058
+ return zoomPaint(annotation, geometry);
162
1059
  }
163
1060
  }
164
1061
  export function underlineFragmentsVisible(effect) {
165
1062
  return effect.fragments.every(fragment => fragment.rectangle.width > 0 && fragment.rectangle.height > 0);
166
1063
  }
1064
+ export function spotlightAperturesVisible(effect) {
1065
+ const { slide_bounds: sb, apertures } = effect;
1066
+ if (sb.width <= 0 || sb.height <= 0 || apertures.length === 0)
1067
+ return false;
1068
+ return apertures.every(ap => {
1069
+ if (ap.shape === 'rounded-rect') {
1070
+ return ap.width > 0 && ap.height > 0 &&
1071
+ Math.min(ap.x + ap.width, sb.x + sb.width) > Math.max(ap.x, sb.x) &&
1072
+ Math.min(ap.y + ap.height, sb.y + sb.height) > Math.max(ap.y, sb.y);
1073
+ }
1074
+ if (ap.rx <= 0 || ap.ry <= 0)
1075
+ return false;
1076
+ const px = Math.max(sb.x, Math.min(sb.x + sb.width, ap.cx));
1077
+ const py = Math.max(sb.y, Math.min(sb.y + sb.height, ap.cy));
1078
+ return ((px - ap.cx) / ap.rx) ** 2 + ((py - ap.cy) / ap.ry) ** 2 < 1;
1079
+ });
1080
+ }
1081
+ export function spotlightApertureBounds(aperture) {
1082
+ if (aperture.shape === 'rounded-rect') {
1083
+ return { x: aperture.x, y: aperture.y, width: aperture.width, height: aperture.height };
1084
+ }
1085
+ return { x: aperture.cx - aperture.rx, y: aperture.cy - aperture.ry, width: aperture.rx * 2, height: aperture.ry * 2 };
1086
+ }
1087
+ export function pointInExpandedAperture(aperture, expandPx, px, py) {
1088
+ if (aperture.shape === 'rounded-rect') {
1089
+ const ew = aperture.width + 2 * expandPx;
1090
+ const eh = aperture.height + 2 * expandPx;
1091
+ if (ew <= 0 || eh <= 0)
1092
+ return false;
1093
+ const ex = aperture.x - expandPx;
1094
+ const ey = aperture.y - expandPx;
1095
+ const er = Math.max(0, Math.min(ew / 2, eh / 2, aperture.radius + expandPx));
1096
+ const qx = Math.abs(px - (ex + ew / 2)) - (ew / 2 - er);
1097
+ const qy = Math.abs(py - (ey + eh / 2)) - (eh / 2 - er);
1098
+ return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - er <= 0;
1099
+ }
1100
+ const erx = aperture.rx + expandPx;
1101
+ const ery = aperture.ry + expandPx;
1102
+ if (erx <= 0 || ery <= 0)
1103
+ return false;
1104
+ return ((px - aperture.cx) / erx) ** 2 + ((py - aperture.cy) / ery) ** 2 <= 1;
1105
+ }
1106
+ export function spotlightFadeFraction(effect, timeMs) {
1107
+ const fadeIn = effect.in_ms > 0 ? (timeMs - effect.start_ms) / effect.in_ms : 1;
1108
+ const fadeOut = effect.out_ms > 0 ? (effect.end_ms - timeMs) / effect.out_ms : 1;
1109
+ return Math.max(0, Math.min(1, fadeIn, fadeOut));
1110
+ }
1111
+ function parseHexRgb(color) {
1112
+ return [
1113
+ Number.parseInt(color.slice(1, 3), 16),
1114
+ Number.parseInt(color.slice(3, 5), 16),
1115
+ Number.parseInt(color.slice(5, 7), 16),
1116
+ ];
1117
+ }
1118
+ function formatHexRgb([r, g, b]) {
1119
+ const hex = (v) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, '0');
1120
+ return `#${hex(r)}${hex(g)}${hex(b)}`;
1121
+ }
1122
+ function buildFeatherSteps(eraseScale, softnessPx) {
1123
+ if (eraseScale <= 0)
1124
+ return [];
1125
+ const count = softnessPx > 0 ? Math.min(24, Math.max(4, Math.ceil(softnessPx / 2))) : 0;
1126
+ if (count === 0)
1127
+ return [{ expandPx: 0, stepAlpha: eraseScale }];
1128
+ const steps = [];
1129
+ let nextCumulativeErase = 0;
1130
+ for (let k = count; k >= 0; k--) {
1131
+ const t = k / (count + 1);
1132
+ const smooth = 1 - t * t * (3 - 2 * t);
1133
+ const cumulativeErase = eraseScale * smooth;
1134
+ const denom = 1 - nextCumulativeErase;
1135
+ const stepAlpha = denom > 1e-9 ? Math.max(0, Math.min(1, (cumulativeErase - nextCumulativeErase) / denom)) : 0;
1136
+ const expandPx = k === 0 ? 0 : (softnessPx * k) / count;
1137
+ if (stepAlpha > 0)
1138
+ steps.push({ expandPx, stepAlpha });
1139
+ nextCumulativeErase = cumulativeErase;
1140
+ }
1141
+ return steps;
1142
+ }
1143
+ function evaluateSpotlights(effects, timeMs) {
1144
+ const active = [];
1145
+ let maxDim = 0;
1146
+ let minLeft = Infinity;
1147
+ let minTop = Infinity;
1148
+ let maxRight = -Infinity;
1149
+ let maxBottom = -Infinity;
1150
+ for (const effect of effects) {
1151
+ if (effect.effect !== 'spotlight')
1152
+ continue;
1153
+ const sb = effect.slide_bounds;
1154
+ if (sb.width <= 0 || sb.height <= 0)
1155
+ continue;
1156
+ const dimOpacity = effect.opacity * spotlightFadeFraction(effect, timeMs);
1157
+ if (dimOpacity <= 1e-6)
1158
+ continue;
1159
+ if (dimOpacity > maxDim)
1160
+ maxDim = dimOpacity;
1161
+ if (sb.x < minLeft)
1162
+ minLeft = sb.x;
1163
+ if (sb.y < minTop)
1164
+ minTop = sb.y;
1165
+ if (sb.x + sb.width > maxRight)
1166
+ maxRight = sb.x + sb.width;
1167
+ if (sb.y + sb.height > maxBottom)
1168
+ maxBottom = sb.y + sb.height;
1169
+ active.push({ effect, dimOpacity, rgb: parseHexRgb(effect.color) });
1170
+ }
1171
+ if (!active.length || maxDim <= 1e-6 || !(minLeft < maxRight && minTop < maxBottom))
1172
+ return undefined;
1173
+ let weightSum = 0;
1174
+ let rSum = 0;
1175
+ let gSum = 0;
1176
+ let bSum = 0;
1177
+ for (const item of active) {
1178
+ weightSum += item.dimOpacity;
1179
+ rSum += item.dimOpacity * item.rgb[0];
1180
+ gSum += item.dimOpacity * item.rgb[1];
1181
+ bSum += item.dimOpacity * item.rgb[2];
1182
+ }
1183
+ const rgb = [
1184
+ Math.round(rSum / weightSum),
1185
+ Math.round(gSum / weightSum),
1186
+ Math.round(bSum / weightSum),
1187
+ ];
1188
+ const items = active.map(({ effect, dimOpacity }) => {
1189
+ const eraseScale = Math.max(0, Math.min(1, dimOpacity / maxDim));
1190
+ return {
1191
+ effect,
1192
+ dimOpacity,
1193
+ eraseScale,
1194
+ steps: buildFeatherSteps(eraseScale, effect.softness_px),
1195
+ };
1196
+ });
1197
+ return {
1198
+ bounds: { x: minLeft, y: minTop, width: maxRight - minLeft, height: maxBottom - minTop },
1199
+ rgb,
1200
+ color: formatHexRgb(rgb),
1201
+ backdropOpacity: maxDim,
1202
+ items,
1203
+ };
1204
+ }
1205
+ export function hasActiveSpotlight(effects, timeMs) {
1206
+ return evaluateSpotlights(effects, timeMs) !== undefined;
1207
+ }
1208
+ export function activeSpotlightBounds(effects, timeMs) {
1209
+ return evaluateSpotlights(effects, timeMs)?.bounds;
1210
+ }
1211
+ export function spotlightMaskSample(effects, timeMs, px, py) {
1212
+ const state = evaluateSpotlights(effects, timeMs);
1213
+ if (!state)
1214
+ return { alpha: 0, rgb: [0, 0, 0] };
1215
+ const insideAnySlide = state.items.some(({ effect }) => {
1216
+ const sb = effect.slide_bounds;
1217
+ return px >= sb.x && px < sb.x + sb.width && py >= sb.y && py < sb.y + sb.height;
1218
+ });
1219
+ if (!insideAnySlide)
1220
+ return { alpha: 0, rgb: state.rgb };
1221
+ let survival = 1;
1222
+ for (const item of state.items) {
1223
+ const sb = item.effect.slide_bounds;
1224
+ if (px < sb.x || px >= sb.x + sb.width || py < sb.y || py >= sb.y + sb.height)
1225
+ continue;
1226
+ for (const step of item.steps) {
1227
+ if (item.effect.apertures.some(ap => pointInExpandedAperture(ap, step.expandPx, px, py))) {
1228
+ survival *= 1 - step.stepAlpha;
1229
+ }
1230
+ }
1231
+ }
1232
+ return { alpha: state.backdropOpacity * survival, rgb: state.rgb };
1233
+ }
1234
+ function addExpandedApertureSubpath(context, aperture, expandPx) {
1235
+ if (aperture.shape === 'rounded-rect') {
1236
+ const ew = aperture.width + 2 * expandPx;
1237
+ const eh = aperture.height + 2 * expandPx;
1238
+ if (ew <= 0 || eh <= 0)
1239
+ return;
1240
+ const ex = aperture.x - expandPx;
1241
+ const ey = aperture.y - expandPx;
1242
+ const er = Math.max(0, Math.min(ew / 2, eh / 2, aperture.radius + expandPx));
1243
+ if (er <= 0) {
1244
+ context.moveTo(ex, ey);
1245
+ context.lineTo(ex + ew, ey);
1246
+ context.lineTo(ex + ew, ey + eh);
1247
+ context.lineTo(ex, ey + eh);
1248
+ context.closePath();
1249
+ return;
1250
+ }
1251
+ context.moveTo(ex + er, ey);
1252
+ context.lineTo(ex + ew - er, ey);
1253
+ context.arcTo(ex + ew, ey, ex + ew, ey + er, er);
1254
+ context.lineTo(ex + ew, ey + eh - er);
1255
+ context.arcTo(ex + ew, ey + eh, ex + ew - er, ey + eh, er);
1256
+ context.lineTo(ex + er, ey + eh);
1257
+ context.arcTo(ex, ey + eh, ex, ey + eh - er, er);
1258
+ context.lineTo(ex, ey + er);
1259
+ context.arcTo(ex, ey, ex + er, ey, er);
1260
+ context.closePath();
1261
+ return;
1262
+ }
1263
+ const erx = aperture.rx + expandPx;
1264
+ const ery = aperture.ry + expandPx;
1265
+ if (erx <= 0 || ery <= 0)
1266
+ return;
1267
+ context.moveTo(aperture.cx + erx, aperture.cy);
1268
+ context.ellipse(aperture.cx, aperture.cy, erx, ery, 0, 0, Math.PI * 2);
1269
+ context.closePath();
1270
+ }
1271
+ export function paintSpotlightMask(context, effects, timeMs) {
1272
+ const state = evaluateSpotlights(effects, timeMs);
1273
+ if (!state)
1274
+ return undefined;
1275
+ context.save();
1276
+ context.beginPath();
1277
+ for (const { effect } of state.items) {
1278
+ const sb = effect.slide_bounds;
1279
+ context.rect(sb.x, sb.y, sb.width, sb.height);
1280
+ }
1281
+ context.clip();
1282
+ context.fillStyle = state.color;
1283
+ context.globalAlpha = state.backdropOpacity;
1284
+ context.globalCompositeOperation = 'source-over';
1285
+ context.fillRect(state.bounds.x, state.bounds.y, state.bounds.width, state.bounds.height);
1286
+ context.fillStyle = '#000000';
1287
+ context.globalCompositeOperation = 'destination-out';
1288
+ for (const item of state.items) {
1289
+ if (!item.effect.apertures.length || !item.steps.length)
1290
+ continue;
1291
+ context.save();
1292
+ const sb = item.effect.slide_bounds;
1293
+ context.beginPath();
1294
+ context.rect(sb.x, sb.y, sb.width, sb.height);
1295
+ context.clip();
1296
+ for (const step of item.steps) {
1297
+ context.globalAlpha = step.stepAlpha;
1298
+ context.beginPath();
1299
+ for (const aperture of item.effect.apertures) {
1300
+ addExpandedApertureSubpath(context, aperture, step.expandPx);
1301
+ }
1302
+ context.fill();
1303
+ }
1304
+ context.restore();
1305
+ }
1306
+ context.restore();
1307
+ return state.bounds;
1308
+ }
167
1309
  export function underlineWipeFractions(effect, timeMs) {
168
1310
  const wipeStart = effect.out_ms > 0 && timeMs > effect.end_ms - effect.out_ms
169
1311
  ? Math.max(0, Math.min(1, 1 - (effect.end_ms - timeMs) / effect.out_ms))
@@ -227,11 +1369,18 @@ export function effectPrimitives(effects, timeMs) {
227
1369
  }
228
1370
  break;
229
1371
  }
1372
+ case 'spotlight':
1373
+ case 'zoom':
1374
+ case 'laser':
1375
+ case 'stamp':
1376
+ break;
230
1377
  }
231
1378
  }
232
1379
  return primitives;
233
1380
  }
234
1381
  export function effectNeedsBaseFrames(effects, timeMs) {
1382
+ if (activeViewTransform(effects, timeMs) !== undefined)
1383
+ return true;
235
1384
  return effectPrimitives(effects, timeMs).some(primitive => primitive.blend !== 'source-over');
236
1385
  }
237
1386
  export function groupEffectsByBlend(effects, timeMs) {
@@ -262,8 +1411,26 @@ export function effectDirtyBounds(effects, timeMs, extraRectangles, viewport) {
262
1411
  if (rect.y + rect.height > maxBottom)
263
1412
  maxBottom = rect.y + rect.height;
264
1413
  };
1414
+ const view = activeViewTransform(effects, timeMs);
1415
+ if (view)
1416
+ expand(view.content);
1417
+ const spotlightBounds = activeSpotlightBounds(effects, timeMs);
1418
+ if (spotlightBounds)
1419
+ expand(spotlightBounds);
265
1420
  for (const primitive of effectPrimitives(effects, timeMs))
266
1421
  expand(primitive.rectangle);
1422
+ for (const effect of effects) {
1423
+ if (effect.effect === 'laser') {
1424
+ const bounds = laserDirtyBounds(effect, timeMs);
1425
+ if (bounds)
1426
+ expand(bounds);
1427
+ }
1428
+ else if (effect.effect === 'stamp') {
1429
+ const bounds = stampDirtyBounds(effect, timeMs);
1430
+ if (bounds)
1431
+ expand(bounds);
1432
+ }
1433
+ }
267
1434
  for (const rect of extraRectangles)
268
1435
  expand(rect);
269
1436
  if (!(minLeft < maxRight && minTop < maxBottom))
@@ -288,6 +1455,25 @@ export function paintPrimitives(context, primitives, options) {
288
1455
  context.restore();
289
1456
  }
290
1457
  }
291
- export function paintEffects(context, effects, timeMs) {
292
- paintPrimitives(context, effectPrimitives(effects, timeMs));
1458
+ export function paintEffects(context, effects, timeMs, options) {
1459
+ const backdrop = options?.backdrop ?? true;
1460
+ const spotlightBounds = activeSpotlightBounds(effects, timeMs);
1461
+ if (spotlightBounds) {
1462
+ if (backdrop && options?.spotlightScratch) {
1463
+ const scratch = options.spotlightScratch;
1464
+ scratch.context.clearRect(spotlightBounds.x, spotlightBounds.y, spotlightBounds.width, spotlightBounds.height);
1465
+ paintSpotlightMask(scratch.context, effects, timeMs);
1466
+ context.save();
1467
+ context.globalAlpha = 1;
1468
+ context.globalCompositeOperation = 'source-over';
1469
+ scratch.drawTo(context, spotlightBounds);
1470
+ context.restore();
1471
+ }
1472
+ else {
1473
+ paintSpotlightMask(context, effects, timeMs);
1474
+ }
1475
+ }
1476
+ paintPrimitives(context, effectPrimitives(effects, timeMs), { backdrop });
1477
+ paintStamps(context, effects, timeMs);
1478
+ paintLasers(context, effects, timeMs);
293
1479
  }