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.
Open File menu ->select new ->Choose Website then.
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
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
- <style>
- body
- {
- margin: 0px;
- padding: 0px;
- font-family: Comic Sans MS;
- outline-color: Yellow;
- }
- #Bubble
- {
- border: 2px solid #9C9898;
- margin-top: 50px;
- margin-left: 60px;
- background-color: #00B2EE;
- box-shadow: 5px 5px 8px #222;
- }
- .title
- {
- text-align: center;
- font-family: Segoe UI Light, Arial, Helvetica;
- font-size: 2.2em;
- margin: 1em;
- }
- .info
- {
- text-align: center;
- font-family: Segoe UI Light, Arial, Helvetica;
- font-size: 1.2em;
- margin: 0.25em;
- }
- </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.
- <script>
- window.requestAnimFrame = (function (callback)
- {
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function (callback)
- {
- window.setTimeout(callback, 1000 / 60);
- };
- })();
- function animate(canvas)
- {
- var context = canvas.getContext("2d");
- var date = new Date();
- var time = date.getTime();
-
- var widthScale = Math.sin(time / 200) * 0.1 + 0.9;
- var heightScale = -1 * Math.sin(time / 200) * 0.1 + 0.9;
-
- context.clearRect(0, 0, canvas.width, canvas.height);
-
- context.beginPath();
- context.save();
- context.translate(canvas.width / 2, canvas.height / 2);
- context.scale(widthScale, heightScale);
- context.arc(0, 0, 65, 0, 2 * Math.PI, false);
- context.restore();
- context.fillStyle = "#FFF68F";
- context.fill();
- context.lineWidth = 2;
- context.strokeStyle = "#555";
- context.stroke();
- context.beginPath();
- context.save();
- context.translate(canvas.width / 2, canvas.height / 2);
- context.scale(widthScale, heightScale);
- context.arc(-30, -30, 15, 0, 2 * Math.PI, false);
- context.restore();
- context.fillStyle = "white";
- context.fill();
-
- requestAnimFrame(function ()
- {
- animate(canvas);
- });
- }
- window.onload = function ()
- {
- var canvas = document.getElementById("Bubble");
- animate(canvas);
- };
- </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.
- <body style="background-color: #D1D1D1"><center>
- <h1>Oscillating Bubble </h1></center>
- <hr />
- <canvas id="Bubble" width="500" height="200">
- </canvas>
- </center>
- </body>
Step 5: The complete code for the OscillatingBubble application.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="oscillatingbubble.aspx.cs" Inherits="bouncinground._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">
- <style>
- Copy and paste step 2 here
- <script>
- Copy and paste step 3 here
- </script>
- </head>
- <body style="background-color: #D1D1D1"><center>
- <h1>Oscillating Bubble </h1></center>
- <hr />
- <canvas id="Bubble" width="500" height="200">
- </canvas>
- </center>
- </body>
- </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.
This is the second phase of the Bubble.
As above given, the process will repeatedly perform the same.
Here are some useful resources