Canvas Shape Tango Animation Using HTML 5

Introduction

 
In this article, we are going to understand canvas shape tango animation using HTML 5. In this section, drag and drop the shapes and press the "Flip press!" button to make the shapes move and refresh the page to generate new random shapes while running the application 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 CanvasShapeTango application can be created. To do so use the following steps.
 
Step 1 : Open a HTML editor or Visual Studio. 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 Web form
  • Rename it to CanvaSshapeTangoAnimation.aspx
tango2.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.             background-color#66CCFF;  
  5.             margin0px;  
  6.             padding0px;  
  7.         }  
  8.         canvas  
  9.         {  
  10.             border1px solid #9C9898;  
  11.             left: 30px;  
  12.         }  
  13.         #tango  
  14.         {  
  15.             positionabsolute;  
  16.             top: 10px;  
  17.             left: 10px;  
  18.             padding10px;  
  19.         }  
  20.         #container  
  21.         {  
  22.             background-imageurl('blue-background.jpg');  
  23.             display: inline-block;  
  24.             overflowhidden;  
  25.             height365px;  
  26.             width580px;  
  27.         }  
  28. </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 CanvasShapeTango application.
 
The whole JavaScript looks as in the following:
  1. <script>  
  2.         function tango(layer)  
  3.         {  
  4.             for (var n = 0; n < layer.getChildren().length; n++)  
  5.              {  
  6.                 var shape = layer.getChildren()[n];  
  7.                 var stage = shape.getStage();  
  8.                 shape.transitionTo({  
  9.                     rotation: Math.random() * Math.PI * 2,  
  10.                     radius: Math.random() * 100 + 20,  
  11.                    x: Math.random() * stage.width,  
  12.                    y: Math.random() * stage.height,  
  13.                     alpha: Math.random(),  
  14.                     duration: 1,  
  15.                     easing: 'ease-in-out'  
  16.                 });  
  17.             }  
  18.         }  
  19.        window.onload = function ()  
  20.         {  
  21.             var stage = new Kinetic.Stage({  
  22.                 container: 'container',  
  23.                 width578,  
  24.                 height363  
  25.             });  
  26.             var layer = new Kinetic.Layer();  
  27.             var colors = ['red''orange''yellow''green''blue''purple'];  
  28.             for (var n = 0; n < 10; n++) {  
  29.                 var mine = new Kinetic.RegularPolygon({  
  30.                     x: Math.random() * stage.width,  
  31.                     y: Math.random() * stage.height,  
  32.                     sides: Math.ceil(Math.random() * 5 + 3),  
  33.                     radius: Math.random() * 100 + 20,  
  34.                     fill: colors[Math.round(Math.random() * 5)],  
  35.                     stroke: 'black',  
  36.                     alpha: Math.random(),  
  37.                     strokeWidth: 4,  
  38.                     draggable: true  
  39.                 });  
  40.                 layer.add(mine);  
  41.             }  
  42.             stage.add(layer);  
  43.             document.getElementById('tango').addEventListener('click', function () {  
  44.                 tango(layer);  
  45.             }, false);  
  46.         };  
  47. </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 CanvaSshapeTangoAnimation.aspx page. Here we pass a Canvas in the canvas tag. 
  1. <body style="background-color: #00CCCC">  
  2.     <center>  
  3.         <h1>  
  4.             Canvas Shape Tango Animation  
  5.         </h1>  
  6.     </center>  
  7.     <hr />  
  8.     <div id="container">  
  9.     </div>  
  10.     <input type="button" id="tango" value="Flip Press!">  
  11. </body> 
Step 5 : The complete code for the CanvasRotatingAnimation application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CanvaSshapeTangoAnimation.aspx.cs" Inherits="CanvasShapeTango._Default" %>  
  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"></script>  
  8.     <script>  
  9.     </script>  
  10. </head>  
  11. <body style="background-color: #00CCCC">  
  12.     <center>  
  13.         <h1>  
  14.             Canvas Shape Tango Animation  
  15.         </h1>  
  16.     </center>  
  17.     <hr />  
  18.     <div id="container">  
  19.     </div>  
  20.     <input type="button" id="tango" value="Flip Press!">  
  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. Drag and drop the shapes and press the "Flip press!" button to make the shapes move and refresh the page to generate new random shapes while running the application in the browser.
 
tango.gif
 
tango1.gif
 
Here are some useful resources