🎥 Make videos programmatically with React
reduced analysis
Jonny Burgerf48ff296c793
diff --git a/packages/paths/src/cut-path.ts b/packages/paths/src/cut-path.tsnew file mode 100644index 0000000000..bde5892321--- /dev/null+++ b/packages/paths/src/cut-path.ts@@ -0,0 +1,8 @@+import {constructFromInstructions} from './helpers/construct';+import {parsePath} from './parse-path';++export const cutPath = (d: string, length: number) => {+ const parsed = parsePath(d);++ const constructed = constructFromInstructions(parsed);+};diff --git a/packages/paths/src/helpers/construct.ts b/packages/paths/src/helpers/construct.tsindex a2dfc6bcdd..f668f51c28 100644--- a/packages/paths/src/helpers/construct.ts+++ b/packages/paths/src/helpers/construct.ts@@ -4,7 +4,7 @@ import {parsePath} from '../parse-path';import {makeArc} from './arc';import {makeCubic, makeQuadratic} from './bezier';import {makeLinearPosition} from './linear';-import type {Instruction, Point, PointArray, Properties} from './types';+import type {Instruction, Point, Properties} from './types';export type Constructed = {segments: Instruction[][];@@ -22,13 +22,13 @@ export const constructFromInstructions = (const functions: (null | Properties)[] = [];let initialPoint: null | Point = null;- let cur: PointArray = [0, 0];- let prev_point: PointArray = [0, 0];+ let cur: Point = [0, 0];+ let prev_point: Point = [0, 0];let curve:| ReturnType<typeof makeCubic>| ReturnType<typeof makeQuadratic>| undefined;- let ringStart: PointArray = [0, 0];+ let ringStart: Point = [0, 0];const segments: Instruction[][] = [];diff --git a/packages/paths/src/helpers/reduced-analysis.ts b/packages/paths/src/helpers/reduced-analysis.tsnew file mode 100644index 0000000000..b9bab2b662--- /dev/null+++ b/packages/paths/src/helpers/reduced-analysis.ts@@ -0,0 +1,111 @@+import {makeCubic} from './bezier';+import {makeLinearPosition} from './linear';+import type {Point, Properties, ReducedInstruction} from './types';++type SegmentInstruction = {+ function: Properties | null;+ length: number;+ instruction: ReducedInstruction;+};++type Segment = {+ startPoint: Point;+ instructions: SegmentInstruction[];+};++export const constructFromInstructions = (+ instructions: ReducedInstruction[],+): Segment[] => {+ let currentPoint: Point = {x: 0, y: 0};+ let moveStart: Point = {x: 0, y: 0};++ const segments: Segment[] = [];++ for (let i = 0; i < instructions.length; i++) {+ const instruction = instructions[i];++ // moveTo+ if (instruction.type === 'M') {+ currentPoint = {x: instruction.x, y: instruction.y};+ moveStart = {x: currentPoint.x, y: currentPoint.y};+ segments.push({+ startPoint: {x: instruction.x, y: instruction.y},+ instructions: [+ {+ instruction,+ function: null,+ length: 0,+ },+ ],+ });+ }++ if (instruction.type === 'L') {+ if (segments.length > 0) {+ const length = Math.sqrt(+ (currentPoint.x - instruction.x) ** 2 ++ (currentPoint.y - instruction.y) ** 2,+ );+ segments[segments.length - 1].instructions.push({+ instruction,+ length,+ function: makeLinearPosition({+ x0: currentPoint.x,+ x1: instruction.x,+ y0: currentPoint.y,+ y1: instruction.y,+ }),+ });+ }++ currentPoint = {x: instruction.x, y: instruction.y};+ }++ if (instruction.type === 'Z') {+ if (segments.length > 0) {+ const length = Math.sqrt(+ (segments[segments.length - 1].startPoint.x - currentPoint.x) ** 2 ++ (segments[segments.length - 1].startPoint.y - currentPoint.y) ** 2,+ );+ segments[segments.length - 1].instructions.push({+ instruction,+ function: makeLinearPosition({+ x0: currentPoint.x,+ x1: moveStart.x,+ y0: currentPoint.y,+ y1: moveStart.y,+ }),+ length,+ });+ }++ currentPoint = {x: moveStart.x, y: moveStart.y};+ }++ if (instruction.type === 'C') {+ const curve = makeCubic({+ startX: currentPoint.x,+ startY: currentPoint.y,+ cp1x: instruction.cp1x,+ cp1y: instruction.cp1y,+ cp2x: instruction.cp2x,+ cp2y: instruction.cp2y,+ x: instruction.x,+ y: instruction.y,+ });++ const length = curve.getTotalLength();+ if (segments.length > 0) {+ segments[segments.length - 1].instructions.push({+ instruction,+ length,+ function: curve,+ });+ }++ currentPoint = {x: instruction.x, y: instruction.y};+ }+ }++ return segments;+};diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.tsindex dc4c84dedc..c3cffc2cd4 100644--- a/packages/paths/src/helpers/types.ts+++ b/packages/paths/src/helpers/types.ts@@ -18,7 +18,7 @@ export interface Point {x: number;y: number;}-export type PointArray = [number, number];+export type Point = [number, number];export type BoundingBox = {x1: number;