🎥 Make videos programmatically with React
iterator command
Jonny Burgercace8d9b9052
diff --git a/packages/paths/src/get-bounding-box.ts b/packages/paths/src/get-bounding-box.tsindex 953979ff14..2e2ff2b662 100644--- a/packages/paths/src/get-bounding-box.ts+++ b/packages/paths/src/get-bounding-box.ts@@ -1,5 +1,3 @@-'use strict';-import type {BoundingBox} from './helpers/types';import {normalizePath} from './normalize-path';import {unarc} from './unarc';diff --git a/packages/paths/src/helpers/iterate.ts b/packages/paths/src/helpers/iterate.tsnew file mode 100644index 0000000000..b66eea2c46--- /dev/null+++ b/packages/paths/src/helpers/iterate.ts@@ -0,0 +1,63 @@+import type {Instruction} from './parse';++export const iterateOverSegments = (+ segments: Instruction[],+ iterate: (options: {+ segment: Instruction;+ x: number;+ y: number;+ }) => Instruction[]+): Instruction[] => {+ let x = 0;+ let y = 0;++ const newSegments = segments.map((s) => {+ const newSeg = iterate({segment: s, x, y});+ switch (s[0]) {+ case 'M':+ case 'L': {+ x = s[1];+ y = s[2];+ break;+ }++ case 'A': {+ x = s[6];+ y = s[7];+ break;+ }++ case 'V': {+ y = s[1];+ break;+ }++ case 'H': {+ x = s[1];+ break;+ }++ case 'C': {+ x = s[5];+ y = s[6];+ break;+ }++ case 'Q': {+ x = s[3];+ y = s[4];+ break;+ }++ case 'Z': {+ break;+ }++ default:+ throw new Error(`Unexpected instruction ${s[0]}`);+ }++ return newSeg;+ });+ return newSegments.flat(1);+};diff --git a/packages/paths/src/unarc.ts b/packages/paths/src/helpers/unarc.tssimilarity index 79%rename from packages/paths/src/unarc.tsrename to packages/paths/src/helpers/unarc.tsindex 638f5f989c..af12082f35 100644--- a/packages/paths/src/unarc.ts+++ b/packages/paths/src/helpers/unarc.ts@@ -1,3 +1,4 @@+import {iterateOverSegments} from './helpers/iterate';import type {Instruction} from './helpers/parse';import {parsePath} from './helpers/parse';@@ -117,88 +118,40 @@ function a2c({});}+// Requires path to be normalizedexport const unarc = (d: string) => {const segments = parsePath(d);- let x = 0;- let y = 0;-- const newSegments = segments.map((s): Instruction[] => {- switch (s[0]) {- case 'M':- case 'L': {- x = s[1];- y = s[2];- return [s];- }-- case 'A': {- const nextX = s[6];- const nextY = s[7];-- const new_segments = a2c({- x1: x,- y1: y,- x2: nextX,- y2: nextY,- fa: s[4],- fs: s[5],- rx: s[1],- ry: s[2],- phi: s[3],- });-- // Degenerated arcs can be ignored by renderer, but should not be dropped- // to avoid collisions with `S A S` and so on. Replace with empty line.- if (new_segments.length === 0) {- return [['L', s[6], s[7]]];- }-- const result: Instruction[] = [];-- new_segments.forEach((_s) => {- result.push(['C', _s[2], _s[3], _s[4], _s[5], _s[6], _s[7]]);- });- return result;- }-- case 'V': {- y = s[1];-- return [s];- }-- case 'H': {- x = s[1];-- return [s];- }-- case 'C': {- x = s[5];- y = s[6];-- return [s];- }-- case 'Q': {- x = s[3];- y = s[4];-- return [s];- }-- case 'Z': {- return [s];- }-- default:- throw new Error(`Unexpected instruction ${s[0]}`);+ const x = 0;+ const y = 0;++ return iterateOverSegments(segments, ({segment}) => {+ const nextX = segment[6];+ const nextY = segment[7];+ const new_segments = a2c({+ x1: x,+ y1: y,+ x2: nextX,+ y2: nextY,+ fa: segment[4],+ fs: segment[5],+ rx: segment[1],+ ry: segment[2],+ phi: segment[3],+ });++ // Degenerated arcs can be ignored by renderer, but should not be dropped+ // to avoid collisions with `S A S` and so on. Replace with empty line.+ if (new_segments.length === 0) {+ return [['L', segment[6], segment[7]]];}- });- const flatted = newSegments.flat(1);+ const result: Instruction[] = [];- return flatted;+ new_segments.forEach((_s) => {+ result.push(['C', _s[2], _s[3], _s[4], _s[5], _s[6], _s[7]]);+ });+ return result;+ });};function get_arc_center({