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.


- 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

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 horizontal and vertical Drag application.
The whole JavaScript looks as in the following:
- <script>
- window.onload = function ()
- {
- var stage = new Kinetic.Stage("container", 578, 200);
- var layer = new Kinetic.Layer();
- var rectHeight = 50;
- var rectWidth = 100;
- var rectY = (stage.height - rectHeight) / 2;
- var hbox = new Kinetic.Text({
- x: 120,
- y: 70,
- fill: "yellow",
- strokeWidth: 4,
- fontSize: 18,
- fontFamily: "Calibri",
- text: "Horizontal",
- textFill: "red",
- padding: 15,
- draggable: true,
- dragConstraint: "horizontal"
- });
- var vbox = new Kinetic.Text({
- x: 350,
- y: 70,
- fill: "white",
- strokeWidth: 4,
- draggable: true,
- fontSize: 18,
- fontFamily: "Calibri",
- text: "Vertical",
- textFill: "red",
- padding: 15,
- draggable: true,
- dragConstraint: "vertical"
- });
- layer.add(hbox);
- layer.add(vbox);
- stage.add(layer);
- };
- </script>




Shubham SrivastavaPosted Mar 25, 2012, 7:04 AM
good one
Abhishek DubeyPosted Mar 25, 2012, 6:45 AM
Nice presentation........