Oscillating Bubble Using HTML 5

Introduction

 
In this article, we are going to understand oscillating bubbles using HTML 5. In this section, an oscillating bubble will grow and shrink while displaying 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 OscillatingBubble application can be created. To do so use 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 oscillatingbubble.aspx
osc.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 the CSS is used for design purposes.
 
CSS Script 
  1. <style>  
  2. body  
  3. {  
  4.   margin0px;  
  5.   padding0px;  
  6.   font-family: Comic Sans MS;  
  7.   outline-color: Yellow;  
  8. }  
  9. #Bubble  
  10. {  
  11.    border2px solid #9C9898;  
  12.    margin-top50px;  
  13.    margin-left60px;  
  14.    background-color#00B2EE;  
  15.    box-shadow: 5px 5px 8px #222;  
  16.  }  
  17. .title  
  18. {  
  19.    text-aligncenter;  
  20.    font-family: Segoe UI Light, ArialHelvetica;  
  21.    font-size2.2em;  
  22.    margin1em;  
  23. }  
  24. .info  
  25. {  
  26.    text-aligncenter;  
  27.    font-family: Segoe UI Light, ArialHelvetica;  
  28.    font-size1.2em;  
  29.    margin0.25em;  
  30. }  
  31. </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 OscillatingBubble application.
 
The whole JavaScript looks as in the following.
  1. <script>  
  2.         window.requestAnimFrame = (function (callback)  
  3.           {  
  4.          return window.requestAnimationFrame ||  
  5.                   window.webkitRequestAnimationFrame ||  
  6.                   window.mozRequestAnimationFrame ||  
  7.                   window.oRequestAnimationFrame ||  
  8.                   window.msRequestAnimationFrame ||  
  9.          function (callback)  
  10.           {  
  11.                  window.setTimeout(callback, 1000 / 60);  
  12.            };  
  13.        })();  
  14.         function animate(canvas)  
  15.         {  
  16.           var context = canvas.getContext("2d");  
  17.           var date = new Date();  
  18.           var time = date.getTime();  
  19.             // update  
  20.           var widthScale = Math.sin(time / 200) * 0.1 + 0.9;  
  21.           var heightScale = -1 * Math.sin(time / 200) * 0.1 + 0.9;  
  22.             // clear  
  23.            context.clearRect(0, 0, canvas.width, canvas.height);  
  24.            // draw  
  25.           context.beginPath();  
  26.           context.save();  
  27.           context.translate(canvas.width / 2, canvas.height / 2);  
  28.           context.scale(widthScale, heightScale);  
  29.           context.arc(0, 0, 65, 0, 2 * Math.PI, false);  
  30.           context.restore();  
  31.           context.fillStyle = "#FFF68F";  
  32.           context.fill();  
  33.           context.lineWidth = 2;  
  34.           context.strokeStyle = "#555";  
  35.           context.stroke();  
  36.           context.beginPath();  
  37.           context.save();  
  38.           context.translate(canvas.width / 2, canvas.height / 2);  
  39.           context.scale(widthScale, heightScale);  
  40.           context.arc(-30, -30, 15, 0, 2 * Math.PI, false);  
  41.           context.restore();  
  42.           context.fillStyle = "white";  
  43.           context.fill();  
  44.             // request new frame  
  45.           requestAnimFrame(function ()  
  46.            {  
  47.              animate(canvas);  
  48.            });  
  49.        }  
  50.         window.onload = function ()  
  51.          {  
  52.             var canvas = document.getElementById("Bubble");  
  53.             animate(canvas);  
  54.         };  
  55. </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 oscillatingbubble.aspx page. Here we pass a #Bubble in the canvas tag. 
  1. <body style="background-color: #D1D1D1"><center>  
  2.     <h1>Oscillating Bubble </h1></center>  
  3.    <hr />  
  4.         <canvas id="Bubble" width="500" height="200">  
  5.         </canvas>  
  6.         </center>  
  7.     </body> 
Step 5: The complete code for the OscillatingBubble application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="oscillatingbubble.aspx.cs" Inherits="bouncinground._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.     <style>  
  6.        Copy and paste step 2 here    
  7.     <script>  
  8.      Copy and paste step 3 here  
  9.     </script>  
  10.     </head>  
  11.     <body style="background-color: #D1D1D1"><center>  
  12.     <h1>Oscillating Bubble </h1></center>  
  13.    <hr />  
  14.         <canvas id="Bubble" width="500" height="200">  
  15.         </canvas>  
  16.         </center>  
  17.     </body>  
  18. </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 oscillating bubble will grow and shrink as we run the application in the browser.
 
This is the first phase of the Bubble.
 
osc 1.gif
 
This is the second phase of the Bubble.
 
osc 2.gif
 
As above given, the process will repeatedly perform the same.