Skip to content

🎥 Make videos programmatically with React

extendviewbox

Jonny Burgera5e92c5bf235
diff --git a/packages/paths/src/extend-viewbox.ts b/packages/paths/src/extend-viewbox.ts
new file mode 100644
index 0000000000..81cdc821cc
--- /dev/null
+++ b/packages/paths/src/extend-viewbox.ts
@@ -0,0 +1,41 @@
+/**
+ * Extends a viewbox in all directions by a scale factor.
+ * @param {string} currentViewBox A valid SVG viewBox
+ * @link https://remotion.dev/docs/paths/extend-viewbox
+ */
+export const extendViewBox = (currentViewBox: string, scale: number) => {
+ const relativeScale = scale - 1;
+ const splitted = currentViewBox
+ .split(' ')
+ .map((a) => a.trim())
+ .filter((a) => a !== '')
+ .map(Number);
+
+ if (splitted.length !== 4) {
+ throw new Error(
+ `currentViewBox must be 4 valid numbers, but got "${currentViewBox}"`
+ );
+ }
+
+ for (const part of splitted) {
+ if (Number.isNaN(part)) {
+ throw new Error(
+ `currentViewBox must be 4 valid numbers, but got "${currentViewBox}"`
+ );
+ }
+
+ if (!Number.isFinite(part)) {
+ throw new Error(
+ `currentViewBox must be 4 valid numbers, but got "${currentViewBox}"`
+ );
+ }
+ }
+
+ const [x, y, width, height] = splitted;
+ return [
+ x - (relativeScale * width) / 2,
+ y - (relativeScale * height) / 2,
+ width + relativeScale * width,
+ height + relativeScale * height,
+ ].join(' ');
+};
diff --git a/packages/paths/src/index.ts b/packages/paths/src/index.ts
index bf305fae25..d5a56fb140 100644
--- a/packages/paths/src/index.ts
+++ b/packages/paths/src/index.ts
@@ -1,4 +1,5 @@
export {evolvePath} from './evolve-path';
+export {extendViewBox} from './extend-viewbox';
export {getLength} from './get-length';
export {getParts} from './get-parts';
export {getPointAtLength} from './get-point-at-length';