Skip to content

🎥 Make videos programmatically with React

add support for transform origin

Jonny Burger4b3bab4772d2
diff --git a/packages/docs/components/shapes/shapes-info.tsx b/packages/docs/components/shapes/shapes-info.tsx
index 95ecef98cc..ba665fb87d 100644
--- a/packages/docs/components/shapes/shapes-info.tsx
+++ b/packages/docs/components/shapes/shapes-info.tsx
@@ -107,6 +107,17 @@ const globalParams: Param[] = [
description: (
<>
CSS properties that will be applied to the <code>{"<svg>"}</code> tag.
+ Default style: <code>{"overflow: 'visible'"}</code>
+ </>
+ ),
+ },
+ {
+ name: "pathStyle",
+ type: "string",
+ description: (
+ <>
+ CSS properties that will be applied to the <code>{"<path>"}</code> tag.
+ Default style: <code>{"overflow: 'visible'"}</code>
</>
),
},
diff --git a/packages/example/src/Shapes/TriangleTest.tsx b/packages/example/src/Shapes/TriangleTest.tsx
index 12c79a091a..c5b6220e54 100644
--- a/packages/example/src/Shapes/TriangleTest.tsx
+++ b/packages/example/src/Shapes/TriangleTest.tsx
@@ -1,8 +1,11 @@
import {Triangle} from '@remotion/shapes';
import React from 'react';
-import {AbsoluteFill} from 'remotion';
+import {AbsoluteFill, interpolate, useCurrentFrame} from 'remotion';
const TriangleTest: React.FC = () => {
+ const frame = useCurrentFrame();
+ const rotation = interpolate(frame, [0, 100], [0, 360]);
+
return (
<AbsoluteFill
style={{
@@ -14,8 +17,8 @@ const TriangleTest: React.FC = () => {
}}
>
<Triangle
- style={{
- margin: '10px',
+ pathStyle={{
+ transform: `rotate(${rotation}deg)`,
}}
length={100}
fill="red"
@@ -23,24 +26,24 @@ const TriangleTest: React.FC = () => {
/>
<Triangle
- style={{
- margin: '10px',
+ pathStyle={{
+ transform: `rotate(${rotation}deg)`,
}}
length={100}
fill="red"
direction="right"
/>
<Triangle
- style={{
- margin: '10px',
+ pathStyle={{
+ transform: `rotate(${rotation}deg)`,
}}
length={100}
fill="red"
direction="top"
/>
<Triangle
- style={{
- margin: '10px',
+ pathStyle={{
+ transform: `rotate(${rotation}deg)`,
}}
length={100}
fill="red"
diff --git a/packages/shapes/src/components/circle.tsx b/packages/shapes/src/components/circle.tsx
index 781791451c..f8ded69606 100644
--- a/packages/shapes/src/components/circle.tsx
+++ b/packages/shapes/src/components/circle.tsx
@@ -7,7 +7,5 @@ import {RenderSvg} from './render-svg';
export type CircleProps = MakeCircleProps & AllShapesProps;
export const Circle: React.FC<CircleProps> = ({radius, ...props}) => {
- const {path, height, width} = makeCircle({radius});
-
- return <RenderSvg width={width} height={height} path={path} {...props} />;
+ return <RenderSvg {...makeCircle({radius})} {...props} />;
};
diff --git a/packages/shapes/src/components/ellipse.tsx b/packages/shapes/src/components/ellipse.tsx
index 7099842098..7330110809 100644
--- a/packages/shapes/src/components/ellipse.tsx
+++ b/packages/shapes/src/components/ellipse.tsx
@@ -7,9 +7,5 @@ import {RenderSvg} from './render-svg';
export type EllipseProps = MakeEllipseOptions & AllShapesProps;
export const Ellipse: React.FC<EllipseProps> = ({rx, ry, ...props}) => {
- const {path, width, height} = makeEllipse({
- rx,
- ry,
- });
- return <RenderSvg width={width} height={height} path={path} {...props} />;
+ return <RenderSvg {...makeEllipse({rx, ry})} {...props} />;
};
diff --git a/packages/shapes/src/components/rect.tsx b/packages/shapes/src/components/rect.tsx
index e7027365a1..c565b2272e 100644
--- a/packages/shapes/src/components/rect.tsx
+++ b/packages/shapes/src/components/rect.tsx
@@ -7,7 +7,5 @@ import {RenderSvg} from './render-svg';
export type RectProps = MakeRectOptions & AllShapesProps;
export const Rect: React.FC<RectProps> = ({width, height, ...props}) => {
- const {path} = makeRect({width, height});
-
- return <RenderSvg height={height} width={width} path={path} {...props} />;
+ return <RenderSvg {...makeRect({height, width})} {...props} />;
};
diff --git a/packages/shapes/src/components/render-svg.tsx b/packages/shapes/src/components/render-svg.tsx
index 245df884e7..1251c6a0c5 100644
--- a/packages/shapes/src/components/render-svg.tsx
+++ b/packages/shapes/src/components/render-svg.tsx
@@ -3,28 +3,38 @@ import React, {useMemo} from 'react';
export type AllShapesProps = Omit<
React.SVGProps<SVGPathElement>,
'width' | 'height' | 'd'
->;
+> & {
+ pathStyle?: React.CSSProperties;
+};
export const RenderSvg = ({
width,
height,
path,
- fill,
style,
- stroke,
- strokeWidth,
+ pathStyle,
+ transformOrigin,
+ ...props
}: {
width: number;
height: number;
path: string;
+ transformOrigin: string;
} & AllShapesProps) => {
- const actualStyle = useMemo(() => {
+ const actualStyle = useMemo((): React.CSSProperties => {
return {
overflow: 'visible',
...(style ?? {}),
};
}, [style]);
+ const actualPathStyle = useMemo((): React.CSSProperties => {
+ return {
+ transformBox: 'fill-box',
+ ...(pathStyle ?? {}),
+ };
+ }, [pathStyle]);
+
return (
<svg
width={width}
@@ -33,7 +43,12 @@ export const RenderSvg = ({
xmlns="http://www.w3.org/2000/svg"
style={actualStyle}
>
- <path d={path} fill={fill} stroke={stroke} strokeWidth={strokeWidth} />
+ <path
+ transform-origin={transformOrigin}
+ d={path}
+ style={actualPathStyle}
+ {...props}
+ />
</svg>
);
};
diff --git a/packages/shapes/src/components/triangle.tsx b/packages/shapes/src/components/triangle.tsx
index 09be0aa758..a761a847f4 100644
--- a/packages/shapes/src/components/triangle.tsx
+++ b/packages/shapes/src/components/triangle.tsx
@@ -11,9 +11,5 @@ export const Triangle: React.FC<TriangleProps> = ({
direction,
...props
}) => {
- const {path, height, width} = makeTriangle({
- length,
- direction,
- });
- return <RenderSvg height={height} width={width} path={path} {...props} />;
+ return <RenderSvg {...makeTriangle({length, direction})} {...props} />;
};
diff --git a/packages/shapes/src/utils/make-circle.ts b/packages/shapes/src/utils/make-circle.ts
index bbfb8e855c..b39215625e 100644
--- a/packages/shapes/src/utils/make-circle.ts
+++ b/packages/shapes/src/utils/make-circle.ts
@@ -1,3 +1,4 @@
+import {makeEllipse} from './make-ellipse';
import type {ShapeInfo} from './shape-info';
export type MakeCircleProps = {
@@ -5,14 +6,5 @@ export type MakeCircleProps = {
};
export const makeCircle = ({radius}: MakeCircleProps): ShapeInfo => {
- const cx = radius;
- const cy = radius;
-
- return {
- height: radius * 2,
- width: radius * 2,
- path: `M ${cx} ${cy} m -${radius} 0 a ${radius} ${radius} 0 1 0 ${
- (radius as number) * 2
- } 0 ${radius} ${radius} 0 1 0 ${-radius * 2} 0`,
- };
+ return makeEllipse({rx: radius, ry: radius});
};
diff --git a/packages/shapes/src/utils/make-ellipse.ts b/packages/shapes/src/utils/make-ellipse.ts
index 494ad43797..84b96a440f 100644
--- a/packages/shapes/src/utils/make-ellipse.ts
+++ b/packages/shapes/src/utils/make-ellipse.ts
@@ -10,5 +10,6 @@ export const makeEllipse = ({rx, ry}: MakeEllipseOptions): ShapeInfo => {
width: rx * 2,
height: ry * 2,
path: `M ${rx} ${0} a ${rx} ${ry} 0 1 0 1 0`,
+ transformOrigin: `${rx} ${ry}`,
};
};
diff --git a/packages/shapes/src/utils/make-rect.ts b/packages/shapes/src/utils/make-rect.ts
index b515af524d..19efa127d3 100644
--- a/packages/shapes/src/utils/make-rect.ts
+++ b/packages/shapes/src/utils/make-rect.ts
@@ -10,5 +10,6 @@ export const makeRect = ({width, height}: MakeRectOptions): ShapeInfo => {
width,
height,
path: `M 0 0 l ${width} 0 l 0 ${height} l ${-width} 0 Z`,
+ transformOrigin: `${width / 2} ${height / 2}`,
};
};
diff --git a/packages/shapes/src/utils/make-triangle.ts b/packages/shapes/src/utils/make-triangle.ts
index 5c92ae2f0c..84230600d0 100644
--- a/packages/shapes/src/utils/make-triangle.ts
+++ b/packages/shapes/src/utils/make-triangle.ts
@@ -45,10 +45,25 @@ export const makeTriangle = ({
],
};
+ const transformOriginX = {
+ left: (shorterSize / 3) * 2,
+ right: shorterSize / 3,
+ top: longerDimension / 2,
+ bottom: longerDimension / 2,
+ }[direction];
+
+ const transformOriginY = {
+ top: (shorterSize / 3) * 2,
+ bottom: shorterSize / 3,
+ left: longerDimension / 2,
+ right: longerDimension / 2,
+ }[direction];
+
return {
path: `M ${points[direction].join(' ')} z`,
width: direction === 'top' || direction === 'bottom' ? length : shorterSize,
height:
direction === 'top' || direction === 'bottom' ? shorterSize : length,
+ transformOrigin: `${transformOriginX} ${transformOriginY}`,
};
};
diff --git a/packages/shapes/src/utils/shape-info.ts b/packages/shapes/src/utils/shape-info.ts
index 7784a5a90a..e47d0547d4 100644
--- a/packages/shapes/src/utils/shape-info.ts
+++ b/packages/shapes/src/utils/shape-info.ts
@@ -2,4 +2,5 @@ export type ShapeInfo = {
path: string;
width: number;
height: number;
+ transformOrigin: string;
};