Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): draw waveform within item bounds, not CSS canvas bounds

Davide Mendoliad8ab206b806d
diff --git a/src/components/video-editor/timeline/BackgroundWaveform.tsx b/src/components/video-editor/timeline/BackgroundWaveform.tsx
index 91f40fb7c..815b472d1 100644
--- a/src/components/video-editor/timeline/BackgroundWaveform.tsx
+++ b/src/components/video-editor/timeline/BackgroundWaveform.tsx
@@ -6,11 +6,15 @@ export interface BackgroundWaveformProps {
peaks: Float32Array | null;
videoDurationMs: number;
/**
- * Vertical inset in CSS pixels applied to the top and bottom of the canvas
- * so the waveform aligns with the item content height rather than the full
- * row height. Defaults to 0.
+ * Pixels to inset the drawn waveform from the top of the canvas row,
+ * so it aligns with the item content top edge. Defaults to 0.
*/
- verticalInset?: number;
+ topInset?: number;
+ /**
+ * Pixels to inset the drawn waveform from the bottom of the canvas row,
+ * so it aligns with the item content bottom edge. Defaults to 0.
+ */
+ bottomInset?: number;
}
/**
@@ -19,6 +23,11 @@ export interface BackgroundWaveformProps {
* `<Row>`, which already provides `relative overflow-hidden` — no wrapper
* element needed.
*
+ * The canvas always uses `inset-0` (full row height). Vertical alignment with
+ * the item content is achieved via `topInset`/`bottomInset` in the draw calls
+ * rather than CSS positioning, so the result is immune to sub-pixel CSS layout
+ * differences.
+ *
* - Accepts pre-computed `peaks` from the caller (see `useAudioPeaks`).
* - Redraws whenever the timeline zoom/pan range changes.
* - `pointer-events: none` — never blocks drag-to-create interactions.
@@ -26,7 +35,8 @@ export interface BackgroundWaveformProps {
export default function BackgroundWaveform({
peaks,
videoDurationMs,
- verticalInset = 0,
+ topInset = 0,
+ bottomInset = 0,
}: BackgroundWaveformProps) {
const { range } = useTimelineContext();
const canvasRef = useRef<HTMLCanvasElement>(null);
@@ -67,12 +77,17 @@ export default function BackgroundWaveform({
const rangeMs = range.end - range.start;
if (rangeMs <= 0 || videoDurationMs <= 0) return;
+ // Draw within [topY, bottomY] so the waveform aligns with item bounds
+ // regardless of CSS sub-pixel rounding on the canvas element itself.
+ const topY = topInset;
+ const bottomY = H - bottomInset;
+ const drawHeight = bottomY - topY;
+ if (drawHeight <= 0) return;
+
const N = peaks.length / 2;
- // Amplitude scaled to fill the canvas height minus the vertical inset on
- // each side, so the waveform top/bottom aligns with the item content bounds.
- const amp = H * 0.9;
+ const amp = drawHeight * 0.9;
- // Rectified (half-wave): amplitude = max(|min|, |max|), drawn from the bottom up.
+ // Rectified (half-wave): amplitude = max(|min|, |max|), drawn upward from bottomY.
const colAmp = new Float32Array(W);
for (let x = 0; x < W; x++) {
const startMs = range.start + (x / W) * rangeMs;
@@ -92,31 +107,25 @@ export default function BackgroundWaveform({
// Filled polygon: bottom-left → top silhouette → bottom-right.
ctx.beginPath();
- ctx.moveTo(0, H);
+ ctx.moveTo(0, bottomY);
for (let x = 0; x < W; x++) {
- ctx.lineTo(x, H - colAmp[x] * amp);
+ ctx.lineTo(x, bottomY - colAmp[x] * amp);
}
- ctx.lineTo(W, H);
+ ctx.lineTo(W, bottomY);
ctx.closePath();
ctx.fillStyle = "rgba(74, 222, 128, 0.55)";
ctx.fill();
// Crisp top-edge stroke for the sharp silhouette.
ctx.beginPath();
- ctx.moveTo(0, H - colAmp[0] * amp);
+ ctx.moveTo(0, bottomY - colAmp[0] * amp);
for (let x = 1; x < W; x++) {
- ctx.lineTo(x, H - colAmp[x] * amp);
+ ctx.lineTo(x, bottomY - colAmp[x] * amp);
}
ctx.strokeStyle = "rgba(74, 222, 128, 0.85)";
ctx.lineWidth = 1;
ctx.stroke();
- }, [peaks, range, canvasSize, videoDurationMs]);
-
- return (
- <canvas
- ref={canvasRef}
- className="absolute left-0 right-0 pointer-events-none w-full"
- style={{ top: verticalInset, bottom: verticalInset }}
- />
- );
+ }, [peaks, range, canvasSize, videoDurationMs, topInset, bottomInset]);
+
+ return <canvas ref={canvasRef} className="absolute inset-0 pointer-events-none w-full h-full" />;
}
diff --git a/src/components/video-editor/timeline/TimelineEditor.tsx b/src/components/video-editor/timeline/TimelineEditor.tsx
index 53c7e1fd9..2bca08a84 100644
--- a/src/components/video-editor/timeline/TimelineEditor.tsx
+++ b/src/components/video-editor/timeline/TimelineEditor.tsx
@@ -803,7 +803,12 @@ function Timeline({
hint={t("hints.pressTrim")}
background={
showTrimWaveform ? (
- <BackgroundWaveform peaks={peaks} videoDurationMs={videoDurationMs} verticalInset={3} />
+ <BackgroundWaveform
+ peaks={peaks}
+ videoDurationMs={videoDurationMs}
+ topInset={0}
+ bottomInset={6}
+ />
) : undefined
}
>