Canvas Sphere Animation Using HTML 5

Introduction

 
In this article, we are going to understand canvas sphere animation using HTML 5. In this section, you will see a 2D projection of the 3D points of a sphere using a tiny sprite 3D engine when the application is running 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 CanvasSphereApplication 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
 
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 to sphereanimation.aspx
cs2.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.   background-color: #1E90FF;  
  4.   margin: 0px;  
  5.   padding: 0px;  
  6. }  
  7.   
  8. sphere {  
  9.   background-color: #282828;  
  10. }  
  11.   
  12. .title {  
  13.   text-align: center;  
  14.   font-family: Segoe UI Light, Arial, Helvetica;  
  15.   font-size: 2.2em;  
  16.   margin: 1em;  
  17. }  
  18.   
  19. .info {  
  20.   text-align: center;  
  21.   font-family: Segoe UI Light, Arial, Helvetica;  
  22.   font-size: 1.2em;  
  23.   margin: 0.25em;  
  24. }  
  25. </style> 
Step 3: In this part, we need to work with some JavaScript. To fully understand how JavaScript works, download the attached .rar file and run the CanvasSphereApplication application.
 
The JavaScript sphere.js looks as in the following:
  1. Sphere = (function() {  
  2.  var canvas;  
  3.  var ctx;  
  4.  var offsetX = 300;  
  5.  var offsetY = 300;  
  6.  var objectsInScene = new Array();  
  7.  var focalLength = 300;  
  8.  var cameraView = {  
  9.   x: 0,  
  10.   y: 0,  
  11.   z: 0,  
  12.   rotX: 0,  
  13.   rotY: 0,  
  14.   rotZ: 0  
  15.  };  
  16.  var space = 15;  
  17.  var PI = Math.PI;  
  18.  var radius = 200;  
  19.  var radian = PI / 180;  
  20.  var mouseX = 0;  
  21.  var mouseY = 0;  
  22.  var objectRadius = 14;  
  23.  var scaleRatio = 0;  
  24.  var scaling = true;  
  25.  var nrOfFollowers = 32;  
  26.  var followRadius = 8;  
  27.  var minDistToLeader = 20;  
  28.  return function() {  
  29.   this.createSphere = function() {  
  30.    canvas = document.getElementById("sphere");  
  31.    ctx = canvas.getContext("2d");  
  32.    if (canvas.getContext) {  
  33.     this.init();  
  34.    }  
  35.   }  
  36.   this.init = function() {  
  37.    for (var i = space; i < 180; i += space) {  
  38.     for (var angle = 0; angle < 360; angle += space) {  
  39.      var object = {};  
  40.      var x = Math.sin(radian * i) * radius;  
  41.      object.x = Math.cos(angle * radian) * x;  
  42.      object.y = Math.cos(radian * i) * radius;  
  43.      object.z = Math.sin(angle * radian) * x;  
  44.      object.glow = .5;  
  45.      object.type = "sphere";  
  46.      objectsInScene.push(object);  
  47.     }  
  48.    }  
  49.    if (distx < minDistToLeader && disty < minDistToLeader && distz < minDistToLeader) {  
  50.     object.leader.glow = 1.0;  
  51.     this.assignLeader(object);  
  52.    }  
  53.   }  
  54.   this.createSphere();  
  55.  }  
  56. })(); 
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 sphereanimation.aspx page. Here we pass a Canvas in the canvas tag. 
  1. <body onload="new Sphere();">  
  2. <center>  
  3. <h1> Canvas Sphere In HTML 5 </h1>  
  4. <hr>  
  5.     <div align="center">  
  6.         <canvas id="sphere" width="600" height="580"></canvas>  
  7.     </div>  
  8.     </center>  
  9. </body> 
Step 5: The complete code for the CanvasSphereApplication application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sphereanimation.aspx.cs" Inherits="CanvasSphereApplication._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.     <title>Canvas sphere</title>  
  6.     <style>  
  7.     </style>  
  8.     <script language="JavaScript" src="sphere.js"></script>  
  9.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
  10.     <style type="text/css">   
  11.     </style>  
  12. </head>  
  13. <body onload="new Sphere();">  
  14. <center>  
  15. <h1> Canvas Sphere In HTML 5 </h1>  
  16. <hr>  
  17.     <div align="center">  
  18.         <canvas id="sphere" width="600" height="580"></canvas>  
  19.     </div>  
  20.     </center>  
  21. </body>  
  22. </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 a 2D projection of the 3D points of a sphere using a tiny sprite 3D engine, while the application is running in the browser.
 
cs.gif
 
cs1.gif
 
Here are some useful resources