NEWS: Total Picture Views: 147066 | Total Article Views: 155658 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. How to read a JSON and work with it | 3. PickUpThings with Becker Robot | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Canvas With Animation Javascript

Description:

In this example I will be showing how to use canvas with animation.

You can view the example at https://www.jmdev.ca/sheridan/example/10/

Code:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Canvas</title>

    <script type="text/javascript">
        var c = 0;
        function draw(x,y){
            var can = document.getElementById("myCanvas");
            var ctx = can.getContext("2d");
            var col = ["Yellow","Red","Blue","Pink","Purple"];
            //ctx.fillStyle = "Yellow";
            ctx.fillStyle = (col[c]);
            if(x == 550){
                y+= 100;
                x = 0;
                //c++;
            }else{
                x += 5;
                c++;
            }
            if(c > 5){
                c = 1;
            }

            ctx.beginPath();
            ctx.arc(x,y,50,0,2*Math.PI,true);
            ctx.fill();

            setTimeout('draw('+ x + ', ' + y + ');',40);
        }
    </script>
</head>
<body>
    <canvas height="500" width="500" id="myCanvas" style="border: 5px solid #ff0000">

    </canvas>
    <button onclick="draw(0,0)">Draw</button>
</body>
</html>
Attachments: None

Tags: Canvas setTimeout

Total Views: 720

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 03/17/15


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.