Skip to content

🎥 Make videos programmatically with React

refactor

Jonny Burgerfa5f6269ab39
diff --git a/packages/paths/src/get-point-at-length.ts b/packages/paths/src/get-point-at-length.ts
index c2a79d7d73..adda7f4f3b 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 {getPartAtLength} from './helpers/get-part-at-length';
+import {getSegmentAtLength} from './helpers/get-part-at-length';
/**
* @description Gets the coordinates of a point which is on an SVG path.
@@ -9,15 +9,15 @@ import {getPartAtLength} from './helpers/get-part-at-length';
*/
export const getPointAtLength = (path: string, length: number) => {
const constructed = construct(path);
- const fractionPart = getPartAtLength(path, length);
- const functionAtPart = constructed.functions[fractionPart.i];
+ const fractionPart = getSegmentAtLength(constructed, length);
+ const functionAtPart = constructed.functions[fractionPart.index];
if (functionAtPart) {
return functionAtPart.getPointAtLength(fractionPart.fraction);
}
- if (constructed.initial_point) {
- return constructed.initial_point;
+ if (constructed.initialPoint) {
+ return constructed.initialPoint;
}
throw new Error('Wrong function at this part.');
diff --git a/packages/paths/src/get-tangent-at-length.ts b/packages/paths/src/get-tangent-at-length.ts
index cc60d553a8..4e4dfa0d74 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 {getPartAtLength} from './helpers/get-part-at-length';
+import {getSegmentAtLength} from './helpers/get-part-at-length';
/**
* @description Gets tangent values x and y of a point which is on an SVG path
@@ -11,13 +11,13 @@ import {getPartAtLength} from './helpers/get-part-at-length';
export const getTangentAtLength = (path: string, length: number) => {
const constructed = construct(path);
- const fractionPart = getPartAtLength(path, length);
- const functionAtPart = constructed.functions[fractionPart.i];
+ const fractionPart = getSegmentAtLength(constructed, length);
+ const functionAtPart = constructed.functions[fractionPart.index];
if (functionAtPart) {
return functionAtPart.getTangentAtLength(fractionPart.fraction);
}
- if (constructed.initial_point) {
+ if (constructed.initialPoint) {
return {x: 0, y: 0};
}
diff --git a/packages/paths/src/helpers/construct.ts b/packages/paths/src/helpers/construct.ts
index ba851229ea..e247f2889b 100644
--- a/packages/paths/src/helpers/construct.ts
+++ b/packages/paths/src/helpers/construct.ts
@@ -6,11 +6,19 @@ import {makeCubic, makeQuadratic} from './bezier';
import {makeLinearPosition} from './linear';
import type {Instruction, Point, PointArray, Properties} from './types';
+export type Constructed = {
+ segments: Instruction[][];
+ initialPoint: Point | null;
+ length: number;
+ partialLengths: number[];
+ functions: (Properties | null)[];
+};
+
export const constructFromInstructions = (instructions: Instruction[]) => {
let length = 0;
- const partial_lengths: number[] = [];
+ const partialLengths: number[] = [];
const functions: (null | Properties)[] = [];
- let initial_point: null | Point = null;
+ let initialPoint: null | Point = null;
let cur: PointArray = [0, 0];
let prev_point: PointArray = [0, 0];
@@ -40,7 +48,7 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
segments.push([instruction]);
functions.push(null);
if (i === 0) {
- initial_point = {x: instruction.x, y: instruction.y};
+ initialPoint = {x: instruction.x, y: instruction.y};
}
} else if (instruction.type === 'm') {
cur = [instruction.dx + cur[0], instruction.dy + cur[1]];
@@ -393,19 +401,19 @@ export const constructFromInstructions = (instructions: Instruction[]) => {
functions.push(arcCurve);
}
- partial_lengths.push(length);
+ partialLengths.push(length);
}
return {
segments,
- initial_point,
+ initialPoint,
length,
- partial_lengths,
+ partialLengths,
functions,
};
};
-export const construct = (string: string) => {
+export const construct = (string: string): Constructed => {
const parsed = parsePath(string);
return constructFromInstructions(parsed);
};
diff --git a/packages/paths/src/helpers/get-part-at-length.ts b/packages/paths/src/helpers/get-part-at-length.ts
index 45695a47c5..f5ef08dd82 100644
--- a/packages/paths/src/helpers/get-part-at-length.ts
+++ b/packages/paths/src/helpers/get-part-at-length.ts
@@ -1,19 +1,29 @@
-import {construct} from './construct';
+import type {Constructed} from './construct';
-export const getPartAtLength = (p: string, fractionLength: number) => {
- const constructed = construct(p);
+type SegmentAtLength = {
+ index: number;
+ fraction: number;
+};
+
+export const getSegmentAtLength = (
+ constructed: Constructed,
+ fractionLength: number,
+): SegmentAtLength => {
if (fractionLength < 0) {
fractionLength = 0;
} else if (fractionLength > constructed.length) {
fractionLength = constructed.length;
}
- let i = constructed.partial_lengths.length - 1;
+ let index = constructed.partialLengths.length - 1;
- while (constructed.partial_lengths[i] >= fractionLength && i > 0) {
- i--;
+ while (constructed.partialLengths[index] >= fractionLength && index > 0) {
+ index--;
}
- i++;
- return {fraction: fractionLength - constructed.partial_lengths[i - 1], i};
+ index++;
+ return {
+ fraction: fractionLength - constructed.partialLengths[index - 1],
+ index,
+ };
};