spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / column10
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source
Logo

The DHTML Lab Jigsaw Puzzle, Part III: NN4
breaking the puzzle and dispersing the pieces


The breakUp() function starts the ball rolling. Since the puzzle may have been dragged to a new position by the user after initialization, breakUp() first establishes the present position of the puzzle and assigns it to the variables puzzLeft and puzzTop. After piece dispersion, the puzzle will be solved from scratch, so pieceToSolve becomes 1. If the user pressed the Break button after choosing a different piece count from the drop down menus (isNewPuzz is true) and the puzzle is already in a broken state (isBroken is true), then some cleanup is performed by calling allDone(). Next breakUp() checks to see if a new puzzle is called for, either because a new image has been loaded or the user has changed the piece count. In either case, isNewPuzz will be true, so createPuzzle() is called, starting the routines described on the previous page.

When we return from createPuzzle(), all our puzzle pieces have been created, and are properly loaded and clipped. At this point, the full puzzle drag is disabled to enable puzzle solving. The white elBlank element is clipped to the puzzle image size and made visible, hiding the image and creating a workspace for the user.

Unlike IE4, we cannot calculate the visible page coordinates, so we disperse our pieces between the top and bottom of the puzzle, vertically, and across the page, horizontally. We set variables that store the possible left and top, start and end page coordinates, and enter a loop that will return random numbers in the requested range. Each piece element is positioned in an acceptable location using the random coordinates, it is made draggable through its draggable property, and finally made visible to the user.

If grid use is enabled, the grid is displayed, isBroken and solvedCount are updated and the puzzle is ready for solving.

The random number generator function is reproduced below for reference.

The Grid

You will recall, from the previous columns, that our grid is a GIF image of a... grid with nine horizontal and nine vertical divisions. Whenever we need to resize the grid, the setGrid() function is called. Here, we give it a new width and height and stretch it to provide the appropriate size divisions that correspond to the piece sizes.

Once we establish what our new grid size should be (gridWidth/gridHeight), we reload the grid GIF into the elGrid element with the new size values. Since the grid image is cached, this is instantaneous. We alert the user to the fact that the grid is now "on," by changing the display of the grid toggle button. Finally, if the puzzle is broken, and solving is called for, we display the grid.

At this point, the user is faced with dispersed puzzle pieces, possibly a grid to assist in solving, and can begin dragging the puzzle pieces to their correct location.


Produced by Peter Belesis and

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Working with the DOM Stylesheets Collection · Administering RBAC in PHP 5 CMS Framework · xref: Automatic Cross Referencing Script
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Combine BottomCount() with Other MDX Functions to Add Sophistication · Creating a Daemon with Python · The Coming Voice-over-WiMAX Revolution

All Rights Reserved. Legal Notices.
Created: Nov. 27, 1997
Revised: Jan. 18, 1998

URL: http://www.webreference.com/dhtml/column10/puzzNSbreak.html