🎥 Make videos programmatically with React
interesting
Jonny Burger4e21d831fa02
diff --git a/packages/paths/src/cut-path.ts b/packages/paths/src/cut-path.tsindex bde5892321..d8d649d4b9 100644--- a/packages/paths/src/cut-path.ts+++ b/packages/paths/src/cut-path.ts@@ -1,8 +1,37 @@-import {constructFromInstructions} from './helpers/construct';+import {cutInstruction} from './cut-instruction';+import {conductAnalysis} from './helpers/reduced-analysis';+import type {ReducedInstruction} from './helpers/types';import {parsePath} from './parse-path';+import {reduceInstructions} from './reduce-instructions';export const cutPath = (d: string, length: number) => {const parsed = parsePath(d);+ const reduced = reduceInstructions(parsed);+ const constructed = conductAnalysis(reduced);- const constructed = constructFromInstructions(parsed);+ const newInstructions: ReducedInstruction[] = [];++ let summedUpLength = 0;++ for (const segment of constructed) {+ for (const instructionAndInfo of segment.instructionsAndInfo) {+ if (summedUpLength + instructionAndInfo.length > length) {+ const remainingLength = length - summedUpLength;+ const progress = remainingLength / instructionAndInfo.length;+ // cut+ const cut = cutInstruction({+ instruction: instructionAndInfo.instruction,+ lastPoint: instructionAndInfo.startPoint,+ progress,+ });+ newInstructions.push(cut);+ return newInstructions;+ }++ summedUpLength += instructionAndInfo.length;+ newInstructions.push(instructionAndInfo.instruction);+ }+ }++ return newInstructions;};diff --git a/packages/paths/src/helpers/construct.ts b/packages/paths/src/helpers/construct.tsindex f668f51c28..a2dfc6bcdd 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, Properties} from './types';+import type {Instruction, Point, PointArray, 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: Point = [0, 0];- let prev_point: Point = [0, 0];+ let cur: PointArray = [0, 0];+ let prev_point: PointArray = [0, 0];let curve:| ReturnType<typeof makeCubic>| ReturnType<typeof makeQuadratic>| undefined;- let ringStart: Point = [0, 0];+ let ringStart: PointArray = [0, 0];const segments: Instruction[][] = [];diff --git a/packages/paths/src/helpers/reduced-analysis.ts b/packages/paths/src/helpers/reduced-analysis.tsindex b9bab2b662..279d95566e 100644--- a/packages/paths/src/helpers/reduced-analysis.ts+++ b/packages/paths/src/helpers/reduced-analysis.ts@@ -6,14 +6,15 @@ type SegmentInstruction = {function: Properties | null;length: number;instruction: ReducedInstruction;+ startPoint: Point;};type Segment = {startPoint: Point;- instructions: SegmentInstruction[];+ instructionsAndInfo: SegmentInstruction[];};-export const constructFromInstructions = (+export const conductAnalysis = (instructions: ReducedInstruction[],): Segment[] => {let currentPoint: Point = {x: 0, y: 0};@@ -30,11 +31,12 @@ export const constructFromInstructions = (moveStart = {x: currentPoint.x, y: currentPoint.y};segments.push({startPoint: {x: instruction.x, y: instruction.y},- instructions: [+ instructionsAndInfo: [{instruction,function: null,length: 0,+ startPoint: currentPoint,},],});@@ -46,7 +48,7 @@ export const constructFromInstructions = ((currentPoint.x - instruction.x) ** 2 +(currentPoint.y - instruction.y) ** 2,);- segments[segments.length - 1].instructions.push({+ segments[segments.length - 1].instructionsAndInfo.push({instruction,length,function: makeLinearPosition({@@ -55,6 +57,7 @@ export const constructFromInstructions = (y0: currentPoint.y,y1: instruction.y,}),+ startPoint: currentPoint,});}@@ -67,7 +70,7 @@ export const constructFromInstructions = ((segments[segments.length - 1].startPoint.x - currentPoint.x) ** 2 +(segments[segments.length - 1].startPoint.y - currentPoint.y) ** 2,);- segments[segments.length - 1].instructions.push({+ segments[segments.length - 1].instructionsAndInfo.push({instruction,function: makeLinearPosition({x0: currentPoint.x,@@ -76,6 +79,7 @@ export const constructFromInstructions = (y1: moveStart.y,}),length,+ startPoint: {...currentPoint},});}@@ -96,10 +100,11 @@ export const constructFromInstructions = (const length = curve.getTotalLength();if (segments.length > 0) {- segments[segments.length - 1].instructions.push({+ segments[segments.length - 1].instructionsAndInfo.push({instruction,length,function: curve,+ startPoint: {...currentPoint},});}diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.tsindex c3cffc2cd4..dc4c84dedc 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 Point = [number, number];+export type PointArray = [number, number];export type BoundingBox = {x1: number;