How to use CSS3 animations and effects

  Read comments...

A guide explaining how to use new CSS3 animations and effects

Important: When you're using the textAccessible option the canvas is wrapped in a DIV which contains the text. This means that you need to apply the animation CSS classes to this wrapper - not the canvas itself - otherwise the CSS effect will not include the text. If you're not using JavaScript to apply the effect (as this page is) you can wrap the canvas in a DIV and apply the effect to that (there's example code below) .

[No canvas support]

CSS3 animations are a very efficient way to add effects to your charts. Browser support is good (Chrome, Firefox, Opera, Safari, IE10+) and there are a lot of effects available. You can chain the effects by setting two to run sequentially.

The CSS3 effects library is available in the css/ folder of the download archive and it is authored by Daniel Eden.

1. Include the CSS3 animations file.

Including the CSS3 file is a simple matter of adding a tag to your page header.

<link rel="stylesheet" href="animations.css" type="text/css" media="screen" />


The animate.css file is quite large (over 60k) so if you only use certain effects from it you can take those effects out of the file and put them in your own CSS file. The front page of this website does this and, gzipped, the CSS that's necessary for the effect thats used (a modified bounceIn effect) comes in at roughly 320 bytes (!).

2. Add the relevant CSS class to your canvas tag

This is just a case of adding a class="" attribute to your canvas tag.

<canvas id="cvs" width="600" height="250" class="animated rotateIn">[No canvas support]</canvas>

Combining effects

There's a blog article here that shows you how you can combine effects by delaying the second effect - allowing the first to finish.

Using CSS3 animations with the textAccessible option

If you're using CSS effects with the textAccessible option then you need to add the relevant CSS class to a wrapper DIV like the code shown here:

<div style="display: inline-block; width: 600px; height: 250px" class="animated rotateIn">
    <canvas width="600" height="250" id="cvs">[No canvas support]</canvas>

The exception to this is if you're applying the effect by using JavaScript (like this page does). In this case you can just use the wrapper that RGraph creates like this:

    var div = document.getElementById('cvs').parentNode;

    div.className = '';
    div.className = 'animated hinge';

Note about using window.onload with animations

If you're missing part of your animation then it may be due to the window.onload event. If your canvas tag is near the top of the page then it will start animating immediately. However if the Javascript that creates the chart is using the window.onload event, which may not be triggered immediately, you may end up not seeing the start of your animation effect. You can mitigate this by using jQuery to add the appropriate CSS clases after the call to the Draw() method.

$('#cvs').addClass('animated hinge');

Also, as a general rule, the jQuery ready event (which utlises the underlying standard DOMContentLoaded event) will result in your charts being displayed quicker than the window.onload event.

Share RGraph...