Working With Spinning Points in HTML 5

Introduction

 
In this article, we are going to have a very interesting section related to designing. In this, the pointer has a radius of 60 and appears to spin while you run the application 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 Spinningpoints 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 a new Webform
  • Rename it to Spinningpoint.aspx
sp.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 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 as added in the Spinningpoints.rar file. Here is the media script.
 
Script for the Media screen
  1. <style media="screen">  
  2.         html  
  3.         {  
  4.             height100%;  
  5.         }  
  6.         body  
  7.         {  
  8.             color#222;  
  9.             font20px/1.4 "Lucida Grande" , Lucida, VerdanaHelveticasans-serif;  
  10.             height100%;  
  11.         }  
  12.         #holder  
  13.         {  
  14.             height300px;  
  15.             left: 0;  
  16.             overflowhidden;  
  17.             positionabsolute;  
  18.             top: 0;  
  19.             width300px;  
  20.             margin0;  
  21.         }  
  22.         #form  
  23.         {  
  24.             height300px;  
  25.             left: 50%;  
  26.             margin-150px 0 0 -300px;  
  27.             overflowhidden;  
  28.             positionabsolute;  
  29.             top: 50%;  
  30.             width600px;  
  31.         }  
  32.         form  
  33.         {  
  34.             color#fff;  
  35.             left: 300px;  
  36.             margin0;  
  37.             padding0;  
  38.             positionabsolute;  
  39.             top: 0;  
  40.             width300px;  
  41.         }  
  42.         dt  
  43.         {  
  44.             clearleft;  
  45.             floatleft;  
  46.             height2em;  
  47.             width150px;  
  48.         }  
  49.         dd  
  50.         {  
  51.             floatleft;  
  52.             height2em;  
  53.             margin0;  
  54.             padding0;  
  55.         }  
  56.         input  
  57.         {  
  58.             font-size1em;  
  59.             width4em;  
  60.         }  
  61.         p  
  62.         {  
  63.             fontitalic 20px/1.4 "Hoefler Text" , Georgia, serif;  
  64.             height100%;  
  65.             margin-3em;  
  66.             width750px;  
  67.         }  
  68.         #copy  
  69.         {  
  70.             bottom: auto;  
  71.             color#fff;  
  72.             font-size: .5em;  
  73.             right: 4em;  
  74.             top: 300px;  
  75.         }  
  76.     </style> 
Step 3 : In this part we need to work on some JavaScript. For fully understanding how the JavaScript works, download the attached .rar file and run the Spinningpoints application.
 
The whole JavaScript looks as in the following
  1. window.onload = function ()  
  2. {  
  3.        var remove = spinner("holder", 70, 120, 12, 25, "#fff");  
  4.        var form =  
  5.        {  
  6.               form: document.getElementsByTagName("form")[0],  
  7.               r1: document.getElementById("radius1"),  
  8.               r2: document.getElementById("radius2")  
  9.        };  
  10.        form.form.onsubmit = function ()  
  11.        {  
  12.               remove();  
  13.               remove = spinner("holder", +form.r1.value, +form.r2.value);  
  14.                return false;  
  15.        };  
  16. };  
  17. function spinner(holderid, R1, R2, count, stroke_width, colour)  
  18. {  
  19.        var sectorsCount = count || 12,  
  20.         color = colour || "#fff",  
  21.         width = stroke_width || 15,  
  22.         r1 = Math.min(R1, R2) || 35,  
  23.         r2 = Math.max(R1, R2) || 60,  
  24.         sectors = [],  
  25.         opacity = [],  
  26.         beta = 2 * Math.PI / sectorsCount,  
  27.         pathParams = { stroke: color, "stroke-width": width, "stroke-linecap""round" };  
  28. }  
  29. var tick;  
  30. (function ticker()  
  31. {  
  32.         opacity.unshift(opacity.pop());  
  33.         for (var i = 0; i < sectorsCount; i++)  
  34.         {  
  35.                sectors[i].attr("opacity", opacity[i]);  
  36.         }  
  37.         tick = setTimeout(ticker, 1000 / sectorsCount);  
  38. })();  
  39. return function ()  
  40. {  
  41.         clearTimeout(tick);  
  42.         r.remove();  
  43. };  
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 Spinningpoint.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.
  1. <body>  
  2.     <center><h2>SPINNING THE POINTS </h2><hr />  
  3.         <div id="form">  
  4.             <div id="holder"></div>  
  5.             <form action="#">  
  6.                 <dl>  
  7.                     <dt><label for="radius1">Radius 1</label></dt>  
  8.                     <dd><input type="text" value="60" id="radius1"></dd>  
  9.                     <dt><label for="radius2">Radius 2</label></dt>  
  10.                     <dd><input type="text" value="100" id="radius2"></dd>  
  11.                     <dd><input type="submit" value="Update"></dd>  
  12.                 </dl>  
  13.             </form>  
  14.         </div>  
  15.         </center>  
  16.     </body> 
Step 5 : The complete code for the Spinningpoints application is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Spinningpoint.aspx.cs" Inherits="Spinningpoints._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" type="text/css" media="screen">  
  8.         <link rel="stylesheet" href="d1.css" type="text/css" media="print">  
  9.         <script src="javascript.js"></script>  
  10.         <style media="screen">  
  11.         copy n paste the step 2 script here.  
  12.     </style>  
  13.     </head>  
  14.     <body>  
  15.     <center><h2>SPINNING THE POINTS </h2><hr />  
  16.         <div id="form">  
  17.             <div id="holder"></div>  
  18.             <form action="#">  
  19.                 <dl>  
  20.                     <dt><label for="radius1">Radius 1</label></dt>  
  21.                     <dd><input type="text" value="60" id="radius1"></dd>  
  22.                     <dt><label for="radius2">Radius 2</label></dt>  
  23.                     <dd><input type="text" value="100" id="radius2"></dd>  
  24.                     <dd><input type="submit" value="Update"></dd>  
  25.                 </dl>  
  26.             </form>  
  27.         </div>  
  28.         </center>  
  29.     </body>  
  30. </html> 
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see the pointer having a radius of 60 appears to spin while you run the application in the browser.
 
sp1.gif
 
You can change the Radius 1 and Radius 2 values. After changing the values the points look as below.
 
sp5.gif
 
Here are some useful resources


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles