canvas-recorder-lib 0.7.3 → 0.8.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.
@@ -6,6 +6,19 @@ This is not a full list of changes: new features and fixes aren't here.
6
6
  While the version is 0.x, every breaking change comes with a new minor version (0.5 → 0.6), and nothing else does.
7
7
  A project that depends on `"^0.5.0"` gets 0.5.x updates but never 0.6.0, so it only meets these changes when someone installs the new version on purpose.
8
8
 
9
+ ## 0.8.0 (2026-11-12)
10
+
11
+ - **`samplesToFourier()` leaves out the frequency 0 term,** the center, unless you pass `{ keepCenter: true }`.
12
+ That term is the average of the samples, a fixed offset. Without it the curve is the same shape, centered on that average.
13
+ `FourierComponent` gets the new default, and so does `getAnimationRules()` given a path string.
14
+ A Fourier component still puts the finished curve in the same place in Dest Rect.
15
+ - **Every term after the center moves down one number,** so Term Groups can add different terms than before.
16
+ For art drawn away from the origin, the center is often term 0, and everything moves. (The showcase's Walking Man: term 0, 45% of the total amplitude.)
17
+ For a shape centered on the origin, the center is tiny and far down the list (the showcase's Star: term 42), so only a group that ends past it changes, by one small term.
18
+ - To get the same picture, subtract 1 from every term number after the center's. The showcase's Fourier Component slide did: `0 1, 2, 3-5, 6-8, 9-11` became `0, 1, 2-4, 5-7, 8-10`.
19
+ The finished curve is the same either way. What goes away is the step that slid the curve to the center.
20
+ - Code that needs the terms to add up to exactly the samples, in place, should pass `{ keepCenter: true }`. The showcase's Fourier Morph does.
21
+
9
22
  ## 0.7.0 (2026-11-07)
10
23
 
11
24
  - **`VisualEditorAPI.getDecodedBuffer()` returns a mono buffer,** at 48 kHz, for every file.
@@ -90,6 +90,10 @@ body {
90
90
  #main {
91
91
  position: absolute;
92
92
  outline: 1px solid rgba(0, 0, 0, 0.3);
93
+ }
94
+
95
+ /* Set by applyZoom() when part of the canvas is out of sight. */
96
+ #main.pannable {
93
97
  cursor: grab;
94
98
  }
95
99
 
@@ -104,7 +108,7 @@ body {
104
108
  pointer-events: none;
105
109
  }
106
110
 
107
- #main:active {
111
+ #main.pannable:active {
108
112
  cursor: grabbing;
109
113
  }
110
114
 
@@ -149,7 +149,7 @@ import {
149
149
  splitSoundClip,
150
150
  splitVideoClip,
151
151
  } from "../src/slide-components/split-video-clip.ts";
152
- import { TranscriptView } from "./transcript-view.ts";
152
+ import { TranscriptView, type TranscriptViewClip } from "./transcript-view.ts";
153
153
  import { TraditionalTextComponent } from "../src/slide-components/traditional-text.ts";
154
154
  import { PaddingComponent } from "../src/slide-components/in-parallel.ts";
155
155
 
@@ -224,20 +224,24 @@ function clampPan(
224
224
  px: number,
225
225
  py: number,
226
226
  ): [number, number] {
227
- const viewportWidth = viewport.clientWidth;
228
- const viewportHeight = viewport.clientHeight;
229
- if (canvasCssWidth <= viewportWidth) {
230
- return [
231
- Math.round((viewportWidth - canvasCssWidth) / 2),
232
- Math.round((viewportHeight - canvasCssHeight) / 2),
233
- ];
234
- }
227
+ // Each direction on its own: a portrait video at 100% can fit across but not down.
235
228
  return [
236
- Math.max(viewportWidth - canvasCssWidth, Math.min(0, px)),
237
- Math.max(viewportHeight - canvasCssHeight, Math.min(0, py)),
229
+ clampPanAxis(canvasCssWidth, viewport.clientWidth, px),
230
+ clampPanAxis(canvasCssHeight, viewport.clientHeight, py),
238
231
  ];
239
232
  }
240
233
 
234
+ /** Centered if the canvas fits this way, otherwise no gap at either edge. */
235
+ function clampPanAxis(
236
+ canvasSize: number,
237
+ viewportSize: number,
238
+ pan: number,
239
+ ): number {
240
+ return canvasSize <= viewportSize
241
+ ? Math.round((viewportSize - canvasSize) / 2)
242
+ : Math.max(viewportSize - canvasSize, Math.min(0, pan));
243
+ }
244
+
241
245
  /**
242
246
  * Resizes the canvas to match zoom and repositions it via left/top.
243
247
  * zoom = 1.0 → 3840×2160 physical pixels, displayed at 1920×1080 CSS px on Retina.
@@ -268,6 +272,11 @@ function applyZoom(zoom: number, px = panX, py = panY) {
268
272
  [panX, panY] = clampPan(cssW, cssH, px, py);
269
273
  canvas.style.left = `${panX}px`;
270
274
  canvas.style.top = `${panY}px`;
275
+ // The hand cursor, only when there's something to drag into view.
276
+ canvas.classList.toggle(
277
+ "pannable",
278
+ cssW > viewport.clientWidth || cssH > viewport.clientHeight,
279
+ );
271
280
  }
272
281
 
273
282
  /** Fits the 16:9 canvas into the viewport at pixel-perfect resolution. */
@@ -297,6 +306,7 @@ function zoomToFit() {
297
306
  canvas.style.height = `${cssH}px`;
298
307
  canvas.style.left = `${Math.round((vpW - cssW) / 2)}px`;
299
308
  canvas.style.top = `${Math.round((vpH - cssH) / 2)}px`;
309
+ canvas.classList.remove("pannable");
300
310
  // Immediately redraw so the canvas clear doesn't show as a gray flash.
301
311
  showFrame(playheadInVideoMs(), true);
302
312
  }
@@ -522,13 +532,10 @@ function checkVideosLocalFiles() {
522
532
  * input (pointermove, etc.).
523
533
  */
524
534
  function showFrame(timeInMs: number, live: boolean) {
525
- // While playing, the timeline keeps the play head on screen.
526
- timelineDisplay.setPlayMs(
527
- timeInMs - rootStartMs,
528
- live && playCheckBox.checked,
529
- );
530
- if (live)
531
- transcriptView?.update(timeInMs - rootStartMs, playCheckBox.checked);
535
+ // While playing or saving, the timeline and 📖 Text keep the play head on screen.
536
+ const following = !live || playCheckBox.checked;
537
+ timelineDisplay.setPlayMs(timeInMs - rootStartMs, following);
538
+ transcriptView?.update(timeInMs - rootStartMs, following);
532
539
  for (const update of goToButtonUpdaters) update();
533
540
  editorPanelsTimeChanged();
534
541
  context.reset();
@@ -1347,6 +1354,8 @@ async function startRecording(
1347
1354
  canvas.style.top = "50%";
1348
1355
  canvas.style.transform = "translate(-50%, -50%)";
1349
1356
  canvas.style.transformOrigin = "";
1357
+ // No panning while saving. The finally block puts the zoom back, and this with it.
1358
+ canvas.classList.remove("pannable");
1350
1359
 
1351
1360
  // `output` is created inside the try block below, but we need to reach it
1352
1361
  // from the catch block too, so we can still finalize (and thus flush)
@@ -1411,6 +1420,7 @@ async function startRecording(
1411
1420
 
1412
1421
  await output.start();
1413
1422
  infoDiv.innerHTML = "Recording in progress...";
1423
+ showTranscriptViewForSave();
1414
1424
 
1415
1425
  const frameDuration = 1000 / FPS;
1416
1426
 
@@ -1534,6 +1544,7 @@ async function startRecording(
1534
1544
  applyZoom(currentZoomFactor);
1535
1545
  }
1536
1546
  suspendLiveAnimationLoop = false;
1547
+ endTranscriptViewForSave();
1537
1548
  }
1538
1549
  }
1539
1550
 
@@ -2458,11 +2469,26 @@ function applyLimits(): void {
2458
2469
  : "";
2459
2470
  }
2460
2471
 
2472
+ /**
2473
+ * Two playhead times this close are the same: {@link playPositionSeconds} shows ⅒ ms.
2474
+ */
2475
+ const PLAYHEAD_TOLERANCE_MS = 0.1;
2476
+
2477
+ /**
2478
+ * Set when {@link refreshChapters} pulled the playhead back inside a chapter that got shorter.
2479
+ * Durations change on every keystroke, so typing 25000 over 30000 makes the chapter 2 ms long
2480
+ * on the way. If the chapter grows again before the playhead moves, it goes back toward `fromMs`.
2481
+ * Both are in the video's time.
2482
+ */
2483
+ let playheadPulledBack: { fromMs: number; toMs: number } | undefined;
2484
+
2461
2485
  /**
2462
2486
  * Switch the Visual Editor to the root picked in the `<select>`. The play
2463
2487
  * limits follow, unless Keep play limits is checked.
2464
2488
  */
2465
2489
  function updateFromSelect() {
2490
+ // Where the playhead was in another chapter means nothing here.
2491
+ playheadPulledBack = undefined;
2466
2492
  const info = chapterList[select.selectedIndex];
2467
2493
  setRootStartMs(info.start);
2468
2494
  if (keepLimitsCheckbox.checked && limitsShowable) {
@@ -2510,12 +2536,42 @@ playRootBtn.addEventListener("click", () => {
2510
2536
  * playback and the editors alone. When it's gone, this switches roots properly.
2511
2537
  */
2512
2538
  function refreshChapters(): void {
2539
+ const playheadMs = playheadInVideoMs();
2540
+ // Not moved since the last clamp: aim for where it was before that.
2541
+ if (
2542
+ playheadPulledBack &&
2543
+ Math.abs(playheadMs - playheadPulledBack.toMs) > PLAYHEAD_TOLERANCE_MS
2544
+ ) {
2545
+ playheadPulledBack = undefined;
2546
+ }
2547
+ const wantedMs = playheadPulledBack?.fromMs ?? playheadMs;
2548
+ // limitsChapter still has the start and end from before this change.
2549
+ const old = limitsChapter;
2550
+ const wasInside =
2551
+ playheadPulledBack !== undefined ||
2552
+ old === undefined ||
2553
+ (playheadMs >= old.start - PLAYHEAD_TOLERANCE_MS &&
2554
+ playheadMs <= old.end + PLAYHEAD_TOLERANCE_MS);
2513
2555
  if (initChapters()) {
2514
2556
  setRootStartMs(chapterList[select.selectedIndex].start);
2515
2557
  applyLimits();
2516
- // The range clamped itself to the new bounds; show the real position again. The
2517
- // number input stays the source of truth, so a playhead past a shortened end is left
2518
- // for playback to wrap rather than moved here.
2558
+ // A shorter chapter can leave the playhead past its end, showing nothing. Keep it inside
2559
+ // the play limits. sectionEndTime already stops just short of the next chapter's start.
2560
+ // A playhead the user put outside the limits stays there.
2561
+ if (wasInside) {
2562
+ const clampedMs = Math.max(
2563
+ sectionStartTime,
2564
+ Math.min(sectionEndTime, wantedMs),
2565
+ );
2566
+ if (Math.abs(clampedMs - playheadMs) > PLAYHEAD_TOLERANCE_MS) {
2567
+ loadPlayPositionSeconds(clampedMs);
2568
+ }
2569
+ playheadPulledBack =
2570
+ clampedMs === wantedMs
2571
+ ? undefined
2572
+ : { fromMs: wantedMs, toMs: clampedMs };
2573
+ }
2574
+ // The range clamped itself to the new bounds; show the real position again.
2519
2575
  loadPlayPositionRange();
2520
2576
  } else {
2521
2577
  updateFromSelect();
@@ -2552,20 +2608,7 @@ onTranscriptsChange(() => {
2552
2608
  // The transcript as text. It rebuilds itself when the clips or transcripts
2553
2609
  // change, so it needs no calls besides the one in showFrame().
2554
2610
  transcriptView = new TranscriptView({
2555
- clips() {
2556
- const root = currentRoot();
2557
- if (!root) return [];
2558
- return soundOwners(root)
2559
- .flatMap(({ owner, startMs }) =>
2560
- (owner.soundClips ?? []).map((clip) => ({
2561
- clip,
2562
- owner,
2563
- startMs: startMs + clip.startMsIntoScene,
2564
- lengthMs: soundClipLengthMs(clip),
2565
- })),
2566
- )
2567
- .sort((a, b) => a.startMs - b.startMs);
2568
- },
2611
+ clips: transcriptViewClips,
2569
2612
  transcript(source) {
2570
2613
  const status = transcriptStatus(source);
2571
2614
  if (!status) return undefined;
@@ -2596,31 +2639,86 @@ transcriptView = new TranscriptView({
2596
2639
  },
2597
2640
  });
2598
2641
  getById("timelineCanvas", HTMLCanvasElement).after(transcriptView.element);
2599
- {
2600
- const READ_KEY = "transcriptView.on";
2601
- const checkbox = getById("transcriptViewCheckbox", HTMLInputElement);
2602
- const timelineCanvas = getById("timelineCanvas", HTMLCanvasElement);
2603
- const apply = () => {
2604
- transcriptView!.hidden = !checkbox.checked;
2605
- // The timeline's CSS sets `display`, which would beat the `hidden` attribute.
2606
- timelineCanvas.style.display = checkbox.checked ? "none" : "";
2607
- if (!checkbox.checked) timelineDisplay.redraw();
2608
- };
2642
+
2643
+ /** The current chapter's sound clips, in order of time, for 📖 Text. */
2644
+ function transcriptViewClips(): TranscriptViewClip[] {
2645
+ const root = currentRoot();
2646
+ if (!root) return [];
2647
+ return soundOwners(root)
2648
+ .flatMap(({ owner, startMs }) =>
2649
+ (owner.soundClips ?? []).map((clip) => ({
2650
+ clip,
2651
+ owner,
2652
+ startMs: startMs + clip.startMsIntoScene,
2653
+ lengthMs: soundClipLengthMs(clip),
2654
+ })),
2655
+ )
2656
+ .sort((a, b) => a.startMs - b.startMs);
2657
+ }
2658
+
2659
+ // MARK: 📖 Text checkbox
2660
+
2661
+ const transcriptViewCheckbox = getById(
2662
+ "transcriptViewCheckbox",
2663
+ HTMLInputElement,
2664
+ );
2665
+ const TRANSCRIPT_VIEW_KEY = "transcriptView.on";
2666
+
2667
+ /** Show 📖 Text or the timeline, to match the checkbox. */
2668
+ function applyTranscriptViewCheckbox() {
2669
+ const on = transcriptViewCheckbox.checked;
2670
+ transcriptView!.hidden = !on;
2671
+ // The timeline's CSS sets `display`, which would beat the `hidden` attribute.
2672
+ getById("timelineCanvas", HTMLCanvasElement).style.display = on ? "none" : "";
2673
+ if (!on) timelineDisplay.redraw();
2674
+ }
2675
+
2676
+ /**
2677
+ * True while a save has switched to 📖 Text, and the user hasn't touched the checkbox since.
2678
+ * Then the save switches back when it's done.
2679
+ */
2680
+ let transcriptViewJustForSave = false;
2681
+
2682
+ /**
2683
+ * A save shows 📖 Text by default, so you can read along while it records.
2684
+ * Only when there's something to read: transcripts are on, and the chapter has sound.
2685
+ */
2686
+ function showTranscriptViewForSave() {
2687
+ if (transcriptViewCheckbox.checked) return;
2688
+ if (!transcriptsEnabled() || transcriptViewClips().length === 0) return;
2689
+ transcriptViewCheckbox.checked = true;
2690
+ transcriptViewJustForSave = true;
2691
+ applyTranscriptViewCheckbox();
2692
+ }
2693
+
2694
+ /** The save is over. Go back to the timeline, unless the user chose since. */
2695
+ function endTranscriptViewForSave() {
2696
+ if (!transcriptViewJustForSave) return;
2697
+ transcriptViewJustForSave = false;
2698
+ transcriptViewCheckbox.checked = false;
2699
+ applyTranscriptViewCheckbox();
2700
+ }
2701
+
2702
+ try {
2703
+ transcriptViewCheckbox.checked =
2704
+ localStorage.getItem(TRANSCRIPT_VIEW_KEY) === "true";
2705
+ } catch {
2706
+ // Remembering is only a convenience.
2707
+ }
2708
+ transcriptViewCheckbox.addEventListener("input", () => {
2709
+ // The user's choice, so a save won't change it back, and it's remembered.
2710
+ transcriptViewJustForSave = false;
2609
2711
  try {
2610
- checkbox.checked = localStorage.getItem(READ_KEY) === "true";
2712
+ localStorage.setItem(
2713
+ TRANSCRIPT_VIEW_KEY,
2714
+ String(transcriptViewCheckbox.checked),
2715
+ );
2611
2716
  } catch {
2612
- // Remembering is only a convenience.
2717
+ // See above.
2613
2718
  }
2614
- checkbox.addEventListener("input", () => {
2615
- try {
2616
- localStorage.setItem(READ_KEY, String(checkbox.checked));
2617
- } catch {
2618
- // See above.
2619
- }
2620
- apply();
2621
- });
2622
- apply();
2623
- }
2719
+ applyTranscriptViewCheckbox();
2720
+ });
2721
+ applyTranscriptViewCheckbox();
2624
2722
 
2625
2723
  timelineDisplay.onSeek = (localMs) => {
2626
2724
  stopAudio();
@@ -4140,6 +4238,8 @@ async function initFromDB(unloadBackup?: string | null): Promise<void> {
4140
4238
  }
4141
4239
  _loadSource = source;
4142
4240
  _baselineTreeJson = currentTreeJson();
4241
+ // The unload backup is in the database now, or older than it. See the end of "Load previous state".
4242
+ sessionStorage.removeItem("pendingScheduleSave");
4143
4243
 
4144
4244
  canvas.style.visibility = "";
4145
4245
  canvasLoading.style.display = "none";
@@ -4257,6 +4357,11 @@ function currentSaveTarget(): Showable | null {
4257
4357
  }
4258
4358
 
4259
4359
  function saveOnUnload() {
4360
+ // Still starting up: the live tree is the TypeScript defaults, not the user's work.
4361
+ // A backup of it would be newer than the real state, so the next startup would load it,
4362
+ // and everything made in the Visual Editor would disappear. The last session's backup,
4363
+ // if there was one, is still in sessionStorage until initFromDB() has saved it.
4364
+ if (_loadSource === undefined) return;
4260
4365
  // When the history dialog is open the live tree may hold a transient preview from
4261
4366
  // _dialogSelectItem. Back up the pre-dialog state instead, so a preview is never
4262
4367
  // persisted as if the user had accepted it.
@@ -7883,6 +7988,15 @@ function applyMarkerDrag(localX: number, localY: number, shiftKey = false) {
7883
7988
  if (fL) getById("left-col", HTMLDivElement).style.flex = fL;
7884
7989
  } finally {
7885
7990
  sessionStorage.clear();
7991
+ // Keep the unload backup until initFromDB() has saved it. If the page reloads before
7992
+ // then, saveOnUnload() saves nothing, so this is the only copy.
7993
+ if (unloadBackup !== null) {
7994
+ try {
7995
+ sessionStorage.setItem("pendingScheduleSave", unloadBackup);
7996
+ } catch {
7997
+ // It fit a moment ago.
7998
+ }
7999
+ }
7886
8000
  }
7887
8001
  }
7888
8002
 
@@ -67,8 +67,9 @@ import {
67
67
  // * fourier-shared.ts, cacheHealth (philDebug.cacheHealth): hits and misses of
68
68
  // the per-t cache in termsToParametricFunction().
69
69
  // * fourier-shared.ts, hasFixedContribution(): finds the frequency 0 term,
70
- // the center. checkTermGroups() already warns when it's somewhere it can't
71
- // animate.
70
+ // the center. samplesToFourier() leaves it out unless asked to keep it, so
71
+ // only a `terms` source can have it. checkTermGroups() already warns when
72
+ // it's a group of its own.
72
73
 
73
74
  export type FourierPanel = {
74
75
  readonly element: HTMLElement;
@@ -90,6 +90,10 @@ body {
90
90
  #main {
91
91
  position: absolute;
92
92
  outline: 1px solid rgba(0, 0, 0, 0.3);
93
+ }
94
+
95
+ /* Set by applyZoom() when part of the canvas is out of sight. */
96
+ #main.pannable {
93
97
  cursor: grab;
94
98
  }
95
99
 
@@ -104,7 +108,7 @@ body {
104
108
  pointer-events: none;
105
109
  }
106
110
 
107
- #main:active {
111
+ #main.pannable:active {
108
112
  cursor: grabbing;
109
113
  }
110
114