DHTML Jigsaw Puzzle: NN4; Creating the Pieces | WebReference

DHTML Jigsaw Puzzle: NN4; Creating the Pieces


The DHTML Lab Jigsaw Puzzle, Part III: NN4
creating the puzzle pieces

When the puzzle displays, we begin play by pressing the Break button which calls the breakUp() function. If the puzzle is a new puzzle, createPuzzle() is called. If createPuzzle() realizes that a new piece count is called for, it calls createPieces(). The logic of this flow has been discussed in detail in Part II.

    function createPieces(){ puzzPieces = puzzAcross * puzzDown; if (puzzPieces > piecesCreated) { makeStart = piecesCreated+1; for(i=makeStart; i<=puzzPieces; i++) { window.status = "Creating puzzle piece... " + i; eval("PIECE" + i + " = new Layer(puzzWidth)"); } piecesCreated = puzzPieces; } activeEl = eval("PIECE" + puzzPieces); }

The createPieces() function reads as its previous IE incarnation. It calculates how many pieces are necessary; if the ones already created are not enough, it creates some more; it stores the amount of pieces created so far and assigns the final piece created (the topmost in z-order) to activeEl, so we can track it.

The new Layer Constructor

A first for these columns is the use of Navigator's new Layer constructor to create a new positioned element on-the-fly. The complete syntax is:

    elementReference = new Layer(width);

This element (layer) will be created, added to the document.layers array, and any HTML written to it will wrap at the width value. This type of element creation can be used only after a page has fully loaded and displayed. It cannot be used in a script outside of a function as, say, new Array can.

Once we have created the element, we can assign values to its various properties to position it, clip it, write to it and so on. The one property that will be missing that all other elements have is ID. We cannot assign an ID value, but Navigator does supply one of its own internally, in the form: _js_layer_layerNumber. The more on-the-fly elements you create, the greater the layerNumber. This incrementation persists throughout a browser session. It does not reinitialize with every page load. So using this internal ID as an identifier is worthless. We will always use the variables we assigned the constructor to. In our example, these are PIECE1, PIECE2...etc. Just for your reference, the first elements created with the constructor would have internal IDs of _js_layer_1, _js_layer_2, etc.

Filling the Pieces

The major difference in createPuzzle() for Navigator is that every puzzle piece has HTML written to it that loads a new image. Our other alternative is to change the src property of the piece elements. This however invokes the deadly "wysiwyg" problem, on reloads. The less said about that, the better.

    function createPuzzle() { if (!isCreated) { createPieces() }; pieceWidth = puzzWidth/puzzAcross; pieceHeight = puzzHeight/puzzDown; while (pieceCount <= puzzPieces) { pixT = topCount * pieceHeight; pixB = (topCount + 1) * pieceHeight; for (i=1; i<=puzzAcross; i++) { window.status = "Clipping image for piece... " + pieceCount; tempEl = eval("PIECE" + pieceCount); tempEl.document.write("<IMG WIDTH="+puzzWidth+" HEIGHT="+puzzHeight+" SRC="+puzzImage.src+">"); tempEl.document.close(); pixR = pieceWidth * i; pixL = pieceWidth * (i-1); tempEl.clip.top = pixT tempEl.clip.left = pixL tempEl.clip.right = pixR tempEl.clip.bottom = pixB tempEl.clipLeft = pixL; tempEl.clipTop = pixT; pieceCount++ } topCount++ } if (isGrid) { setGrid() }; isNewPuzz = false; isCreated = true; pieceCount = 1; topCount = 0; window.status = ""; }

Therefore, createPuzzle() first establishes the width and height of the pieces to be displayed. It then goes through the puzzle, one row at a time, loading each piece element with an image, and clips it accordingly. It assigns the left and top clip values to each element's clipLeft and clipTop properties (for drag & drop use later).

If grid use is turned on, setGrid() is called, and then the appropriate values are assigned to our variables. When createPuzzle() exits, control is returned to the breakUp() function that disperses the pieces.

Produced by Peter Belesis and

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

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