Canvas Drag and Drop Region Using HTML 5


Introduction

In this article we will understand canvas drag and drop regions using HTML 5. Here in this article you can move the rectangular shape as well as the circle when 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 canvasclippingregion 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 the 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 Web form
  • Rename it to dragdropclippingregion.aspx

dragndrop.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

<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 canvasclippingregion application.

The whole JavaScript looks as in the following

window.onload = function ()
         {
            var stage = new Kinetic.Stage("container", 500, 200);
            var layer = new Kinetic.Layer();
            var draggingShape = undefined;
            var draggingRectOffsetX = 0;
            var draggingRectOffsetY = 0;
            var box = new Kinetic.Shape({
                drawFunc: function () {
                    var context = this.getContext();
                    context.beginPath();
                    context.rect(this._x, this._y, 200, 100);
                    context.fillStyle = "#ddd";
                    context.fill();
                    context.closePath();
                },
               _x: 100,
                _y: 50
            });
            box.on("mousedown", function () {
                draggingShape = this;
                var mousePos = stage.getMousePosition();
                draggingRectOffsetX = mousePos.x - box._x;
                draggingRectOffsetY = mousePos.y - box._y;
            });
            box.on("mouseover", function () {
                document.body.style.cursor = "pointer";
            });
            box.on("mouseout", function () {
                document.body.style.cursor = "default";
            });
            layer.add(box);
            var circle = new Kinetic.Shape({
                drawFunc: function () {
                    var context = this.getContext();
                    context.beginPath();
                    context.rect(box._x, box._y, 200, 100);
                    context.clip();
                    context.beginPath();
                    context.arc(this._x, this._y, 50, 0, 2 * Math.PI, false);
                    context.fillStyle = "yellow";
                    context.fill();
                    context.closePath();
                },
                _x: 300,
                _y: 50
            });

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 dragdropclippingregion.aspx page. Here we pass a Canvas in the canvas tag.

<body style="background-color: #FFFFCC">
    <center>
        <h2>
            Canvas Drag and Drop Region
        </h2>
    </center>
    <hr />
    <div id="container">
    </div>
</body>

Step 5 : The complete code for the
canvasclippingregion application:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dragdropclippingregion.aspx.cs" Inherits="dragdropclippingregion" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style>
    </style>
    <script src="jscript.js">
    </script>
    <script>
    </script>
</head>
<
body style="background-color: #FFFFCC">
    <center>
        <h2>
            Canvas Drag and Drop Region
        </h2>
    </center>
    <hr />
    <div id="container">
    </div>
</body>
</
html>

Step 6 : Output Press F5

Note :
For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. You can
move the rectangular shape as well as the circle when the application is running in the browser.

dragndrop1.gif

After changing the rectangular and circle shape.

dragndrop2.gif

Here are the some useful resources.

Drag and Drop Title in HTML 5
Working With Canvas Tag in HTML5
Drag and Drop in HTML 5
Drag-and-Drop Operation in Windows Forms and C#


Up Next
    Ebook Download
    View all
    Learn
    View all