Skip to content

Latest commit

Β 

History

History
39 lines (28 loc) Β· 1.42 KB

no-canvas-to-image.md

File metadata and controls

39 lines (28 loc) Β· 1.42 KB

no-canvas-to-image

πŸ“ Prefer drawing canvases directly instead of converting them to images.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

CanvasRenderingContext2D#drawImage() accepts a canvas as the image source. Converting a canvas to a data URL and loading it as an image, or copying it through getImageData() and putImageData(), does unnecessary work when the goal is just to draw one canvas onto another.

This rule intentionally checks simple syntactic patterns and does not infer DOM types. For the toDataURL() pattern, it only checks identifiers and non-computed property names that contain canvas, case-insensitively.

Examples

// ❌
const image = await loadImage(canvas.toDataURL());
context.drawImage(image, 0, 0);

// βœ…
context.drawImage(canvas, 0, 0);
// ❌
const imageData = sourceContext.getImageData(0, 0, width, height);
targetContext.putImageData(imageData, 0, 0);

// βœ…
targetContext.drawImage(sourceCanvas, 0, 0);
// βœ…
const imageData = context.getImageData(0, 0, width, height);
imageData.data[0] = 0;
context.putImageData(imageData, 0, 0);