Canvas Rotating Rectangles Animation Using HTML 5

Introduction

In this article, we are going to understand rotating rectangle 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 souse the following steps.

Step 1. Open an HTML editor or Visual Studio.

Visual Studio

Open File menu ->select new ->Choose Website

Choose Website

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 Webform.
  • Rename it to canvasrotatingrects.aspx
     HTML

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 CSSis 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 CanvasRotatingAnimation application.

The whole JavaScript looks as in the following.

<script>
    window.onload = function () {
        var stage = new Kinetic.Stage({
            container: "container",
            width: 578,
            height: 200
        });
        var layer = new Kinetic.Layer();
        var blueRect = new Kinetic.Rect({
            x: 50,
            y: 75,
            width: 100,
            height: 50,
            fill: "#99FFCC",
            stroke: "black",
            strokeWidth: 4
        });
        var yellowRect = new Kinetic.Rect({
            x: 220,
            y: 75,
            width: 100,
            height: 50,
            fill: "yellow",
            stroke: "black",
            strokeWidth: 4,
            centerOffset: {
                x: 50,
                y: 25
            }
        });
        var redRect = new Kinetic.Rect({
            x: 400,
            y: 75,
            width: 100,
            height: 50,
            fill: "red",
            stroke: "black",
            strokeWidth: 4,
            centerOffset: {
                x: -100,
                y: 0
            }
        });
        layer.add(blueRect);
        layer.add(yellowRect);
        layer.add(redRect);
        stage.add(layer);
        stage.onFrame(function (frame) {
            blueRect.rotate(Math.PI / 100);
            yellowRect.rotate(Math.PI / 100);
            redRect.rotate(Math.PI / 100);
            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 canvasrotatingrects.aspx page. Here we pass a Canvas in the canvas tag.

<body style="background-color: #FFCC66">
    <center>
        <h1>
            Canvas Rotating Rectangles Animation
        </h1>
    </center>
    <hr />
    <p id="container">
    </p>
</body>

Step 5. The complete code for the CanvasRotatingAnimation application is.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="canvasrotatingrects.aspx.cs" Inherits="CanvasRotatingAnimation._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: #FFCC66">
    <center>
        <h1>
            Canvas Rotating Rectangles Animation
        </h1>
    </center>
    <hr />
    <p id="container">
    </p>
</body>
</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 corner, a yellow rectangle about its center, and a red rectangle about an outside point, while the application is running in the browser.

Browser

Canvas

Here are some useful resources.