Tutorial: Page (1) of 1 - 10/19/09 Email this story to a friend. email article Print this page (Article printing at MyDmn.com).print page facebook
Creating Animation using the HTML 5 Canvas Tag Baked into HTML 5 is a new tag element called CANVAS By Matthew David

There is a battle Royale happening in the Web-O-Sphere between technologies that enable you to create cool, interactive animations online. The current King of the Hill is Adobe's Flash, with Microsoft's SilverLight coming in guns blazing. The black horse contender is the emerging HTML 5 standard. Baked into HTML 5 is a new tag element called CANVAS. Not sure what CANVAS is? Do you own a Mac? Most of the Widgets you run on your Dashboard are built with HTML 5's CANVAS element. In this article you will learn how to easily animate drawing using the new CANVAS tag built into HTML 5.

The CANVAS element gives you the capability to build and create Flash-like applications without having to use Flash. It is in the early stages of development, but some of the things you can already accomplish are very impressive. If you are running Google's Chrome, FireFox or Safari then you will want to check out http://www.chromeexperiments.com/ a site the pushes the capabilities of what can be done in your browser. In particular, look at the CANVAS experiments. Is it me, or do they look very Flash-like?

Creating CANVAS animation
There are two parts you need to create a visual element using CANVAS. The first is the CANVAS tag itself used in your HTML. In many ways, the CANVAS tag is very much the same as any other element used in HTML. Here is an example:


<canvas id="myCanvas" width="640" height="480"></canvas>

The tag uses the new HTML5 element CANVAS as the opening and closing tag. Width and height attributes specify the size of the CANVAS space on the screen. It is the ID that is important. Here the ID is named "myCanvas." Using JavaScript, you can now program the illustration that will appear in the CANVAS tag. The following example creates a black, outlined square that appears in your Web page using JavaScript and Canvas:
<html>
  <head>
    <title>Basic Canvas Drawing</title>
    <script type="text/javascript">
      function draw(){
        var canvas = document.getElementById('myCanvas');
        if (canvas.getContext){
          var ctx = canvas.getContext('2d');
        }
      }
    </script>
    <style type="text/css">
      canvas { border: 1px solid black; }
    </style>
  </head>
  <body onload="draw();">
    <canvas id="myCanvas" width="150" height="150"></canvas>
  </body>
</html>

So you now have a basic rectangle on the page. Big deal, right? Using JavaScript you can now begin to programmatically paint your object. The following shows two semi-transparent intersecting squares.


<html>
 <head>
  <script type="application/x-javascript">
    function draw() {
      var canvas = document.getElementById("myCanvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        ctx.fillStyle = "rgb(0,0,500)";
        ctx.fillRect (10, 10, 150, 150);
        ctx.fillStyle = "rgba(0, 300, 0, 0.5)";
        ctx.fillRect (75, 75, 150, 150);
      }
    }
  </script>
 </head>
 <body onload="draw();">
   <canvas id="myCanvas" width="300" height="300"></canvas>
 </body>
</html>

Adding Animation to your Canvas
Animation requires additional work. To make your life easier there is a great JavaSript library called CAKE (Canvas Animation Kit Experiment) that you can download at http://code.google.com/p/cakejs/. Using the CAKE library you can easily create CANVAS based animation. The following code will create a pulsing blue circle:

window.onload = function()
{
 var CAKECanvas = new Canvas(document.body, 600, 400);
 
 var myCircle = new Circle(100,
  {
   id: 'myCircle',
   x: CAKECanvas.width / 3, 
   y: CAKECanvas.height / 2,
   stroke: 'blue',
   strokeWidth: 20,
   endAngle: Math.PI*2
  }
 );
 
 myCircle.addFrameListener(
  function(t, dt)
  {
   this.scale = Math.sin(t / 1000);
  }
 );
 
 CAKECanvas.append(myCircle);
};

To accomplish the animation you are using the SCALE method. The effect is a very similar to Adobe's Flash, but with the benefit of running correctly on Web browsers found on mobile devices such as the iPhone, MyTouch and Palm Pre.
The next step you need to take is to create more complex solutions using CANVAS. There are a host of great web sites to help you. A good place to start is Mozilla's own Canvas Tutorial site (https://developer.mozilla.org/en/Canvas_tutorial). You will find in-depth coverage for what you can and cannot do with CANVAS. You may well be amazed at what you can do.


