HTML5 Canvas Twirl Sphere

Tutorials

Today, I would like to tell about creation of animated Twirl Sphere (I modified our previous 3D sphere, and, I used same way of accessing pixels of canvas). Our sphere goes around the canvas continuously. This example is cross browser solution (because of html5).

 

In the result, you should to get something like this:

HTML5 Canvas Twirl Sphere

Here are our demo and downloadable package:

Live Demo
download in package

Ok, please download our source files and let’s start coding !


Step 1. HTML

This is markup of our result page. Here it is:

index.html

1 <div class="container">
2     <canvas id="slideshow" width="1024" height="630"></canvas>
3     <canvas id="obj" width="256" height="256"></canvas>
4 </div>

I prepared two canvas objects: first one for resource image, and second one – for our Twirl sphere.

Step 2. CSS

css/main.css

01 .container {
02     height630px;
03     margin50px auto;
04     positionrelative;
05     width1024px;
06     z-index1;
07 }
08 #obj {
09     positionabsolute;
10     z-index2;
11 }

We should keep our Sphere object above our main canvas.

Step 3. JS

And now, our main js script file. Here it is:

js/script.js

01 var canvas, ctx;
02 var canvasObj, ctxObj;
03 var iDstW = 256;
04 var iDstH = 256;
05 var iXSpeed = 4;
06 var iYSpeed = 3;
07 var iLastX = iDstW / 2;
08 var iLastY = iDstH / 2;
09 var oImage;
10 var aMap = [];
11 var aMapT = [];
12 var aBitmap;
13 var mathTwirl = function(px,py) {
14     var x = px - iDstW / 2;
15     var y = py - iDstH / 2;
16     var r = Math.sqrt(x * x + y * y);
17     var maxR = iDstW / 2;
18     if (r > maxR) return {'x':px, 'y':py, 't': 1};
19     var a = Math.atan2(y, x);
20     a -= 1 - r / maxR;
21     var dx = Math.cos(a) * r;
22     var dy = Math.sin(a) * r;
23     return {'x': dx+iDstW/2, 'y': dy+iDstH/2, 't': 1.5}
24 }
25 window.onload = function(){
26     // load background
27     oImage = new Image();
28     oImage.src = 'images/bg.jpg';
29     oImage.onload = function () {
30         // creating canvas and context objects
31         canvas = document.getElementById('slideshow');
32         ctx = canvas.getContext('2d');
33         canvasObj = document.getElementById('obj');
34         ctxObj = canvasObj.getContext('2d');
35         // clear context
36         ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
37         // and draw source image
38         ctx.drawImage(oImage, 0, 0);
39         aBitmap = ctx.getImageData(0, 0, iDstW, iDstH);
40         for (var y = 0; y < iDstH; y++) {
41             for (var x = 0; x < iDstW; x++) {
42                 var t = mathTwirl(x, y);
43                 aMap[(x + y * iDstH) * 2 + 0] = Math.max(Math.min(t.x, iDstW - 1), 0);
44                 aMap[(x + y * iDstH) * 2 + 1] = Math.max(Math.min(t.y, iDstH - 1), 0);
45                 aMapT[(x + y * iDstH)] = t.t;
46             }
47         }
48         // begin updating scene
49         updateScene();
50     };
51     function updateScene() {
52         // update last coordinates
53         iLastX = iLastX + iXSpeed;
54         iLastY = iLastY + iYSpeed;
55         // reverse speed
56         if (iLastX + 1 > ctx.canvas.width - iDstW/2) {
57             iXSpeed = -4;
58         }
59         if (iLastX - 1 < iDstW/2) {
60             iXSpeed = 4;
61         }
62         if (iLastY + 1 > ctx.canvas.height - iDstH/2) {
63             iYSpeed = -3;
64         }
65         if (iLastY - 1 < iDstH/2) {
66             iYSpeed = 3;
67         }
68         // shifting of the second object
69         canvasObj.style.left = iLastX - Math.floor(iDstW / 2) + 'px';
70         canvasObj.style.top = iLastY - (Math.floor(iDstH / 2)) + 'px';
71         // draw result Twirl sphere
72         var aData = ctx.getImageData(iLastX - Math.ceil(iDstW / 2), iLastY - Math.ceil(iDstH / 2), iDstW, iDstH + 1);
73         for (var j = 0; j < iDstH; j++) {
74             for (var i = 0; i < iDstW; i++) {
75                 var u = aMap[(i + j * iDstH) * 2];
76                 var v = aMap[(i + j * iDstH) * 2 + 1];
77                 var t = aMapT[(i + j * iDstH)];
78                 var x = Math.floor(u);
79                 var y = Math.floor(v);
80                 var kx = u - x;
81                 var ky = v - y;
82                 for (var c = 0; c < 3; c++) {
83                     aBitmap.data[(i + j * iDstH) * 4 + c] =
84                       (aData.data[(x + y * iDstH) * 4 + c] * (1 - kx) + aData.data[((x + 1) + y * iDstH) * 4 + c] * kx) * (1-ky) * t +
85                       (aData.data[(x + (y + 1) * iDstH) * 4 + c] * (1 - kx) + aData.data[((x + 1) + (y + 1) * iDstH) * 4 + c] * kx) * (ky) * t;
86                 }
87             }
88         }
89         ctxObj.putImageData(aBitmap,0,0);
90         // update timer
91         setTimeout(updateScene, 16);
92     }
93 };

During initialization, the script is preparing two canvas objects and two contexts. After, it loads background image, and draw it at our first context. After it prepares hash table of sphere transformations: aMap (with using of new mathTwirl function). And, in the end – it starts timer which updates the main scene. This function (updateScene) updates coordinates of our Sphere object, and draws updated sphere at our second context.


Live Demo
download in package

Conclusion

I hope that today’s Twirl HTML5 Sphere lesson has been interesting for you. We have done another one nice html5 example. I will be glad to see your thanks and comments. Good luck!

Rate article