Canvas Scaling Animation Using HTML 5

Introduction

 
This article explains scaling animation using the HTML 5 canvas. In this section, you will see the drag and drop of hexagons with animation while the application is running in the browser.
 
Here we will use some JavaScript and some styles along with HTML code. Just go through the steps to see how to create this application. Let's see how the CanvasScalingAnimation application can be created. To do so use the following steps.
 
Step 1 : Open a HTML editor or Visual Studio.
 
sd.gif
 
Open File menu ->select new ->Choose Website:
 
0000.jpg
 
This is where we will create an HTML5 application.    
  • Go to Solution Explorer
  • Right-click on the Application name
  • Select Add-->add new item
  • Now in the window that opens, select an HTML page or new Webform
  • Rename it to canvasscalinganimation.aspx
sc2.gif
 
Step 2: In this section, we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or between the <head>--</head> tags. Here the CSS is used for design purposes.
 
CSS Script
  1. <style>  
  2. body  
  3. {  
  4.   margin0px;  
  5.   padding0px;  
  6. }  
  7. Canvas  
  8. {  
  9.    border2px solid #9C9898;  
  10.    margin-top50px;  
  11.    margin-left50px;  
  12.    background-color#009999;  
  13.    box-shadow: 5px 5px 8px #222;  
  14.  }  
  15. .title  
  16. {  
  17.    text-aligncenter;  
  18.    font-family: Segoe UI Light, ArialHelvetica;  
  19.    font-size2.2em;  
  20.    margin1em;  
  21. }  
  22. .info  
  23. {  
  24.    text-aligncenter;  
  25.    font-family: Segoe UI Light, ArialHelvetica;  
  26.    font-size1.2em;  
  27.    margin0.25em;  
  28. }  
  29. </style> 
Step 3 : In this part we need to work on some JavaScript. To fully understand how JavaScript works, download the attached .rar file and run the CanvasScalingAnimation application.
 
The whole JavaScript looks as in the following.
  1. <script>  
  2.         window.onload = function ()  
  3.           {  
  4.             var stage = new Kinetic.Stage("container", 600, 200);  
  5.             var layer = new Kinetic.Layer();         
  6.             var blueHex = new Kinetic.RegularPolygon({  
  7.                 x: 100,  
  8.                 y: stage.height / 2,  
  9.                 sides: 6,  
  10.                 radius: 70,  
  11.                 fill: "white",  
  12.                 stroke: "black",  
  13.                 strokeWidth: 4,  
  14.                 draggable: true  
  15.             });  
  16.              var yellowHex = new Kinetic.RegularPolygon({  
  17.                 x: stage.width / 2,  
  18.                 y: stage.height / 2,  
  19.                 sides: 6,  
  20.                 radius: 70,  
  21.                 fill: "yellow",  
  22.                 stroke: "black",  
  23.                 strokeWidth: 4,  
  24.                 draggable: true  
  25.            });  
  26.             var redHex = new Kinetic.RegularPolygon({  
  27.                 x: 470,  
  28.                 y: stage.height / 2,  
  29.                 sides: 6,  
  30.                 radius: 70,  
  31.                 fill: "gray",  
  32.                 stroke: "black",  
  33.                 strokeWidth: 4,  
  34.                 centerOffset: {  
  35.                     x: 70,  
  36.                     y: 0  
  37.                 },  
  38.                 draggable: true  
  39.             });  
  40.             layer.add(blueHex);  
  41.             layer.add(yellowHex);  
  42.             layer.add(redHex);  
  43.             stage.add(layer);  
  44.             var period = 2000; // in ms  
  45.             stage.onFrame(function (frame) {  
  46.                 var scale = Math.sin(frame.time * 2 * Math.PI / period) + 0.001;  
  47.                 // scale x and y  
  48.                 blueHex.setScale(scale);  
  49.                 // scale only y  
  50.                 yellowHex.scale.y = scale;  
  51.                // scale only x  
  52.                 redHex.scale.x = scale;  
  53.                 layer.draw();  
  54.             });  
  55.             stage.start();  
  56.         };  
  57. </script> 
Step 4 : In this section we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the canvasscalinganimation.aspx page. Here we pass a Canvas in the canvas tag. 
  1. <body style="background-color: #33CCFF">  
  2.     <center>  
  3.         <h1>  
  4.             Canvas Scaling Animation  
  5.         </h1>  
  6.     </center>  
  7.     <hr />  
  8.     <div id="container">  
  9.     </div>  
  10. </body> 
Step 5 : The complete code for the CanvasScalingAnimation application is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="canvasscalinganimation.aspx.cs" Inherits="canvasscalinganimation" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <style>  
  6.     </style>  
  7.     <script src="jscript.js">  
  8.     </script>  
  9.     <script>     
  10. </script>  
  11. </head>  
  12. <body style="background-color: #33CCFF">  
  13.     <center>  
  14.         <h1>  
  15.             Canvas Scaling Animation  
  16.         </h1>  
  17.     </center>  
  18.     <hr />  
  19.     <div id="container">  
  20.     </div>  
  21. </body>  
  22. </html> 
Step 6: Output Press F5
 
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see the Drag and drop the hexagons as they animate while the application is running in the browser.
 
sc.gif
 
sc1.gif
 
Here are some useful resources.