JavaScript dynamicPuzzle

colorBar

dynamicPuzzle is a very small (~ 1.6kB minified; ~ 2.5kB source code), way easy and save to install and fun app that is still very flexible in usage.

You can display up to 10 images at a time by randomly (or controlled) shuffling around up to 10 slices of each image on top of each other. It's just 'one of those non-sense', 'a thousand times seen' app,..
But if you are creative it can be a lot of fun to play around. It's so small and gets even smaller as soon as it's running (recycling effect...) and barely needs any resources.
It might be a nice gag for your page for no costs (in all manners).

- full support for IE5, IE5.5 and IE6 and all other browsers.

Table of content:

Demo

^ back to top

Download

^ back to top

Usage and installation

Usage is as simple as:
<script type="text/javascript" src="puzzle.js"></script> somewhere in your document, adding an id="puzzle" to some element (DIV, P, SPAN,...) and makeing shure that the image 'puzzle.jpg' is in the same directory as the script, et voilá.

You don't even need to take care of the DOM to be loaded first (so no window.onload or
$(function(){...} in jQuery... necessary). There is almost nothing you can do wrong,... and if so (no image, no id, etc.) it doesn't produce any javaScript errors,... it then just doesn't work.
There is no need to set it up, but if you want to,... you may be very creative an flexible to do so.

If you want to make your own image, you have to get 2 to 10 square images you want to diplay in a row from left to right (as you see with the demos). This image has to be in the same directory as the script or, if you add the right parameter to the function, it can even be somewhere else.
If you don't want to use the arguments of the function, it also needs to be called 'puzzle.jpg'

How and which parameters to add:

The function 'puzzle()' ends with ...}()); The arguments are placed right between the first two round brackets, seperated by a coma, like this: ...}(here, and_here,...));
The parameters are all optional and in the following order (set to 'null' if you want to skip one):

The app automatically calculates the amount of images you got and automatically crops the image a little bit if the amount of slices don't exactly fit to the dimensions of the image (255 pixels / 7 slices = 36.43 pixels, so the image ends up to be 7 x 36 = 252 pixels in dimension).

The surrounding DIV gets the class name 'puzzle', just in case you need it for your CSS. But be carefull with borders and stuff if you want to make it run in IE5.5...

Get rid of the '(' in the beginning of the function and the '())' at the end if you want to use puzzle() more then once on your page (This causes the 'eating it self up' effect)

You can have a/some e.g. setInterval('puzzleShifter(1)',1100); in your code to add some more slice changes to your effect. the '1' is for the second image (numbering starts with 0) and the '1100' is the time in milliseconds for the next shuffling of this image.

^ back to top

Background

This app is the result of a fun class I had last December. The students were very motivated and after hours of analyzing and speed optimizing web pages, cutting down HTML, CSS and even javaScripts of different pages to the most efficient,... my students were so excited about what's possible,... that we decided to do a remake of an existing, totally overloaded thing we found on the web.

It was quite exactly the same puzzle effect app, based on jQuery with an additional library (7.15KB) and a 'setup-file' of another 14.6KB (besides the 75 HTML requests of 3 5x5 sliced images).

We wanted to be able to do exactly the same thing as the original app and decided to keep it below 100 lines of code. As soon as we discovered the techniques, we wanted to cut it down to a 50 liner... we ended up with 40 lines, so we added some nice features and got back to 49 lines ;o).
The topic of that afternoon was 'how to use sprites' that we used earlier on a page's navigation... so the basic idea was to not use so many single images but only 1 and slice it by changing the background image position of DIV-Elements. The shifting is a simple classRenaming of those DIVs...

It's possible to make this app even shorter, but you'll end up with setting up very difficult style structures for the DIVs and ballast for the DOM. This way you just have some simple DIVs with only a className and at the end there is only a function left that includes 4 (actually 2) simple lines of code...

It would be nice to have some transition effect to it,... go ahed and add them ;o)

^ back to top