Canvas Rotating Rectangles Animation Using HTML 5

Introduction

 
In this article, we are going to understand rotating rectangles animation using the HTML 5 canvas. In this section, we'll rotate a light green rectangle about the top left corner, a yellow rectangle about its center, and a red rectangle about an outside point.
 
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 CanvasRotatingAnimation 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 Web form
  • Rename it to canvasrotatingrects.aspx
rta2.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 CanvasRotatingAnimation application.
 
The whole JavaScript looks as in the following:
  1. <script>  
  2.         window.onload = function ()  
  3.          {  
  4.             var stage = new Kinetic.Stage({  
  5.                 container: "container",  
  6.                 width: 578,  
  7.                 height: 200  
  8.             });  
  9.             var layer = new Kinetic.Layer();  
  10.             var blueRect = new Kinetic.Rect({  
  11.                 x: 50,  
  12.                 y: 75,  
  13.                 width: 100,  
  14.                 height: 50,  
  15.                 fill: "#99FFCC",  
  16.                 stroke: "black",  
  17.                 strokeWidth: 4  
  18.             });  
  19.             var yellowRect = new Kinetic.Rect({  
  20.                 x: 220,  
  21.                 y: 75,  
  22.                 width: 100,  
  23.                 height: 50,  
  24.                 fill: "yellow",  
  25.                 stroke: "black",  
  26.                 strokeWidth: 4,  
  27.                 centerOffset: {  
  28.                     x: 50,  
  29.                     y: 25  
  30.                 }  
  31.             });  
  32.             var redRect = new Kinetic.Rect({  
  33.                 x: 400,  
  34.                 y: 75,  
  35.                 width: 100,  
  36.                 height: 50,  
  37.                 fill: "red",  
  38.                 stroke: "black",  
  39.                 strokeWidth: 4,  
  40.                 centerOffset: {  
  41.                     x: -100,  
  42.                     y: 0  
  43.                 }  
  44.             });  
  45.             layer.add(blueRect);  
  46.             layer.add(yellowRect);  
  47.             layer.add(redRect);  
  48.             stage.add(layer);  
  49.             stage.onFrame(function (frame) {  
  50.                 blueRect.rotate(Math.PI / 100);  
  51.                 yellowRect.rotate(Math.PI / 100);  
  52.                 redRect.rotate(Math.PI / 100);  
  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 canvasrotatingrects.aspx page. Here we pass a Canvas in the canvas tag. 
  1. <body style="background-color: #FFCC66">  
  2.     <center>  
  3.         <h1>  
  4.             Canvas Rotating Rectangles Animation  
  5.         </h1>  
  6.     </center>  
  7.     <hr />  
  8.     <div id="container">  
  9.     </div>  
  10. </body> 
Step 5 : The complete code for the CanvasRotatingAnimation application is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="canvasrotatingrects.aspx.cs" Inherits="CanvasRotatingAnimation._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: #FFCC66">  
  12.     <center>  
  13.         <h1>  
  14.             Canvas Rotating Rectangles Animation  
  15.         </h1>  
  16.     </center>  
  17.     <hr />  
  18.     <div id="container">  
  19.     </div>  
  20. </body>  
  21. </html> 
Step 6: Output Press F5
 
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. A rotating light green rectangle about the top left the corner, a yellow rectangle about its center, and a red rectangle about an outside point, while the application is running in the browser.
 
rta.gif
 
rta1.gif
 
Here are some useful resources