<canvas></canvas>
'use strict'
let image,
imageData,
ctx = new OffscreenCanvas(0, 0).getContext('2d'),
main = document.querySelector('canvas'),
c = main.getContext('2d'),
width, height,
imageAsBlob,
imageAsBitmap
async function globalMeasureThatScriptPrepareFunction() {
image = await loadImage();
({
width,
height
} = image)
ctx.canvas.width = main.width = width
ctx.canvas.height = main.height = height
ctx.drawImage(image, 0, 0, width, height)
imageData = ctx.getImageData(0, 0, width, height)
imageAsBlob = await loadImage(URL.createObjectURL(await ctx.canvas.convertToBlob()))
}
function loadImage(url = `https://www.measurethat.net/images/Picture1.png`) {
return new Promise(resolve => {
const img = new Image
img.src = url
img.onload = () => resolve(img)
})
}
function clear() {
c.clearRect(0, 0, width, height)
}
const repeatCount = 40
function test(num) {
switch (num) {
case 1:
for (let n = repeatCount; n--;) {
c.drawImage(image, 0, 0, width, height)
clear()
}
break
case 2:
for (let n = repeatCount; n--;) {
c.putImageData(imageData, 0, 0,)
clear()
}
break
case 3:
for (let n = repeatCount; n--;) {
c.drawImage(imageAsBlob, 0, 0)
clear()
}
break
}
}
test(1)
test(2)
test(3)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
draw image directly | |
draw imageData | |
draw image blob |
Test name | Executions per second |
---|---|
draw image directly | 31184.7 Ops/sec |
draw imageData | 298.0 Ops/sec |
draw image blob | 30245.3 Ops/sec |
The benchmark defined in the provided JSON focuses on measuring the performance of different methods for drawing images onto a canvas in a web context using JavaScript. The three methods being compared are:
Drawing the image directly - test(1)
: This method involves using the drawImage
function directly on the image. Here, the full image is rendered onto the canvas without any transformation or manipulation.
Using image data - test(2)
: In this method, the image is first converted to an ImageData
object using the getImageData
function, which retrieves pixel data from the canvas. The putImageData
function is then utilized to render this pixel data back onto the canvas.
Drawing an image blob - test(3)
: This approach uses ImageBitmap
created from a blob of the image data generated by convertToBlob
. The image is drawn on the canvas using the drawImage
method that takes this blob representation.
Draw Image Directly (test(1)
)
Draw ImageData (test(2)
)
ImageData
object contributes to lower performance.Draw Image Blob (test(3)
)
ImageBitmap
can provide performance benefits since it allows the browser to handle the image data more efficiently.ImageBitmap
may introduce additional overhead compared to drawing directly.In this benchmark, no external libraries are utilized aside from the standard web APIs. The script primarily uses the HTML5 <canvas>
element capabilities, including:
The benchmark results reveal significant performance differences between the methods tested:
Other alternatives beyond the methods tested include:
Each approach has its own use cases, and the best choice depends on the specific needs of the application, such as the required performance, the need for pixel manipulation, or the complexity of the graphics being rendered.