How to use CSS3 transitions with your canvas tag

Share RGraph:   

Summary
A guide for using CSS3 transitions with your canvas tags. Transitions can be used to amoothly switch from chart to chart

By using CSS transitions you can make smooth CSS based animations and effects for your canvas tag. The effect shown below switches between two charts using CSS transitions and changing the CSS width and height (the HTML width and height attributes which set the width and height of the canvas remain unchanged). The code changes the CSS width and height settings at the correct times and the CSS transition does the rest. The CSS transition is defined on the canvas tag (in CSS like this:

<style>
    canvas {
        transition: width .5s, height .5s, top .5s, left .5s;
    }
</style>

The rest of the code is shown below:

[No canvas support]

 

<script>
    window.onload = (function ()
    {
        /**
        * Used to track which chart is currently being displayed
        */
        var type = true;

        /**
        * This function is called to draw the bar chart
        */
        function drawBar ()
        {
            var bar = new RGraph.Bar({
                id: 'cvs',
                data: [5,8,16,5,8,9,4,3,2,4,3,2],
                options: {
                    labels: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
                    textSize: 14,
                    textAccessible: false
                }
            }).draw();
        
            type = false;
        }




        /**
        * This function is called to draw the line chart
        */
        function drawLine ()
        {
            var line = new RGraph.Line({
                id: 'cvs',
                data: [5,8,16,5,8,9,4,3,2,4,3,2],
                options: {
                    labels: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
                    textSize: 14,
                    textAccessible: false
                }
            }).draw();
                
            type = true;
        }



        /**
        * This is the onclick event for the button
        */
        document.getElementById('myButton').onclick = function (e)
        {
            var ca = document.getElementById('cvs');
            var $wrapper = $('#wrapper');

            /**
            * This CSS change results in the canvas shrinking (due to the CSS transition)
            */
            ca.style.width  = 0;
            ca.style.height = 0;
            ca.style.top    = '100px'
            ca.style.left   = '300px';
            
            /**
            * Now that the canvas hasd been shrunk - wait 0.75 seconds, draw the alternate
            * chart and unshrink it.
            */
            setTimeout(function ()
            {
                RGraph.reset(ca);
                ca.__rgraph_aa_translated__ = false;
                
                if (!type) {
                    drawBar();
                } else {
                    drawLine();
                }
                
                type = !type;


                /**
                * This CSS change results in the canvas expanding (due to the CSS transition)
                */
                ca.style.width    = '600px';
                ca.style.height   = '250px';
                ca.style.position = 'relative';
                ca.style.left     = 0;
                ca.style.top      = 0;
                
            }, 750)
            
        }
        
        drawBar();
    });
</script>
    

Comments

Add a new comment...