var canvas = document.createElement('canvas')
var context = canvas.getContext('2d')
var bitmap
var img = new Image()
img.onload = async () => {
canvas.width = img.width
canvas.height = img.height
context.drawImage(img, 0, 0)
bitmap = await createImageBitmap(img)
}
img.src = 'https://neurostof.nl/ns/letters.jpg'
var c = document.createElement('canvas').getContext('2d')
// bitmap && c.drawImage(img, 250, 250, 250, 250, 0, 0, 1000, 1000)
// bitmap && c.drawImage(canvas, 250, 250, 250, 250, 0, 0, 1000, 1000)
// bitmap && c.drawImage(bitmap, 250, 250, 250, 250, 0, 0, 1000, 1000)
bitmap && c.drawImage(img, 250, 250, 250, 250, 0, 0, 1000, 1000)
bitmap && c.drawImage(canvas, 250, 250, 250, 250, 0, 0, 1000, 1000)
bitmap && c.drawImage(bitmap, 250, 250, 250, 250, 0, 0, 1000, 1000)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
img | |
canvas | |
bitmap |
Test name | Executions per second |
---|---|
img | 280802784.0 Ops/sec |
canvas | 267623824.0 Ops/sec |
bitmap | 267332032.0 Ops/sec |
Let's break down the provided JSON and explain what is tested, compared options, pros/cons, and other considerations.
Benchmark Definition
The benchmark definition defines three test cases: drawing an image using drawImage()
on different targets (bitmap, canvas, and canvas with cropped bitmap).
Options Compared
canvas
element.canvas
element using the cropped version of the original bitmap.Pros and Cons
Library and Purpose
The createImageBitmap()
library is used to create a bitmap object from an image source (in this case, the loaded img
element). This library allows for efficient image processing and manipulation on Web APIs like Canvas.
Special JavaScript Feature or Syntax
The benchmark uses asynchronous code (async/await
) for img.onload event handling. This syntax is supported by most modern browsers and allows for more readable and manageable code.
Other Considerations
Alternatives
If you want to compare these options in different browsers or platforms, you might consider:
Keep in mind that each alternative will introduce new considerations, such as potential incompatibilities with different browsers or platforms, and may affect the overall quality of the results.