🎥 Make videos programmatically with React
split up into more
Jonny Burger685e768fe0c3
diff --git a/packages/paths/src/interpolate-path/de-casteljau.ts b/packages/paths/src/interpolate-path/de-casteljau.tsnew file mode 100644index 0000000000..84a991e0c8--- /dev/null+++ b/packages/paths/src/interpolate-path/de-casteljau.ts@@ -0,0 +1,49 @@+/**+ * de Casteljau's algorithm for drawing and splitting bezier curves.+ * Inspired by https://pomax.github.io/bezierinfo/+ *+ * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]+ * The original segment to split.+ * @param {Number} t Where to split the curve (value between [0, 1])+ * @return {Object} An object { left, right } where left is the segment from 0..t and+ * right is the segment from t..1.+ */+export function decasteljau(+ points: number[][],+ t: number,+): {left: number[][]; right: number[][]} {+ const left: number[][] = [];+ const right: number[][] = [];++ function decasteljauRecurse(_points: number[][], _t: number) {+ if (_points.length === 1) {+ left.push(_points[0]);+ right.push(_points[0]);+ } else {+ const newPoints = Array(_points.length - 1);++ for (let i = 0; i < newPoints.length; i++) {+ if (i === 0) {+ left.push(_points[0]);+ }++ if (i === newPoints.length - 1) {+ right.push(_points[i + 1]);+ }++ newPoints[i] = [+ (1 - _t) * _points[i][0] + _t * _points[i + 1][0],+ (1 - _t) * _points[i][1] + _t * _points[i + 1][1],+ ];+ }++ decasteljauRecurse(newPoints, _t);+ }+ }++ if (points.length) {+ decasteljauRecurse(points, t);+ }++ return {left, right: right.reverse()};+}diff --git a/packages/paths/src/interpolate-path/points-to-command.ts b/packages/paths/src/interpolate-path/points-to-command.tsnew file mode 100644index 0000000000..b832dd43d7--- /dev/null+++ b/packages/paths/src/interpolate-path/points-to-command.ts@@ -0,0 +1,40 @@+import type {Command} from './command';++/**+ * 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 pointsToCommand(points: number[][]): Command {+ let x2: number | undefined;+ let y2: number | undefined;+ let x1: number | undefined;+ let y1: number | undefined;++ if (points.length === 4) {+ x2 = points[2][0];+ y2 = points[2][1];+ }++ if (points.length >= 3) {+ x1 = points[1][0];+ y1 = points[1][1];+ }++ const x = points[points.length - 1][0];+ const y = points[points.length - 1][1];++ let type: 'C' | 'Q' | 'L' = 'L';++ if (points.length === 4) {+ // start, control1, control2, end+ type = 'C';+ } else if (points.length === 3) {+ // start, control, end+ type = 'Q';+ }++ return {x2, y2, x1, y1, x, y, type};+}diff --git a/packages/paths/src/interpolate-path/split-curve-as-points.ts b/packages/paths/src/interpolate-path/split-curve-as-points.tsnew file mode 100644index 0000000000..927a0e89f3--- /dev/null+++ b/packages/paths/src/interpolate-path/split-curve-as-points.ts@@ -0,0 +1,47 @@+import {decasteljau} from './de-casteljau';++/**+ * Runs de Casteljau's algorithm enough times to produce the desired number of segments.+ *+ * @param {Number[][]} points Array of [x,y] points for de Casteljau (the initial segment to split)+ * @param {Number} segmentCount Number of segments to split the original into+ * @return {Number[][][]} Array of segments+ */+export function splitCurveAsPoints(+ points: number[][],+ segmentCount: number,+): number[][][] {+ segmentCount = segmentCount || 2;++ const segments = [];+ let remainingCurve = points;+ const tIncrement = 1 / segmentCount;++ // x-----x-----x-----x+ // t= 0.33 0.66 1+ // x-----o-----------x+ // r= 0.33+ // x-----o-----x+ // r= 0.5 (0.33 / (1 - 0.33)) === tIncrement / (1 - (tIncrement * (i - 1))++ // x-----x-----x-----x----x+ // t= 0.25 0.5 0.75 1+ // x-----o----------------x+ // r= 0.25+ // x-----o----------x+ // r= 0.33 (0.25 / (1 - 0.25))+ // x-----o----x+ // r= 0.5 (0.25 / (1 - 0.5))++ for (let i = 0; i < segmentCount - 1; i++) {+ const tRelative = tIncrement / (1 - tIncrement * i);+ const split = decasteljau(remainingCurve, tRelative);+ segments.push(split.left);+ remainingCurve = split.right;+ }++ // last segment is just to the end from the last point+ segments.push(remainingCurve);++ return segments;+}diff --git a/packages/paths/src/interpolate-path/split-curve.ts b/packages/paths/src/interpolate-path/split-curve.tsindex d01c3eeedb..caa01bcf20 100644--- a/packages/paths/src/interpolate-path/split-curve.ts+++ b/packages/paths/src/interpolate-path/split-curve.ts@@ -32,141 +32,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/import type {Command} from './command';--/**- * de Casteljau's algorithm for drawing and splitting bezier curves.- * Inspired by https://pomax.github.io/bezierinfo/- *- * @param {Number[][]} points Array of [x,y] points: [start, control1, control2, ..., end]- * The original segment to split.- * @param {Number} t Where to split the curve (value between [0, 1])- * @return {Object} An object { left, right } where left is the segment from 0..t and- * right is the segment from t..1.- */-function decasteljau(- points: number[][],- t: number,-): {left: number[][]; right: number[][]} {- const left: number[][] = [];- const right: number[][] = [];-- function decasteljauRecurse(_points: number[][], _t: number) {- if (_points.length === 1) {- left.push(_points[0]);- right.push(_points[0]);- } else {- const newPoints = Array(_points.length - 1);-- for (let i = 0; i < newPoints.length; i++) {- if (i === 0) {- left.push(_points[0]);- }-- if (i === newPoints.length - 1) {- right.push(_points[i + 1]);- }-- newPoints[i] = [- (1 - _t) * _points[i][0] + _t * _points[i + 1][0],- (1 - _t) * _points[i][1] + _t * _points[i + 1][1],- ];- }-- decasteljauRecurse(newPoints, _t);- }- }-- if (points.length) {- decasteljauRecurse(points, t);- }-- return {left, right: right.reverse()};-}--/**- * 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.- */-function pointsToCommand(points: number[][]): Command {- let x2: number | undefined;- let y2: number | undefined;- let x1: number | undefined;- let y1: number | undefined;-- if (points.length === 4) {- x2 = points[2][0];- y2 = points[2][1];- }-- if (points.length >= 3) {- x1 = points[1][0];- y1 = points[1][1];- }-- const x = points[points.length - 1][0];- const y = points[points.length - 1][1];-- let type: 'C' | 'Q' | 'L' = 'L';-- if (points.length === 4) {- // start, control1, control2, end- type = 'C';- } else if (points.length === 3) {- // start, control, end- type = 'Q';- }-- return {x2, y2, x1, y1, x, y, type};-}--/**- * Runs de Casteljau's algorithm enough times to produce the desired number of segments.- *- * @param {Number[][]} points Array of [x,y] points for de Casteljau (the initial segment to split)- * @param {Number} segmentCount Number of segments to split the original into- * @return {Number[][][]} Array of segments- */-function splitCurveAsPoints(- points: number[][],- segmentCount: number,-): number[][][] {- segmentCount = segmentCount || 2;-- const segments = [];- let remainingCurve = points;- const tIncrement = 1 / segmentCount;-- // x-----x-----x-----x- // t= 0.33 0.66 1- // x-----o-----------x- // r= 0.33- // x-----o-----x- // r= 0.5 (0.33 / (1 - 0.33)) === tIncrement / (1 - (tIncrement * (i - 1))-- // x-----x-----x-----x----x- // t= 0.25 0.5 0.75 1- // x-----o----------------x- // r= 0.25- // x-----o----------x- // r= 0.33 (0.25 / (1 - 0.25))- // x-----o----x- // r= 0.5 (0.25 / (1 - 0.5))-- for (let i = 0; i < segmentCount - 1; i++) {- const tRelative = tIncrement / (1 - tIncrement * i);- const split = decasteljau(remainingCurve, tRelative);- segments.push(split.left);- remainingCurve = split.right;- }-- // last segment is just to the end from the last point- segments.push(remainingCurve);-- return segments;-}+import {pointsToCommand} from './points-to-command';+import {splitCurveAsPoints} from './split-curve-as-points';/*** Convert command objects to arrays of points, run de Casteljau's algorithm on it@@ -194,5 +61,7 @@ export const splitCurve = (points.push([commandEnd.x as number, commandEnd.y as number]);- return splitCurveAsPoints(points, segmentCount).map(pointsToCommand);+ return splitCurveAsPoints(points, segmentCount).map((p) =>+ pointsToCommand(p),+ );};