remotion-dev/remotion · error · Error

Failed to acquire 2D context for invert effect. The canvas m

Error message

Failed to acquire 2D context for invert effect. The canvas may have been assigned a different context type.

What it means

Thrown in the apply() step of the invert effect when target.getContext('2d') returns null. Identical mechanism to the hue effect: the target canvas was already assigned a non-2D context type (webgl/webgl2/bitmaprenderer), and a canvas can only hold one context family. The message names the invert effect for clarity but the root cause is canvas/context reuse across backends.

Source

Thrown at packages/effects/src/invert.ts:51

	const {amount} = resolve(params);
	validateUnitInterval(amount, 'amount');
};

export const invert = createEffect<InvertParams, null>({
	type: 'dev.remotion.effects.invert',
	label: 'invert()',
	documentationLink: 'https://www.remotion.dev/docs/effects/invert',
	backend: '2d',
	calculateKey: (params) => {
		const r = resolve(params);
		return `invert-${r.amount}`;
	},
	setup: () => null,
	apply: ({source, target, width, height, params}) => {
		const ctx = target.getContext('2d');
		if (!ctx) {
			throw new Error(
				'Failed to acquire 2D context for invert effect. The canvas may have been assigned a different context type.',
			);
		}

		const r = resolve(params);

		ctx.clearRect(0, 0, width, height);
		ctx.filter = `invert(${r.amount * 100}%)`;
		ctx.drawImage(source, 0, 0, width, height);
		ctx.filter = 'none';
	},
	cleanup: () => undefined,
	schema: invertSchema,
	validateParams: validateInvertParams,
});

View on GitHub (pinned to 78fe4bb3fd)

Solutions

  1. Provide a fresh canvas to the invert effect that has never been used with a non-2D context.
  2. Never call getContext('webgl...') and getContext('2d') on the same canvas.
  3. Use the Remotion engine's scheduler unchanged; it allocates canvases per backend.
  4. Group effects by backend so each canvas stays single-context-family.

Example fix

// before: same canvas reused across backends
const gl = canvas.getContext('webgl2');   // earlier
const ctx2d = canvas.getContext('2d');    // for invert -> null

// after: one canvas per backend
const canvas2d = document.createElement('canvas');
const canvasGl = document.createElement('canvas');
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the canvas passed to the invert effect has never been acquired as a non-2D context.
function canvasHas2dAvailable(canvas: HTMLCanvasElement): boolean {
  return canvas.dataset.contextType === undefined || canvas.dataset.contextType === '2d';
}

Try / catch

try {
  invert({ amount: 1 }).apply({ source, target, width, height, params: { amount: 1 } });
} catch (e) {
  if (e instanceof Error && /Failed to acquire 2D context for invert effect/.test(e.message)) {
    // target canvas had an incompatible context; allocate a fresh 2D canvas
  } else throw e;
}

Prevention

When it happens

Trigger: The invert effect receiving a target canvas previously acquired as WebGL or bitmaprenderer; an engine or custom pipeline that pools one canvas across incompatible backends.

Common situations: Custom integrations passing the same canvas to both WebGL2 and 2D-backend effects; hand-rolled effect pipelines that reuse a canvas; a forked engine with naive canvas pooling.

Related errors


AI-assisted analysis of remotion-dev/remotion@78fe4bb3fd (2026-08-12). Data as JSON: /api/errors/46bbcfbe5d6b2a38. Report an issue: GitHub.