Reader Level:

Canvas Animated Positioning Using HTML 5

By Deepak Dwij on Mar 26 2012
In this article we are going to understand animated positioning using a HTML 5 canvas.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 6.2k
  • 0


In this section, the shape's position changes and then the layer is redrawn with each animation frame using the onFrame() method. The onFrame() method is passed a frame object which contains two properties of interest. The number of milliseconds that the animation has been running is in frame.time.

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


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 animatedpostioning.aspx


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

  margin: 0px;
  padding: 0px;
   border: 2px solid
   margin-top: 50px;
   margin-left: 50px;
#9C9898 ;
   box-shadow: 5px 5px 8px #222;
   text-align: center;
   font-family: Segoe UI Light, Arial, Helvetica;
   font-size: 2.2em;
   margin: 1em;
   text-align: center;
   font-family: Segoe UI Light, Arial, Helvetica;
   font-size: 1.2em;
   margin: 0.25em;

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 CanvasAnimatedPostion application.

The whole JavaScript looks as in the following.

        window.onload = function ()
            var stage = new Kinetic.Stage({
                container: "container",
                width: 578,
                height: 200
            var layer = new Kinetic.Layer();
            var hexagon = new Kinetic.RegularPolygon({
               x: stage.width / 2,
               y: stage.height / 2,
               sides: 6,
                radius: 70,
                fill: "yellow",
                stroke: "black",
                strokeWidth: 4
            var amplitude = 150;
            var period = 2000;
            // in ms
            var centerX = stage.width / 2;
            stage.onFrame(function (frame) {
                hexagon.x = amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX;

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 animatedpostioning.aspx page. Here we pass a Canvas in the canvas tag. 

<body style="background-color: #33CCFF">
            Canvas Animated Postioning
    <hr />
    <div id="container">

Step 5 : The complete code for the CanvasAnimatedPostion application:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="animatedpostioning.aspx.cs" Inherits="CanvasAnimatedPostion._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <script src="jscript.js"></script>
body style="background-color: #33CCFF">
            Canvas Animated Postioning
    <hr />
    <div id="container">

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 shape's position and then redraw the layer with each animation frame using the onFrame() method, while the application is running in the browser.



Here are the some useful resources.

Animated Yearly Chart Using HTML 5
Canvas Reset Transform Using HTML 5
Canvas Anchor Points Using HTML 5
Canvas Mirror Transform Using HTML 5
Canvas Oscillation Animation Using HTML 5