🎥 Make videos programmatically with React
@remotion/effects: Add scale() effect with subpath export
JonnyBurger08f2dd9a9781
diff --git a/packages/effects/package.json b/packages/effects/package.jsonindex b10a922ac9..b6f7673c72 100644--- a/packages/effects/package.json+++ b/packages/effects/package.json@@ -57,6 +57,11 @@"module": "./dist/esm/invert.mjs","import": "./dist/esm/invert.mjs"},+ "./scale": {+ "types": "./dist/scale.d.ts",+ "module": "./dist/esm/scale.mjs",+ "import": "./dist/esm/scale.mjs"+ },"./wave": {"types": "./dist/wave.d.ts","module": "./dist/esm/wave.mjs",@@ -81,6 +86,9 @@"invert": ["dist/invert.d.ts"],+ "scale": [+ "dist/scale.d.ts"+ ],"wave": ["dist/wave.d.ts"]diff --git a/packages/effects/scale.ts b/packages/effects/scale.tsnew file mode 100644index 0000000000..c3d7ed58b7--- /dev/null+++ b/packages/effects/scale.ts@@ -0,0 +1 @@+export {scale, type ScaleParams} from './src/scale.js';diff --git a/packages/effects/src/index.ts b/packages/effects/src/index.tsindex 2aa897e592..3baa9fc960 100644--- a/packages/effects/src/index.ts+++ b/packages/effects/src/index.ts@@ -1,3 +1,4 @@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.tsnew file mode 100644index 0000000000..de5e2d2598--- /dev/null+++ b/packages/effects/src/scale.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,+});diff --git a/packages/effects/src/test/effect-params.test.ts b/packages/effects/src/test/effect-params.test.tsindex 7008fafaf0..425491abe4 100644--- a/packages/effects/src/test/effect-params.test.ts+++ b/packages/effects/src/test/effect-params.test.ts@@ -4,6 +4,7 @@ import {brightness} from '../brightness.js';import {grayscale} from '../grayscale.js';import {hue} from '../hue.js';import {invert} from '../invert.js';+import {scale} from '../scale.js';import {tint} from '../tint.js';import {wave} from '../wave/index.js';@@ -174,3 +175,50 @@ test('hue() degrees produces distinct effect keys', () => {const rotated = hue({degrees: 90});expect(noRotation.effectKey).not.toBe(rotated.effectKey);});++test('scale() throws when scale is not passed', () => {+ expect(() => scale({} as Parameters<typeof scale>[0])).toThrow(+ '"scale" must be a finite number, but got undefined',+ );+});++test('scale() accepts valid params', () => {+ expect(() => scale({scale: 1.5})).not.toThrow();+});++test('scale() accepts horizontal-only scaling', () => {+ expect(() =>+ scale({scale: 1.5, horizontal: true, vertical: false}),+ ).not.toThrow();+});++test('scale() accepts vertical-only scaling', () => {+ expect(() =>+ scale({scale: 1.5, horizontal: false, vertical: true}),+ ).not.toThrow();+});++test('scale() accepts both axes disabled', () => {+ expect(() =>+ scale({scale: 1.5, horizontal: false, vertical: false}),+ ).not.toThrow();+});++test('scale() rejects non-positive scale', () => {+ expect(() => scale({scale: 0})).toThrow('"scale" must be greater than 0');+});++test('scale() axis flags produce distinct effect keys', () => {+ const both = scale({scale: 1.5});+ const horizontalOnly = scale({scale: 1.5, vertical: false});+ const verticalOnly = scale({scale: 1.5, horizontal: false});+ const neither = scale({scale: 1.5, horizontal: false, vertical: false});++ const keys = [+ both.effectKey,+ horizontalOnly.effectKey,+ verticalOnly.effectKey,+ neither.effectKey,+ ];+ expect(new Set(keys).size).toBe(keys.length);+});diff --git a/packages/effects/src/test/scale.test.ts b/packages/effects/src/test/scale.test.tsnew file mode 100644index 0000000000..1ff7d22143--- /dev/null+++ b/packages/effects/src/test/scale.test.ts@@ -0,0 +1,70 @@+import {describe, it, expect} from 'bun:test';+import {scale} from '../scale.js';++describe('scale', () => {+ it('should accept valid params with scale only', () => {+ expect(() => scale({scale: 1.5})).not.toThrow();+ });++ it('should accept valid params with all params', () => {+ expect(() =>+ scale({scale: 2, horizontal: true, vertical: false}),+ ).not.toThrow();+ });++ it('should accept valid params with non-uniform scaling', () => {+ expect(() =>+ scale({scale: 0.5, horizontal: false, vertical: true}),+ ).not.toThrow();+ });++ it('should throw when scale is 0', () => {+ expect(() => scale({scale: 0})).toThrow();+ });++ it('should throw when scale is negative', () => {+ expect(() => scale({scale: -1})).toThrow();+ });++ it('should throw when horizontal is not a boolean', () => {+ const params = {scale: 1.5, horizontal: 'invalid'} as {+ scale: number;+ horizontal: string;+ };+ expect(() => scale(params as Parameters<typeof scale>[0])).toThrow();+ });++ it('should throw when vertical is not a boolean', () => {+ const params = {scale: 1.5, vertical: 'invalid'} as {+ scale: number;+ vertical: string;+ };+ expect(() => scale(params as Parameters<typeof scale>[0])).toThrow();+ });++ it('should throw when params is not an object', () => {+ const params = 'not an object';+ expect(() => scale(params as Parameters<typeof scale>[0])).toThrow();+ });++ it('should produce distinct effect keys for different parameters', () => {+ const uniform = scale({scale: 1.5});+ const horizontalOnly = scale({scale: 1.5, vertical: false});+ const verticalOnly = scale({scale: 1.5, horizontal: false});++ const uniformKey = uniform.effectKey;+ const horizontalOnlyKey = horizontalOnly.effectKey;+ const verticalOnlyKey = verticalOnly.effectKey;++ expect(uniformKey).not.toEqual(horizontalOnlyKey);+ expect(uniformKey).not.toEqual(verticalOnlyKey);+ expect(horizontalOnlyKey).not.toEqual(verticalOnlyKey);+ });++ it('should default to uniform scaling', () => {+ const uniform = scale({scale: 2});+ const explicit = scale({scale: 2, horizontal: true, vertical: true});++ expect(uniform.effectKey).toBe(explicit.effectKey);+ });+});