DHTML Jigsaw Puzzle: IE4; The Grid | WebReference

DHTML Jigsaw Puzzle: IE4; The Grid

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.

    function setGrid() { elImGrid.style.width = (9/puzzAcross) * puzzWidth; elImGrid.style.height = (9/puzzDown) * puzzHeight; elGrid.style.clip = "rect(0 " + puzzWidth + " " + puzzHeight + " 0)"; gridBut.value = "Grid ON"; if (isBroken) {elGrid.style.visibility = "visible"} }

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:

    elImGrid.style.width = puzzWidth; elImGrid.style.height = puzzHeight;

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:

    width of grid = ( maximum pieces across/selected pieces across ) * width of puzzle image; height of grid = ( maximum pieces down/selected pieces down ) * height of puzzle image; grid clip = puzzle image top, puzzle image width, puzzle image height, puzzle image left;
    elImGrid.style.width =
(9/puzzAcross) * puzzWidth; elImGrid.style.height = (9/puzzDown) * puzzHeight; elGrid.style.clip = "rect(0 " + puzzWidth + " " + puzzHeight + " 0)";

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:

    gridBut.value = "Grid 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:

    function setGrid() { elImGrid.style.width = (9/puzzAcross) * puzzWidth; elImGrid.style.height = (9/puzzDown) * puzzHeight; elGrid.style.clip = "rect(0 " + puzzWidth + " " + puzzHeight + " 0)"; gridBut.value = "Grid ON"; if (isBroken) {elGrid.style.visibility = "visible"} }

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:

    function gridToggle(){ if (isGrid) { elGrid.style.visibility = "hidden"; elImGrid.style.width = 1; elImGrid.style.height = 1; gridBut.value = "Grid OFF"; } else { setGrid() }; isGrid = !isGrid; }

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

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

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