Skip to content

noCanvasToImage (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noCanvasToImage": "error"
}
}
}
}

Disallow converting a canvas to an image only to draw it onto another canvas.

drawImage() accepts a canvas as the image to draw. Turning a canvas into a data URL and loading that URL as an image does extra work that drawImage() doesn’t need: the browser encodes the pixels into an image file and then decodes the file back into pixels. Copying pixels with getImageData() and writing them back with putImageData() also copies every pixel through JavaScript, when drawImage() can copy them directly.

The rule reports two patterns:

  • the first argument of drawImage() is the result of loadImage(canvas.toDataURL()). The rule only checks a toDataURL() call on a variable or property whose name contains canvas, in any letter case, such as canvas, sourceCanvas, or this.canvas.
  • the first argument of putImageData() is the result of a getImageData() call.

The result can be passed directly, after await, or through a const variable that is declared before the call and used only once. The rule recognizes these patterns by their names; it doesn’t check whether the objects really are canvases.

const image = await loadImage(canvas.toDataURL());
context.drawImage(image, 0, 0);
code-block.js:2:19 lint/nursery/noCanvasToImage ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This image is loaded from a canvas only to be drawn with drawImage().

1 │ const image = await loadImage(canvas.toDataURL());
> 2 │ context.drawImage(image, 0, 0);
│ ^^^^^
3 │

ℹ Converting the canvas to a data URL and loading it as an image encodes and decodes every pixel, but drawImage() accepts the canvas itself.

ℹ Pass the canvas to drawImage() instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const imageData = sourceContext.getImageData(0, 0, width, height);
targetContext.putImageData(imageData, 0, 0);
code-block.js:2:28 lint/nursery/noCanvasToImage ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ These pixels are copied from a canvas with getImageData() only to be written to a canvas with putImageData().

1 │ const imageData = sourceContext.getImageData(0, 0, width, height);
> 2 │ targetContext.putImageData(imageData, 0, 0);
│ ^^^^^^^^^
3 │

ℹ Copying pixels through getImageData() is slower than drawing one canvas onto another with drawImage().

ℹ Pass the source canvas to drawImage() instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

context.drawImage(canvas, 0, 0);
targetContext.drawImage(sourceCanvas, 0, 0);
// The pixels are changed before they're written back.
const imageData = context.getImageData(0, 0, width, height);
imageData.data[0] = 0;
context.putImageData(imageData, 0, 0);