Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Fix SVG sizing with `preserveAspectRatio="none"`

Jonny Burger265475635e84
diff --git a/packages/web-renderer/src/drawing/turn-svg-into-drawable.ts b/packages/web-renderer/src/drawing/turn-svg-into-drawable.ts
index e54de9067f..32dfe43360 100644
--- a/packages/web-renderer/src/drawing/turn-svg-into-drawable.ts
+++ b/packages/web-renderer/src/drawing/turn-svg-into-drawable.ts
@@ -10,9 +10,21 @@ const drawableBySvg = new WeakMap<
>();
export const turnSvgIntoDrawable = (svg: SVGSVGElement) => {
- const {fill, color} = getComputedStyle(svg);
+ const {fill, color, width, height} = getComputedStyle(svg);
const originalStyle = svg.getAttribute('style');
+ const originalWidth = svg.getAttribute('width');
+ const originalHeight = svg.getAttribute('height');
+ const needsExplicitDimensions =
+ svg.preserveAspectRatio.baseVal.align ===
+ SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE;
+
+ // SVG images need explicit dimensions to preserve their viewport when
+ // preserveAspectRatio="none" removes the intrinsic aspect ratio.
+ if (needsExplicitDimensions) {
+ svg.setAttribute('width', width);
+ svg.setAttribute('height', height);
+ }
// Position and transforms are applied by the canvas renderer. Remove them
// from the serialized SVG so they are not applied again inside its viewport.
@@ -42,6 +54,20 @@ export const turnSvgIntoDrawable = (svg: SVGSVGElement) => {
svg.setAttribute('style', originalStyle);
}
+ if (needsExplicitDimensions) {
+ if (originalWidth === null) {
+ svg.removeAttribute('width');
+ } else {
+ svg.setAttribute('width', originalWidth);
+ }
+
+ if (originalHeight === null) {
+ svg.removeAttribute('height');
+ } else {
+ svg.setAttribute('height', originalHeight);
+ }
+ }
+
const embeddedFontStyle = getEmbeddedFontStyleForSvg(svg);
const fontStyleKey = embeddedFontStyle?.cacheKey ?? null;
const cached = drawableBySvg.get(svg);