Skip to content

🎥 Make videos programmatically with React

@remotion/effects: Fix scale() effect to use subpath export

Jonny Burger43a8601f5494
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 1bfe8e2904..5dc7c50f86 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -12,6 +12,7 @@ const effectEntrypoints = [
'src/grayscale.ts',
'src/hue.ts',
'src/invert.ts',
+ 'src/scale.ts',
'src/wave.ts',
];
diff --git a/packages/effects/scale.ts b/packages/effects/scale.ts
deleted file mode 100644
index c3d7ed58b7..0000000000
--- a/packages/effects/scale.ts
+++ /dev/null
@@ -1 +0,0 @@
-export {scale, type ScaleParams} from './src/scale.js';
diff --git a/packages/effects/src/index.ts b/packages/effects/src/index.ts
index 3baa9fc960..2aa897e592 100644
--- a/packages/effects/src/index.ts
+++ b/packages/effects/src/index.ts
@@ -1,4 +1,3 @@
export {EffectInternals} from './effect-internals.js';
export {halftoneSchema, type HalftoneParams} from './halftone.js';
export {tintSchema, type TintParams} from './tint.js';
-export {scale, type ScaleParams} from './scale.js';
diff --git a/packages/effects/src/scale.ts b/packages/effects/src/scale.ts
index de5e2d2598..b99e2b5a4f 100644
--- a/packages/effects/src/scale.ts
+++ b/packages/effects/src/scale.ts
@@ -1,132 +1 @@
-import type {SequenceSchema} from 'remotion';
-import {Internals} from 'remotion';
-import {
- assertEffectParamsObject,
- assertRequiredFiniteNumber,
-} from './validate-effect-param.js';
-
-const {createEffect} = Internals;
-
-const scaleSchema = {
- scale: {
- type: 'number',
- min: 0.1,
- max: 10,
- step: 0.1,
- default: 1,
- description: 'Scale amount',
- },
- horizontal: {
- type: 'boolean',
- default: true,
- description: 'Apply horizontal scaling',
- },
- vertical: {
- type: 'boolean',
- default: true,
- description: 'Apply vertical scaling',
- },
-} as const satisfies SequenceSchema;
-
-export type ScaleParams = {
- /** Scale factor. Defaults to `1`. Must be greater than 0. */
- readonly scale: number;
- /** Whether to apply horizontal scaling. Defaults to `true`. */
- readonly horizontal?: boolean;
- /** Whether to apply vertical scaling. Defaults to `true`. */
- readonly vertical?: boolean;
-};
-
-type ScaleResolved = {
- scale: number;
- horizontal: boolean;
- vertical: boolean;
-};
-
-const resolve = (p: ScaleParams): ScaleResolved => ({
- scale: p.scale,
- horizontal: p.horizontal ?? true,
- vertical: p.vertical ?? true,
-});
-
-const validateScaleParams = (params: ScaleParams): void => {
- assertEffectParamsObject(params, 'Scale');
- assertRequiredFiniteNumber(params.scale, 'scale');
-
- if (
- typeof params.horizontal !== 'undefined' &&
- typeof params.horizontal !== 'boolean'
- ) {
- throw new TypeError(
- `"horizontal" must be a boolean, but got ${JSON.stringify(params.horizontal)}`,
- );
- }
-
- if (
- typeof params.vertical !== 'undefined' &&
- typeof params.vertical !== 'boolean'
- ) {
- throw new TypeError(
- `"vertical" must be a boolean, but got ${JSON.stringify(params.vertical)}`,
- );
- }
-
- const resolved = resolve(params);
- if (resolved.scale <= 0) {
- throw new TypeError(
- `"scale" must be greater than 0, but got ${JSON.stringify(resolved.scale)}`,
- );
- }
-};
-
-export const scale = createEffect<ScaleParams, null>({
- type: 'remotion/scale',
- label: 'Scale',
- backend: '2d',
- calculateKey: (params) => {
- const r = resolve(params);
- return `scale-${r.scale}-${r.horizontal}-${r.vertical}`;
- },
- setup: () => null,
- apply: ({source, target, width, height, params}) => {
- const ctx = target.getContext('2d');
- if (!ctx) {
- throw new Error(
- 'Failed to acquire 2D context for scale effect. The canvas may have been assigned a different context type.',
- );
- }
-
- const r = resolve(params);
-
- const scaleX = r.horizontal ? r.scale : 1;
- const scaleY = r.vertical ? r.scale : 1;
-
- // If scale is identity (1, 1), just copy the source
- if (scaleX === 1 && scaleY === 1) {
- ctx.clearRect(0, 0, width, height);
- ctx.drawImage(source, 0, 0, width, height);
- return;
- }
-
- ctx.clearRect(0, 0, width, height);
-
- // Save the current context state
- ctx.save();
-
- // Translate to center, scale, translate back
- const centerX = width / 2;
- const centerY = height / 2;
-
- ctx.translate(centerX, centerY);
- ctx.scale(scaleX, scaleY);
- ctx.translate(-centerX, -centerY);
-
- ctx.drawImage(source, 0, 0, width, height);
-
- // Restore the context state
- ctx.restore();
- },
- cleanup: () => undefined,
- schema: scaleSchema,
- validateParams: validateScaleParams,
-});
+export {scale, type ScaleParams} from './scale/index.js';
diff --git a/packages/effects/src/scale/index.ts b/packages/effects/src/scale/index.ts
new file mode 100644
index 0000000000..b2ce29b010
--- /dev/null
+++ b/packages/effects/src/scale/index.ts
@@ -0,0 +1,132 @@
+import type {SequenceSchema} from 'remotion';
+import {Internals} from 'remotion';
+import {
+ assertEffectParamsObject,
+ assertRequiredFiniteNumber,
+} from '../validate-effect-param.js';
+
+const {createEffect} = Internals;
+
+const scaleSchema = {
+ scale: {
+ type: 'number',
+ min: 0.1,
+ max: 10,
+ step: 0.1,
+ default: 1,
+ description: 'Scale amount',
+ },
+ horizontal: {
+ type: 'boolean',
+ default: true,
+ description: 'Apply horizontal scaling',
+ },
+ vertical: {
+ type: 'boolean',
+ default: true,
+ description: 'Apply vertical scaling',
+ },
+} as const satisfies SequenceSchema;
+
+export type ScaleParams = {
+ /** Scale factor. Defaults to `1`. Must be greater than 0. */
+ readonly scale: number;
+ /** Whether to apply horizontal scaling. Defaults to `true`. */
+ readonly horizontal?: boolean;
+ /** Whether to apply vertical scaling. Defaults to `true`. */
+ readonly vertical?: boolean;
+};
+
+type ScaleResolved = {
+ scale: number;
+ horizontal: boolean;
+ vertical: boolean;
+};
+
+const resolve = (p: ScaleParams): ScaleResolved => ({
+ scale: p.scale,
+ horizontal: p.horizontal ?? true,
+ vertical: p.vertical ?? true,
+});
+
+const validateScaleParams = (params: ScaleParams): void => {
+ assertEffectParamsObject(params, 'Scale');
+ assertRequiredFiniteNumber(params.scale, 'scale');
+
+ if (
+ typeof params.horizontal !== 'undefined' &&
+ typeof params.horizontal !== 'boolean'
+ ) {
+ throw new TypeError(
+ `"horizontal" must be a boolean, but got ${JSON.stringify(params.horizontal)}`,
+ );
+ }
+
+ if (
+ typeof params.vertical !== 'undefined' &&
+ typeof params.vertical !== 'boolean'
+ ) {
+ throw new TypeError(
+ `"vertical" must be a boolean, but got ${JSON.stringify(params.vertical)}`,
+ );
+ }
+
+ const resolved = resolve(params);
+ if (resolved.scale <= 0) {
+ throw new TypeError(
+ `"scale" must be greater than 0, but got ${JSON.stringify(resolved.scale)}`,
+ );
+ }
+};
+
+export const scale = createEffect<ScaleParams, null>({
+ type: 'remotion/scale',
+ label: 'Scale',
+ backend: '2d',
+ calculateKey: (params) => {
+ const r = resolve(params);
+ return `scale-${r.scale}-${r.horizontal}-${r.vertical}`;
+ },
+ setup: () => null,
+ apply: ({source, target, width, height, params}) => {
+ const ctx = target.getContext('2d');
+ if (!ctx) {
+ throw new Error(
+ 'Failed to acquire 2D context for scale effect. The canvas may have been assigned a different context type.',
+ );
+ }
+
+ const r = resolve(params);
+
+ const scaleX = r.horizontal ? r.scale : 1;
+ const scaleY = r.vertical ? r.scale : 1;
+
+ // If scale is identity (1, 1), just copy the source
+ if (scaleX === 1 && scaleY === 1) {
+ ctx.clearRect(0, 0, width, height);
+ ctx.drawImage(source, 0, 0, width, height);
+ return;
+ }
+
+ ctx.clearRect(0, 0, width, height);
+
+ // Save the current context state
+ ctx.save();
+
+ // Translate to center, scale, translate back
+ const centerX = width / 2;
+ const centerY = height / 2;
+
+ ctx.translate(centerX, centerY);
+ ctx.scale(scaleX, scaleY);
+ ctx.translate(-centerX, -centerY);
+
+ ctx.drawImage(source, 0, 0, width, height);
+
+ // Restore the context state
+ ctx.restore();
+ },
+ cleanup: () => undefined,
+ schema: scaleSchema,
+ validateParams: validateScaleParams,
+});