Joonas Lehtinen of IT Mill has shown us something that we should probably never do. As a proof of concept he decided to see if it was “possible to do a 3D-style animation in plain JavaScript without Flash, SVG, Canvas or any other fancy stuff.”
He ended up with a solution that:
- Slice a image in 1px wide slices
- Embed all the slides in the HTML using data-uris to avoid loading large number of images
- Move and scale image slices for each animation frame to create some nice effect
So, first you litter your HTML with:
HTML:
-
-
<img id=“slice-0″ src=“data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAABACAIAAABUc4oXAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAlwSFlzAAAewwAAHsMBvJeX2gAAACFJREFUCNdj+P//PxMDAwPd8INH75nw2Mnw/ftvBjq6CQDNIw8MeLLR3AAAAABJRU5ErkJggg==”/>
-
<img id=“slice-1″ src=“data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAABACAIAAABUc4oXAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAlvRkZzAAAAAQAAAAAAEXZlawAAAAlwSFlzAAAewwAAHsMBvJeX2gAAAEpJREFUCNdj+P//PxMDAwNN8PsP35lOnn3MdOzUQ6brN18xvXrzlenJs49M+OwUE+Vh4uZiZbI2V2DSUBVluHPvDcO/f/8YaOROAMXBIDUsb8h5AAAAAElFTkSuQmCC”/>
-
and then you put them all together via JavaScript:
JAVASCRIPT:
-
-
// Draw a frame
-
function draw(x) {
-
var prevx=0;
-
for (i=0;i<384;i++) {
-
var rad = i*3.14/384;
-
var nextx = Math.round((1-Math.cos(rad))*180);
-
var h=Math.round(40+50*Math.sin(rad));
-
var s = document.getElementById(“slice-”+((i+x*2)%384));
-
s.height=h;
-
var w = Math.round(nextx-prevx);
-
if (w==0) {
-
s.style.display=“none”;
-
} else {
-
s.style.display=“block”;
-
s.width= w;
-
s.style.top=“”+(50-Math.round(h/1.5))+“px”;
-
s.style.left=“”+(prevx)+“px”;
-
}
-
prevx=nextx;
-
}
-
}
-
-
// Animate the images
-
var frame=0;
-
function animate() {
-
draw(frame++);
-
setTimeout(“animate()”,1);
-
}
-
-
// Initialize the image slices
-
for (i=0;i<384;i++) {
-
var s = document.getElementById(“slice-”+i);
-
s.style.position=“absolute”;
-
s.style.display=“none”;
-
}
-
animate();
-
Then sit and watch the animation.

No User Responded in " 3D-style animation in JavaScript "
Sorry the comment area are closed