var img = new Image();
var imageBitmap;
var doneLoading = false;
let canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
let canvas2 = document.createElement('canvas');
canvas2.width = 640;
canvas2.height = 480;
var ctx = canvas.getContext('2d');
var ctx2 = canvas2.getContext('2d');
var cv = document.createElement('canvas');
cv.width = 700;
cv.height = 700;
var cvx = cv.getContext('2d');
img.addEventListener('load', function() {
cvx.drawImage(img,0,0);
doneLoading = true;
});
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg';
if (doneLoading)
ctx.drawImage(img,0,0);
if (doneLoading)
ctx2.drawImage(cv,0,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
canvas Image |
Test name | Executions per second |
---|---|
Image | 252215.0 Ops/sec |
canvas Image | 219829.3 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and some pros/cons of different approaches.
Benchmark Overview
The benchmark is designed to compare the performance of drawing an image using two different methods:
Image
object.canvas
element's getContext('2d')
method, which can render WebGL 2D content.Script Preparation Code
The script preparation code creates several HTML elements:
canvas
elements (canvas
and canvas2
) with the same dimensions.canvas
element (cv
) with a larger size (700x700).canvas
element is assigned to a separate 2D drawing context (ctx
, ctx2
, and cvx
, respectively).The script then sets up an event listener for the img
object's load
event. When the image finishes loading, it draws the image on each of the three canvas
elements using the corresponding drawing contexts.
Individual Test Cases
There are two individual test cases:
if (doneLoading) ctx.drawImage(img, 0, 0);
. It's essentially testing whether the browser can draw an image on the canvas
element when it's loaded.if (doneLoading) ctx2.drawImage(cv, 0, 0);
. Similar to the previous test case, but using the drawing context of the larger cv
canvas.Benchmark Results
The latest benchmark results show two sets of execution counts per second for each test case:
These results suggest that the browser performs better when using the canvas
element's drawing context to render WebGL 2D content compared to directly drawing an image on the canvas
element.
Pros and Cons
Canvas with WebGL 2D:
Pros:
Cons:
Image:
Pros:
Cons:
Other Considerations
When comparing these two approaches, it's essential to consider factors such as:
Alternatives
If you're looking for alternative approaches, consider:
canvas
element's drawing context without enabling WebGL 2.0. This approach can simplify the implementation but may sacrifice performance.I hope this explanation helps!