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.
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. </style>
  7. <script src="jscript.js">
  8. </script>
  9. <script>
  10. </script>
  11. </head>
  12. <body style="background-color: #FFFAF0">
  13. <center>
  14. <h1>
  15. Canvas Shape Layering
  16. </h1>
  17. <hr />
  18. <div id="container">
  19. </div>
  20. <div id="buttons">
  21. <button id="toTop">
  22. Move yellow box to top
  23. </button>
  24. <button id="toBottom">
  25. Move yellow box to bottom
  26. </button>
  27. <button id="up">
  28. Move yellow box up
  29. </button>
  30. <button id="down">
  31. Move yellow box down
  32. </button>
  33. <button id="zIndex">
  34. Set yellow box zIndex to 3
  35. </button>
  36. </div>
  37. </center>
  38. </body>
  39. </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