Reader Level:
ARTICLE

Canvas Scaling Animation Using HTML 5

Posted by Deepak Dwij Articles | HTML 5 March 25, 2012
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.
  • 0
  • 0
  • 4151
Download Files:
 

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

<style>
body
{
  margin: 0px;
  padding: 0px;
}
Canvas
{
   border: 2px solid #9C9898;
   margin-top: 50px;
   margin-left: 50px;
   background-color: #009999;
   box-shadow: 5px 5px 8px #222;
 }
.title
{
   text-align: center;
   font-family: Segoe UI Light, Arial, Helvetica;
   font-size: 2.2em;
   margin: 1em;
}
.info
{
   text-align: center;
   font-family: Segoe UI Light, Arial, Helvetica;
   font-size: 1.2em;
   margin: 0.25em;
}
</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.

<script>
        window.onload = function ()
          {
            var stage = new Kinetic.Stage("container", 600, 200);
            var layer = new Kinetic.Layer();       
            var blueHex = new Kinetic.RegularPolygon({
                x: 100,
                y: stage.height / 2,
                sides: 6,
                radius: 70,
                fill: "white",
                stroke: "black",
                strokeWidth: 4,
                draggable: true
            });
             var yellowHex = new Kinetic.RegularPolygon({
                x: stage.width / 2,
                y: stage.height / 2,
                sides: 6,
                radius: 70,
                fill: "yellow",
                stroke: "black",
                strokeWidth: 4,
                draggable: true
           });
            var redHex = new Kinetic.RegularPolygon({
                x: 470,
                y: stage.height / 2,
                sides: 6,
                radius: 70,
                fill: "gray",
                stroke: "black",
                strokeWidth: 4,
                centerOffset: {
                    x: 70,
                    y: 0
                },
                draggable: true
            });
            layer.add(blueHex);
            layer.add(yellowHex);
            layer.add(redHex);
            stage.add(layer);
            var period = 2000; // in ms
            stage.onFrame(function (frame) {
                var scale = Math.sin(frame.time * 2 * Math.PI / period) + 0.001;
                // scale x and y
                blueHex.setScale(scale);
                // scale only y
                yellowHex.scale.y = scale;
               // scale only x
                redHex.scale.x = scale;
                layer.draw();
            });
            stage.start();
        };
</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. 

<body style="background-color: #33CCFF">
    <center>
        <h1>
            Canvas Scaling Animation
        </h1>
    </center>
    <hr />
    <div id="container">
    </div>
</body>



Step 5 : The complete code for the
CanvasScalingAnimation application is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="canvasscalinganimation.aspx.cs" Inherits="canvasscalinganimation" %>
<!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: #33CCFF">
    <center>
        <h1>
            Canvas Scaling Animation
        </h1>
    </center>
    <hr />
    <div id="container">
    </div>
</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.
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 the some useful resources.

Canvas Oscillation Animation Using HTML 5
Canvas Reset Transform Using HTML 5
Canvas Anchor Points Using HTML 5
Canvas Mirror Transform Using HTML 5
Canvas Shape Layering Using HTML 5


COMMENT USING

Trending up