spacer

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

home / experts / dhtml / column9
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Logo

The DHTML Lab Jigsaw Puzzle, Part II: IE4 cont'd
setting the grid


A Quick Refresher

As you will recall, our grid had nine horizontal divisions and nine vertical divisions. This reflects the maximum number of allowable puzzle pieces in a row and column, set in our SELECT menus. If you change the maximum for your puzzles, make sure you also change the grid GIF.

Our grid begins life with a visibility of hidden. It is placed in the top-left corner of the puzzle image. If all elements were initially visible, we would have this result:

Adapting the Grid

Recall that when we created our puzzle and broke it, we checked the value of the isGrid variable and called a function, setGrid(), accordingly.

Our grid can be any size, originally. When we want to use it, we must expand it to a size where each of the grid divisions is equal to the size of a puzzle piece. The easiest example is if we want to have 81 pieces, 9 across and 9 down. Our grid should simply expand to the size of the puzzle image. Our statements would be straight-forward:

If we have less than the maximum pieces, which will probably be true in most cases, we have to s t r e t c h the grid image to be larger than the puzzle image, leaving only the required divisions in the same space as the puzzle image. For example, if we have selected three pieces across and two down, only six (3*2) grid divisions should be used. Seven (9-2) vertical divisions and six (9-3) horizontal divisions go unused and should lie outside the puzzle space. Then we should clip the grid to display only the required divisions.

That is:

Once we have expanded our grid to the correct size and clipped it so only the required divisions are visible, we change the value of the grid toggle button to inform the user that the grid is now on:

The grid can be toggled on or off at any time. This does not make it visible. It will only become visible if the puzzle is ready to be solved, that is, if it is broken. Our complete function then becomes:

Toggling Grid Visibility

At any time, the user may choose to turn the grid off by pressing the grid toggle button. The onClick event handler of the button calls the gridToggle() function:

The gridToggle() function checks to see if grid use is on through the isGrid variable. If it is, the grid is made hidden. In cases where the puzzle is not yet broken and the grid is turned on, this line is redundant, as the grid is already hidden. The grid is then given a width and height of 1. Any small number will do. Remember, when we use it again, we will expand it. Finally, the grid toggle button value is updated to inform the user of the change.

If grid use is found to be off, gridToggle() calls our setGrid() function which turns it on, as we know.

In either case, the value of isGrid is toggled before the function returns.

NOTE: When using a small number of puzzle pieces, most of the grid will lie outside the puzzle image, even though it is not visible. Any pieces that occupy the same space as the invisible part of the grid will have a slower reaction time to drags and solves. Although the actual slowdown depends on the speed of the computer being used, it is better to use a grid when many puzzle pieces are created. In fact, that is when it is most useful.

With the pieces all over our page and the grid on, if we have so chosen, we can now move our pieces to solve the puzzle.


Produced by Peter Belesis and

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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

URL: http://www.webreference.com/dhtml/column9/puzzGrid.html