Change the Curve Position Using HTML 5


Introduction

In this article we are going to introduce the concept of changing the curve position using HTML 5. Here you will learn that you can change the curve position after displaying in the browser.

Here we will use some JavaScript and some styles along with the HTML code. Just go through the steps to see how to create this application.

Let's see how the changecurve text application can be created. To do so go through 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 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 by changecurve.aspx

cc1.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 in between the <head>--</head>.

Here two CSS files are used for design purposes; one is for the section div, named d1.css and the other d2.css for the whole body of the web page.

d1.css scripting

body
{
   background: #FFFF99;
    color: #fff;
    font: 300 100.1% "Helvetica Neue" , Helvetica, "Arial Unicode MS" , Arial, sans-serif;
}

#holder
{
    height: 442px;
    left: 42%;
    margin: -240px 0 0 -320px;
    position: absolute;
    top: 57%;
    width: 842px;
    font-size: x-large;
    background-color: #008000;
}
#duplicate {
    bottom: 0;
    font: 300 .7em "Helvetica Neue", Helvetica, ;
    position: absolute;
    right: 1em;
    text-align: right;
}
#duplicate d {
    color: #fff;
}


d2.css scripting.

body {
    background: #fff;
    color: #000;
    font: 100.1% "Comic Sans MS", Lucida, Verdana, sans-serif;
}
#holder {
    height: 480px;
    left: 50%;
    margin: 0 0 0 -320px;
    position: absolute;
    top: 0;
    width: 640px;
}
#duplicate {
    bottom: 0;
    font-size: .7em;
    position: absolute;
    right: 1em;
    text-align: right;
}

Step 3 : In this part we need to work on some JavaScript over here. For fully understanding the working of the JavaScript, download the attached .rar file and run the changecurve application. In this JavaScript there are five kinds of curves we are using; these curves are in different in colors. We use the curve() function  in which we can define our own colors by changing the hsb() function co-ordinates. Here is the function.

                curve(70, 100, 110, 100, 130, 200, 170, 200, "hsb(0, .75, .75)");

For moving up the curve position

In this part of the JavaScript we use the function move (dx,dy) which has the two arguments dx and dy.

     function move(dx, dy)
      {
        this.update(dx - (this.dx || 0), dy - (this.dy || 0));
        this.dx = dx;
        this.dy = dy;
      }

The whole JavaScript looks like as follows:

      window.onload = function ()
       {
            var r = Color("holder", 620, 420),
            discattr = { fill: "#fff", stroke: "none" };
            r.rect(0, 0, 619, 419, 10).attr({ stroke: "#666" });
            r.text(310, 20, "Change the curve postion").attr({ fill: "#fff", "font-size": 16 });
            function curve(x, y, ax, ay, bx, by, zx, zy, color)
                {
                    var path = [["M", x, y], ["C", ax, ay, bx, by, zx, zy]],
                        path2 = [["M", x, y], ["L", ax, ay], ["M", bx, by], ["L", zx, zy]],
                        curve = r.path(path).attr({ stroke: color || Color.getColor(), "stroke-width": 4, "stroke-linecap": "round" }),
                        controls = r.set(
                        r.path(path2).attr({ stroke: "#ccc", "stroke-dasharray": ". " }),
                        r.circle(x, y, 5).attr(discattr),
                        r.circle(ax, ay, 5).attr(discattr),
                        r.circle(bx, by, 5).attr(discattr),
                        r.circle(zx, zy, 5).attr(discattr)
                        );
                    controls[1].update = function (x, y) {
                        var X = this.attr("cx") + x,
                            Y = this.attr("cy") + y;
                        this.attr({ cx: X, cy: Y });
                        path[0][1] = X;
                        path[0][2] = Y;
                        path2[0][1] = X;
                        path2[0][2] = Y;
                        controls[2].update(x, y);
               function move(dx, dy)
                 {
                        this.update(dx - (this.dx || 0), dy - (this.dy || 0));
                        this.dx = dx;
                        this.dy = dy;
                 }
               function up()
                 {
                        this.dx = this.dy = 0;
                 }
                        curve(70, 100, 110, 100, 130, 200, 170, 200, "hsb(0, .75, .75)");
                        curve(170, 100, 210, 100, 230, 200, 270, 200, "hsb(.8, .75, .75)");
                        curve(270, 100, 310, 100, 330, 200, 370, 200, "hsb(.3, .75, .75)");
                        curve(370, 100, 410, 100, 430, 200, 470, 200, "hsb(.6, .75, .75)");
                        curve(470, 100, 510, 100, 530, 200, 570, 200, "hsb(.1, .75, .75)");
           };

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 changecurve.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.

<body>
       <div id="holder"></div>
</
body
>

Step 5 : The Complete code for the changecurve application.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="changecurve.aspx.cs" Inherits="changecurve._Default" %>
<!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">
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title></title>
        <link rel="stylesheet" href="d1.css" type="text/css" media="screen">
        <link rel="stylesheet" href="d2.css" type="text/css" media="print">
        <script src="changecurve.js" type="text/javascript" charset="utf-8"></script>
        <script>
                From Step 4 JavaScript copy from there and paste it here.
        </script>
    </head>
    <body>
        <div id="holder"></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 change the curve position by dragging the left side from the top of the curve.

cc.gif

After changing the curve positions.

cc3.gif

Here are some useful resources

How to Draw the Bézier curve in Windows Phone 7
Drawing Bezier Curves in WPF
Position based indexers in C#

Extending ASPX Panel Control to produce Rounded Corners
 

Up Next
    Ebook Download
    View all
    Learn
    View all