noCanvasToImage (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noCanvasToImage - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/no-canvas-to-image
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noCanvasToImage": "error" } } }}Description
Section titled “Description”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 ofloadImage(canvas.toDataURL()). The rule only checks atoDataURL()call on a variable or property whose name containscanvas, in any letter case, such ascanvas,sourceCanvas, orthis.canvas. - the first argument of
putImageData()is the result of agetImageData()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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.