jigsawpuzzlegame 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +41 -19
  2. package/jigsaw-puzzle-game.js +1998 -1960
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -52,10 +52,11 @@ Make sure your container has a defined size:
52
52
 
53
53
  | Option | Type | Default | Description |
54
54
  |--------|------|---------|-------------|
55
- | `image` | string | `null` | URL or data URL of the image to use for the puzzle |
56
- | `numPieces` | number | `20` | Number of puzzle pieces (approximate - actual count depends on optimal grid layout) |
57
- | `shapeType` | number | `0` | Shape type for puzzle pieces (0-3):<br>• `0` - Classic jigsaw shape (curved tabs)<br>• `1` - Alternative shape 1<br>• `2` - Alternative shape 2<br>• `3` - Straight edges (rectangular pieces) |
58
- | `allowRotation` | boolean | `false` | Whether pieces can be rotated by clicking/tapping (90° increments) |
55
+ | `image` | string | `null` | URL or data URL of the image to use for the puzzle (ignored if `savedData` is provided) |
56
+ | `savedData` | string\|null | `null` | JSON string of saved game state:<br>• Non-empty string: use this saved data<br>• Empty string (`""`): load from localStorage<br>• Not provided or `null`: use normal initialization with `image` and other parameters |
57
+ | `numPieces` | number | `20` | Number of puzzle pieces (approximate - actual count depends on optimal grid layout, ignored if `savedData` is provided) |
58
+ | `shapeType` | number | `0` | Shape type for puzzle pieces (0-3, ignored if `savedData` is provided):<br>• `0` - Classic jigsaw shape (curved tabs)<br>• `1` - Alternative shape 1<br>• `2` - Alternative shape 2<br>• `3` - Straight edges (rectangular pieces) |
59
+ | `allowRotation` | boolean | `false` | Whether pieces can be rotated by clicking/tapping (90° increments, ignored if `savedData` is provided) |
59
60
  | `onReady` | function | `null` | Callback function called when the puzzle is ready (image loaded and displayed) |
60
61
  | `onWin` | function | `null` | Callback function called when the puzzle is completed |
61
62
  | `onStart` | function | `null` | Callback function called when a game starts |
@@ -119,10 +120,10 @@ puzzle.setOptions({
119
120
 
120
121
  ### `save([callback])`
121
122
 
122
- Saves the current game state. If no callback is provided, saves to localStorage.
123
+ Saves the current game state. Gets the state data, converts it to a JSON string, then either calls the callback with the string or saves to localStorage.
123
124
 
124
125
  **Parameters:**
125
- - `callback` (function, optional) - Function that receives the saved data as JSON string
126
+ - `callback` (function, optional) - Function that receives the saved data as JSON string. If not provided, saves to localStorage automatically.
126
127
 
127
128
  ```javascript
128
129
  // Save to localStorage (default)
@@ -135,20 +136,30 @@ puzzle.save((savedData) => {
135
136
  });
136
137
  ```
137
138
 
138
- ### `load([savedData])`
139
+ ### Loading Saved Games
139
140
 
140
- Loads a previously saved game state.
141
-
142
- **Parameters:**
143
- - `savedData` (string, optional) - JSON string of saved data. If not provided, loads from localStorage
141
+ To load a saved game, create a new puzzle instance with the `savedData` option:
144
142
 
145
143
  ```javascript
146
- // Load from localStorage
147
- puzzle.load();
144
+ // Load from explicit string
145
+ const savedString = localStorage.getItem('myPuzzleSave');
146
+ const puzzle = new JigsawPuzzle('puzzle-container', {
147
+ savedData: savedString,
148
+ onReady: () => puzzle.start()
149
+ });
148
150
 
149
- // Load from custom data
150
- const savedData = localStorage.getItem('myPuzzleSave');
151
- puzzle.load(savedData);
151
+ // Load from localStorage automatically (pass empty string)
152
+ const puzzle = new JigsawPuzzle('puzzle-container', {
153
+ savedData: "", // Empty string triggers localStorage lookup
154
+ onReady: () => puzzle.start()
155
+ });
156
+
157
+ // Create new puzzle (no saved data)
158
+ const puzzle = new JigsawPuzzle('puzzle-container', {
159
+ image: 'image.jpg',
160
+ numPieces: 25,
161
+ onReady: () => puzzle.start()
162
+ });
152
163
  ```
153
164
 
154
165
  ### `destroy()`
@@ -196,11 +207,18 @@ function saveGame() {
196
207
  });
197
208
  }
198
209
 
199
- // Load game
210
+ // Load game - create new instance with saved data
200
211
  function loadGame() {
201
212
  const saved = localStorage.getItem('puzzleSave');
202
213
  if (saved) {
203
- puzzle.load(saved);
214
+ if (puzzle) puzzle.destroy();
215
+ puzzle = new JigsawPuzzle('puzzle-container', {
216
+ savedData: saved,
217
+ onReady: () => puzzle.start(),
218
+ onWin: () => {
219
+ alert('You won!');
220
+ }
221
+ });
204
222
  }
205
223
  }
206
224
  ```
@@ -225,12 +243,16 @@ Works in all modern browsers (Chrome, Firefox, Safari, Edge).
225
243
 
226
244
  ## Styling
227
245
 
228
- The puzzle adds these CSS classes you can style:
246
+ The library automatically injects its required CSS styles when loaded, so **no external CSS file is needed**. The styles are injected once per page load, even if multiple puzzle instances are created.
247
+
248
+ The puzzle uses these CSS classes (automatically styled by the library):
229
249
 
230
250
  - `.polypiece` - Individual puzzle pieces
231
251
  - `.polypiece.moving` - Pieces during animation
232
252
  - `.gameCanvas` - Reference image canvas (hidden during play)
233
253
 
254
+ You can override these styles in your own CSS if needed. The library injects a `<style>` element with id `jigsaw-puzzle-styles` that you can target or override.
255
+
234
256
  ## Troubleshooting
235
257
 
236
258
  ### Puzzle doesn't start