Skip to content

🎥 Make videos programmatically with React

Move timeline pixel alignment into timeline-utils

JonnyBurger4a0d98246e4d
diff --git a/packages/convert/app/components/player/filmstrip.tsx b/packages/convert/app/components/player/filmstrip.tsx
index b86ba0449f..3d37d6a081 100644
--- a/packages/convert/app/components/player/filmstrip.tsx
+++ b/packages/convert/app/components/player/filmstrip.tsx
@@ -395,6 +395,7 @@ export const Filmstrip: React.FC<{
width: canvasWidth,
frameHeight: FILMSTRIP_HEIGHT,
devicePixelRatio: window.devicePixelRatio,
+ horizontalOffset: 0,
signal: controller.signal,
}).catch(() => undefined);
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index eaabb9ced1..70259736fc 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -12,6 +12,7 @@ import {
getTimestampFromFrameDatabaseKey,
makeFrameDatabaseKey,
resizeVideoFrame,
+ snapCanvasPositionToDevicePixel,
type WaveformVolume,
WEBCODECS_TIMESCALE,
} from '@remotion/timeline-utils';
@@ -137,9 +138,14 @@ const TimelineVideoInfoSegment: React.FC<{
clearCanvas();
for (let x = 0; x < canvas.width; x += thumbnailWidth) {
- const left = Math.floor(x + horizontalOffset) - horizontalOffset;
- const right =
- Math.floor(x + thumbnailWidth + horizontalOffset) - horizontalOffset;
+ const left = snapCanvasPositionToDevicePixel({
+ horizontalOffset,
+ position: x,
+ });
+ const right = snapCanvasPositionToDevicePixel({
+ horizontalOffset,
+ position: x + thumbnailWidth,
+ });
ctx.drawImage(frame, left, 0, right - left, canvas.height);
}
};
diff --git a/packages/studio/src/helpers/align-canvas-to-device-pixels.ts b/packages/studio/src/helpers/align-canvas-to-device-pixels.ts
index 4d7941e018..23785b48e8 100644
--- a/packages/studio/src/helpers/align-canvas-to-device-pixels.ts
+++ b/packages/studio/src/helpers/align-canvas-to-device-pixels.ts
@@ -1,3 +1,5 @@
+import {getDevicePixelAlignedCanvasLayout} from '@remotion/timeline-utils';
+
export const alignCanvasToDevicePixels = ({
canvas,
cssWidth,
@@ -12,15 +14,12 @@ export const alignCanvasToDevicePixels = ({
const containerLeft =
canvas.parentElement?.getBoundingClientRect().left ??
canvas.getBoundingClientRect().left;
- const containerLeftInPixels = containerLeft * pixelRatio;
- // Keep the canvas bitmap on the device-pixel grid. The returned offset lets
- // callers preserve the exact timeline position in their drawing coordinates.
- const horizontalOffset =
- cssWidth === 0
- ? 0
- : containerLeftInPixels - Math.floor(containerLeftInPixels);
- const width = Math.ceil(cssWidth * pixelRatio + horizontalOffset);
- const height = Math.ceil(cssHeight * pixelRatio);
+ const {height, horizontalOffset, width} = getDevicePixelAlignedCanvasLayout({
+ containerLeft,
+ cssHeight,
+ cssWidth,
+ devicePixelRatio: pixelRatio,
+ });
canvas.width = width;
canvas.height = height;
diff --git a/packages/timeline-utils/src/audio-waveform/draw-peaks.ts b/packages/timeline-utils/src/audio-waveform/draw-peaks.ts
index 481c7a2ce6..d7eb587e5f 100644
--- a/packages/timeline-utils/src/audio-waveform/draw-peaks.ts
+++ b/packages/timeline-utils/src/audio-waveform/draw-peaks.ts
@@ -38,14 +38,14 @@ export const drawBars = ({
peaks,
volume,
width,
- horizontalOffset = 0,
+ horizontalOffset,
}: {
readonly canvas: HTMLCanvasElement | OffscreenCanvas;
readonly peaks: Float32Array;
readonly color: string;
readonly volume: WaveformVolume;
readonly width: number;
- readonly horizontalOffset?: number;
+ readonly horizontalOffset: number;
}) => {
const ctx = canvas.getContext('2d');
diff --git a/packages/timeline-utils/src/device-pixel-alignment.ts b/packages/timeline-utils/src/device-pixel-alignment.ts
new file mode 100644
index 0000000000..7e8ed162c5
--- /dev/null
+++ b/packages/timeline-utils/src/device-pixel-alignment.ts
@@ -0,0 +1,33 @@
+export const getDevicePixelAlignedCanvasLayout = ({
+ cssWidth,
+ cssHeight,
+ containerLeft,
+ devicePixelRatio,
+}: {
+ readonly cssWidth: number;
+ readonly cssHeight: number;
+ readonly containerLeft: number;
+ readonly devicePixelRatio: number;
+}) => {
+ const containerLeftInPixels = containerLeft * devicePixelRatio;
+ const horizontalOffset =
+ cssWidth === 0
+ ? 0
+ : containerLeftInPixels - Math.floor(containerLeftInPixels);
+
+ return {
+ height: Math.ceil(cssHeight * devicePixelRatio),
+ horizontalOffset,
+ width: Math.ceil(cssWidth * devicePixelRatio + horizontalOffset),
+ };
+};
+
+export const snapCanvasPositionToDevicePixel = ({
+ position,
+ horizontalOffset,
+}: {
+ readonly position: number;
+ readonly horizontalOffset: number;
+}) => {
+ return Math.floor(position + horizontalOffset) - horizontalOffset;
+};
diff --git a/packages/timeline-utils/src/index.ts b/packages/timeline-utils/src/index.ts
index d66d09d6b6..3f2451ac7d 100644
--- a/packages/timeline-utils/src/index.ts
+++ b/packages/timeline-utils/src/index.ts
@@ -14,6 +14,10 @@ export {
emitWaveformProgress,
} from './audio-waveform/waveform-peak-processor';
export {clampTimestampsToDuration} from './clamp-timestamps-to-duration';
+export {
+ getDevicePixelAlignedCanvasLayout,
+ snapCanvasPositionToDevicePixel,
+} from './device-pixel-alignment';
export {extractFrames} from './extract-frames';
export type {
ExtractFramesProps,
diff --git a/packages/timeline-utils/src/render-frame-strip-to-canvas.ts b/packages/timeline-utils/src/render-frame-strip-to-canvas.ts
index e3ffb1e171..d3e2f262da 100644
--- a/packages/timeline-utils/src/render-frame-strip-to-canvas.ts
+++ b/packages/timeline-utils/src/render-frame-strip-to-canvas.ts
@@ -24,6 +24,7 @@ export type RenderFrameStripToCanvasOptions = {
readonly width: number;
readonly frameHeight: number;
readonly devicePixelRatio: number;
+ readonly horizontalOffset: number;
readonly signal?: AbortSignal;
};
@@ -44,9 +45,11 @@ export const renderFrameStripToCanvas = async ({
width,
frameHeight,
devicePixelRatio,
+ horizontalOffset,
signal,
}: RenderFrameStripToCanvasOptions): Promise<void> => {
- const naturalWidth = Math.max(1, Math.ceil(width));
+ const drawingWidth = Math.max(1, width);
+ const naturalWidth = Math.max(1, Math.ceil(width + horizontalOffset));
const naturalHeight = Math.max(1, Math.ceil(frameHeight));
const segmentDuration = toSeconds - fromSeconds;
@@ -59,6 +62,7 @@ export const renderFrameStripToCanvas = async ({
const ctx = getContext(canvas);
ctx.clearRect(0, 0, naturalWidth, naturalHeight);
+ ctx.setTransform(1, 0, 0, 1, horizontalOffset, 0);
const filledSlots = new Map<number, number | undefined>();
let aspectRatio = getAspectRatioFromCache(src);
@@ -66,7 +70,7 @@ export const renderFrameStripToCanvas = async ({
if (aspectRatio !== null) {
ensureSlots({
filledSlots,
- naturalWidth,
+ naturalWidth: drawingWidth,
fromSeconds,
toSeconds,
aspectRatio,
@@ -75,13 +79,14 @@ export const renderFrameStripToCanvas = async ({
fillWithCachedFrames({
ctx,
- naturalWidth,
+ naturalWidth: drawingWidth,
filledSlots,
src,
segmentDuration,
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
});
const hasUnfilledSlots = Array.from(filledSlots.values()).some(
@@ -101,7 +106,7 @@ export const renderFrameStripToCanvas = async ({
ensureSlots({
filledSlots,
- naturalWidth,
+ naturalWidth: drawingWidth,
fromSeconds,
toSeconds,
aspectRatio,
@@ -136,7 +141,7 @@ export const renderFrameStripToCanvas = async ({
ensureSlots({
filledSlots,
- naturalWidth,
+ naturalWidth: drawingWidth,
fromSeconds,
toSeconds,
aspectRatio,
@@ -146,12 +151,13 @@ export const renderFrameStripToCanvas = async ({
fillFrameWhereItFits({
ctx,
filledSlots,
- visualizationWidth: naturalWidth,
+ visualizationWidth: drawingWidth,
frame: transformed,
segmentDuration,
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
});
} catch (e) {
if (frame) {
@@ -171,12 +177,13 @@ export const renderFrameStripToCanvas = async ({
fillWithCachedFrames({
ctx,
- naturalWidth,
+ naturalWidth: drawingWidth,
filledSlots,
src,
segmentDuration,
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
});
};
diff --git a/packages/timeline-utils/src/render-frame-strip.ts b/packages/timeline-utils/src/render-frame-strip.ts
index 7a5aa3e4a8..4d467d0b6b 100644
--- a/packages/timeline-utils/src/render-frame-strip.ts
+++ b/packages/timeline-utils/src/render-frame-strip.ts
@@ -1,3 +1,4 @@
+import {snapCanvasPositionToDevicePixel} from './device-pixel-alignment';
import type {FrameDatabaseKey} from './frame-database';
import {
frameDatabase,
@@ -122,7 +123,7 @@ export const drawSlot = ({
fromSeconds: number;
devicePixelRatio: number;
frameHeight: number;
- horizontalOffset?: number;
+ horizontalOffset: number;
}) => {
const durationOfOneFrame = getDurationOfOneFrame({
visualizationWidth,
@@ -138,14 +139,14 @@ export const drawSlot = ({
const idealRight = idealLeft + thumbnailWidth;
// When the canvas is aligned to the device-pixel grid, use the same absolute
// grid for both edges so adjacent thumbnails neither shimmer nor leave seams.
- const left =
- horizontalOffset === undefined
- ? idealLeft
- : Math.floor(idealLeft + horizontalOffset) - horizontalOffset;
- const right =
- horizontalOffset === undefined
- ? idealRight
- : Math.floor(idealRight + horizontalOffset) - horizontalOffset;
+ const left = snapCanvasPositionToDevicePixel({
+ horizontalOffset,
+ position: idealLeft,
+ });
+ const right = snapCanvasPositionToDevicePixel({
+ horizontalOffset,
+ position: idealRight,
+ });
ctx.drawImage(
frame,
@@ -176,7 +177,7 @@ export const fillWithCachedFrames = ({
fromSeconds: number;
devicePixelRatio: number;
frameHeight: number;
- horizontalOffset?: number;
+ horizontalOffset: number;
}) => {
const prefix = getFrameDatabaseKeyPrefix(src);
const keys = Array.from(frameDatabase.keys()).filter((k) =>
@@ -251,7 +252,7 @@ export const fillFrameWhereItFits = ({
fromSeconds: number;
devicePixelRatio: number;
frameHeight: number;
- horizontalOffset?: number;
+ horizontalOffset: number;
}) => {
const slots = Array.from(filledSlots.keys());
diff --git a/packages/timeline-utils/src/test/render-frame-strip.test.ts b/packages/timeline-utils/src/test/render-frame-strip.test.ts
index f716808376..244c024eec 100644
--- a/packages/timeline-utils/src/test/render-frame-strip.test.ts
+++ b/packages/timeline-utils/src/test/render-frame-strip.test.ts
@@ -23,6 +23,7 @@ test('filmstrip thumbnails keep their global position after a split', () => {
frame,
frameHeight: frame.displayHeight,
fromSeconds: 0,
+ horizontalOffset: 0,
segmentDuration: 3,
timestamp: frame.timestamp,
visualizationWidth: pixelsPerSecond * 3,
@@ -34,6 +35,7 @@ test('filmstrip thumbnails keep their global position after a split', () => {
frame,
frameHeight: frame.displayHeight,
fromSeconds: splitAtSeconds,
+ horizontalOffset: (splitAtSeconds * pixelsPerSecond) % 1,
segmentDuration: 3 - splitAtSeconds,
timestamp: frame.timestamp,
visualizationWidth: pixelsPerSecond * (3 - splitAtSeconds),