Moving a Cursor on a Dedicated Path Using HTML 5

Introduction

 
In this article, we are going to understand the concept of moving a cursor on a dedicated path using HTML 5. In this, you will see that a cursor is moving on a dedicated path while 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 cursorchangepath application can be created. To do so go through 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 cursorchangepath.aspx
csr.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> tags.
 
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. {  
  3.    background#009ACD;  
  4.     color#fff;  
  5.     font300 100.1% "Helvetica Neue" , Helvetica"Arial Unicode MS" , Arialsans-serif;  
  6. }  
  7. #holder  
  8. {  
  9.     height442px;  
  10.     left: 42%;  
  11.     margin-240px 0 0 -320px;  
  12.     positionabsolute;  
  13.     top: 57%;  
  14.     width842px;  
  15.     font-sizex-large;  
  16.     background-color#008000;  
  17. }  
  18. #duplicate {  
  19.     bottom: 0;  
  20.     font300 .7em "Helvetica Neue"Helvetica, ;  
  21.     positionabsolute;  
  22.     right: 1em;  
  23.     text-alignright;  
  24. }  
  25. #duplicate d {  
  26.     color#fff;  
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;  
Step 3: In this part, we need to work on some JavaScript. To fully understanding how the JavaScript works download the attached .rar file and run the cursorchangepath application.
 
The whole JavaScript looks as in the following.
  1. Color("holder", 640, 480, function ()  
  2.           {  
  3.                 var r = this,  
  4.                 p = r.path("M295.186,122.908c12.434,18.149,32.781,18.149,45.215,0l12.152-17.736c12.434-18.149,122.908z").attr({ stroke: "#666", opacity: .3, "stroke-width": 10 }),  
  5.                 over = r.path().attr({ stroke: "#fff" }),  
  6.                 len = p.getTotalLength(),  
  7.                 e = r.ellipse(0, 0, 7, 3).attr({ stroke: "none", fill: "#fff" }).onAnimation(function ()  
  8.                  {  
  9.                     var t = this.attr("transform");  
  10.                     over.attr({ path: "M316,248L" + t[0][1] + "," + t[0][2] + "z" });  
  11.                  });  
  12.                r.circle(316, 248, 5).attr({ stroke: "none", fill: "#fff" });  
  13.                r.customAttributes.along = function (v)  
  14.                 {  
  15.                    var ppoint = p.getPointAtLength(v * len);  
  16.                    return {  
  17.                    transform: "t" + [point.x, point.y] + "r" + point.alpha  
  18.                 };  
  19.           };  
  20.             e.attr({ along: 0 });  
  21.             var rotateAlongThePath = true;  
  22.             function run()  
  23.                 {  
  24.                  e.animate({ along: 1 }, 2e4, function ()  
  25.                   {  
  26.                     e.attr({ along: 0 });  
  27.                     setTimeout(run);  
  28.                 });  
  29.             }  
  30.             run();  
  31.             // logo  
  32.             var logo = r.set(  
  33.             r.rect(13, 13, 116, 116, 30).attr({ stroke: "none", fill: "#fff", transform: "r45", opacity: .2 }),  
  34.             r.path("M129.657,71.361c0,3.812-1.105,7.451-3.153,10.563c-1.229,1.677-2.509,7.588c-3. 46.869").attr({ fill: "#f89938", stroke: "none", opacity: .5 }),  
  35.             r.circle(71, 32, 19).attr({ stroke: "none", fill: "#39f", opacity: .5 }));  
  36.             logo.transform("t245,177...");  
  37.             // logo end  
  38.         }); 
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 cursorchangepath.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.
  1. <body>  
  2.     <center>  
  3.         <h2>  
  4.             Changing Cursor Path on A dedicated path  
  5.         </h2>  
  6.     </center>  
  7.         <div id="holder"></div>  
  8. </body> 
Step 5 : The Complete code for the cursorchangepath application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="cursorchangepath.aspx.cs"Inherits="_Default" %>  
  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.     <meta charset="utf-8">  
  6.     <title></title>  
  7.     <link rel="stylesheet" href="d1.css" media="screen">  
  8.     <link rel="stylesheet" href="d2.css" media="print">  
  9.     <script src="javascript.js"></script>  
  10.     <script>  
  11.       From Step 3 JavaScript copy from there and paste it here.  
  12.       </script>  
  13. </head>  
  14. <body>  
  15.     <center>  
  16.         <h2>  
  17.             Changing Cursor Path on A dedicated path  
  18.         </h2>  
  19.     </center>  
  20.         <div id="holder"></div>  
  21.     </body>  
  22. </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 will see here that the cursor is moving on a dedicated path.
 
csr1.gif
 
Here you can see the cursor is moving from its position.