canvas-recorder-lib 0.7.4 → 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
 
@@ -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
  }
@@ -1344,6 +1354,8 @@ async function startRecording(
1344
1354
  canvas.style.top = "50%";
1345
1355
  canvas.style.transform = "translate(-50%, -50%)";
1346
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");
1347
1359
 
1348
1360
  // `output` is created inside the try block below, but we need to reach it
1349
1361
  // from the catch block too, so we can still finalize (and thus flush)
@@ -2457,11 +2469,26 @@ function applyLimits(): void {
2457
2469
  : "";
2458
2470
  }
2459
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
+
2460
2485
  /**
2461
2486
  * Switch the Visual Editor to the root picked in the `<select>`. The play
2462
2487
  * limits follow, unless Keep play limits is checked.
2463
2488
  */
2464
2489
  function updateFromSelect() {
2490
+ // Where the playhead was in another chapter means nothing here.
2491
+ playheadPulledBack = undefined;
2465
2492
  const info = chapterList[select.selectedIndex];
2466
2493
  setRootStartMs(info.start);
2467
2494
  if (keepLimitsCheckbox.checked && limitsShowable) {
@@ -2509,12 +2536,42 @@ playRootBtn.addEventListener("click", () => {
2509
2536
  * playback and the editors alone. When it's gone, this switches roots properly.
2510
2537
  */
2511
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);
2512
2555
  if (initChapters()) {
2513
2556
  setRootStartMs(chapterList[select.selectedIndex].start);
2514
2557
  applyLimits();
2515
- // The range clamped itself to the new bounds; show the real position again. The
2516
- // number input stays the source of truth, so a playhead past a shortened end is left
2517
- // 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.
2518
2575
  loadPlayPositionRange();
2519
2576
  } else {
2520
2577
  updateFromSelect();
@@ -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
 
@@ -97,19 +97,18 @@ let isRecording = false;
97
97
  * When the canvas is smaller than the viewport, centers it instead.
98
98
  */
99
99
  function clampPan(canvasCssWidth, canvasCssHeight, px, py) {
100
- const viewportWidth = viewport.clientWidth;
101
- const viewportHeight = viewport.clientHeight;
102
- if (canvasCssWidth <= viewportWidth) {
103
- return [
104
- Math.round((viewportWidth - canvasCssWidth) / 2),
105
- Math.round((viewportHeight - canvasCssHeight) / 2),
106
- ];
107
- }
100
+ // Each direction on its own: a portrait video at 100% can fit across but not down.
108
101
  return [
109
- Math.max(viewportWidth - canvasCssWidth, Math.min(0, px)),
110
- Math.max(viewportHeight - canvasCssHeight, Math.min(0, py)),
102
+ clampPanAxis(canvasCssWidth, viewport.clientWidth, px),
103
+ clampPanAxis(canvasCssHeight, viewport.clientHeight, py),
111
104
  ];
112
105
  }
106
+ /** Centered if the canvas fits this way, otherwise no gap at either edge. */
107
+ function clampPanAxis(canvasSize, viewportSize, pan) {
108
+ return canvasSize <= viewportSize
109
+ ? Math.round((viewportSize - canvasSize) / 2)
110
+ : Math.max(viewportSize - canvasSize, Math.min(0, pan));
111
+ }
113
112
  /**
114
113
  * Resizes the canvas to match zoom and repositions it via left/top.
115
114
  * zoom = 1.0 → 3840×2160 physical pixels, displayed at 1920×1080 CSS px on Retina.
@@ -141,6 +140,8 @@ function applyZoom(zoom, px = panX, py = panY) {
141
140
  [panX, panY] = clampPan(cssW, cssH, px, py);
142
141
  canvas.style.left = `${panX}px`;
143
142
  canvas.style.top = `${panY}px`;
143
+ // The hand cursor, only when there's something to drag into view.
144
+ canvas.classList.toggle("pannable", cssW > viewport.clientWidth || cssH > viewport.clientHeight);
144
145
  }
145
146
  /** Fits the 16:9 canvas into the viewport at pixel-perfect resolution. */
146
147
  function zoomToFit() {
@@ -165,6 +166,7 @@ function zoomToFit() {
165
166
  canvas.style.height = `${cssH}px`;
166
167
  canvas.style.left = `${Math.round((vpW - cssW) / 2)}px`;
167
168
  canvas.style.top = `${Math.round((vpH - cssH) / 2)}px`;
169
+ canvas.classList.remove("pannable");
168
170
  // Immediately redraw so the canvas clear doesn't show as a gray flash.
169
171
  showFrame(playheadInVideoMs(), true);
170
172
  }
@@ -1119,6 +1121,8 @@ async function startRecording(saveStartMs = 0, saveEndMs = toShow.duration, pixe
1119
1121
  canvas.style.top = "50%";
1120
1122
  canvas.style.transform = "translate(-50%, -50%)";
1121
1123
  canvas.style.transformOrigin = "";
1124
+ // No panning while saving. The finally block puts the zoom back, and this with it.
1125
+ canvas.classList.remove("pannable");
1122
1126
  // `output` is created inside the try block below, but we need to reach it
1123
1127
  // from the catch block too, so we can still finalize (and thus flush)
1124
1128
  // whatever got encoded before the error.
@@ -2029,11 +2033,24 @@ function applyLimits() {
2029
2033
  ? `Play “${root.description}” instead: set the play limits to its start and end, and go to its start.`
2030
2034
  : "";
2031
2035
  }
2036
+ /**
2037
+ * Two playhead times this close are the same: {@link playPositionSeconds} shows ⅒ ms.
2038
+ */
2039
+ const PLAYHEAD_TOLERANCE_MS = 0.1;
2040
+ /**
2041
+ * Set when {@link refreshChapters} pulled the playhead back inside a chapter that got shorter.
2042
+ * Durations change on every keystroke, so typing 25000 over 30000 makes the chapter 2 ms long
2043
+ * on the way. If the chapter grows again before the playhead moves, it goes back toward `fromMs`.
2044
+ * Both are in the video's time.
2045
+ */
2046
+ let playheadPulledBack;
2032
2047
  /**
2033
2048
  * Switch the Visual Editor to the root picked in the `<select>`. The play
2034
2049
  * limits follow, unless Keep play limits is checked.
2035
2050
  */
2036
2051
  function updateFromSelect() {
2052
+ // Where the playhead was in another chapter means nothing here.
2053
+ playheadPulledBack = undefined;
2037
2054
  const info = chapterList[select.selectedIndex];
2038
2055
  setRootStartMs(info.start);
2039
2056
  if (keepLimitsCheckbox.checked && limitsShowable) {
@@ -2080,12 +2097,36 @@ playRootBtn.addEventListener("click", () => {
2080
2097
  * playback and the editors alone. When it's gone, this switches roots properly.
2081
2098
  */
2082
2099
  function refreshChapters() {
2100
+ const playheadMs = playheadInVideoMs();
2101
+ // Not moved since the last clamp: aim for where it was before that.
2102
+ if (playheadPulledBack &&
2103
+ Math.abs(playheadMs - playheadPulledBack.toMs) > PLAYHEAD_TOLERANCE_MS) {
2104
+ playheadPulledBack = undefined;
2105
+ }
2106
+ const wantedMs = playheadPulledBack?.fromMs ?? playheadMs;
2107
+ // limitsChapter still has the start and end from before this change.
2108
+ const old = limitsChapter;
2109
+ const wasInside = playheadPulledBack !== undefined ||
2110
+ old === undefined ||
2111
+ (playheadMs >= old.start - PLAYHEAD_TOLERANCE_MS &&
2112
+ playheadMs <= old.end + PLAYHEAD_TOLERANCE_MS);
2083
2113
  if (initChapters()) {
2084
2114
  setRootStartMs(chapterList[select.selectedIndex].start);
2085
2115
  applyLimits();
2086
- // The range clamped itself to the new bounds; show the real position again. The
2087
- // number input stays the source of truth, so a playhead past a shortened end is left
2088
- // for playback to wrap rather than moved here.
2116
+ // A shorter chapter can leave the playhead past its end, showing nothing. Keep it inside
2117
+ // the play limits. sectionEndTime already stops just short of the next chapter's start.
2118
+ // A playhead the user put outside the limits stays there.
2119
+ if (wasInside) {
2120
+ const clampedMs = Math.max(sectionStartTime, Math.min(sectionEndTime, wantedMs));
2121
+ if (Math.abs(clampedMs - playheadMs) > PLAYHEAD_TOLERANCE_MS) {
2122
+ loadPlayPositionSeconds(clampedMs);
2123
+ }
2124
+ playheadPulledBack =
2125
+ clampedMs === wantedMs
2126
+ ? undefined
2127
+ : { fromMs: wantedMs, toMs: clampedMs };
2128
+ }
2129
+ // The range clamped itself to the new bounds; show the real position again.
2089
2130
  loadPlayPositionRange();
2090
2131
  }
2091
2132
  else {