<canvas id='master' width='100' height='100'></canvas>
<canvas id='pid' width='100' height='100'></canvas>
<canvas id='di' width='100' height='100'></canvas>
<canvas id='rect' 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');
var rect = document.getElementById('rect').getContext('2d');
masterctx.fillRect(0,0,50,50);
masterctx.fillStyle = "red";
masterctx.fillRect(50,50,100,100);
let imgData1 = masterctx.getImageData(0,0,100,100);
let 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);
rect.fillRect(0,0,50,50);
rect.fillStyle = "red";
rect.fillRect(50,50,100,100);
--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 | |
rect |
Test name | Executions per second |
---|---|
DrawImage whole master into di canvas | 629303.1 Ops/sec |
PutImageData whole master into pid canvas | 55835.2 Ops/sec |
DrawImage half master into di canvas | 620745.9 Ops/sec |
PutImageData half master into pid canvas | 61442.8 Ops/sec |
rect | 1054374.2 Ops/sec |
The benchmark you've provided focuses on comparing the performance of different canvas rendering methods in a web environment using JavaScript. The specific methods being evaluated are drawImage
and putImageData
from the HTML5 Canvas API, as well as a direct rectangle rendering using fillRect
. Let's break down the components of this benchmark.
DrawImage whole master into di canvas
di.drawImage(master,0,0);
master
canvas onto the di
canvas.drawImage
is typically optimized for performance in most browsers and can leverage hardware acceleration, making it faster for drawing images or canvases.PutImageData whole master into pid canvas
pid.putImageData(imgData1,0,0);
putImageData
method to transfer pixel data from imgData1
(which contains the entire image data of the master
canvas) to the pid
canvas.putImageData
is generally slower than drawImage
for copying large sections because it operates directly on pixel data and may not take advantage of optimizations like those available for images.DrawImage half master into di canvas
di.drawImage(master,25,0,50,100,25,0,50,100);
master
canvas to the di
canvas, effectively specifying a rectangular portion of the source and destination.drawImage
.PutImageData half master into pid canvas
pid.putImageData(imgData2,25,0);
imgData2
, which represents a smaller portion of the master
canvas.drawImage
.Direct Rectangle Rendering
rect.fillRect(0,0,50,50);
rect.fillStyle = "red";
rect.fillRect(50,50,100,100);
rect
canvas.rect.fillRect
shows the highest performance, measuring 1,054,374.25
executions per second. This indicates that direct drawing operations are often the fastest due to their straightforward nature.
drawImage
for the whole master Canvas yielded 629,303.06
FPS, significantly faster than both putImageData
tests, indicating the efficiency of this method for image rendering.
The drawImage
for half the master had nearly the same performance with 620,745.87
FPS, reinforcing the fact that only specifying a section doesn't significantly impact performance.
putImageData
tests showed lower execution rates, with the full image at 55,835.24
and half image at 61,442.75
FPS, highlighting the overhead associated with this method as it involves manipulating raw pixel data.
Pros of drawImage
:
Cons of drawImage
:
Pros of putImageData
:
Cons of putImageData
:
Other alternatives for rendering images or graphics on a canvas could include:
WebGL: For 3D graphics or advanced 2D rendering, leveraging the GPU for performance gains. This is particularly useful for scenarios requiring complex visualizations or animations.
SVG (Scalable Vector Graphics): Especially suitable for vector-based graphics. SVG elements can be manipulated easily via the DOM but may not perform as well with a large number of elements as canvas.
Using Libraries: Libraries like PIXI.js or Fabric.js can provide a higher-level abstraction for graphics rendering that might offer optimized methods for specific tasks, though they would come with their own learning curves.
In conclusion, the benchmark effectively highlights the performance differences between various canvas drawing methods, providing insights into when to use each method based on specific requirements in terms of speed and data manipulation capabilities.