Shaded Ball in Random Colors Using HTML 5

Introduction

In this article we are going to understand the shaded ball using the 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 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 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

body
{
    background: #008080;
    color: #800000;
    font: 300 x-large 'Comic Sans MS';
    font-style: normal;
 }
#section
{
    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;
}
#section {
    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 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 a 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 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 is looks like as follow.


<script>
    Circle.fn.ball = function (a, b, c, dee)
    {
      dee = dee || 0;
      return this.set(
                this.ellipse(a, b + c - c / 5, c, c / 2).attr({ fill: "rhsb(" + dee + ", 1, .25)-hsb(" + dee + ", 1, .25)", stroke: "none", opacity: 0 }),
                this.ellipse(a, b, c, c).attr({ fill: "c(.5,.9)hsb(" + hue + ", 1, .75)-hsb(" + dee + ", .5, .25)", stroke: "none" }),
                this.ellipse(a, b, c - c / 5, c - c / 20).attr({ stroke: "none", fill: "r(.5,.1)#ccc-#ccc", opacity: 0 })
            );
    };
      window.onload = function ()
      {
        var Col = Circle("section"), a = 310, b = 180, c = 150;
        Col.ball(a, b, c, Math.random());
      };
</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.

<body>
    <center>
    <h2>Shaded ball in HTML 5</h2>
    <hr />
        <div id="section"></div>
    </center>
</
body>

Step 5 : The Complete code for ShadedBall application.

<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShadedBall.aspx.cs" Inherits="shadedball._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">
      <title>Ball</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="ball.js" type="text/javascript" charset="utf-8"></script>
        <script>
          Circle.fn.ball = function (a, b, c, dee) {
             dee = dee || 0;
              return this.set(
               this.ellipse(a, b + c - c / 5, c, c / 2).attr({ fill: "rhsb(" + dee + ", 1, .25)-hsb(" + dee + ", 1, .25)", stroke: "none", opacity: 0 }),
                this.ellipse(a, b, c, c).attr({ fill: "c(.5,.9)hsb(" + dee + ", 1, .75)-hsb(" + dee + ", .5, .25)", stroke: "none" }),
                this.ellipse(a, b, c - c / 5, c - c / 20).attr({ stroke: "none", fill: "c(.5,.1)#ccc-#ccc", opacity: 0 })
            );
          };
           window.onload = function () {
           var Col = Circle("holder"), a = 310, b = 180, c = 150;
           Col.ball(a, b, c, Math.random());
         };
</script>
    </head>
    <body>
    <center>
    <h2>Shaded ball in HTML 5</h2>
    <hr />
        <div id="section"></div>
    </center>
    </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. As you refresh your browser the color of ball will change randomly, same as you can see in the the following images.

sd1.gif
sd2.gif
sd4.gif

sd3.gif

Here are some use resources

HTML 5 Color Picker
How do I Generate Random Number in C#?
Generating Random Number and String in C#

Up Next
    Ebook Download
    View all
    Learn
    View all