🎥 Make videos programmatically with React
get parts at length
Jonny Burger750ba98f49cd
diff --git a/packages/paths/src/get-length.ts b/packages/paths/src/get-length.tsindex 4294f2cb97..e7f12b2751 100644--- a/packages/paths/src/get-length.ts+++ b/packages/paths/src/get-length.ts@@ -1,108 +1,8 @@// Copied from: https://github.com/rveciana/svg-path-propertiesimport {construct} from './helpers/construct';-import type {PartProperties} from './helpers/types';export const getLength = (p: string) => {const constructucted = construct(p);return constructucted.length;};--export const getPartAtLength = (p: string, fractionLength: number) => {- const constructed = construct(p);- if (fractionLength < 0) {- fractionLength = 0;- } else if (fractionLength > constructed.length) {- fractionLength = constructed.length;- }-- let i = constructed.partial_lengths.length - 1;-- while (constructed.partial_lengths[i] >= fractionLength && i > 0) {- i--;- }-- i++;- return {fraction: fractionLength - constructed.partial_lengths[i - 1], i};-};--export const getPointAtLength = (p: string, fractionLength: number) => {- const constructed = construct(p);- const fractionPart = getPartAtLength(p, fractionLength);- const functionAtPart = constructed.functions[fractionPart.i];-- if (functionAtPart) {- return functionAtPart.getPointAtLength(fractionPart.fraction);- }-- if (constructed.initial_point) {- return constructed.initial_point;- }-- throw new Error('Wrong function at this part.');-};--export const getTangentAtLength = (p: string, fractionLength: number) => {- const constructed = construct(p);-- const fractionPart = getPartAtLength(p, fractionLength);- const functionAtPart = constructed.functions[fractionPart.i];- if (functionAtPart) {- return functionAtPart.getTangentAtLength(fractionPart.fraction);- }-- if (constructed.initial_point) {- return {x: 0, y: 0};- }-- throw new Error('Wrong function at this part.');-};--export const getPropertiesAtLength = (p: string, fractionLength: number) => {- const constructed = construct(p);-- const fractionPart = getPartAtLength(p, fractionLength);- const functionAtPart = constructed.functions[fractionPart.i];- if (functionAtPart) {- return functionAtPart.getPropertiesAtLength(fractionPart.fraction);- }-- if (constructed.initial_point) {- return {- x: constructed.initial_point.x,- y: constructed.initial_point.y,- tangentX: 0,- tangentY: 0,- };- }-- throw new Error('Wrong function at this part.');-};--export const getParts = (p: string) => {- const parts = [];- const constructed = construct(p);-- let i = 0;- for (const fn of constructed.functions) {- if (!fn) {- continue;- }-- const properties: PartProperties = {- start: fn.getPointAtLength(0),- end: fn.getPointAtLength(- constructed.partial_lengths[i] - constructed.partial_lengths[i - 1]- ),- length:- constructed.partial_lengths[i] - constructed.partial_lengths[i - 1],- getPointAtLength: fn.getPointAtLength,- getTangentAtLength: fn.getTangentAtLength,- getPropertiesAtLength: fn.getPropertiesAtLength,- };- i++;- parts.push(properties);- }-- return parts;-};diff --git a/packages/paths/src/get-part-at-length.ts b/packages/paths/src/get-part-at-length.tsnew file mode 100644index 0000000000..79048c179f--- /dev/null+++ b/packages/paths/src/get-part-at-length.ts@@ -0,0 +1,19 @@+import {construct} from './helpers/construct';++export const getPartAtLength = (p: string, fractionLength: number) => {+ const constructed = construct(p);+ if (fractionLength < 0) {+ fractionLength = 0;+ } else if (fractionLength > constructed.length) {+ fractionLength = constructed.length;+ }++ let i = constructed.partial_lengths.length - 1;++ while (constructed.partial_lengths[i] >= fractionLength && i > 0) {+ i--;+ }++ i++;+ return {fraction: fractionLength - constructed.partial_lengths[i - 1], i};+};diff --git a/packages/paths/src/get-parts.ts b/packages/paths/src/get-parts.tsnew file mode 100644index 0000000000..3bac5bc1ac--- /dev/null+++ b/packages/paths/src/get-parts.ts@@ -0,0 +1,30 @@+import {construct} from './helpers/construct';+import type {PartProperties} from './helpers/types';++export const getParts = (p: string) => {+ const parts = [];+ const constructed = construct(p);++ let i = 0;+ for (const fn of constructed.functions) {+ if (!fn) {+ continue;+ }++ const properties: PartProperties = {+ start: fn.getPointAtLength(0),+ end: fn.getPointAtLength(+ constructed.partial_lengths[i] - constructed.partial_lengths[i - 1]+ ),+ length:+ constructed.partial_lengths[i] - constructed.partial_lengths[i - 1],+ getPointAtLength: fn.getPointAtLength,+ getTangentAtLength: fn.getTangentAtLength,+ getPropertiesAtLength: fn.getPropertiesAtLength,+ };+ i++;+ parts.push(properties);+ }++ return parts;+};diff --git a/packages/paths/src/get-point-at-length.ts b/packages/paths/src/get-point-at-length.tsnew file mode 100644index 0000000000..b36ba43935--- /dev/null+++ b/packages/paths/src/get-point-at-length.ts@@ -0,0 +1,18 @@+import {getPartAtLength} from './get-part-at-length';+import {construct} from './helpers/construct';++export const getPointAtLength = (p: string, fractionLength: number) => {+ const constructed = construct(p);+ const fractionPart = getPartAtLength(p, fractionLength);+ const functionAtPart = constructed.functions[fractionPart.i];++ if (functionAtPart) {+ return functionAtPart.getPointAtLength(fractionPart.fraction);+ }++ if (constructed.initial_point) {+ return constructed.initial_point;+ }++ throw new Error('Wrong function at this part.');+};diff --git a/packages/paths/src/get-properties-at-length.ts b/packages/paths/src/get-properties-at-length.tsnew file mode 100644index 0000000000..e35cb2e035--- /dev/null+++ b/packages/paths/src/get-properties-at-length.ts@@ -0,0 +1,23 @@+import {getPartAtLength} from './get-length';+import {construct} from './helpers/construct';++export const getPropertiesAtLength = (p: string, fractionLength: number) => {+ const constructed = construct(p);++ const fractionPart = getPartAtLength(p, fractionLength);+ const functionAtPart = constructed.functions[fractionPart.i];+ if (functionAtPart) {+ return functionAtPart.getPropertiesAtLength(fractionPart.fraction);+ }++ if (constructed.initial_point) {+ return {+ x: constructed.initial_point.x,+ y: constructed.initial_point.y,+ tangentX: 0,+ tangentY: 0,+ };+ }++ throw new Error('Wrong function at this part.');+};diff --git a/packages/paths/src/get-tangent-at-length.ts b/packages/paths/src/get-tangent-at-length.tsnew file mode 100644index 0000000000..ca39ae2020--- /dev/null+++ b/packages/paths/src/get-tangent-at-length.ts@@ -0,0 +1,17 @@+import {construct} from './helpers/construct';++export const getTangentAtLength = (p: string, fractionLength: number) => {+ const constructed = construct(p);++ const fractionPart = getPartAtLength(p, fractionLength);+ const functionAtPart = constructed.functions[fractionPart.i];+ if (functionAtPart) {+ return functionAtPart.getTangentAtLength(fractionPart.fraction);+ }++ if (constructed.initial_point) {+ return {x: 0, y: 0};+ }++ throw new Error('Wrong function at this part.');+};diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.tsindex d40d2b6a91..c90f58a990 100644--- a/packages/paths/src/index.ts+++ b/packages/paths/src/index.ts@@ -1 +1,6 @@export {getLength} from './get-length';+export {getPartAtLength} from './get-part-at-length';+export {getParts} from './get-parts';+export {getPointAtLength} from './get-point-at-length';+export {getPropertiesAtLength} from './get-properties-at-length';+export {getTangentAtLength} from './get-tangent-at-length';