Bouncing Ball a Particular Point Using HTML5

Introduction

This is a simple application for beginners that shows how to create a bouncing ball at a particular point using HTML 5 and CSS tools. We know that HTML 5 is the advanced version of HTML. Basically HTML 5 can be used to develop 3D applications. This article is intended to help with the use of HTML5 tools to develop a bouncing ball in a particular point application. CSS is the acronym for Cascading Style Sheet that is used for design. CSS defines how HTML elements are to be displayed. Canvas is an important tag of a HTML 5 that is used to show the image and text in a HTML 5 application. I hope this article helps to make a bouncing ball at a particular point using HTML 5 and CSS tools.

Step 1 : First Open a HTML editor such as Notepad.

  • Open start->Notepad.
  • The name of editor is "Bounce".

notpad.gif

Step 2 :
Create a Folder on a desktop.

  • Right-click on desktop->new->Folder.
  • Name of folder is "Ball".

folder.gif

Step 3 :
Open Visual Studio.

  • Go to file -> New->Projects.
  • Crete an ASP. NET Web Application.
  • Name of "bounceDemo.aspx".

new.gif

webapplication.gif

Step 4 :
Add a HTML file to your web application.

  • Right-click on Solution Explorer.
  • Add->add new item->HTML form.
  • The Name of the HTML form is "bounce.html".

html.gif

Step 5 :
Now in this step we define a CSS file with the name P2.css. In CSS we define all properties of a holder, body and copy class; that is height, width, margin, position, font, color etc.

Code

body
{
    background: #fff;
    color: #FF99CC;
    font: 100.1% "Lucida Grande";
}
#holder
{
    height: 500px;
    left: 70%;
    margin: 0 0 0 -320px;
    position:fixed;
    top: 0;
    width: 640px;
}
#copy
{
    bottom: 1;
    font-size: .8em;
    position:fixed;
    right: 2em;
    text-align:center;
}


Step 6 :
Now we define a left side circle position that sets the alignment of a circle.

Code

var ptx = ctx.set();
   targets.push(ctx.circle(400, 200, 20),
   ctx.circle(400, 250, 40),
   ctx.circle(400, 200, 20),
   ctx.circle(400, 250, 20),
   ctx.circle(400, 400, 20),
   ctx.circle(400, 450, 20),
   ctx.circle(400, 400, 20);
   ctx.circle(400, 450, 20));

Step 7 : Now we have define the inside text of a circle.

Code

ptx.push(ctx.text(440, 200, "C"),
    ctx.text(440, 250, "C++"),
    ctx.text(440, 200, "JAVA"),
    ctx.text(440, 250, "C#"),
    ctx.text(440, 400, "ASP.NET"),
    ctx.text(440, 450, "WPF"),
    ctx.text(440, 400, "WCF"),
    ctx.text(440, 450, "Silverlight"));
 

Step 8 : Now in this step we define a function; how to fill a color in a circle.

Code

  rxt[0].click(function (
      this.cx = this.cx || 400;
      this.animate({ cx: this.cx, "stroke-width": this.cx / 200, fill: this.cx - 200 ?
      "hsb(.1, .85, .85)"
: "#FF99CC", "fill
      opacity"
: +!!(this.cx - 200) }, 2000);
      this.cx = this.cx == 400 ? 200 : 400;
                }
);
       rxt[2].click(function ()
{
      this.cx = this.cx || 400;
      this.animate({ cx: this.cx, "stroke-width": this.cx / 200, fill: this.cx - 200 ?
     "hsb(.2, .85, .85)"
: "#FF99CC", "fill
      opacity"
: +!!(this.cx - 200) }, 2000, "C++");
      this.cx = this.cx == 400 ? 200 : 400;
              }
);
}

Step 9 : The complete code of a bouncing ball application is as follows:

Code   

<html>
  <head>
  <title>Tom application</title>
  <link rel="stylesheet" href="P2.css" type="text/css" media="screen"/>
 
<script src="Scripts/R2.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript">
   window.onload = function ()
{
   var ctx = Tom("holder", 800, 600);
   var ptr = ctx.set();
  
var ptx = ctx.set();
   targets.push(ctx.circle(400, 200, 20),
   ctx.circle(400, 250, 40),
   ctx.circle(400, 200, 20),
   ctx.circle(400, 250, 20),
   ctx.circle(400, 400, 20),
   ctx.circle(400, 450, 20),
   ctx.circle(400, 400, 20);
   ctx.circle(400, 450, 20));
   ptx.attr({ font: "22px Fontin-Sans, Arial", fill: "#fff", "text-anchor": "start" });
   var rxt = ctx.set();
 
  ptx.push(ctx.text(440, 200, "C"),
   ctx.text(440, 250, "C++"),
   ctx.text(440, 200, "JAVA"),
   ctx.text(440, 250, "C#"),
   ctx.text(440, 400, "ASP.NET"),
   ctx.text(440, 450, "WPF"),
   ctx.text(440, 400, "WCF"),
   ctx.text(440, 450, "Silverlight"));
 
   rxt[0].click(function (
      this.cx = this.cx || 400;
      this.animate({ cx: this.cx, "stroke-width": this.cx / 200, fill: this.cx - 200 ?
      "hsb(.1, .85, .85)"
: "#FF99CC", "fill
      opacity"
: +!!(this.cx - 200) }, 2000);
      this.cx = this.cx == 400 ? 200 : 400;
                }
     );
       rxt[2].click(function ()
    {
      this.cx = this.cx || 400;
      this.animate({ cx: this.cx, "stroke-width": this.cx / 200, fill: this.cx - 200 ?
     "hsb(.2, .85, .85)"
: "#FF99CC", "fill
      opacity"
: +!!(this.cx - 200) }, 2000, "C++");
      this.cx = this.cx == 400 ? 200 : 400;
              }
      );
 }
 
</script>
   </head>
   <body bgcolor="#ff66cc">
   <div id="holder" style="background-color: #804040"></div>
  </body>
</html>

Step 10  :  Press Ctrl + F5 to run the application in a browser.

  out.jpg

 out1.jpg

 out2.jpg

 out3.gif

Resources

Here is some useful resources :

Up Next
    Ebook Download
    View all
    Learn
    View all