Skip to content

🎥 Make videos programmatically with React

Add internal Studio canvas pixel ratio override

Jonny Burger43e41ce9e65c
diff --git a/packages/it-tests/src/monorepo/studio-pixel-ratio.test.ts b/packages/it-tests/src/monorepo/studio-pixel-ratio.test.ts
new file mode 100644
index 0000000000..03e5c1c17f
--- /dev/null
+++ b/packages/it-tests/src/monorepo/studio-pixel-ratio.test.ts
@@ -0,0 +1,21 @@
+import {expect, test} from 'bun:test';
+import {readFileSync} from 'fs';
+import path from 'path';
+
+test('Studio canvas pixel ratio override is disabled in committed code', () => {
+ const source = readFileSync(
+ path.join(
+ __dirname,
+ '..',
+ '..',
+ '..',
+ 'studio',
+ 'src',
+ 'helpers',
+ 'studio-pixel-ratio.ts',
+ ),
+ 'utf-8',
+ );
+
+ expect(source).toContain('const OVERRIDE_PIXEL_RATIO: number | null = null;');
+});
diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsx
index 79414bbd11..a5b73581ef 100644
--- a/packages/studio/src/components/AudioWaveform.tsx
+++ b/packages/studio/src/components/AudioWaveform.tsx
@@ -11,6 +11,7 @@ import {Internals} from 'remotion';
import {WHITE_ALPHA_70, WHITE_ALPHA_60} from '../helpers/colors';
import {TIMELINE_FRAME_WIDTH_AT_MAX_ZOOM} from '../helpers/get-timeline-max-zoom';
import {resolveStudioColor} from '../helpers/resolve-studio-color';
+import {getStudioPixelRatio} from '../helpers/studio-pixel-ratio';
import {TIMELINE_BORDER} from '../helpers/timeline-layout';
const EMPTY_PEAKS = new Float32Array(0);
@@ -153,7 +154,7 @@ const AudioWaveformInner: React.FC<{
return;
}
- const pixelRatio = window.devicePixelRatio;
+ const pixelRatio = getStudioPixelRatio();
const h = Math.ceil(height * pixelRatio);
const w = Math.ceil(visualizationWidth * pixelRatio);
const drawingWidth = visualizationWidth * pixelRatio;
@@ -185,7 +186,7 @@ const AudioWaveformInner: React.FC<{
return;
}
- const pixelRatio = window.devicePixelRatio;
+ const pixelRatio = getStudioPixelRatio();
const h = Math.ceil(height * pixelRatio);
const w = Math.ceil(visualizationWidth * pixelRatio);
const drawingWidth = visualizationWidth * pixelRatio;
diff --git a/packages/studio/src/components/EditorRuler/Ruler.tsx b/packages/studio/src/components/EditorRuler/Ruler.tsx
index c91ed186f2..a0e158625c 100644
--- a/packages/studio/src/components/EditorRuler/Ruler.tsx
+++ b/packages/studio/src/components/EditorRuler/Ruler.tsx
@@ -11,6 +11,7 @@ import {BACKGROUND, SHADOW_BLACK} from '../../helpers/colors';
import {getRulerGuideHighlight} from '../../helpers/editor-guide-selection';
import {drawMarkingOnRulerCanvas} from '../../helpers/editor-ruler';
import {getRulerCanvasSize} from '../../helpers/ruler-canvas-size';
+import {getStudioPixelRatio} from '../../helpers/studio-pixel-ratio';
import {EditorShowGuidesContext} from '../../state/editor-guides';
import {forceSpecificCursor} from '../ForceSpecificCursor';
import {PREVENT_CLEAR_SELECTION_ON_POINTER_DOWN_ATTR} from '../Timeline/should-clear-selection-on-pointer-down';
@@ -87,6 +88,7 @@ const Ruler: React.FC<RulerProps> = ({
orientation,
size,
});
+ const pixelRatio = getStudioPixelRatio();
useEffect(() => {
drawMarkingOnRulerCanvas({
@@ -98,8 +100,8 @@ const Ruler: React.FC<RulerProps> = ({
orientation,
rulerCanvasRef,
guideHighlight,
- canvasHeight: rulerHeight * window.devicePixelRatio,
- canvasWidth: rulerWidth * window.devicePixelRatio,
+ canvasHeight: rulerHeight * pixelRatio,
+ canvasWidth: rulerWidth * pixelRatio,
});
}, [
scale,
@@ -112,6 +114,7 @@ const Ruler: React.FC<RulerProps> = ({
size,
rulerHeight,
rulerWidth,
+ pixelRatio,
]);
const rulerStyle: React.CSSProperties = useMemo(
@@ -174,8 +177,8 @@ const Ruler: React.FC<RulerProps> = ({
return (
<canvas
ref={rulerCanvasRef}
- width={rulerWidth * window.devicePixelRatio}
- height={rulerHeight * window.devicePixelRatio}
+ width={rulerWidth * pixelRatio}
+ height={rulerHeight * pixelRatio}
style={rulerStyle}
aria-label={`${isVerticalRuler ? 'Vertical' : 'Horizontal'} ruler`}
aria-readonly={!canCreateGuides}
diff --git a/packages/studio/src/components/Timeline/TimelineTimeIndicators.tsx b/packages/studio/src/components/Timeline/TimelineTimeIndicators.tsx
index 74f3674a85..949d640309 100644
--- a/packages/studio/src/components/Timeline/TimelineTimeIndicators.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTimeIndicators.tsx
@@ -7,6 +7,7 @@ import {
WHITE_ALPHA_15,
} from '../../helpers/colors';
import {resolveStudioColor} from '../../helpers/resolve-studio-color';
+import {getStudioPixelRatio} from '../../helpers/studio-pixel-ratio';
import {
TIMELINE_ITEM_BORDER_BOTTOM,
TIMELINE_PADDING,
@@ -282,7 +283,7 @@ const TimelineTimeIndicatorsInner = React.memo<{
const tickColor = resolveStudioColor(WHITE_ALPHA_15, computedStyle);
const labelColor = resolveStudioColor(LIGHT_TEXT, computedStyle);
const {clientWidth: width, scrollLeft} = scrollable;
- const pixelRatio = window.devicePixelRatio;
+ const pixelRatio = getStudioPixelRatio();
const canvasWidth = Math.ceil(width * pixelRatio);
const canvasHeight = TIMELINE_TIME_INDICATOR_HEIGHT * pixelRatio;
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index dd7b8d513d..3413b6a4bb 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -18,6 +18,7 @@ import React, {useEffect, useMemo, useRef, useState} from 'react';
import type {LoopDisplay} from 'remotion';
import {Internals, useVideoConfig} from 'remotion';
import {BLACK_ALPHA_30} from '../../helpers/colors';
+import {getStudioPixelRatio} from '../../helpers/studio-pixel-ratio';
import {
TIMELINE_LAYER_FILMSTRIP_HEIGHT,
TIMELINE_VIDEO_INFO_WAVEFORM_HEIGHT,
@@ -108,7 +109,7 @@ const TimelineVideoInfoSegment: React.FC<{
}
const controller = new AbortController();
- const pixelRatio = window.devicePixelRatio;
+ const pixelRatio = getStudioPixelRatio();
const canvas = document.createElement('canvas');
canvas.width = Math.ceil(visualizationWidth * pixelRatio);
diff --git a/packages/studio/src/helpers/editor-ruler.ts b/packages/studio/src/helpers/editor-ruler.ts
index 331df2325f..b3ff25f24e 100644
--- a/packages/studio/src/helpers/editor-ruler.ts
+++ b/packages/studio/src/helpers/editor-ruler.ts
@@ -8,6 +8,7 @@ import {
} from './colors';
import type {RulerGuideHighlight} from './editor-guide-selection';
import {resolveStudioColor} from './resolve-studio-color';
+import {getStudioPixelRatio} from './studio-pixel-ratio';
type Orientation = 'horizontal' | 'vertical';
@@ -182,7 +183,8 @@ export const drawMarkingOnRulerCanvas = ({
canvas.width = canvasWidth;
canvas.height = canvasHeight;
- context.scale(window.devicePixelRatio, window.devicePixelRatio);
+ const pixelRatio = getStudioPixelRatio();
+ context.scale(pixelRatio, pixelRatio);
context.clearRect(0, 0, canvasWidth, canvasHeight);
diff --git a/packages/studio/src/helpers/studio-pixel-ratio.ts b/packages/studio/src/helpers/studio-pixel-ratio.ts
new file mode 100644
index 0000000000..19b7a59d7a
--- /dev/null
+++ b/packages/studio/src/helpers/studio-pixel-ratio.ts
@@ -0,0 +1,5 @@
+// Set this to a number temporarily to increase Studio canvas backing resolution.
+const OVERRIDE_PIXEL_RATIO: number | null = null;
+
+export const getStudioPixelRatio = (): number =>
+ OVERRIDE_PIXEL_RATIO ?? window.devicePixelRatio;