remotion-dev/remotion · error · Error

Failed to acquire 2D context for hue effect. The canvas may

Error message

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

What it means

Thrown in the apply() step of the hue effect when target.getContext('2d') returns null. Unlike the WebGL helper-canvas errors, this 'target' is the working canvas the Remotion engine hands to 2D-backend effects, and the message explicitly suggests the canvas may already hold a different (incompatible) context type. A canvas can only host one context family; once it has been given a 'webgl'/'webgl2'/'bitmaprenderer' context, subsequent getContext('2d') calls return null.

Source

Thrown at packages/effects/src/hue.ts:47

const validateHueParams = (params: HueParams): void => {
	assertEffectParamsObject(params, 'Hue');
	assertOptionalFiniteNumber(params.degrees, 'degrees');
};

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

		const r = resolve(params);

		ctx.clearRect(0, 0, width, height);
		ctx.filter = `hue-rotate(${r.degrees}deg)`;
		ctx.drawImage(source, 0, 0, width, height);
		ctx.filter = 'none';
	},
	cleanup: () => undefined,
	schema: hueSchema,
	validateParams: validateHueParams,
});

View on GitHub (pinned to 78fe4bb3fd)

Solutions

  1. Give the hue effect a canvas that has never been used with a non-2D context — use a fresh canvas per effect.
  2. If building a custom pipeline, never call getContext('webgl...') and getContext('2d') on the same canvas.
  3. Use the Remotion engine's effect scheduler as-is, which allocates canvases per backend.
  4. Reorder so all 2D-backend effects share one canvas and all WebGL effects share another.

Example fix

// before: same canvas reused across backends
const ctx2d = canvas.getContext('2d');     // for hue
const gl = canvas.getContext('webgl2');   // earlier, on same canvas

// 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 hue effect has never been acquired as a non-2D context.
function canvasHas2dAvailable(canvas: HTMLCanvasElement): boolean {
  // Note: calling getContext('2d') itself claims the context, so only call this on a throwaway probe,
  // or trust the engine to hand each effect a fresh canvas per backend.
  return canvas.dataset.contextType === undefined || canvas.dataset.contextType === '2d';
}

Try / catch

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

Prevention

When it happens

Trigger: The hue effect receiving a target canvas that the engine (or another effect) previously acquired as WebGL or bitmaprenderer; an engine misconfiguration that reuses a canvas across incompatible effect backends.

Common situations: Custom integrations that pass the same canvas to both WebGL2-backend and 2D-backend effects; a fork of the Remotion engine that pools canvases naively; mixing hue() after a WebGL2 effect on the same canvas in a hand-rolled pipeline.

Related errors


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