🎥 Make videos programmatically with React
`@remotion/paths`: Cut cubic curves at the requested arc length in `cutPath()`
serdarucel8c5f4bb814c3
diff --git a/packages/paths/src/cut-instruction.ts b/packages/paths/src/cut-instruction.tsindex 2e91da8380..bfde8b3d20 100644--- a/packages/paths/src/cut-instruction.ts+++ b/packages/paths/src/cut-instruction.ts@@ -1,3 +1,4 @@+import {getCubicArcLength, t2length} from './helpers/bezier-functions';import type {CInstruction,LInstruction,@@ -40,15 +41,21 @@ export function cutCInstruction({lastPoint: Point;instruction: CInstruction;}): CInstruction {- // De Casteljau's algorithm for Bezier splitting- const u = progress;-// Points of original curveconst p0 = {x: lastPoint.x, y: lastPoint.y};const p1 = {x: instruction.cp1x, y: instruction.cp1y};const p2 = {x: instruction.cp2x, y: instruction.cp2y};const p3 = {x: instruction.x, y: instruction.y};+ const sx = [p0.x, p1.x, p2.x, p3.x];+ const sy = [p0.y, p1.y, p2.y, p3.y];+ const totalLength = getCubicArcLength({sx, sy, t: 1});+ const u = t2length({+ length: progress * totalLength,+ totalLength,+ func: (t) => getCubicArcLength({sx, sy, t}),+ });+ // De Casteljau's algorithm for Bezier splitting// First level of interpolationconst p01 = interpolatePoint(p0, p1, u);const p12 = interpolatePoint(p1, p2, u);diff --git a/packages/paths/src/test/cut-path.test.ts b/packages/paths/src/test/cut-path.test.tsindex b05179f866..e43743f8e7 100644--- a/packages/paths/src/test/cut-path.test.ts+++ b/packages/paths/src/test/cut-path.test.ts@@ -1,5 +1,8 @@import {expect, test} from 'bun:test';import {cutPath} from '../cut-path';+import {getLength} from '../get-length';+import {getPointAtLength} from '../get-point-at-length';+import {parsePath} from '../parse-path';test('Should be able to cut a simple path', () => {const result = cutPath('M 0 0 L 100 0', 50);@@ -23,3 +26,32 @@ test('Should handle complex path with curves', () => {expect(result).toMatch(/^M 0 0 C/);expect(result.length).toBeGreaterThan(0);});++test('Should cut a cubic curve at the requested arc length', () => {+ const path = 'M 0 0 C 0 100 100 100 100 0';+ const result = cutPath(path, 50);+ expect(getLength(result)).toBeCloseTo(50, 0);+});++test('Should end a cut cubic curve at the point at the requested length', () => {+ const path = 'M 0 0 C 0 100 100 100 100 0';+ const result = parsePath(cutPath(path, 50));+ const endpoint = result[result.length - 1];+ const expected = getPointAtLength(path, 50);+ if (endpoint.type !== 'C' || !expected) {+ throw new Error('Expected a cubic endpoint and a point on the path');+ }++ expect(endpoint.x).toBeCloseTo(expected.x, 1);+ expect(endpoint.y).toBeCloseTo(expected.y, 1);+});++test('Should cut a cubic curve with coincident control points by arc length', () => {+ const result = parsePath(cutPath('M 0 0 C 0 0 0 0 100 0', 50));+ const endpoint = result[result.length - 1];+ if (endpoint.type !== 'C') {+ throw new Error('Expected a cubic endpoint');+ }++ expect(endpoint.x).toBeCloseTo(50, 1);+});