Skip to content

🎥 Make videos programmatically with React

refactors

Jonny Burgere84dedb5c6dd
diff --git a/packages/paths/src/get-point-at-length.ts b/packages/paths/src/get-point-at-length.ts
index adda7f4f3b..a3eaf3ff27 100644
--- a/packages/paths/src/get-point-at-length.ts
+++ b/packages/paths/src/get-point-at-length.ts
@@ -1,5 +1,5 @@
import {construct} from './helpers/construct';
-import {getSegmentAtLength} from './helpers/get-part-at-length';
+import {getInstructionIndexAtLength} from './helpers/get-part-at-length';
/**
* @description Gets the coordinates of a point which is on an SVG path.
@@ -9,7 +9,7 @@ import {getSegmentAtLength} from './helpers/get-part-at-length';
*/
export const getPointAtLength = (path: string, length: number) => {
const constructed = construct(path);
- const fractionPart = getSegmentAtLength(constructed, length);
+ const fractionPart = getInstructionIndexAtLength(constructed, length);
const functionAtPart = constructed.functions[fractionPart.index];
if (functionAtPart) {
diff --git a/packages/paths/src/get-tangent-at-length.ts b/packages/paths/src/get-tangent-at-length.ts
index 4e4dfa0d74..fc42993ef3 100644
--- a/packages/paths/src/get-tangent-at-length.ts
+++ b/packages/paths/src/get-tangent-at-length.ts
@@ -1,5 +1,5 @@
import {construct} from './helpers/construct';
-import {getSegmentAtLength} from './helpers/get-part-at-length';
+import {getInstructionIndexAtLength} from './helpers/get-part-at-length';
/**
* @description Gets tangent values x and y of a point which is on an SVG path
@@ -11,7 +11,7 @@ import {getSegmentAtLength} from './helpers/get-part-at-length';
export const getTangentAtLength = (path: string, length: number) => {
const constructed = construct(path);
- const fractionPart = getSegmentAtLength(constructed, length);
+ const fractionPart = getInstructionIndexAtLength(constructed, length);
const functionAtPart = constructed.functions[fractionPart.index];
if (functionAtPart) {
return functionAtPart.getTangentAtLength(fractionPart.fraction);
diff --git a/packages/paths/src/helpers/arc.ts b/packages/paths/src/helpers/arc.ts
index c2b5f827eb..39016086a1 100644
--- a/packages/paths/src/helpers/arc.ts
+++ b/packages/paths/src/helpers/arc.ts
@@ -244,10 +244,6 @@ export const makeArc = ({
});
const length = lengthProperties.arcLength;
- const getTotalLength = () => {
- return length;
- };
-
const getPointAtLength = (fractionLength: number): Point => {
if (fractionLength < 0) {
fractionLength = 0;
@@ -269,44 +265,44 @@ export const makeArc = ({
return {x: position.x, y: position.y};
};
- const getTangentAtLength = (fractionLength: number): Point => {
- if (fractionLength < 0) {
- fractionLength = 0;
- } else if (fractionLength > length) {
- fractionLength = length;
- }
-
- const point_dist = 0.05; // needs testing
- const p1 = getPointAtLength(fractionLength);
- let p2: Point;
-
- if (fractionLength < 0) {
- fractionLength = 0;
- } else if (fractionLength > length) {
- fractionLength = length;
- }
-
- if (fractionLength < length - point_dist) {
- p2 = getPointAtLength(fractionLength + point_dist);
- } else {
- p2 = getPointAtLength(fractionLength - point_dist);
- }
-
- const xDist = p2.x - p1.x;
- const yDist = p2.y - p1.y;
- const dist = Math.sqrt(xDist * xDist + yDist * yDist);
-
- if (fractionLength < length - point_dist) {
- return {x: -xDist / dist, y: -yDist / dist};
- }
-
- return {x: xDist / dist, y: yDist / dist};
- };
-
return {
getPointAtLength,
- getTangentAtLength,
- getTotalLength,
+ getTangentAtLength: (fractionLength: number): Point => {
+ if (fractionLength < 0) {
+ fractionLength = 0;
+ } else if (fractionLength > length) {
+ fractionLength = length;
+ }
+
+ const point_dist = 0.05; // needs testing
+ const p1 = getPointAtLength(fractionLength);
+ let p2: Point;
+
+ if (fractionLength < 0) {
+ fractionLength = 0;
+ } else if (fractionLength > length) {
+ fractionLength = length;
+ }
+
+ if (fractionLength < length - point_dist) {
+ p2 = getPointAtLength(fractionLength + point_dist);
+ } else {
+ p2 = getPointAtLength(fractionLength - point_dist);
+ }
+
+ const xDist = p2.x - p1.x;
+ const yDist = p2.y - p1.y;
+ const dist = Math.sqrt(xDist * xDist + yDist * yDist);
+
+ if (fractionLength < length - point_dist) {
+ return {x: -xDist / dist, y: -yDist / dist};
+ }
+
+ return {x: xDist / dist, y: yDist / dist};
+ },
+ getTotalLength: () => {
+ return length;
+ },
type: 'arc',
};
};
diff --git a/packages/paths/src/helpers/get-part-at-length.ts b/packages/paths/src/helpers/get-part-at-length.ts
index f5ef08dd82..196e46965b 100644
--- a/packages/paths/src/helpers/get-part-at-length.ts
+++ b/packages/paths/src/helpers/get-part-at-length.ts
@@ -5,7 +5,7 @@ type SegmentAtLength = {
fraction: number;
};
-export const getSegmentAtLength = (
+export const getInstructionIndexAtLength = (
constructed: Constructed,
fractionLength: number,
): SegmentAtLength => {
diff --git a/packages/paths/src/helpers/linear.ts b/packages/paths/src/helpers/linear.ts
index 12fa563258..7f07977d64 100644
--- a/packages/paths/src/helpers/linear.ts
+++ b/packages/paths/src/helpers/linear.ts
@@ -12,29 +12,23 @@ export const makeLinearPosition = ({
y0: number;
y1: number;
}): Properties => {
- const getTotalLength = () => {
- return Math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2);
- };
-
- const getPointAtLength = (pos: number): Point => {
- let fraction = pos / Math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2);
-
- fraction = Number.isNaN(fraction) ? 1 : fraction;
- const newDeltaX = (x1 - x0) * fraction;
- const newDeltaY = (y1 - y0) * fraction;
-
- return {x: x0 + newDeltaX, y: y0 + newDeltaY};
- };
+ return {
+ getTotalLength: () => {
+ return Math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2);
+ },
+ getPointAtLength: (pos: number): Point => {
+ let fraction = pos / Math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2);
- const getTangentAtLength = (): Point => {
- const module = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
- return {x: (x1 - x0) / module, y: (y1 - y0) / module};
- };
+ fraction = Number.isNaN(fraction) ? 1 : fraction;
+ const newDeltaX = (x1 - x0) * fraction;
+ const newDeltaY = (y1 - y0) * fraction;
- return {
- getTotalLength,
- getPointAtLength,
- getTangentAtLength,
+ return {x: x0 + newDeltaX, y: y0 + newDeltaY};
+ },
+ getTangentAtLength: (): Point => {
+ const module = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
+ return {x: (x1 - x0) / module, y: (y1 - y0) / module};
+ },
type: 'linear',
};
};
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.ts
index 9cc5ff8049..a7dd3e538f 100644
--- a/packages/paths/src/helpers/types.ts
+++ b/packages/paths/src/helpers/types.ts
@@ -4,7 +4,7 @@ export interface Properties {
getTotalLength(): number;
getPointAtLength(pos: number): Point;
getTangentAtLength(pos: number): Point;
- type: string;
+ type: 'linear' | 'cubic-bezier' | 'quadratic-bezier' | 'arc';
}
export interface Part {