Skip to content

🎥 Make videos programmatically with React

use default interpolationThreshold

Jonny Burger26a685f97b37
diff --git a/packages/paths/src/get-bounding-box.ts b/packages/paths/src/get-bounding-box.ts
index a90b2b0a6e..e58939bce5 100644
--- a/packages/paths/src/get-bounding-box.ts
+++ b/packages/paths/src/get-bounding-box.ts
@@ -1,6 +1,9 @@
import {removeATSHVInstructions} from './helpers/remove-a-s-t-curves';
-import type {AbsoluteInstruction, BoundingBox} from './helpers/types';
-import {normalizePath} from './normalize-path';
+import type {
+ AbsoluteInstruction,
+ BoundingBox,
+ ReducedInstruction,
+} from './helpers/types';
import {parsePath} from './parse-path';
type minMax = [min: number, max: number];
@@ -85,17 +88,24 @@ function minmaxC(A: [number, number, number, number]): minMax {
}
export const getBoundingBox = (d: string): BoundingBox => {
+ const parsed = parsePath(d) as AbsoluteInstruction[];
+ const unarced = removeATSHVInstructions(parsed);
+
+ return getBoundingBoxFromInstructions(unarced);
+};
+
+export const getBoundingBoxFromInstructions = (
+ instructions: ReducedInstruction[]
+): BoundingBox => {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
- const parsed = parsePath(normalizePath(d)) as AbsoluteInstruction[];
- const unarced = removeATSHVInstructions(parsed);
let x = 0;
let y = 0;
- for (const seg of unarced) {
+ for (const seg of instructions) {
switch (seg.type) {
case 'M':
case 'L': {
diff --git a/packages/paths/src/warp-path/index.ts b/packages/paths/src/warp-path/index.ts
index 46ad68e386..0690994771 100644
--- a/packages/paths/src/warp-path/index.ts
+++ b/packages/paths/src/warp-path/index.ts
@@ -1,3 +1,5 @@
+import {getBoundingBoxFromInstructions} from '../get-bounding-box';
+import type {ReducedInstruction} from '../helpers/types';
import {parsePath} from '../parse-path';
import {reduceInstructions} from '../reduce-instructions';
import {serializeInstructions} from '../serialize-instructions';
@@ -8,14 +10,28 @@ import {
warpTransform,
} from './warp-helpers';
+const getDefaultInterpolationThreshold = (
+ instructions: ReducedInstruction[]
+) => {
+ const boundingBox = getBoundingBoxFromInstructions(instructions);
+ const longer = Math.max(
+ boundingBox.y2 - boundingBox.y1,
+ boundingBox.x2 - boundingBox.x1
+ );
+ return longer * 0.01;
+};
+
export const warpPath = (
path: string,
transformer: WarpPathFn,
- interpolationThreshold: number
+ options?: {
+ interpolationThreshold?: number;
+ }
): string => {
const reduced = reduceInstructions(parsePath(path));
const withZFix = fixZInstruction(reduced);
- const interpolated = svgPathInterpolate(withZFix, interpolationThreshold);
+
+ const interpolated = svgPathInterpolate(withZFix, options?.interpolationThreshold ?? getDefaultInterpolationThreshold(withZFix)));
return serializeInstructions(warpTransform(interpolated, transformer));
};