Canvas Horizontal and Vertical Drag Using HTML 5

Introduction

 
In this article, we are going to understand horizontal and vertical dragging using the HTML 5 canvas. In this section, drag and drop the yellow rectangle and observe that it can only move horizontally. Drag and drop the white rectangle and observe that it can only move vertically while the application is running 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 horizontal and vertical Drag applications 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 HandVDrag.aspx
hnv4.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. }  
  7. Canvas  
  8. {  
  9.    border2px solid #9C9898;  
  10.    margin-top50px;  
  11.    margin-left50px;  
  12.    background-color#009999;  
  13.    box-shadow: 5px 5px 8px #222;  
  14.  }  
  15. .title  
  16. {  
  17.    text-aligncenter;  
  18.    font-family: Segoe UI Light, ArialHelvetica;  
  19.    font-size2.2em;  
  20.    margin1em;  
  21. }  
  22. .info  
  23. {  
  24.    text-aligncenter;  
  25.    font-family: Segoe UI Light, ArialHelvetica;  
  26.    font-size1.2em;  
  27.    margin0.25em;  
  28. }  
  29. </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 horizontal and vertical Drag 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 rectHeight = 50;  
  7.             var rectWidth = 100;  
  8.             var rectY = (stage.height - rectHeight) / 2;  
  9.             var hbox = new Kinetic.Text({  
  10.                 x: 120,  
  11.                 y: 70,  
  12.                 fill: "yellow",  
  13.                 strokeWidth: 4,  
  14.                 fontSize: 18,  
  15.                 fontFamily: "Calibri",  
  16.                 text: "Horizontal",  
  17.                 textFill: "red",  
  18.                 padding: 15,  
  19.                 draggable: true,  
  20.                 dragConstraint: "horizontal"  
  21.             });  
  22.             var vbox = new Kinetic.Text({  
  23.                 x: 350,  
  24.                 y: 70,  
  25.                 fill: "white",  
  26.                 strokeWidth: 4,  
  27.                 draggable: true,  
  28.                 fontSize: 18,  
  29.                 fontFamily: "Calibri",  
  30.                 text: "Vertical",  
  31.                 textFill: "red",  
  32.                 padding: 15,  
  33.                 draggable: true,  
  34.                 dragConstraint: "vertical"  
  35.             });  
  36.             layer.add(hbox);  
  37.             layer.add(vbox);  
  38.             stage.add(layer);  
  39.         };  
  40. </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 HandVDrag.aspx page. Here we pass a Canvas in the canvas tag. 
  1. <body style="background-color: #33CCFF">  
  2.     <center>  
  3.         <h2>  
  4.             Canvas Horizontal and vertical Drag  
  5.         </h2>  
  6.     </center>  
  7.     <hr />  
  8.     <div id="container">  
  9.     </div>  
  10. </body> 
Step 5: The complete code for the horizontal and vertical Drag application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HandVDrag.aspx.cs" Inherits="horizontal_and_vertical_Drag.HandVDrag" %>  
  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"></script>  
  8.     <script>  
  9.     </script>  
  10. </head>  
  11. <body style="background-color: #33CCFF">  
  12.     <center>  
  13.         <h2>  
  14.             Canvas Horizontal and vertical Drag  
  15.         </h2>  
  16.     </center>  
  17.     <hr />  
  18.     <div id="container">  
  19.     </div>  
  20. </body>  
  21. </html> 
Step 6 : Output Press F5
 
Note : For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. Drag and drop the yellow rectangle and observe that it can only move horizontally. Drag and drop the white rectangle and observe that it can only move vertically while the application is running in the browser.
 
hnv.gif
 
When you drag the yellow rectangle the position of rectangle changes only horizontally.
 
hnv1.gif
 
When you drag the white rectangle the position of rectangle changes only vertically.
 
hnv23.gif
 
Here are some useful resources.