canvas-clear (v24)

Revision 24 of this benchmark created on


Description

Different methods for clearing the canvas.

Setup

var c = document.createElement('canvas');
    c.width = 1700;
    c.height = 1000;
    
    var ctx = c.getContext('2d');
    ctx.fillStyle = '#000';
    ctx.fillRect(0, 0, 1700, 1000);
    
    var fill, global;

Teardown


    var p = ctx.getImageData(0, 0, 1, 1);
    if (p.data[3] != 0) throw ("canvas did not clear!");
  

Test runner

Ready to run.

Testing in
TestOps/sec
clearRect
ctx.clearRect(0, 0, c.width, c.height);
 
ready
fillRect
global = ctx.globalCompositeOperation;
fill = ctx.fillStyle;
ctx.globalCompositeOperation = 'copy';
ctx.fillStyle = 'rgba(0,0,0,0)';
ctx.fillRect(0, 0, c.width, c.height);
ctx.globalCompositeOperation = global;
ctx.fillStyle = fill;
ready
set width
c.width = c.width;
ready
clear transform then clearRect
// Store the current transformation matrix
ctx.save();

// Use the identity matrix while clearing the canvas
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, c.width, c.height);

// Restore the transform
ctx.restore();
 
ready
putImageData
ctx.putImageData(ctx.createImageData(c.width, c.height), 0, 0); // clear context by putting empty image data
ready
clear transform then clearRect manual
// Use the identity matrix while clearing the canvas
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, c.width, c.height);

// Restore the transform
ctx.setTransform(1, 0, 0, 1, 0, 0);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.