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 change curve 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 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 new Webform
  • 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
  1. body {  
  2.   background#FFFF99;  
  3.   color#fff;  
  4.   font300 100.1% "Helvetica Neue" , Helvetica"Arial Unicode MS" , Arialsans-serif;  
  5. }  
  6. #holder {  
  7.   height442px;  
  8.   left: 42%;  
  9.   margin-240px 0 0 -320px;  
  10.   positionabsolute;  
  11.   top: 57%;  
  12.   width842px;  
  13.   font-sizex-large;  
  14.   background-color#008000;  
  15. }  
  16. #duplicate {  
  17.   bottom: 0;  
  18.   font300 .7em "Helvetica Neue"Helvetica,;  
  19.   positionabsolute;  
  20.   right: 1em;  
  21.   text-alignright;  
  22. }  
  23. #duplicate d {  
  24.   color#fff;  
  25. }  
d2.css scripting.
  1. body {  
  2.   background#fff;  
  3.   color#000;  
  4.   font100.1% "Comic Sans MS", Lucida, Verdanasans-serif;  
  5. }  
  6. #holder {  
  7.   height480px;  
  8.   left: 50%;  
  9.   margin0 0 0 -320px;  
  10.   positionabsolute;  
  11.   top: 0;  
  12.   width640px;  
  13. }  
  14. #duplicate {  
  15.   bottom: 0;  
  16.   font-size: .7em;  
  17.   positionabsolute;  
  18.   right: 1em;  
  19.   text-alignright;  
  20. }  
Step 3: In this part, we need to work on some JavaScript over here. To fully understand 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 indifferent 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.
  1. function move(dx, dy) {  
  2.  this.update(dx - (this.dx || 0), dy - (this.dy || 0));  
  3.  this.dx = dx;  
  4.  this.dy = dy;  
  5. }  
The whole JavaScript looks like as follows:
  1. window.onload = function() {  
  2.   var r = Color("holder", 620, 420),  
  3.    discattr = {  
  4.     fill: "#fff",  
  5.     stroke: "none"  
  6.    };  
  7.   r.rect(0, 0, 619, 419, 10).attr({  
  8.    stroke: "#666"  
  9.   });  
  10.   r.text(310, 20, "Change the curve postion").attr({  
  11.    fill: "#fff",  
  12.    "font-size": 16  
  13.   });  
  14.   function curve(x, y, ax, ay, bx, by, zx, zy, color) {  
  15.    var path = [  
  16.      ["M", x, y],  
  17.      ["C", ax, ay, bx, by, zx, zy]  
  18.     ],  
  19.     path2 = [  
  20.      ["M", x, y],  
  21.      ["L", ax, ay],  
  22.      ["M", bx, by],  
  23.      ["L", zx, zy]  
  24.     ],  
  25.     curve = r.path(path).attr({  
  26.      stroke: color || Color.getColor(),  
  27.      "stroke-width": 4,  
  28.      "stroke-linecap""round"  
  29.     }),  
  30.     controls = r.set(  
  31.      r.path(path2).attr({  
  32.       stroke: "#ccc",  
  33.       "stroke-dasharray"". "  
  34.      }),  
  35.      r.circle(x, y, 5).attr(discattr),  
  36.      r.circle(ax, ay, 5).attr(discattr),  
  37.      r.circle(bx, by, 5).attr(discattr),  
  38.      r.circle(zx, zy, 5).attr(discattr)  
  39.     );  
  40.    controls[1].update = function(x, y) {  
  41.     var X = this.attr("cx") + x,  
  42.      Y = this.attr("cy") + y;  
  43.     this.attr({  
  44.      cx: X,  
  45.      cy: Y  
  46.     });  
  47.     path[0][1] = X;  
  48.     path[0][2] = Y;  
  49.     path2[0][1] = X;  
  50.     path2[0][2] = Y;  
  51.     controls[2].update(x, y);  
  52.     function move(dx, dy) {  
  53.      this.update(dx - (this.dx || 0), dy - (this.dy || 0));  
  54.      this.dx = dx;  
  55.      this.dy = dy;  
  56.     }  
  57.     function up() {  
  58.      this.dx = this.dy = 0;  
  59.     }  
  60.     curve(70, 100, 110, 100, 130, 200, 170, 200, "hsb(0, .75, .75)");  
  61.     curve(170, 100, 210, 100, 230, 200, 270, 200, "hsb(.8, .75, .75)");  
  62.     curve(270, 100, 310, 100, 330, 200, 370, 200, "hsb(.3, .75, .75)");  
  63.     curve(370, 100, 410, 100, 430, 200, 470, 200, "hsb(.6, .75, .75)");  
  64.     curve(470, 100, 510, 100, 530, 200, 570, 200, "hsb(.1, .75, .75)");  
  65.    };  
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.
  1. <body>  
  2.     <div id="holder"></div>  
  3. </body>  
Step 5: The Complete code for the changecurve application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="changecurve.aspx.cs" Inherits="changecurve._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7.             <title></title>  
  8.             <link rel="stylesheet" href="d1.css" type="text/css" media="screen">  
  9.                 <link rel="stylesheet" href="d2.css" type="text/css" media="print">  
  10.                     <script src="changecurve.js" type="text/javascript" charset="utf-8"></script>  
  11.                     <script>  
  12.                 From Step 4 JavaScript copy from there and paste it here.  
  13.         </script>  
  14.                 </head>  
  15.                 <body>  
  16.                     <div id="holder"></div>  
  17.                 </body>  
  18.             </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 


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles