<canvas id='master' width='100' height='100'></canvas>
<canvas id='pid' width='100' height='100'></canvas>
<canvas id='di' width='100' height='100'></canvas>
var master = document.getElementById('master');
var masterctx = master.getContext('2d');
var pid = document.getElementById('pid').getContext('2d');
var di = document.getElementById('di').getContext('2d');
masterctx.fillRect(0,0,50,50);
masterctx.fillStyle = "red";
masterctx.fillRect(50,50,100,100);
var imgData1 = masterctx.getImageData(0,0,100,100);
var imgData2 = masterctx.getImageData(25,0,75,100);
di.drawImage(master,0,0);
pid.putImageData(imgData1,0,0);
di.drawImage(master,25,0,50,100,25,0,50,100);
pid.putImageData(imgData2,25,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DrawImage whole master into di canvas | |
PutImageData whole master into pid canvas | |
DrawImage half master into di canvas | |
PutImageData half master into pid canvas |
Test name | Executions per second |
---|---|
DrawImage whole master into di canvas | 387301.6 Ops/sec |
PutImageData whole master into pid canvas | 27808.7 Ops/sec |
DrawImage half master into di canvas | 386118.8 Ops/sec |
PutImageData half master into pid canvas | 27917.6 Ops/sec |
Overview
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test compares the performance of two 2D graphics APIs in JavaScript: DrawImage
and PutImageData
. The test consists of four individual benchmark cases, each measuring the execution time of drawing or putting an image data object onto a canvas.
Test Cases
DrawImage whole master into di canvas
This test case draws the entire master
image (a 50x100 red rectangle) onto the di
canvas at position (0, 0).
PutImageData whole master into pid canvas
This test case puts the imgData1
image data object onto the pid
canvas at position (0, 0), where imgData1
is a subset of the original master
image (50x100 rectangle starting from x=25 and y=0).
DrawImage half master into di canvas
This test case draws only half of the master
image (the bottom-right 50x100 rectangle) onto the di
canvas at position (0, 25).
PutImageData half master into pid canvas
This test case puts a subset of the original master
image (imgData2
) onto the pid
canvas at position (25, 0), which is similar to the previous case but with a different subset of pixels.
Options Compared
The two options compared are:
Pros and Cons of Each Approach
PutImageData
, especially for larger images.DrawImage
due to the need to create an image data object from a source pixel buffer.Library and Purpose
The getImageData()
method in JavaScript returns an ImageData
object, which contains pixel data for a specified area of an image. The putImageData()
method takes an ImageData
object as an argument and places its pixels onto another canvas or image element. These methods are essential tools in 2D graphics programming.
Special JS Feature
There is no special JavaScript feature or syntax explicitly used in this benchmark, but the use of requestAnimationFrame()
for animation and timing-related functionality is implicit, as MeasureThat.net generates these frames automatically during benchmarking.
Other Alternatives
If you were to create similar benchmarks using other 2D graphics APIs or methods, you might consider:
: This method can be used with
drawImage()` for creating an image data object from a source pixel buffer.DrawImage
or PutImageData
.