Our new tutorial tells us about creation of animated 3D Sphere (through direct access to pixels of canvas). The sphere itself is getting around the canvas continuously. This example should work in the most of modern browsers (like Firefox, Chrome, Safari and even in IE).
In the result, you should to get something like this:
Here are our demo and downloadable package:
Ok, download the source files and lets 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 = "631" ></ canvas > |
3 |
< canvas id = "obj" width = "256" height = "256" ></ canvas > |
I prepared 2 canvas objects here: first one for source image, and the second one – to our Sphere.
Step 2. CSS
css/main.css
We should put our Sphere object above our main canvas.
Step 3. JS
js/script.js
02 |
var canvasObj, ctxObj; |
07 |
var iLastX = iDstW / 2; |
08 |
var iLastY = iDstH / 2; |
12 |
var mathSphere = function (px, py) { |
13 |
var x = px - iDstW / 2; |
14 |
var y = py - iDstH / 2; |
15 |
var r = Math.sqrt(x * x + y * y); |
17 |
if (r > maxR) return { 'x' :px, 'y' :py}; |
18 |
var a = Math.atan2(y, x); |
19 |
var k = (r / maxR) * (r / maxR) * 0.5 + 0.5; |
20 |
var dx = Math.cos(a) * r * k; |
21 |
var dy = Math.sin(a) * r * k; |
22 |
return { 'x' : dx + iDstW / 2, 'y' : dy + iDstH / 2}; |
24 |
window.onload = function (){ |
27 |
oImage.src = 'images/bg.jpg' ; |
28 |
oImage.onload = function () { |
30 |
canvas = document.getElementById( 'slideshow' ); |
31 |
ctx = canvas.getContext( '2d' ); |
32 |
canvasObj = document.getElementById( 'obj' ); |
33 |
ctxObj = canvasObj.getContext( '2d' ); |
35 |
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
37 |
ctx.drawImage(oImage, 0, 0); |
38 |
aBitmap = ctx.getImageData(0, 0, iDstW, iDstH); |
39 |
for ( var y = 0; y < iDstH; y++) { |
40 |
for ( var x = 0; x < iDstW; x++) { |
41 |
var t = mathSphere(x, y); |
42 |
aMap[(x + y * iDstH) * 2 + 0] = Math.max(Math.min(t.x, iDstW - 1), 0); |
43 |
aMap[(x + y * iDstH) * 2 + 1] = Math.max(Math.min(t.y, iDstH - 1), 0); |
49 |
function updateScene() { |
51 |
iLastX = iLastX + iXSpeed; |
52 |
iLastY = iLastY + iYSpeed; |
54 |
if (iLastX > ctx.canvas.width - iDstW/2) { |
57 |
if (iLastX < iDstW/2) { |
60 |
if (iLastY > ctx.canvas.height - iDstH/2) { |
63 |
if (iLastY < iDstH/2) { |
67 |
canvasObj.style.left = iLastX - Math.floor(iDstW / 2) + 'px' ; |
68 |
canvasObj.style.top = iLastY - (Math.floor(iDstH / 2)) + 'px' ; |
70 |
var aData = ctx.getImageData(iLastX - Math.ceil(iDstW / 2), iLastY - Math.ceil(iDstH / 2), iDstW, iDstH + 1); |
71 |
for ( var j = 0; j < iDstH; j++) { |
72 |
for ( var i = 0; i < iDstW; i++) { |
73 |
var u = aMap[(i + j * iDstH) * 2]; |
74 |
var v = aMap[(i + j * iDstH) * 2 + 1]; |
75 |
var x = Math.floor(u); |
76 |
var y = Math.floor(v); |
79 |
for ( var c = 0; c < 4; c++) { |
80 |
aBitmap.data[(i + j * iDstH) * 4 + c] = |
81 |
(aData.data[(x + y * iDstH) * 4 + c] * (1 - kx) + aData.data[((x + 1) + y * iDstH) * 4 + c] * kx) * (1-ky) + |
82 |
(aData.data[(x + (y + 1) * iDstH) * 4 + c] * (1 - kx) + aData.data[((x + 1) + (y + 1) * iDstH) * 4 + c] * kx) * (ky); |
86 |
ctxObj.putImageData(aBitmap,0,0); |
88 |
setTimeout(updateScene, 16); |
During initialization, the script is preparing two canvas objects and two contexts. Then, it loads our main background image, and draw it at our first context. After it prepares hash table of sphere transformations: aMap. And, in the end – it starts timer which updates main scene. In this function (updateScene) we update coordinates of our Sphere object, and draw updated sphere at our second context.
Conclusion
I hope that today’s 3D 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!