🎥 Make videos programmatically with React
calculate shape info
Jonny Burgerc162d6c798e7
diff --git a/packages/shapes/src/components/circle.tsx b/packages/shapes/src/components/circle.tsxindex 9609b9c4c9..781791451c 100644--- a/packages/shapes/src/components/circle.tsx+++ b/packages/shapes/src/components/circle.tsx@@ -7,14 +7,7 @@ import {RenderSvg} from './render-svg';export type CircleProps = MakeCircleProps & AllShapesProps;export const Circle: React.FC<CircleProps> = ({radius, ...props}) => {- return (- <RenderSvg- width={radius * 2}- height={radius * 2}- path={makeCircle({- radius,- })}- {...props}- />- );+ const {path, height, width} = makeCircle({radius});++ return <RenderSvg width={width} height={height} path={path} {...props} />;};diff --git a/packages/shapes/src/components/ellipse.tsx b/packages/shapes/src/components/ellipse.tsxindex 6fa5551390..7099842098 100644--- a/packages/shapes/src/components/ellipse.tsx+++ b/packages/shapes/src/components/ellipse.tsx@@ -7,9 +7,9 @@ import {RenderSvg} from './render-svg';export type EllipseProps = MakeEllipseOptions & AllShapesProps;export const Ellipse: React.FC<EllipseProps> = ({rx, ry, ...props}) => {- const path = makeEllipse({+ const {path, width, height} = makeEllipse({rx,ry,});- return <RenderSvg width={rx * 2} height={ry * 2} path={path} {...props} />;+ return <RenderSvg width={width} height={height} path={path} {...props} />;};diff --git a/packages/shapes/src/components/rect.tsx b/packages/shapes/src/components/rect.tsxindex f1bc3ff592..e7027365a1 100644--- a/packages/shapes/src/components/rect.tsx+++ b/packages/shapes/src/components/rect.tsx@@ -7,15 +7,7 @@ import {RenderSvg} from './render-svg';export type RectProps = MakeRectOptions & AllShapesProps;export const Rect: React.FC<RectProps> = ({width, height, ...props}) => {- return (- <RenderSvg- height={height}- width={width}- path={makeRect({- width,- height,- })}- {...props}- />- );+ const {path} = makeRect({width, height});++ return <RenderSvg height={height} width={width} path={path} {...props} />;};diff --git a/packages/shapes/src/components/render-svg.tsx b/packages/shapes/src/components/render-svg.tsxindex 26a4652d0c..a1de8c4679 100644--- a/packages/shapes/src/components/render-svg.tsx+++ b/packages/shapes/src/components/render-svg.tsx@@ -35,7 +35,6 @@ export const RenderSvg = ({viewBox={`0 0 ${width} ${height}`}xmlns="http://www.w3.org/2000/svg"style={actualStyle}- data-shape-type="triangle"><path d={path} fill={fill} stroke={stroke} strokeWidth={strokeWidth} /></svg>diff --git a/packages/shapes/src/components/triangle.tsx b/packages/shapes/src/components/triangle.tsxindex fb4da1aea4..09be0aa758 100644--- a/packages/shapes/src/components/triangle.tsx+++ b/packages/shapes/src/components/triangle.tsx@@ -7,20 +7,13 @@ import {RenderSvg} from './render-svg';export type TriangleProps = MakeTriangleProps & AllShapesProps;export const Triangle: React.FC<TriangleProps> = ({- // TODO: Do not use width and height, but use length of a sidelength,direction,...props}) => {- return (- <RenderSvg- height={100}- width={100}- path={makeTriangle({- length,- direction,- })}- {...props}- />- );+ const {path, height, width} = makeTriangle({+ length,+ direction,+ });+ return <RenderSvg height={height} width={width} path={path} {...props} />;};diff --git a/packages/shapes/src/utils/make-circle.ts b/packages/shapes/src/utils/make-circle.tsindex 10dd91a50d..bbfb8e855c 100644--- a/packages/shapes/src/utils/make-circle.ts+++ b/packages/shapes/src/utils/make-circle.ts@@ -1,12 +1,18 @@+import type {ShapeInfo} from './shape-info';+export type MakeCircleProps = {radius: number;};-export const makeCircle = ({radius}: MakeCircleProps) => {+export const makeCircle = ({radius}: MakeCircleProps): ShapeInfo => {const cx = radius;const cy = radius;- return `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 {+ 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`,+ };};diff --git a/packages/shapes/src/utils/make-ellipse.ts b/packages/shapes/src/utils/make-ellipse.tsindex 8567aeb9d3..494ad43797 100644--- a/packages/shapes/src/utils/make-ellipse.ts+++ b/packages/shapes/src/utils/make-ellipse.ts@@ -1,8 +1,14 @@+import type {ShapeInfo} from './shape-info';+export type MakeEllipseOptions = {rx: number;ry: number;};-export const makeEllipse = ({rx = 50, ry = 50}: MakeEllipseOptions) => {- return `M ${rx} ${0} a ${rx} ${ry} 0 1 0 1 0`;+export const makeEllipse = ({rx, ry}: MakeEllipseOptions): ShapeInfo => {+ return {+ width: rx * 2,+ height: ry * 2,+ path: `M ${rx} ${0} a ${rx} ${ry} 0 1 0 1 0`,+ };};diff --git a/packages/shapes/src/utils/make-rect.ts b/packages/shapes/src/utils/make-rect.tsindex b8afb97124..ef31bd6ad6 100644--- a/packages/shapes/src/utils/make-rect.ts+++ b/packages/shapes/src/utils/make-rect.ts@@ -1,10 +1,17 @@+import type {ShapeInfo} from './shape-info';+export type MakeRectOptions = {width: number;height: number;};-export const makeRect = ({width, height}: MakeRectOptions) => {+export const makeRect = ({width, height}: MakeRectOptions): ShapeInfo => {const x = 0;const y = 0;- return `M ${x} ${y} l ${width} 0 l 0 ${height} l ${-width} 0 Z`;++ return {+ width,+ height,+ path: `M ${x} ${y} l ${width} 0 l 0 ${height} l ${-width} 0 Z`,+ };};diff --git a/packages/shapes/src/utils/make-triangle.ts b/packages/shapes/src/utils/make-triangle.tsindex b1ed172d83..5c92ae2f0c 100644--- a/packages/shapes/src/utils/make-triangle.ts+++ b/packages/shapes/src/utils/make-triangle.ts@@ -1,5 +1,7 @@// Copied from https://stackblitz.com/edit/react-triangle-svg?file=index.js+import type {ShapeInfo} from './shape-info';+export type MakeTriangleProps = {length: number;direction: 'right' | 'left' | 'top' | 'bottom';@@ -8,7 +10,7 @@ export type MakeTriangleProps = {export const makeTriangle = ({length,direction = 'right',-}: MakeTriangleProps) => {+}: MakeTriangleProps): ShapeInfo => {const longerDimension = length;const shorterSize = Math.sqrt(length ** 2 * 0.75); // Calculated on paper;@@ -42,5 +44,11 @@ export const makeTriangle = ({`0 ${longerDimension / 2}`,],};- return `M ${points[direction].join(' ')} z`;++ return {+ path: `M ${points[direction].join(' ')} z`,+ width: direction === 'top' || direction === 'bottom' ? length : shorterSize,+ height:+ direction === 'top' || direction === 'bottom' ? shorterSize : length,+ };};diff --git a/packages/shapes/src/utils/shape-info.ts b/packages/shapes/src/utils/shape-info.tsnew file mode 100644index 0000000000..7784a5a90a--- /dev/null+++ b/packages/shapes/src/utils/shape-info.ts@@ -0,0 +1,5 @@+export type ShapeInfo = {+ path: string;+ width: number;+ height: number;+};