Skip to content

🎥 Make videos programmatically with React

Keep canvas capture regions fixed while scrolling

Jonny Burger91e5d6417f31
diff --git a/packages/canvas-capture-extension/src/capture.ts b/packages/canvas-capture-extension/src/capture.ts
index ded7b79453..3aec881a94 100644
--- a/packages/canvas-capture-extension/src/capture.ts
+++ b/packages/canvas-capture-extension/src/capture.ts
@@ -14,6 +14,7 @@ import {
const maxCanvasDimension = 32_767;
const maxEncodedDimension = 32_766;
+// The capture window in viewport CSS pixels, independent of page scrolling.
export type CaptureCrop = CropRectangle;
export type CapturePreflight = {
@@ -49,25 +50,6 @@ const getWholePageSize = () => ({
),
});
-const resolveCrop = (
- crop: CaptureCrop | null,
- sourceWidth: number,
- sourceHeight: number,
-) => {
- if (!crop) {
- return null;
- }
-
- const left = Math.max(0, Math.min(crop.left, sourceWidth - 1));
- const top = Math.max(0, Math.min(crop.top, sourceHeight - 1));
- return {
- left,
- top,
- width: Math.max(1, Math.min(crop.width, sourceWidth - left)),
- height: Math.max(1, Math.min(crop.height, sourceHeight - top)),
- };
-};
-
const validateCaptureSize = ({
width,
height,
@@ -79,7 +61,7 @@ const validateCaptureSize = ({
readonly crop: CaptureCrop | null;
readonly scale: number;
}) => {
- const resolvedCrop = resolveCrop(crop, width, height) ?? {
+ const resolvedCrop = crop ?? {
left: 0,
top: 0,
width,
@@ -316,10 +298,7 @@ export class PageCapture {
sourceSize.height,
Math.max(window.devicePixelRatio, this.#scale),
);
- const initialCrop = this.#resolveCrop(
- sourceSize.width,
- sourceSize.height,
- ) ?? {left: 0, top: 0, ...sourceSize};
+ const initialCrop = this.#crop ?? {left: 0, top: 0, ...sourceSize};
syncCanvasSize(
this.#captureCanvas,
initialCrop.width,
@@ -338,6 +317,7 @@ export class PageCapture {
getFilename: () => getFilename(format),
});
this.#wrapped.canvas.addEventListener('paint', this.#onPaint);
+ window.addEventListener('scroll', this.#requestPaint, true);
this.#resizeObserver = new ResizeObserver(this.#requestPaint);
this.#resizeObserver.observe(this.#wrapped.content);
}
@@ -382,6 +362,7 @@ export class PageCapture {
this.#restored = true;
this.#resizeObserver.disconnect();
this.#wrapped.canvas.removeEventListener('paint', this.#onPaint);
+ window.removeEventListener('scroll', this.#requestPaint, true);
this.#recorder.dispose();
this.#wrapped.restore();
};
@@ -399,23 +380,13 @@ export class PageCapture {
this.#wrapped.canvas.requestPaint?.();
};
- #resolveCrop = (sourceWidth: number, sourceHeight: number) => {
- return resolveCrop(this.#crop, sourceWidth, sourceHeight);
- };
-
#getRecordingRect = () => {
- const contentRect = this.#wrapped.content.getBoundingClientRect();
- const crop = this.#resolveCrop(contentRect.width, contentRect.height);
+ const crop = this.#crop;
if (!crop) {
- return contentRect;
+ return this.#wrapped.content.getBoundingClientRect();
}
- return new DOMRect(
- contentRect.left + crop.left,
- contentRect.top + crop.top,
- crop.width,
- crop.height,
- );
+ return new DOMRect(crop.left, crop.top, crop.width, crop.height);
};
#draw = () => {
@@ -425,12 +396,16 @@ export class PageCapture {
}
this.#assertValidSize(width, height);
- const crop = this.#resolveCrop(width, height) ?? {
- left: 0,
- top: 0,
- width,
- height,
- };
+ const contentRect = this.#wrapped.content.getBoundingClientRect();
+ // Follow the viewport without shrinking the output at the page edges.
+ // drawImage clips any missing source pixels, leaving the matte visible.
+ const crop = this.#crop
+ ? {
+ ...this.#crop,
+ left: this.#crop.left - contentRect.left,
+ top: this.#crop.top - contentRect.top,
+ }
+ : {left: 0, top: 0, width, height};
syncDisplayCanvasSize(
this.#wrapped.canvas,
width,
diff --git a/packages/canvas-capture-extension/src/content.ts b/packages/canvas-capture-extension/src/content.ts
index 1899dfbb26..b15a27d185 100644
--- a/packages/canvas-capture-extension/src/content.ts
+++ b/packages/canvas-capture-extension/src/content.ts
@@ -61,16 +61,6 @@ export const startContent = () => {
const getFormatLabel = (format: CaptureFormat) =>
format === 'mp4' ? 'H.264 MP4' : 'VP9 WebM';
- const getCropRelativeTo = (
- selection: SelectionRectangle,
- target: DOMRect,
- ): CaptureCrop => ({
- left: selection.left - target.left,
- top: selection.top - target.top,
- width: Math.max(1, selection.width),
- height: Math.max(1, selection.height),
- });
-
const createController = (): ExtensionController => {
const host = document.createElement('div');
host.dataset.remotionCanvasCapture = 'true';
@@ -714,11 +704,10 @@ export const startContent = () => {
}
if (selectedTarget.type === 'whole-page') {
- const pageRect = document.body.getBoundingClientRect();
return {
crop: {
- left: -pageRect.left,
- top: -pageRect.top,
+ left: 0,
+ top: 0,
width: window.innerWidth,
height: window.innerHeight,
},
@@ -909,17 +898,7 @@ export const startContent = () => {
interactionShield.style.display =
capture || startingRecording ? 'none' : 'block';
const target = resolveCaptureTarget();
- let rect: Pick<DOMRect, 'left' | 'top' | 'width' | 'height'> | null =
- null;
- if (target?.crop) {
- const pageRect = document.body.getBoundingClientRect();
- rect = {
- left: pageRect.left + target.crop.left,
- top: pageRect.top + target.crop.top,
- width: target.crop.width,
- height: target.crop.height,
- };
- }
+ const rect = target?.crop;
if (!rect) {
dimensionsZoomPopover.hidden = true;
@@ -1094,7 +1073,7 @@ export const startContent = () => {
});
selectedTarget = {
type: 'page-crop',
- crop: getCropRelativeTo(rect, document.body.getBoundingClientRect()),
+ crop: rect,
};
updateHighlight();
};
@@ -1151,11 +1130,7 @@ export const startContent = () => {
selectionLayer.style.display = 'none';
selectionBox.style.display = 'none';
- const crop = getCropRelativeTo(
- selection,
- document.body.getBoundingClientRect(),
- );
- selectedTarget = {type: 'page-crop', crop};
+ selectedTarget = {type: 'page-crop', crop: selection};
controlsDismissed = false;
encoderSupportKey = null;
@@ -1196,7 +1171,6 @@ export const startContent = () => {
selectionLayer.addEventListener('pointerup', finishSelection);
selectionLayer.addEventListener('pointercancel', cancelSelection);
- window.addEventListener('scroll', updateHighlight, true);
window.addEventListener('resize', () => {
updateHighlight();
const supportCheck = refreshEncoderSupport();
diff --git a/packages/canvas-capture-extension/src/recorder.ts b/packages/canvas-capture-extension/src/recorder.ts
index bcf627cea8..2bb24845b4 100644
--- a/packages/canvas-capture-extension/src/recorder.ts
+++ b/packages/canvas-capture-extension/src/recorder.ts
@@ -364,6 +364,7 @@ export class CanvasCaptureRecorder {
readonly #options: CanvasCaptureRecorderOptions;
#recording: RecordingState | null = null;
#recordingAction: Promise<void> = Promise.resolve();
+ #cursorPosition: {clientX: number; clientY: number} | null = null;
#disposed = false;
constructor(options: CanvasCaptureRecorderOptions) {
@@ -371,6 +372,11 @@ export class CanvasCaptureRecorder {
window.addEventListener('pointermove', this.#onCursorMove);
// Native HTML drag-and-drop suppresses pointermove events while dragging.
window.addEventListener('dragover', this.#onCursorMove, true);
+ window.addEventListener('wheel', this.#onCursorMove, {
+ capture: true,
+ passive: true,
+ });
+ window.addEventListener('scroll', this.#recordCursorPosition, true);
window.addEventListener('pointerdown', this.#onPointerDown, true);
window.addEventListener('pointerup', this.#onPointerUp, true);
}
@@ -445,6 +451,7 @@ export class CanvasCaptureRecorder {
captureMetadata: null,
isFinalizing: false,
};
+ this.#recordCursorPosition();
};
stopRecording = async (): Promise<File | null> => {
@@ -519,6 +526,8 @@ export class CanvasCaptureRecorder {
this.#disposed = true;
window.removeEventListener('pointermove', this.#onCursorMove);
window.removeEventListener('dragover', this.#onCursorMove, true);
+ window.removeEventListener('wheel', this.#onCursorMove, true);
+ window.removeEventListener('scroll', this.#recordCursorPosition, true);
window.removeEventListener('pointerdown', this.#onPointerDown, true);
window.removeEventListener('pointerup', this.#onPointerUp, true);
@@ -535,24 +544,28 @@ export class CanvasCaptureRecorder {
};
#onCursorMove = (event: MouseEvent) => {
+ this.#cursorPosition = {clientX: event.clientX, clientY: event.clientY};
+ this.#recordCursorPosition();
+ };
+
+ #recordCursorPosition = () => {
const recording = this.#recording;
- if (!recording || recording.isFinalizing) {
+ if (!recording || recording.isFinalizing || !this.#cursorPosition) {
return;
}
+ const {clientX, clientY} = this.#cursorPosition;
const rect = this.#options.getContentRect();
const density = this.#options.getDensity();
recording.mouseMovements.push({
timeInSeconds: (performance.now() - recording.startedAt) / 1000,
- clientX: event.clientX,
- clientY: event.clientY,
- pageX: event.pageX,
- pageY: event.pageY,
- canvasX: (event.clientX - rect.left) * density,
- canvasY: (event.clientY - rect.top) * density,
- cursor: getCursorForElement(
- document.elementFromPoint(event.clientX, event.clientY),
- ),
+ clientX,
+ clientY,
+ pageX: clientX + window.scrollX,
+ pageY: clientY + window.scrollY,
+ canvasX: (clientX - rect.left) * density,
+ canvasY: (clientY - rect.top) * density,
+ cursor: getCursorForElement(document.elementFromPoint(clientX, clientY)),
});
};