🎥 Make videos programmatically with React
aha
Jonny Burger0545ad8ca410
diff --git a/packages/paths/src/interpolate-path/points-to-command.ts b/packages/paths/src/interpolate-path/points-to-command.tsindex b832dd43d7..4eb87e08b5 100644--- a/packages/paths/src/interpolate-path/points-to-command.ts+++ b/packages/paths/src/interpolate-path/points-to-command.ts@@ -1,3 +1,4 @@+import type {ReducedInstruction} from '../helpers/types';import type {Command} from './command';/**@@ -38,3 +39,51 @@ export function pointsToCommand(points: number[][]): Command {return {x2, y2, x1, y1, x, y, type};}++/**+ * Convert segments represented as points back into a command object+ *+ * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]+ * Represents a segment+ * @return {Object} A command object representing the segment.+ */+export function pointsToInstruction(points: number[][]): ReducedInstruction {+ const x = points[points.length - 1][0];+ const y = points[points.length - 1][1];++ if (points.length === 4) {+ const x1 = points[1][0];+ const y1 = points[1][1];+ const x2 = points[2][0];+ const y2 = points[2][1];++ return {+ type: 'C',+ cp1x: x1,+ cp1y: y1,+ cp2x: x2,+ cp2y: y2,+ x,+ y,+ };+ }++ if (points.length === 3) {+ const x1 = points[1][0];+ const y1 = points[1][1];++ return {+ type: 'Q',+ cpx: x1,+ cpy: y1,+ x,+ y,+ };+ }++ return {+ type: 'L',+ x,+ y,+ };+}diff --git a/packages/paths/src/interpolate-path/split-curve.ts b/packages/paths/src/interpolate-path/split-curve.tsindex caa01bcf20..833d47c9ec 100644--- a/packages/paths/src/interpolate-path/split-curve.ts+++ b/packages/paths/src/interpolate-path/split-curve.ts@@ -31,8 +31,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/+import type {+ CInstruction,+ LInstruction,+ QInstruction,+ ReducedInstruction,+} from '../helpers/types';import type {Command} from './command';-import {pointsToCommand} from './points-to-command';+import {pointsToCommand, pointsToInstruction} from './points-to-command';import {splitCurveAsPoints} from './split-curve-as-points';/**@@ -65,3 +71,35 @@ export const splitCurve = (pointsToCommand(p),);};++/**+ * Convert command objects to arrays of points, run de Casteljau's algorithm on it+ * to split into to the desired number of segments.+ *+ * @param {Object} commandStart The start command object+ * @param {Object} instructionEnd The end command object+ * @param {Number} segmentCount The number of segments to create+ * @return {Object[]} An array of commands representing the segments in sequence+ */+export const splitCurveInstructions = (+ instructionStartX: number,+ instructionStartY: number,+ instructionEnd: LInstruction | QInstruction | CInstruction,+ segmentCount: number,+): ReducedInstruction[] => {+ const points = [[instructionStartX, instructionStartY]] as number[][];+ if (instructionEnd.type === 'Q') {+ points.push([instructionEnd.cpx, instructionEnd.cpy]);+ }++ if (instructionEnd.type === 'C') {+ points.push([instructionEnd.cp1x, instructionEnd.cp1y]);+ points.push([instructionEnd.cp2x, instructionEnd.cp2y]);+ }++ points.push([instructionEnd.x as number, instructionEnd.y as number]);++ return splitCurveAsPoints(points, segmentCount).map((p) => {+ return pointsToInstruction(p);+ });+};