Quick update: Thanks for the endorsement Mr. doob! I’m honored
Here is a web site with a cool Javascript effect by Mr. doob, recently posted on HN & Reddit. I’m trying to get better at my Javascript so here’s my dissection of this interesting effect, if you’re interested in learning how this is done.
First, create a HTML 5 canvas element (highly recommended short read about canvas here). Make its width and height the same size as window.innerWidth and window.innerHeight so that it fills up the content area of the browser window. Append the canvas element to the document body.
var canvas = document.createElement( 'canvas' ); canvas.width = window.innerWidth; canvas.height = window.innerHeight; canvas.style.display = 'block'; document.body.appendChild( canvas );
As of now, there’s only a 2D context to pick from. In future, there might be a 3D context based on OpenGL ES (quote). So just get the context with getContext, and then create the image element.
var context = canvas.getContext( '2d' ); var image = document.createElement( 'img' );
Now let’s add an event handler to the image for when the image loads. ‘this’ refers to the image element itself. bitmapWidthHalf and bitmapHeightHalf is exactly what it means: half the length of the image’s width and height respectively. Math.floor is used to round the result of the division down to the nearest integer.
(more…)
