jigsawpuzzlegame 1.0.0

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.
package/README.md ADDED
@@ -0,0 +1,261 @@
1
+ # JigsawPuzzle
2
+
3
+ A lightweight, reusable JavaScript class for creating interactive jigsaw puzzle games in web applications. Features customizable piece shapes, rotation support, save/load functionality, and touch/mouse controls.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install jigsaw-puzzle
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```javascript
14
+ import { JigsawPuzzle } from 'jigsaw-puzzle';
15
+
16
+ const puzzle = new JigsawPuzzle('puzzle-container', {
17
+ image: 'https://example.com/image.jpg',
18
+ numPieces: 20,
19
+ shapeType: 0,
20
+ allowRotation: false,
21
+ onReady: () => {
22
+ puzzle.start();
23
+ },
24
+ onWin: () => {
25
+ console.log('Puzzle solved!');
26
+ }
27
+ });
28
+ ```
29
+
30
+ ## HTML Setup
31
+
32
+ ```html
33
+ <div id="puzzle-container"></div>
34
+ <script type="module" src="your-script.js"></script>
35
+ ```
36
+
37
+ Make sure your container has a defined size:
38
+
39
+ ```css
40
+ #puzzle-container {
41
+ width: 100vw;
42
+ height: 100vh;
43
+ position: relative;
44
+ }
45
+ ```
46
+
47
+ ## Configuration Options
48
+
49
+ | Option | Type | Default | Description |
50
+ |--------|------|---------|-------------|
51
+ | `image` | string | `null` | URL or data URL of the image to use for the puzzle |
52
+ | `numPieces` | number | `20` | Number of puzzle pieces (approximate - actual count depends on optimal grid layout) |
53
+ | `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) |
54
+ | `allowRotation` | boolean | `false` | Whether pieces can be rotated by clicking/tapping (90° increments) |
55
+ | `onReady` | function | `null` | Callback function called when the puzzle is ready (image loaded and displayed) |
56
+ | `onWin` | function | `null` | Callback function called when the puzzle is completed |
57
+ | `onStart` | function | `null` | Callback function called when a game starts |
58
+ | `onStop` | function | `null` | Callback function called when a game is stopped |
59
+
60
+ ## API Methods
61
+
62
+ ### `start()`
63
+
64
+ Starts a new game with the current settings. Creates the puzzle pieces and distributes them.
65
+
66
+ ```javascript
67
+ puzzle.start();
68
+ ```
69
+
70
+ **Important:** Call this only after the puzzle is ready (use the `onReady` callback).
71
+
72
+ ### `stop()`
73
+
74
+ Stops the current game and returns to the image preview state.
75
+
76
+ ```javascript
77
+ puzzle.stop();
78
+ ```
79
+
80
+ ### `reset()`
81
+
82
+ Resets the puzzle to initial state. Reloads the current image and prepares for a new game.
83
+
84
+ ```javascript
85
+ puzzle.reset();
86
+ // Then wait for onReady callback and call puzzle.start()
87
+ ```
88
+
89
+ ### `setImage(imageUrl)`
90
+
91
+ Sets a new image for the puzzle.
92
+
93
+ **Parameters:**
94
+ - `imageUrl` (string) - URL or data URL of the image
95
+
96
+ ```javascript
97
+ puzzle.setImage('https://example.com/new-image.jpg');
98
+ // Image will reload, wait for onReady callback
99
+ ```
100
+
101
+ ### `setOptions(newOptions)`
102
+
103
+ Updates puzzle options without creating a new instance.
104
+
105
+ **Parameters:**
106
+ - `newOptions` (object) - Object with options to update (partial updates allowed)
107
+
108
+ ```javascript
109
+ puzzle.setOptions({
110
+ numPieces: 50,
111
+ allowRotation: true,
112
+ shapeType: 1
113
+ });
114
+ ```
115
+
116
+ ### `save([callback])`
117
+
118
+ Saves the current game state. If no callback is provided, saves to localStorage.
119
+
120
+ **Parameters:**
121
+ - `callback` (function, optional) - Function that receives the saved data as JSON string
122
+
123
+ ```javascript
124
+ // Save to localStorage (default)
125
+ puzzle.save();
126
+
127
+ // Save with custom callback
128
+ puzzle.save((savedData) => {
129
+ localStorage.setItem('myPuzzleSave', savedData);
130
+ // Or send to server, download as file, etc.
131
+ });
132
+ ```
133
+
134
+ ### `load([savedData])`
135
+
136
+ Loads a previously saved game state.
137
+
138
+ **Parameters:**
139
+ - `savedData` (string, optional) - JSON string of saved data. If not provided, loads from localStorage
140
+
141
+ ```javascript
142
+ // Load from localStorage
143
+ puzzle.load();
144
+
145
+ // Load from custom data
146
+ const savedData = localStorage.getItem('myPuzzleSave');
147
+ puzzle.load(savedData);
148
+ ```
149
+
150
+ ### `destroy()`
151
+
152
+ Completely destroys the puzzle instance, cleaning up all resources. Use this when you want to remove the puzzle and create a new one in the same container.
153
+
154
+ ```javascript
155
+ puzzle.destroy();
156
+ puzzle = new JigsawPuzzle('puzzle-container', {
157
+ image: 'new-image.jpg',
158
+ numPieces: 30
159
+ });
160
+ ```
161
+
162
+ ## Complete Example
163
+
164
+ ```javascript
165
+ import { JigsawPuzzle } from 'jigsaw-puzzle';
166
+
167
+ const puzzle = new JigsawPuzzle('puzzle-container', {
168
+ image: 'https://example.com/image.jpg',
169
+ numPieces: 20,
170
+ shapeType: 0,
171
+ allowRotation: false,
172
+ onReady: () => {
173
+ // Puzzle is ready, start the game
174
+ puzzle.start();
175
+ },
176
+ onWin: () => {
177
+ alert('Congratulations! You solved the puzzle!');
178
+ },
179
+ onStart: () => {
180
+ console.log('Game started');
181
+ },
182
+ onStop: () => {
183
+ console.log('Game stopped');
184
+ }
185
+ });
186
+
187
+ // Save game
188
+ function saveGame() {
189
+ puzzle.save((data) => {
190
+ localStorage.setItem('puzzleSave', data);
191
+ console.log('Game saved!');
192
+ });
193
+ }
194
+
195
+ // Load game
196
+ function loadGame() {
197
+ const saved = localStorage.getItem('puzzleSave');
198
+ if (saved) {
199
+ puzzle.load(saved);
200
+ }
201
+ }
202
+ ```
203
+
204
+ ## User Interactions
205
+
206
+ - **Mouse/Touch:** Click and drag pieces to move them
207
+ - **Rotation:** If `allowRotation` is enabled, quick click/tap rotates pieces 90°
208
+ - **Piece Merging:** When pieces are close and correctly aligned, they automatically merge
209
+ - **Pan:** Click and drag on empty space to pan all pieces
210
+ - **Zoom:** Mouse wheel to zoom in/out, or pinch gesture on touch devices
211
+
212
+ ## Browser Compatibility
213
+
214
+ Requires modern browser features:
215
+ - ES6 Modules support
216
+ - Canvas API
217
+ - Path2D API
218
+ - Touch events (for mobile support)
219
+
220
+ Works in all modern browsers (Chrome, Firefox, Safari, Edge).
221
+
222
+ ## Styling
223
+
224
+ The puzzle adds these CSS classes you can style:
225
+
226
+ - `.polypiece` - Individual puzzle pieces
227
+ - `.polypiece.moving` - Pieces during animation
228
+ - `.gameCanvas` - Reference image canvas (hidden during play)
229
+
230
+ ## Troubleshooting
231
+
232
+ ### Puzzle doesn't start
233
+ - Make sure you're calling `start()` in the `onReady` callback
234
+ - Check that the image URL is valid and accessible (CORS issues if loading from different domain)
235
+ - Verify the container element exists and has a size
236
+
237
+ ### Image doesn't load
238
+ - Check browser console for CORS errors
239
+ - Use data URLs or images from the same domain
240
+ - Ensure the image URL is correct
241
+
242
+ ### Pieces don't merge
243
+ - Make sure pieces are close enough (the puzzle calculates optimal distance)
244
+ - Check that pieces are in the same rotation if rotation is enabled
245
+ - Verify pieces are correctly aligned
246
+
247
+ ## License
248
+
249
+ Copyright (c) 2026 by Dillon (https://codepen.io/Dillo/pen/QWKLYab) & Henrik Rasmussen (https://www.raketten.net)
250
+
251
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
252
+
253
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
254
+
255
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
256
+
257
+ ## Authors
258
+
259
+ - Dillon - https://codepen.io/Dillo/pen/QWKLYab
260
+ - Henrik Rasmussen - https://www.raketten.net
261
+