🎥 Make videos programmatically with React
cornerradius
Jonny Burgerbff003e018e3
diff --git a/packages/docs/components/demos/Rect.tsx b/packages/docs/components/demos/Rect.tsxindex e5edd51a39..47d5053871 100644--- a/packages/docs/components/demos/Rect.tsx+++ b/packages/docs/components/demos/Rect.tsx@@ -8,7 +8,8 @@ export const RectDemo: React.FC<{edgeRoundness: number;darkMode: boolean;debug: boolean;-}> = ({ width, height, debug, edgeRoundness, darkMode }) => {+ cornerRadius: number;+}> = ({ width, height, debug, edgeRoundness, cornerRadius, darkMode }) => {return (<AbsoluteFillstyle={{@@ -22,6 +23,7 @@ export const RectDemo: React.FC<{width={width}height={height}debug={debug}+ cornerRadius={cornerRadius}/></AbsoluteFill>);diff --git a/packages/docs/components/demos/Triangle.tsx b/packages/docs/components/demos/Triangle.tsxindex 0300bae1ba..06c9371989 100644--- a/packages/docs/components/demos/Triangle.tsx+++ b/packages/docs/components/demos/Triangle.tsx@@ -9,7 +9,8 @@ export const TriangleDemo: React.FC<{darkMode: boolean;direction: TriangleProps["direction"];debug: boolean;-}> = ({ length, edgeRoundness, debug, direction, darkMode }) => {+ cornerRadius: number;+}> = ({ length, edgeRoundness, debug, direction, cornerRadius, darkMode }) => {return (<AbsoluteFillstyle={{@@ -23,6 +24,7 @@ export const TriangleDemo: React.FC<{direction={direction}length={length}debug={debug}+ cornerRadius={cornerRadius}/></AbsoluteFill>);diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsxindex a365e25d62..14b491ef4e 100644--- a/packages/docs/components/demos/index.tsx+++ b/packages/docs/components/demos/index.tsx@@ -67,7 +67,7 @@ export const Demo: React.FC<{borderBottom: "1px solid var(--ifm-color-emphasis-300)",}}inputProps={{ ...state, darkMode: isDarkTheme }}- autoPlay+ autoPlay={demo.autoPlay}loop/><div className={styles.containerrow}>diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.tsindex ea1a92e17e..419348ef0d 100644--- a/packages/docs/components/demos/types.ts+++ b/packages/docs/components/demos/types.ts@@ -64,6 +64,15 @@ export const rectDemo: DemoType = {name: "height",optional: "no",},+ {+ name: "cornerRadius",+ default: 0,+ max: 100,+ min: 0,+ step: 1,+ type: "numeric",+ optional: "no",+ },{name: "edgeRoundness",default: 1,@@ -109,6 +118,15 @@ export const triangleDemo: DemoType = {type: "numeric",optional: "default-disabled",},+ {+ name: "cornerRadius",+ default: 0,+ max: 100,+ min: 0,+ step: 1,+ type: "numeric",+ optional: "no",+ },{name: "direction",type: "enum",diff --git a/packages/shapes/src/components/rect.tsx b/packages/shapes/src/components/rect.tsxindex 3b62badd20..60bf9b805b 100644--- a/packages/shapes/src/components/rect.tsx+++ b/packages/shapes/src/components/rect.tsx@@ -10,7 +10,13 @@ export const Rect: React.FC<RectProps> = ({width,edgeRoundness,height,+ cornerRadius,...props}) => {- return <RenderSvg {...makeRect({height, width, edgeRoundness})} {...props} />;+ return (+ <RenderSvg+ {...makeRect({height, width, edgeRoundness, cornerRadius})}+ {...props}+ />+ );};diff --git a/packages/shapes/src/components/triangle.tsx b/packages/shapes/src/components/triangle.tsxindex f7787450ae..f26c8b8a81 100644--- a/packages/shapes/src/components/triangle.tsx+++ b/packages/shapes/src/components/triangle.tsx@@ -10,11 +10,12 @@ export const Triangle: React.FC<TriangleProps> = ({length,direction,edgeRoundness,+ cornerRadius,...props}) => {return (<RenderSvg- {...makeTriangle({length, direction, edgeRoundness})}+ {...makeTriangle({length, direction, edgeRoundness, cornerRadius})}{...props}/>);diff --git a/packages/shapes/src/utils/join-points.ts b/packages/shapes/src/utils/join-points.tsindex be3552d0e6..6d1db51fc5 100644--- a/packages/shapes/src/utils/join-points.ts+++ b/packages/shapes/src/utils/join-points.ts@@ -1,65 +1,104 @@import type {Instruction} from './instructions';+const shortenVector = (vector: readonly [number, number], radius: number) => {+ const [x, y] = vector;+ const currentLength = Math.sqrt(x * x + y * y);+ const scalingFactor = (currentLength - radius) / currentLength;+ return [x * scalingFactor, y * scalingFactor] as const;+};++const scaleVectorToLength = (+ vector: readonly [number, number],+ length: number+) => {+ const [x, y] = vector;+ const currentLength = Math.sqrt(x * x + y * y);+ const scalingFactor = length / currentLength;+ return [x * scalingFactor, y * scalingFactor] as const;+};+export const joinPoints = (points: [number, number][],- {edgeRoundness}: {edgeRoundness: number | null}+ {+ edgeRoundness,+ cornerRadius,+ }: {edgeRoundness: number | null; cornerRadius: number}) => {- return points.map(([x, y], i): Instruction => {- const prevPointIndex = i === 0 ? points.length - 2 : i - 1;- const prevPoint = points[prevPointIndex];- const nextPointIndex = i === points.length - 1 ? 1 : i + 1;- const nextPoint = points[nextPointIndex];- const middleOfLine = [(x + nextPoint[0]) / 2, (y + nextPoint[1]) / 2];- const prevPointMiddleOfLine = [- (x + prevPoint[0]) / 2,- (y + prevPoint[1]) / 2,- ];+ return points+ .map(([x, y], i): Instruction | Instruction[] => {+ const prevPointIndex = i === 0 ? points.length - 2 : i - 1;+ const prevPoint = points[prevPointIndex];+ const nextPointIndex = i === points.length - 1 ? 1 : i + 1;+ const nextPoint = points[nextPointIndex];+ const middleOfLine = [(x + nextPoint[0]) / 2, (y + nextPoint[1]) / 2];+ const prevPointMiddleOfLine = [+ (x + prevPoint[0]) / 2,+ (y + prevPoint[1]) / 2,+ ];++ if (i === 0) {+ if (edgeRoundness !== null) {+ return {+ type: 'M',+ x: middleOfLine[0],+ y: middleOfLine[1],+ };+ }- if (i === 0) {- if (edgeRoundness !== null) {return {type: 'M',- x: middleOfLine[0],- y: middleOfLine[1],+ x,+ y,};}- return {- type: 'M',- x,- y,- };- }+ const prevVector = [x - prevPoint[0], y - prevPoint[1]] as const;+ const nextVector = [nextPoint[0] - x, nextPoint[1] - y] as const;- if (edgeRoundness === null) {- return {- type: 'L',- x,- y,- };- }+ if (edgeRoundness === null) {+ const prevVectorMinusRadius = shortenVector(prevVector, cornerRadius);+ const nextVectorMinusRadius = scaleVectorToLength(+ nextVector,+ cornerRadius+ );- const prevVector = [x - prevPoint[0], y - prevPoint[1]] as const;- const nextVector = [nextPoint[0] - x, nextPoint[1] - y] as const;+ return [+ {+ type: 'L',+ x: prevPoint[0] + prevVectorMinusRadius[0],+ y: prevPoint[1] + prevVectorMinusRadius[1],+ },+ {+ type: 'C',+ cp1x: x,+ cp1y: y,+ cp2x: x,+ cp2y: y,+ x: x + nextVectorMinusRadius[0],+ y: y + nextVectorMinusRadius[1],+ },+ ];+ }- const controlPoint1 = [- prevPointMiddleOfLine[0] + prevVector[0] * edgeRoundness * 0.5,- prevPointMiddleOfLine[1] + prevVector[1] * edgeRoundness * 0.5,- ] as const;+ const controlPoint1 = [+ prevPointMiddleOfLine[0] + prevVector[0] * edgeRoundness * 0.5,+ prevPointMiddleOfLine[1] + prevVector[1] * edgeRoundness * 0.5,+ ] as const;- const controlPoint2 = [- middleOfLine[0] - nextVector[0] * edgeRoundness * 0.5,- middleOfLine[1] - nextVector[1] * edgeRoundness * 0.5,- ] as const;+ const controlPoint2 = [+ middleOfLine[0] - nextVector[0] * edgeRoundness * 0.5,+ middleOfLine[1] - nextVector[1] * edgeRoundness * 0.5,+ ] as const;- return {- type: 'C',- cp1x: controlPoint1[0],- cp1y: controlPoint1[1],- cp2x: controlPoint2[0],- cp2y: controlPoint2[1],- x: middleOfLine[0],- y: middleOfLine[1],- };- });+ return {+ type: 'C',+ cp1x: controlPoint1[0],+ cp1y: controlPoint1[1],+ cp2x: controlPoint2[0],+ cp2y: controlPoint2[1],+ x: middleOfLine[0],+ y: middleOfLine[1],+ };+ })+ .flat(1);};diff --git a/packages/shapes/src/utils/make-rect.ts b/packages/shapes/src/utils/make-rect.tsindex f1274dcf5f..84cf5d7d9f 100644--- a/packages/shapes/src/utils/make-rect.ts+++ b/packages/shapes/src/utils/make-rect.ts@@ -6,12 +6,14 @@ export type MakeRectOptions = {width: number;height: number;edgeRoundness?: number | null;+ cornerRadius?: number;};export const makeRect = ({width,height,edgeRoundness = null,+ cornerRadius = 0,}: MakeRectOptions): ShapeInfo => {const transformOrigin: [number, number] = [width / 2, height / 2];const instructions = joinPoints(@@ -22,7 +24,7 @@ export const makeRect = ({[0, height],[0, 0],],- {edgeRoundness}+ {edgeRoundness, cornerRadius});const path = serializeInstructions(instructions);diff --git a/packages/shapes/src/utils/make-triangle.ts b/packages/shapes/src/utils/make-triangle.tsindex 457312a3ae..b59cc54bbd 100644--- a/packages/shapes/src/utils/make-triangle.ts+++ b/packages/shapes/src/utils/make-triangle.ts@@ -10,12 +10,14 @@ export type MakeTriangleProps = {length: number;direction: Direction;edgeRoundness?: number | null;+ cornerRadius?: number;};export const makeTriangle = ({length,direction = 'right',edgeRoundness = null,+ cornerRadius = 0,}: MakeTriangleProps): ShapeInfo => {const longerDimension = length;const shorterSize = Math.sqrt(length ** 2 * 0.75); // Calculated on paper;@@ -61,7 +63,10 @@ export const makeTriangle = ({right: longerDimension / 2,}[direction];- const instructions = joinPoints(points[direction], {edgeRoundness});+ const instructions = joinPoints(points[direction], {+ edgeRoundness,+ cornerRadius,+ });const path = serializeInstructions(instructions);return {