opacity animations using css vs. html5 canvas

JavaScript performance comparison

Test case created by Chris Shorrock

Info

Still a work in progress (i.e. some weirdness is present)...

Tests the speed of rendering an semi-opaque image on top of another using HTML and CSS vs. Canvas

Preparation code

<div style="position: relative; width: 256px; height: 256px;">
  <img src="#" id="background" width="256" height="256" style="position: absolute; left: 0px; top 0px";>
  <img src="#" id="overlay" width="90" height="46">
</div>

<canvas id="canvas" style="position: relative; width: 256px; height: 256px;"></canvas>
<script>
  var backgroundSrc = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OUU0NkFBNjVCNDhCMTF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LXLeEFywLSNnQjPsDzYUf4LyLp6ZcQHlig3Fe1m7UTOXsugPB+qwbIPfViymJxGzTEarUd6WX5n4N0M2n/gXbHvIhaAF2xy5MLRgmyHnNoY5DWRKw3WlDptJWg1CPq7zqChyKWjCfm4o9nFdcZfSa5kTyCbH4sh+Nzk2DPt/t+02BNsasjF0jbGtIpduDHMaSLyAWcaQNOTgUw73pshWMppl9vFDQ2+BIl+qqj7+loMdelTzgc6aYVtFxvgWYACSeU6ptSTspwAAAABJRU5ErkJggg==";
 
  var posX = 100;
  var posY = 100;
  var changeBy = 0.01;
  var opacity = 0;
 
  var increment = function() {
    opacity = opacity + changeBy;
    if (opacity >= 1) {
      opacity = 1;
      changeBy = -0.01;
    } else if (opacity <= 0) {
      opacity = 0;
      changeBy = 0.01;
    }
  }
 
  // set's up the canvas by painting the background onto the canvas, as well as loading the two images into image objects.
  var backgroundImage = new Image();
  var fadeImage       = new Image();
  var context         = document.getElementById("canvas").getContext("2d");
 
  backgroundImage.onload = function(event) {
    backgroundImage.width = 256;
    backgroundImage.height = 256;
    context.drawImage(backgroundImage, 0, 0, 256, 256);
  }
 
  backgroundImage.src = backgroundSrc;
  fadeImage.src = fadeSrc;
 
  // set's up the html images with the appropriate src
  document.getElementById("background").src = backgroundSrc;
  var htmlOverlay = document.getElementById("overlay");
  htmlOverlay.src = fadeSrc;
  htmlOverlay.style.position = "absolute";
  htmlOverlay.style.left = posX + "px";
  htmlOverlay.style.top = posY + "px";
 
 
 
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
css
increment();
htmlOverlay.style.opacity = opacity;
pending…
canvas
increment();

// clear the rect - redraw the background at the spot where we
// need it
context.clearRect(posX, posY, 90, 46);
context.drawImage(backgroundImage, posX, posY, 90, 46, posX, posY, 90, 46);

// create a drawing context - set opacity - draw faded image.
context.save();
context.globalAlpha = opacity;
context.drawImage(fadeImage, posX, posY, 90, 46);
context.restore();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

2 comments

Alexander Ewering commented :

Pretty much what I thought. I'm generally surprised at how many people exclusively make use of Canvas, even though the possibilities with CSS 3 and "normal" HTML are now almost endless, and most stuff is far easier to implement using those.

Quack Ado commented :

This is not always true. See the articles from others. 1. Mark Greenwood: www.wiliam.com.au/wiliam-blog/image-animation-performance-html-5-canvas-vs-html-4 And his result: http://phrogz.net/tmp/imagemovespeed.html 2. Sean Gaffney: http://www.netmagazine.com/opinions/css3-vs-javascript

Google more before concluding, imho.

Add a comment