Page: 1


Matthew has written four Flash books, contributed to a dozen Web books, and has published over 400 articles. He is passionate about exposing Internet's potential for all of us. Matthew works directly with many companies as a business strategist coaching IT architects and business leaders to work tightly with each other towards common goals.
Related Sites: Digital Producer ,   Audio Video Producer ,   Corporate Media News ,   Digital Media Designer ,   Oceania ,   Flash
Related Newsletter: DMN Newsletter ,   Pixels Newsletter ,   Digital Media Net ,   DMNForums ,   Tutorial Finder
To Comment on This Article, Click HERE

Most Recent Reader Comments:
  • Creating Animation using the HTML 5 Canvas Tag by DMN Editorial at Oct. 19, 2009 6:02 pm gmt

    Click Here To Read All Posts
    Must be Registered to Respond (Free Registration!!!, CLICK HERE)

  • DMO TEXT LINKS
    (Click here to place a textlink on this site)

    Vegas Pro 8 + Free Vegas Seminar Series
    Master classes for cutting-edge video production
    A $100 value free with purchase
    CLICK HERE!!!

    Perfect your production with iStock video and audio
    Create your next production for less with video from iStock. Our royalty-free footage is available for web, NTSC/PAL and as large as 1080 HD.
    You can also find soundtracks for your masterpiece with over 50,000 iStock audio tracks.
    CLICK HERE!!!

    Get The Ultimatte!
    Hardware for processing bluescreen and greenscreen composing!
    Ultimatte.com


    graphics software
    Creative Suite 4 Design Premium (Full Product, PC)
    By Adobe, start from $ 479.99
    Photoshop CS4 (Full Product, PC)
    By Adobe, start from $ 314.99
    Photoshop Elements 7 (Full Product, PC)
    By Adobe, start from $ 29.94
    With 1 Reviews.
    Acrobat 9 Pro (Full Product, PC)
    By Adobe, start from $ 104.99
    With 2 Reviews.
    Photoshop Elements 8 (Full Product, PC)
    By Adobe, start from $ 49.94
    Design Premium CS4 (Full Product, Mac)
    By Adobe, start from $ 656.01
    HOT THREADS on DMN Forums
    Content-type: text/html  Rss  Add to Google Reader or
Homepage    Add to My AOL  Add to Excite MIX  Subscribe in
NewsGator Online 
    Real-Time - what users are saying - Right Now!
        • Re: Reverse Mode in Vegas 9 • gspencer
    iTech SolarVoice 908
    Bluetooth headsets have become more and more popular throughout the years and now more and more cell phones have been able to take advantage of this technology. In this review we'll be looking at a brand new Bluetooth headset from iTech, the SolarVoice 908, one of the first solar charging headsets on the market. Read More
    Content Insider 134 - Flash
    Thank goodness you're a pack rat! You know, have to grab, keep everything that flies by...ok everything except that report or news item or YouTube yuck you saw last week and now desperately need. Can't find it it save your behind. And still you don't have enough storage...there are new worlds to conquer, new worlds to destroy or take home with you. Don't worry Flash will always be there for you Dale!!!! Read More
    War Dogs of the Pacific
    On this Veteran's Day 2009, as we honor those who served and gave their lives for America, there are stories about man's best friend being told by those who fought alongside their "War Dogs of the Pacific." Most Americans have no idea that dogs were trained to sniff out the enemy during World War II, 549 to be exact fought with the U.S. Marines on Guam, Saipan, and other battlegrounds of the Pacific Theater. Read More
    Microsoft ships Windows 7- should you buy it?
    Did you hear? Microsoft is releasing a new version of Windows - should you buy it? Windows 7 is here. In this article you will find out if Windows 7 is worth the wait and whether or not you should upgrade. Or is Windows 7 just another Vista? Read More
    @ Copyright, 2008 Digital Media Online, All Rights Reserved