<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.clearRect(0, 0, 100, 100);
di.drawImage(master,0,0);
pid.putImageData(imgData1,0,0);
di.clearRect(0, 0, 100, 100);
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 | 298332.7 Ops/sec |
PutImageData whole master into pid canvas | 30257.1 Ops/sec |
DrawImage half master into di canvas | 246354.9 Ops/sec |
PutImageData half master into pid canvas | 34305.8 Ops/sec |
Let's break down what is being tested in the provided JSON benchmark.
Benchmark Definition
The benchmark compares two methods for rendering images on HTML5 canvases: PutImageData
and DrawImage
. Specifically, it measures the performance of these methods when drawing a 200x100 pixel image onto a canvas, with various configurations:
Options Compared
The benchmark is comparing the performance of two main options:
PutImageData
: This method involves creating an image data object from the canvas, and then using that object to draw the image onto another canvas.DrawImage
: This method directly draws the image onto the destination canvas.Pros and Cons
Here are some pros and cons of each approach:
PutImageData
:DrawImage
:PutImageData
, as it doesn't require creating an additional image data object.Library
In this benchmark, the CanvasRenderingContext2D
API is used to manipulate the canvases. This API provides a set of methods for performing common 2D graphics operations, such as drawing images, filling shapes with colors, and manipulating image data.
Special JS Feature or Syntax
None mentioned in this benchmark. However, it's worth noting that the PutImageData
method uses a feature of the HTML5 canvas API called "image data" (also known as " pixel buffer objects"), which allows for low-level manipulation of the canvas pixels. This can be useful in certain scenarios where precise control over the rendering process is required.
Alternatives
If you're looking to benchmark other methods for drawing images on canvases, here are some alternatives:
CanvasRenderingContext2D.drawImage()
with a SrcRect
argument: This method allows for more precise control over the region of the image being drawn.Keep in mind that each of these alternatives has its own trade-offs in terms of complexity, performance, and compatibility with different browsers and devices.