graphico 1.1.2 → 1.2.1

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 CHANGED
@@ -13,6 +13,7 @@
13
13
  - **User interaction** captured via keyboard inputs or mouse movements or clicks
14
14
  - Multiple audio tracks supported by system **audio player** with volume, mute, and loop toggle
15
15
  - Built-in logic for **screenshots** and **screen recordings** that capture both video and audio
16
+ - Secure and convenient **browser storage** that persists across browser and system restarts
16
17
 
17
18
  ## Caveats
18
19
 
package/dist/canvas.js CHANGED
@@ -410,6 +410,63 @@ var Canvas = /** @class */ (function () {
410
410
  this.layers[layer].clearRect(0, 0, this.config.width, this.config.height);
411
411
  }
412
412
  };
413
+ /**
414
+ * Save arbitrary data to the browser storage.
415
+ * **Warning:** This will overwrite any existing data under these object keys.
416
+ * @param data The arbirary data object to save
417
+ * @param name An optional "namespace" to store the data, which can be assigned when a user has multiple profiles, for example
418
+ */
419
+ Canvas.prototype.saveData = function (data, name) {
420
+ if (name === void 0) { name = 'data'; }
421
+ var key = this.encode(name);
422
+ var value = this.encode(data);
423
+ localStorage.setItem(key, value);
424
+ this.log("Saved \"".concat(data, "\" in ").concat(name, "."));
425
+ };
426
+ /**
427
+ * Load arbitrary data from the browser storage.
428
+ * **Warning:** The loaded data object is not strictly type-checked or sanitized, so it may have missing fields or contain extra fields. It's good practice to manually validate the data once it's loaded from storage.
429
+ * @param name An optional "namespace" to load data from, assigned when saving data
430
+ * @returns The data object loaded from browser storage
431
+ */
432
+ Canvas.prototype.loadData = function (name) {
433
+ if (name === void 0) { name = 'data'; }
434
+ var key = this.encode(name);
435
+ var value = localStorage.getItem(key);
436
+ if (value) {
437
+ try {
438
+ var decoded = this.decode(value);
439
+ this.log("Loaded \"".concat(decoded, "\" from ").concat(name, "."));
440
+ return decoded;
441
+ }
442
+ catch (_a) {
443
+ this.log("Failed to load ".concat(name, "."));
444
+ }
445
+ }
446
+ return undefined;
447
+ };
448
+ /**
449
+ * Clear **all** saved data. This action is permanent!
450
+ * @param name An optional "namespace" to clear data from.
451
+ */
452
+ Canvas.prototype.clearData = function (name) {
453
+ if (name === void 0) { name = 'data'; }
454
+ var key = this.encode(name);
455
+ localStorage.removeItem(key);
456
+ this.log("Removed ".concat(name, " from storage."));
457
+ };
458
+ /**
459
+ * Encode data for local browser storage.
460
+ */
461
+ Canvas.prototype.encode = function (raw) {
462
+ return btoa(encodeURIComponent(JSON.stringify(raw)));
463
+ };
464
+ /**
465
+ * Decode data from local browser storage.
466
+ */
467
+ Canvas.prototype.decode = function (code) {
468
+ return JSON.parse(decodeURIComponent(atob(code)));
469
+ };
413
470
  /**
414
471
  * Log a message to the debug console.
415
472
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphico",
3
- "version": "1.1.2",
3
+ "version": "1.2.1",
4
4
  "description": "Canvas 2D rendering toolkit for games and visual projects",
5
5
  "homepage": "https://npm.nicfv.com/",
6
6
  "bin": "",
package/types/canvas.d.ts CHANGED
@@ -155,6 +155,33 @@ export declare class Canvas {
155
155
  * @param layer The zero-indexed layer to clear, if unset, will clear all layers
156
156
  */
157
157
  clear(layer?: number): void;
158
+ /**
159
+ * Save arbitrary data to the browser storage.
160
+ * **Warning:** This will overwrite any existing data under these object keys.
161
+ * @param data The arbirary data object to save
162
+ * @param name An optional "namespace" to store the data, which can be assigned when a user has multiple profiles, for example
163
+ */
164
+ saveData<T>(data: T, name?: string): void;
165
+ /**
166
+ * Load arbitrary data from the browser storage.
167
+ * **Warning:** The loaded data object is not strictly type-checked or sanitized, so it may have missing fields or contain extra fields. It's good practice to manually validate the data once it's loaded from storage.
168
+ * @param name An optional "namespace" to load data from, assigned when saving data
169
+ * @returns The data object loaded from browser storage
170
+ */
171
+ loadData<T>(name?: string): Partial<T> | undefined;
172
+ /**
173
+ * Clear **all** saved data. This action is permanent!
174
+ * @param name An optional "namespace" to clear data from.
175
+ */
176
+ clearData(name?: string): void;
177
+ /**
178
+ * Encode data for local browser storage.
179
+ */
180
+ private encode;
181
+ /**
182
+ * Decode data from local browser storage.
183
+ */
184
+ private decode;
158
185
  /**
159
186
  * Log a message to the debug console.
160
187
  */