π₯ Make videos programmatically with React
`remotion`: Add `pixelDensity` prop to `<Solid>` (#8102)
Jonny Burger403b3f803822
diff --git a/packages/core/src/effects/Solid.tsx b/packages/core/src/effects/Solid.tsxindex b83a30a6c6..6dcc1f6558 100644--- a/packages/core/src/effects/Solid.tsx+++ b/packages/core/src/effects/Solid.tsx@@ -38,6 +38,25 @@ type OptionalProps = {readonly effects: EffectsProp;readonly className: string | undefined;readonly style: React.CSSProperties | undefined;+ readonly pixelDensity: number | undefined;+};++const resolveSolidPixelDensity = (pixelDensity: number | undefined): number => {+ if (pixelDensity === undefined) {+ return 1;+ }++ if (+ typeof pixelDensity !== 'number' ||+ !Number.isFinite(pixelDensity) ||+ pixelDensity <= 0+ ) {+ throw new Error(+ `<Solid>: \`pixelDensity\` must be a positive finite number. Received: ${String(pixelDensity)}.`,+ );+ }++ return pixelDensity;};type InnerSolidProps = MandatoryProps &@@ -86,11 +105,16 @@ const SolidInner: React.FC<effects = [],className,style,+ pixelDensity,overrideId,reference,}) => {const {delayRender, continueRender, cancelRender} = useDelayRender();+ const resolvedPixelDensity = resolveSolidPixelDensity(pixelDensity);+ const canvasWidth = Math.ceil(width * resolvedPixelDensity);+ const canvasHeight = Math.ceil(height * resolvedPixelDensity);+const [outputCanvas, setOutputCanvas] = useState<HTMLCanvasElement | null>(null,);@@ -156,12 +180,12 @@ const SolidInner: React.FC<}runEffectChain({- state: chainState.get(width, height)!,+ state: chainState.get(canvasWidth, canvasHeight)!,source: sourceCanvas,effects: memoizedEffects,output: outputCanvas,- width,- height,+ width: canvasWidth,+ height: canvasHeight,}).then((completed) => {if (completed) {@@ -180,21 +204,29 @@ const SolidInner: React.FC<outputCanvas,sourceCanvas,chainState,- width,- height,+ canvasWidth,+ canvasHeight,delayRender,continueRender,cancelRender,memoizedEffects,]);+ const canvasStyle = useMemo(() => {+ return {+ width,+ height,+ ...(style ?? {}),+ };+ }, [height, style, width]);+return (<canvasref={canvasRef}- width={width}- height={height}+ width={canvasWidth}+ height={canvasHeight}className={className}- style={style}+ style={canvasStyle}/>);};@@ -222,6 +254,7 @@ const SolidOuter = forwardRef<from,hidden,showInTimeline,+ pixelDensity,...props},ref,@@ -261,6 +294,7 @@ const SolidOuter = forwardRef<className={className}style={style}effects={effects}+ pixelDensity={pixelDensity}/></Sequence>);diff --git a/packages/core/src/test/solid.test.tsx b/packages/core/src/test/solid.test.tsxindex dbc9a2b602..ffcc02006e 100644--- a/packages/core/src/test/solid.test.tsx+++ b/packages/core/src/test/solid.test.tsx@@ -88,3 +88,39 @@ test('<Solid> accepts an empty effects array', () => {expect(container.querySelector('canvas')).not.toBeNull();});++test('<Solid> scales the backing canvas by pixelDensity while keeping logical CSS size', () => {+ const {container} = render(+ <WrapSequenceContext>+ <Solid color="red" width={120} height={80} pixelDensity={2} />+ </WrapSequenceContext>,+ );++ const canvas = container.querySelector('canvas');+ expect(canvas?.getAttribute('width')).toBe('240');+ expect(canvas?.getAttribute('height')).toBe('160');+ expect(canvas?.style.width).toBe('120px');+ expect(canvas?.style.height).toBe('80px');+});++test('<Solid> rounds up the backing canvas for fractional pixelDensity', () => {+ const {container} = render(+ <WrapSequenceContext>+ <Solid color="red" width={101} height={50} pixelDensity={1.5} />+ </WrapSequenceContext>,+ );++ const canvas = container.querySelector('canvas');+ expect(canvas?.getAttribute('width')).toBe('152');+ expect(canvas?.getAttribute('height')).toBe('75');+});++test('<Solid> throws for an invalid pixelDensity', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Solid color="red" width={120} height={80} pixelDensity={0} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/`pixelDensity` must be a positive finite number/);+});diff --git a/packages/docs/docs/remotion/html-in-canvas.mdx b/packages/docs/docs/remotion/html-in-canvas.mdxindex 76aca2c59d..7b7810048d 100644--- a/packages/docs/docs/remotion/html-in-canvas.mdx+++ b/packages/docs/docs/remotion/html-in-canvas.mdx@@ -78,7 +78,8 @@ _number_Controls the backing bitmap density. Default: `1`.-Set a higher value to paint to a higher-resolution backing canvas while keeping the logical canvas size unchanged.+Set a higher value to paint to a higher-resolution backing canvas while keeping the logical canvas size unchanged.+This is useful to counteract quality loss when the canvas is scaled up β for example via the [`scale`](/docs/scaling) render option, or on a high-DPI display in the preview. Pass [`usePixelDensity()`](/docs/use-pixel-density) to use the render `scale` while rendering and `window.devicePixelRatio` while previewing.### `children`diff --git a/packages/docs/docs/scaling.mdx b/packages/docs/docs/scaling.mdxindex a035aa734e..b1993f505a 100644--- a/packages/docs/docs/scaling.mdx+++ b/packages/docs/docs/scaling.mdx@@ -50,7 +50,27 @@ Elements that can be upscaled and that will enhance increased resolution are:- SVG elements- Images (if their resolution is sufficient to display in a higher resolution)+Canvas and WebGL elements cannot be upscaled automatically, since their backing bitmap resolution is fixed by design.+You can however opt into a higher backing resolution for the following components by passing a `pixelDensity` prop:++- [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) β [`pixelDensity`](/docs/remotion/html-in-canvas#pixeldensity)+- [`<Solid>`](/docs/solid) β [`pixelDensity`](/docs/solid#pixeldensity)++To support the `scale` option during a render, pass [`usePixelDensity()`](/docs/use-pixel-density), which resolves to the render `scale`:++```tsx twoslash title="Supporting scale in the render"+import {AbsoluteFill, Solid, usePixelDensity} from 'remotion';++export const MyComp: React.FC = () => {+ const pixelDensity = usePixelDensity();+ return (+ <AbsoluteFill>+ <Solid color="#3344ff" width={1920} height={1080} pixelDensity={pixelDensity} />+ </AbsoluteFill>+ );+};+```+Elements that **cannot** be upscaled for increased resolution are:- Videos-- Canvas and WebGL elementsdiff --git a/packages/docs/docs/solid.mdx b/packages/docs/docs/solid.mdxindex 63fae8537c..757137a85b 100644--- a/packages/docs/docs/solid.mdx+++ b/packages/docs/docs/solid.mdx@@ -25,17 +25,39 @@ export const MyComp: React.FC = () => {### `width`-Width of the canvas in pixels. Must be a positive integer.+Logical width of the canvas in pixels. Must be a positive integer.### `height`-Height of the canvas in pixels. Must be a positive integer.+Logical height of the canvas in pixels. Must be a positive integer.### `color?`Fill color of the rectangle.If omitted, it is `transparent`.+### `pixelDensity?`<AvailableFrom v="4.0.472" />++_number_++Controls the backing bitmap density. Default: `1`.++Set a higher value to paint to a higher-resolution backing canvas while keeping the logical canvas size unchanged.+This is useful to counteract quality loss when the `<Solid>` is scaled up β for example via the [`scale`](/docs/scaling) render option, or on a high-DPI display in the preview. Pass [`usePixelDensity()`](/docs/use-pixel-density) to use the render `scale` while rendering and `window.devicePixelRatio` while previewing:++```tsx twoslash title="MyComp.tsx"+import {AbsoluteFill, Solid, usePixelDensity} from 'remotion';++export const MyComp: React.FC = () => {+ const pixelDensity = usePixelDensity();+ return (+ <AbsoluteFill>+ <Solid color="#3344ff" width={1920} height={1080} pixelDensity={pixelDensity} />+ </AbsoluteFill>+ );+};+```+### `className?`A class name to apply to the `<canvas>` element.@@ -76,3 +98,5 @@ Effects are an upcoming feature of Remotion - if you are here, you are early!- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/effects/Solid.tsx)- [`<AbsoluteFill>`](/docs/absolute-fill)+- [Output scaling](/docs/scaling)+- [`usePixelDensity()`](/docs/use-pixel-density)