Setting one HTML5 Canvas Pixel (v46)

Revision 46 of this benchmark created by Henri on


Preparation HTML

<p>(Scroll down to see the 'Run Tests' button.)</p>
<canvas id="c" width="800" height="300"></canvas>
<script>
  $c = document.getElementById('c');
  $ctx = $c.getContext('2d');
  $ctx.clearRect(0, 0, 800, 300);
  $px = $ctx.createImageData(1, 1);
var cpx=$ctx.getImageData(0,0,800, 300)
  $pxls = [];
  // Precompute random pixels so this time isn't included in the tests
  for (var i = 0; i < 10000; ++i) $pxls.push({
   x: Math.random() * 800 << 0,
   y: Math.random() * 300 << 0,
   r: Math.random() * 255 << 0,
   g: Math.random() * 255 << 0,
   b: Math.random() * 255 << 0,
   a: Math.random() * 128 << 0 + 128
  });
  $i = 0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
get/putImageData
var px = $pxls[$i++ % 10000];
var idata = $ctx.getImageData(0, 0, 800, 300);
var d = idata.data;
var o = (px.y * 800 + px.x) * 4;
d[o] = 0;
d[o + 1] = 0;
d[o + 2] = 0;
d[o + 3] = px.a;
$ctx.putImageData(idata, 0, 0, 20, 20, 20, 20);
ready
fillRect
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = 'rgba(0,0,0,1)';
$ctx.fillRect(px.x, px.y, 20, 20);
ready
fillRect with hex color & globalalpha
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = '#000000';
$ctx.globalAlpha = 1;
$ctx.fillRect(px.x, px.y, 20, 20);
ready
using original image data with dirty
var px = $pxls[$i++ % 10000];
index = (px.x + px.y * cpx.width) * 4;
cpx.data[index] = 0;
cpx.data[index + 1] = 0;
cpx.data[index + 2] = 0;
cpx.data[index + 3] = 1;
$ctx.putImageData(cpx, 0, 0, px.x, px.y, 20, 20);
ready
fillRect with globalAlpha
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = 'rgb(0,0,0)';
$ctx.globalAlpha = (px.a / 255);
$ctx.fillRect(px.x, px.y, 20, 20);
ready

Revisions

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