Skip to content

🎥 Make videos programmatically with React

triangle same length

Jonny Burgera97f9d515438
diff --git a/packages/example/src/Shapes/TriangleTest.tsx b/packages/example/src/Shapes/TriangleTest.tsx
index 5e7c6faece..12c79a091a 100644
--- a/packages/example/src/Shapes/TriangleTest.tsx
+++ b/packages/example/src/Shapes/TriangleTest.tsx
@@ -17,8 +17,7 @@ const TriangleTest: React.FC = () => {
style={{
margin: '10px',
}}
- width={100}
- height={100}
+ length={100}
fill="red"
direction="left"
/>
@@ -27,8 +26,7 @@ const TriangleTest: React.FC = () => {
style={{
margin: '10px',
}}
- width={100}
- height={100}
+ length={100}
fill="red"
direction="right"
/>
@@ -36,8 +34,7 @@ const TriangleTest: React.FC = () => {
style={{
margin: '10px',
}}
- width={100}
- height={100}
+ length={100}
fill="red"
direction="top"
/>
@@ -45,8 +42,7 @@ const TriangleTest: React.FC = () => {
style={{
margin: '10px',
}}
- width={100}
- height={100}
+ length={100}
fill="red"
direction="bottom"
/>
diff --git a/packages/shapes/src/components/triangle.tsx b/packages/shapes/src/components/triangle.tsx
index 1bc3a1de3d..fb4da1aea4 100644
--- a/packages/shapes/src/components/triangle.tsx
+++ b/packages/shapes/src/components/triangle.tsx
@@ -8,18 +8,16 @@ export type TriangleProps = MakeTriangleProps & AllShapesProps;
export const Triangle: React.FC<TriangleProps> = ({
// TODO: Do not use width and height, but use length of a side
- width,
- height,
+ length,
direction,
...props
}) => {
return (
<RenderSvg
- height={height}
- width={width}
+ height={100}
+ width={100}
path={makeTriangle({
- width,
- height,
+ length,
direction,
})}
{...props}
diff --git a/packages/shapes/src/test/make-triangle.test.ts b/packages/shapes/src/test/make-triangle.test.ts
index ccd60c8d03..9f993fdade 100644
--- a/packages/shapes/src/test/make-triangle.test.ts
+++ b/packages/shapes/src/test/make-triangle.test.ts
@@ -3,8 +3,7 @@ import {makeTriangle} from '../utils/make-triangle';
test('Should be able to make a triangle path', () => {
const rightTriangle = makeTriangle({
- width: 100,
- height: 100,
+ length: 100,
direction: 'right',
});
expect(rightTriangle).toEqual('M 0 0 L 0 100 L 100 50 z');
diff --git a/packages/shapes/src/utils/make-triangle.ts b/packages/shapes/src/utils/make-triangle.ts
index 2012ff733c..b1ed172d83 100644
--- a/packages/shapes/src/utils/make-triangle.ts
+++ b/packages/shapes/src/utils/make-triangle.ts
@@ -1,21 +1,46 @@
// Copied from https://stackblitz.com/edit/react-triangle-svg?file=index.js
export type MakeTriangleProps = {
- width: number;
- height: number;
+ length: number;
direction: 'right' | 'left' | 'top' | 'bottom';
};
export const makeTriangle = ({
- width,
- height,
+ length,
direction = 'right',
}: MakeTriangleProps) => {
+ const longerDimension = length;
+ const shorterSize = Math.sqrt(length ** 2 * 0.75); // Calculated on paper;
+
const points = {
- top: [`${width / 2} 0`, 'L', `0 ${height}`, 'L', `${width} ${height}`],
- right: [`0 0`, 'L', `0 ${height}`, 'L', `${width} ${height / 2}`],
- bottom: [`0 0`, 'L', `${width} 0`, 'L', `${width / 2} ${height}`],
- left: [`${width} 0`, 'L', `${width} ${height}`, 'L', `0 ${height / 2}`],
+ top: [
+ `${longerDimension / 2} 0`,
+ 'L',
+ `0 ${shorterSize}`,
+ 'L',
+ `${longerDimension} ${shorterSize}`,
+ ],
+ right: [
+ `0 0`,
+ 'L',
+ `0 ${longerDimension}`,
+ 'L',
+ `${shorterSize} ${longerDimension / 2}`,
+ ],
+ bottom: [
+ `0 0`,
+ 'L',
+ `${longerDimension} 0`,
+ 'L',
+ `${longerDimension / 2} ${shorterSize}`,
+ ],
+ left: [
+ `${shorterSize} 0`,
+ 'L',
+ `${shorterSize} ${longerDimension}`,
+ 'L',
+ `0 ${longerDimension / 2}`,
+ ],
};
return `M ${points[direction].join(' ')} z`;
};