Canvas Planet Image Map Using HTML 5

Introduction

 
In this article, we are going to understand the concept of a canvas planet image map using HTML 5. In this section, you will see, with the mouse over the planets, the names of the planets and use of a checkbox to show and hide the map overlay while running 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 PlanetImageMap 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 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 a new Webform
  • Rename it to canvasplanetimage.aspx
planet0.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.             margin0px;  
  5.            padding0px;  
  6.             font-family: Calibri;  
  7.         }  
  8.         canvas  
  9.         {  
  10.             border1px solid #9C9898;  
  11.             margin-left15px;  
  12.         }  
  13.         #page  
  14.         {  
  15.             positionrelative;  
  16.             width580px;  
  17.             height253px;  
  18.         }  
  19.         #controls  
  20.         {  
  21.             positionabsolute;  
  22.             right: 10px;  
  23.             top: 10px;  
  24.             z-index99999;  
  25.         }         
  26.         label  
  27.         {  
  28.             colorwhite;  
  29.             vertical-aligntop;  
  30.         }  
  31. </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 PlanetImageMap application.
 
The whole JavaScript looks as in the following
  1. window.onload = function ()  
  2.    {  
  3.             var stage = new Kinetic.Stage("container", 578, 251);  
  4.             var planetsLayer = new Kinetic.Layer();  
  5.             var circlesLayer = new Kinetic.Layer();  
  6.             var messageLayer = new Kinetic.Layer();  
  7.             var planets = {  
  8.                 Mercury: {  
  9.                     x: 46,  
  10.                     y: 126,  
  11.                     radius: 32  
  12.                 },  
  13.                 Venus: {  
  14.                     x: 179,  
  15.                     y: 126,  
  16.                     radius: 79  
  17.                 },  
  18.                 Earth: {  
  19.                     x: 366,  
  20.                     y: 127,  
  21.                     radius: 85  
  22.                 },  
  23.                 Mars: {  
  24.                     x: 515,  
  25.                     y: 127,  
  26.                     radius: 45  
  27.                 }  
  28.             };  
  29.             var imageObj = new Image();  
  30.             imageObj.onload = function ()  
  31.                {  
  32.                 // draw shape overlays  
  33.                 for (var pubKey in planets)  
  34.                  {  
  35.                     (function () {  
  36.                         var key = pubKey;  
  37.                         var planet = planets[key];  
  38.                         var planetOverlay = new Kinetic.Shape({  
  39.                         drawFunc: function () {  
  40.                         var context = this.getContext();  
  41.                         context.beginPath();  
  42.                         context.arc(planet.x, planet.y, planet.radius, 0, Math.PI * 2, false);  
  43.                         context.closePath();  
  44.                         if (this.show) {  
  45.                         context.fillStyle = "red";  
  46.                         context.fill();  
  47.                          }  
  48.                        },  
  49.                       // custom property  
  50.                        show: false  
  51.                    });  
  52.                          planetOverlay.on("mouseover"function ()  
  53.                         {  
  54.                             writeMessage(messageLayer, key);  
  55.                         });  
  56.                         planetOverlay.on("mouseout"function ()  
  57.                         {  
  58.                             writeMessage(messageLayer, "");  
  59.                         });  
  60.                         circlesLayer.add(planetOverlay);  
  61.                   } ());  
  62.      } 
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 canvasplanetimage.aspx page. Here we pass a Canvas in the canvas tag.
  1. <body style="background-color: #FFFFCC">  
  2.     <center>  
  3.         <h2>  
  4.             Canvas Planet Image Map  
  5.         </h2>  
  6.     </center>  
  7.     <hr />  
  8.     <div id="page">  
  9.         <div id="container">  
  10.         </div>  
  11.         <div id="controls">  
  12.             <input type="checkbox" id="checkbox">  
  13.             <label>  
  14.                 Show map overlay  
  15.             </label>  
  16.         </div>  
  17.     </div>  
  18. </body> 
Step 5 : The complete code for the PlanetImageMap application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="canvasplanetimage.aspx.cs" Inherits="PlanetImageMap.canvasplanetimage" %>  
  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.     <script src="jscript.js">  
  6.     </script>  
  7.     <script>  
  8.     </script>  
  9. </head>  
  10. <body style="background-color: #FFFFCC">  
  11.     <center>  
  12.         <h2>  
  13.             Canvas Planet Image Map  
  14.         </h2>  
  15.     </center>  
  16.     <hr />  
  17.     <div id="page">  
  18.         <div id="container">  
  19.         </div>  
  20.         <div id="controls">  
  21.             <input type="checkbox" id="checkbox">  
  22.             <label>  
  23.                 Show map overlay  
  24.             </label>  
  25.         </div>  
  26.     </div>  
  27. </body>  
  28. </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 mouse over the planets to see their names and use the checkbox to show and hide the map overlay while running the application on the browser.
 
planet.gif
 
When you hover the mouse over the left-hand side as shown in the following figure you will get the name as Mercury.
 
planet1.gif
 
When you hover the mouse over the third to the left-hand side as shown in the figure you will get the name as Earth.
 
planet2.gif
 
When you press the checkbox you will get the overlay image.
 
planet3.gif
 
Here are some useful resources.