Shaded Ball in Random Colors Using HTML 5

Introduction

 
In this article, we are going to understand the shaded ball using HTML 5. In this application, you will come to know that the shaded ball is changing the color when the hit the URL again and again 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 shaded ball 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 new Webform
  • Rename it by ShadedBall.aspx
sd0.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#008080;  
  3.   color#800000;  
  4.   font300 x-large 'Comic Sans MS';  
  5.   font-stylenormal;  
  6. }  
  7. #section {  
  8.   height442px;  
  9.   left: 42%;  
  10.   margin-240px 0 0 -320px;  
  11.   positionabsolute;  
  12.   top: 57%;  
  13.   width842px;  
  14.   font-sizex-large;  
  15.   background-color#008000;  
  16. }  
  17. #duplicate {  
  18.   bottom: 0;  
  19.   font300 .7em "Helvetica Neue"Helvetica,;  
  20.   positionabsolute;  
  21.   right: 1em;  
  22.   text-alignright;  
  23. }  
  24. #duplicate d {  
  25.   color#fff;  
  26. }  
d2.css scripting.
  1. body {  
  2.   background#fff;  
  3.   color#000;  
  4.   font100.1% "Comic Sans MS", Lucida, Verdanasans-serif;  
  5. }  
  6. #section {  
  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. For understanding the full working of JavaScript download the attached .rar file and run the ShadedBall application. In this given JavaScript file we create a function by which we set an ellipse and put the co-ordinates to show the ellipse in the proper size. 
 
ellipse(a, b + c - c / 5, c, c / 2).attr({ fill: "rhsb(" + dee + ", 1, .25)-hsb(" + dee + ", 1, .25)", stroke: "none", opacity: 0 })
 
Where a,b,c r the co-ordinates which is pass in the function (function (a, b, c, dee) ).
 
After that, we use the function that will create a circle and every time you open the application it changes its color by generating the random colors by changing the random no. by using the Math class and random function (Math.random()).
 
Col.ball(x, y, r, Math.random());
 
The whole JavaScript looks as follows.
  1. < script >  
  2.  Circle.fn.ball = function(a, b, c, dee) {  
  3.   dee = dee || 0;  
  4.   return this.set(  
  5.    this.ellipse(a, b + c - c / 5, c, c / 2).attr({  
  6.     fill: "rhsb(" + dee + ", 1, .25)-hsb(" + dee + ", 1, .25)",  
  7.     stroke: "none",  
  8.     opacity: 0  
  9.    }),  
  10.    this.ellipse(a, b, c, c).attr({  
  11.     fill: "c(.5,.9)hsb(" + hue + ", 1, .75)-hsb(" + dee + ", .5, .25)",  
  12.     stroke: "none"  
  13.    }),  
  14.    this.ellipse(a, b, c - c / 5, c - c / 20).attr({  
  15.     stroke: "none",  
  16.     fill: "r(.5,.1)#ccc-#ccc",  
  17.     opacity: 0  
  18.    })  
  19.   );  
  20.  };  
  21. window.onload = function() {  
  22.  var Col = Circle("section"),  
  23.   a = 310,  
  24.   b = 180,  
  25.   c = 150;  
  26.  Col.ball(a, b, c, Math.random());  
  27. }; <  
  28. /script>  
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 Shadedball.aspx page.
  1. <body>  
  2.     <center>  
  3.         <h2>Shaded ball in HTML 5</h2>  
  4.         <hr />  
  5.         <div id="section"></div>  
  6.     </center>  
  7. </body>  
Step 5: The Complete code for the ShadedBall application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShadedBall.aspx.cs" Inherits="shadedball._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.         <title>Ball</title>  
  7.         <link rel="stylesheet" href="d1.css" type="text/css" media="screen">  
  8.             <link rel="stylesheet" href="d2.css" type="text/css" media="print">  
  9.                 <script src="ball.js" type="text/javascript" charset="utf-8"></script>  
  10.                 <script>  
  11.           Circle.fn.ball = function (a, b, c, dee) {  
  12.              dee = dee || 0;  
  13.               return this.set(  
  14.                this.ellipse(a, b + c - c / 5, c, c / 2).attr({ fill: "rhsb(" + dee + ", 1, .25)-hsb(" + dee + ", 1, .25)", stroke: "none", opacity: 0 }),  
  15.                 this.ellipse(a, b, c, c).attr({ fill: "c(.5,.9)hsb(" + dee + ", 1, .75)-hsb(" + dee + ", .5, .25)", stroke: "none" }),  
  16.                 this.ellipse(a, b, c - c / 5, c - c / 20).attr({ stroke: "none", fill: "c(.5,.1)#ccc-#ccc", opacity: 0 })  
  17.             );  
  18.           };  
  19.            window.onload = function () {  
  20.            var Col = Circle("holder"), a = 310, b = 180, c = 150;  
  21.            Col.ball(a, b, c, Math.random());  
  22.          };  
  23. </script>  
  24.             </head>  
  25.             <body>  
  26.                 <center>  
  27.                     <h2>Shaded ball in HTML 5</h2>  
  28.                     <hr />  
  29.                     <div id="section"></div>  
  30.                 </center>  
  31.             </body>  
  32.         </html>  
Step 6: Output Press F5
 
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. As you refresh your browser the color of the ball will change randomly, same as you can see in the following images.
 
sd1.gif
 
sd2.gif
 
sd4.gif
 
sd3.gif