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 the 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

<style>
        body
        {
            background-color: #66CCFF;
            margin: 0px;
            padding: 0px;
        }
        canvas
        {
            border: 1px solid #9C9898;
            left: 30px;
        }
        #tango
        {
            position: absolute;
            top: 10px;
            left: 10px;
            padding: 10px;
        }
        #container
        {
            background-image: url('blue-background.jpg');
            display: inline-block;
            overflow: hidden;
            height: 365px;
            width: 580px;
        }
</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:

<script>
        function tango(layer)
        {
            for (var n = 0; n < layer.getChildren().length; n++)
             {
                var shape = layer.getChildren()[n];
                var stage = shape.getStage();
                shape.transitionTo({
                    rotation: Math.random() * Math.PI * 2,
                    radius: Math.random() * 100 + 20,
                   x: Math.random() * stage.width,
                   y: Math.random() * stage.height,
                    alpha: Math.random(),
                    duration: 1,
                    easing: 'ease-in-out'
                });
            }
        }
       window.onload = function ()
        {
            var stage = new Kinetic.Stage({
                container: 'container',
                width: 578,
                height: 363
            });
            var layer = new Kinetic.Layer();
            var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
            for (var n = 0; n < 10; n++) {
                var mine = new Kinetic.RegularPolygon({
                    x: Math.random() * stage.width,
                    y: Math.random() * stage.height,
                    sides: Math.ceil(Math.random() * 5 + 3),
                    radius: Math.random() * 100 + 20,
                    fill: colors[Math.round(Math.random() * 5)],
                    stroke: 'black',
                    alpha: Math.random(),
                    strokeWidth: 4,
                    draggable: true
                });
                layer.add(mine);
            }
            stage.add(layer);
            document.getElementById('tango').addEventListener('click', function () {
                tango(layer);
            }, false);
        };
</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. 

<
body style="background-color: #00CCCC">
    <center>
        <h1>
            Canvas Shape Tango Animation
        </h1>
    </center>
    <hr />
    <div id="container">
    </div>
    <input type="button" id="tango" value="Flip Press!">
</body>

Step 5 : The complete code for the CanvasRotatingAnimation application:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CanvaSshapeTangoAnimation.aspx.cs" Inherits="CanvasShapeTango._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style>
    </style>
    <script src="jscript.js"></script>
    <script>
    </script>
</head>
<
body style="background-color: #00CCCC">
    <center>
        <h1>
            Canvas Shape Tango Animation
        </h1>
    </center>
    <hr />
    <div id="container">
    </div>
    <input type="button" id="tango" value="Flip Press!">
</body>
</
html>

Step 6 : Output Press F5

Note :
For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. D
rag 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 the some useful resources

Canvas Shape Layering Using HTML 5
Canvas Scaling Animation Using HTML 5
Canvas Oscillation Animation Using HTML 5
Canvas Sphere Animation Using HTML 5
Canvas Rotating Rectangles Animation Using HTML 5
 

Up Next
    Ebook Download
    View all
    Learn
    View all