drawImage vs CanvasPattern (v12)

Revision 12 of this benchmark created on


Description

For drawing sprites, how does fillRect() with a CanvasPattern compare to drawImage?

Preparation HTML

<canvas id="screen"></canvas>
<script>
  var ctx = document.getElementById("screen").getContext("2d");
  var image = document.createElement("img");
  image.src = "http://icons.iconarchive.com/icons/ph03nyx/super-mario/48/Retro-Mushroom-1UP-2-icon.png";
  
  var patternNoRepeat = ctx.createPattern(image, "no-repeat");
  var patternRepeat = ctx.createPattern(image, "repeat");
var PI2 = Math.PI * 2;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
1
ctx.save();
ctx.beginPath();
ctx.fillStyle = "#c00";
ctx.arc(image.width/2, image.width/2, image.width/2, 0, Math.PI * 2, true);
ctx.fill();
ctx.restore();
ready
2
ctx.save();
ctx.fillStyle = patternNoRepeat;
ctx.fillRect(0, 0, image.width, image.height);
ctx.globalCompositeOperation = "source-in";
ctx.fillStyle = "#c00";
ctx.fillRect(0, 0, image.width, image.height);
ctx.restore();
ready
3
ctx.save();
ctx.beginPath();
ctx.fillStyle = "#c00";
ctx.arc(image.width/2, image.width/2, image.width/2, 0, PI2, true);
ctx.fill();
ctx.restore();
ready
4
ctx.save();
ctx.drawImage(image, 0, 0);
ctx.globalCompositeOperation = "source-in";
ctx.fillStyle = "#c00";
ctx.fillRect(0, 0, image.width, image.height);
ctx.restore();
ready

Revisions

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