Canvas Shape Layering Using HTML 5

Introduction

 
In this article, we are going to have a very interesting section about canvas shape layering using HTML 5. To layer shapes we can use one of the following layering methods like moveToTop(), moveToBottom(), moveUp() and moveDown(). Drag and drop the boxes to move them around, and then use the buttons on the left to reorder the yellow box. We will see the layering with various colors change in accordance with a button click while displaying 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 ShapeLayering 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 then.
 
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 Webform
  • Rename it to shapelayeringpage.aspx
layer0.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.   margin: 0px;  
  5.   padding: 0px;  
  6. }  
  7. Canvas  
  8. {  
  9.    border: 2px solid #9C9898;  
  10.    margin-top: 50px;  
  11.    box-shadow: 5px 5px 8px #222;  
  12.  }  
  13. #buttons  
  14.  {  
  15.    position: absolute;  
  16.    left: 60px;  
  17.  }  
  18.  button  
  19.  {  
  20.    margin-top: 10px;  
  21.    display: block;  
  22.   }  
  23. </style> 
Step 3: In this part, we need to work on some JavaScript. To fully understanding how the JavaScript works, download the attached .rar file and run the ShapeLayering application. The whole JavaScript looks as in the following.
  1. <script>  
  2.        window.onload = function ()  
  3.         {  
  4.             var stage = new Kinetic.Stage("container", 578, 200);  
  5.             var layer = new Kinetic.Layer();  
  6.             var offsetX = 0;  
  7.             var offsetY = 0;  
  8.             var colors = ["red""orange""yellow""green""blue""purple"];  
  9.             for (var n = 0; n < 6; n++)  
  10.              {  
  11.                 (function ()  
  12.                   {  
  13.                     var i = n;  
  14.                     var box = new Kinetic.Rect({  
  15.                         x: i * 30 + 210,  
  16.                         y: i * 18 + 40,  
  17.                         width: 100,  
  18.                         height: 50,  
  19.                         fill: colors[i],  
  20.                         stroke: "black",  
  21.                         strokeWidth: 4,  
  22.                         draggable: true,  
  23.                         name: colors[i]  
  24.                     });  
  25.                     box.on("mouseover"function () {  
  26.                         document.body.style.cursor = "pointer";  
  27.                     });  
  28.                     box.on("mouseout"function () {  
  29.                         document.body.style.cursor = "default";  
  30.                     });  
  31.                     layer.add(box);  
  32.                 })();  
  33.             }  
  34.             stage.add(layer);  
  35.             // add button event bindings  
  36.             document.getElementById("toTop").addEventListener("click"function ()  
  37.             {  
  38.                 layer.getChild("yellow").moveToTop();  
  39.                 layer.draw();  
  40.             }, false);  
  41.             document.getElementById("toBottom").addEventListener("click"function ()  
  42.             {  
  43.                 layer.getChild("yellow").moveToBottom();  
  44.                 layer.draw();  
  45.             }, false);  
  46.             document.getElementById("up").addEventListener("click"function ()  
  47.             {  
  48.                 layer.getChild("yellow").moveUp();  
  49.                 layer.draw();  
  50.             }, false);  
  51.             document.getElementById("down").addEventListener("click"function ()  
  52.             {  
  53.                 layer.getChild("yellow").moveDown();  
  54.                 layer.draw();  
  55.             }, false);  
  56.             document.getElementById("zIndex").addEventListener("click"function ()  
  57.             {  
  58.                 layer.getChild("yellow").setZIndex(3);  
  59.                 layer.draw();  
  60.             }, false);  
  61.         };  
  62. </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 shapelayeringpage.aspx page. Here we pass a Canvas in the canvas tag.
  1. <body style="background-color: #FFFAF0">  
  2.     <center>  
  3.         <h1>  
  4.             Canvas Shape Layering  
  5.        </h1>  
  6.         <hr />  
  7.        <div id="container">  
  8.        </div>  
  9.        <div id="buttons">  
  10.             <button id="toTop">  
  11.                 Move yellow box to top  
  12.             </button>  
  13.             <button id="toBottom">  
  14.                 Move yellow box to bottom  
  15.             </button>  
  16.             <button id="up">  
  17.                Move yellow box up  
  18.             </button>  
  19.             <button id="down">  
  20.                Move yellow box down  
  21.             </button>  
  22.             <button id="zIndex">  
  23.                 Set yellow box zIndex to 3  
  24.             </button>  
  25.        </div>  
  26.     </center>  
  27. </body> 
Step 5 : The complete code for the ShapeLayering application is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="shapelayeringpage.aspx.cs" Inherits="ShapeLayering._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.           
  7.     </style>  
  8.     <script src="jscript.js">  
  9.    </script>  
  10.   <script>  
  11.     </script>  
  12. </head>  
  13. <body style="background-color: #FFFAF0">  
  14.     <center>  
  15.         <h1>  
  16.             Canvas Shape Layering  
  17.        </h1>  
  18.         <hr />  
  19.        <div id="container">  
  20.        </div>  
  21.        <div id="buttons">  
  22.             <button id="toTop">  
  23.                 Move yellow box to top  
  24.             </button>  
  25.             <button id="toBottom">  
  26.                 Move yellow box to bottom  
  27.             </button>  
  28.             <button id="up">  
  29.                Move yellow box up  
  30.             </button>  
  31.             <button id="down">  
  32.                Move yellow box down  
  33.             </button>  
  34.             <button id="zIndex">  
  35.                 Set yellow box zIndex to 3  
  36.             </button>  
  37.        </div>  
  38.     </center>  
  39. </body>  
  40. </html> 
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see the layering with different colors changes in accordance with button click while displaying the application in the browser.
 
layer.gif
 
When you press the button to move the yellow box to the top, the box will be shown at the top.
 
layer1.gif
 
When you press the button to move the yellow box to the bottom, the box will be shown at the bottom.
 
layer2.gif
 
When you press the button to move the yellow box up, the box will be shown at the up.
 
layer3.gif
 
Here are some useful resources