graphico 1.1.1 → 1.2.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 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
@@ -70,6 +70,7 @@ var Canvas = /** @class */ (function () {
70
70
  main.style.height = "".concat(this.config.height * this.config.scale, "px");
71
71
  main.style.border = "".concat(this.config.scale, "px solid ").concat(this.config.border);
72
72
  main.style.cursor = this.config.showMouse ? 'default' : 'none';
73
+ main.style.boxSizing = 'content-box';
73
74
  this.config.parent.appendChild(main);
74
75
  // Create main canvas graphics object
75
76
  var graphics = main.getContext('2d');
@@ -409,6 +410,79 @@ var Canvas = /** @class */ (function () {
409
410
  this.layers[layer].clearRect(0, 0, this.config.width, this.config.height);
410
411
  }
411
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
+ for (var rawkey in data) {
422
+ var rawval = data[rawkey];
423
+ var key = this.encode([name, rawkey]);
424
+ var value = this.encode([rawval, typeof rawval]);
425
+ localStorage.setItem(key, value);
426
+ this.log("Saved \"".concat(name, ".").concat(rawkey, "\" = \"").concat(rawval, ".").concat(typeof rawval, "\" as \"").concat(key, "\" = \"").concat(value, "\"."));
427
+ }
428
+ };
429
+ /**
430
+ * Load arbitrary data from the browser storage.
431
+ * **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.
432
+ * @param name An optional "namespace" to load data from, assigned when saving data
433
+ * @returns The data object loaded from browser storage
434
+ */
435
+ Canvas.prototype.loadData = function (name) {
436
+ var _a;
437
+ if (name === void 0) { name = 'data'; }
438
+ var loaded = {};
439
+ for (var _i = 0, _b = Object.keys(localStorage); _i < _b.length; _i++) {
440
+ var key = _b[_i];
441
+ try {
442
+ var decodedKey = this.decode(key);
443
+ var decodedVal = this.decode((_a = localStorage.getItem(key)) !== null && _a !== void 0 ? _a : '');
444
+ if (decodedKey[0] === name && decodedKey.length >= 2 && decodedVal.length >= 1) {
445
+ loaded[decodedKey[1]] = decodedVal[0];
446
+ this.log("Loaded \"".concat(decodedKey.join('.'), "\" = \"").concat(decodedVal.join('.'), "\" from \"").concat(key, "\""));
447
+ }
448
+ }
449
+ catch (_c) {
450
+ this.log("Skipping \"".concat(key, "\"..."));
451
+ }
452
+ }
453
+ return loaded;
454
+ };
455
+ /**
456
+ * Clear **all** saved data. This action is permanent!
457
+ * @param name An optional "namespace" to clear data from.
458
+ */
459
+ Canvas.prototype.clearData = function (name) {
460
+ if (name === void 0) { name = 'data'; }
461
+ for (var _i = 0, _a = Object.keys(localStorage); _i < _a.length; _i++) {
462
+ var key = _a[_i];
463
+ try {
464
+ if (this.decode(key)[0] === name) {
465
+ localStorage.removeItem(key);
466
+ this.log("Removed ".concat(key, " from storage."));
467
+ }
468
+ }
469
+ catch (_b) {
470
+ this.log("Skipping \"".concat(key, "\"..."));
471
+ }
472
+ }
473
+ };
474
+ /**
475
+ * Encode data for local browser storage.
476
+ */
477
+ Canvas.prototype.encode = function (raw) {
478
+ return raw.map(function (item) { return btoa(encodeURIComponent(JSON.stringify(item))); }).join('.');
479
+ };
480
+ /**
481
+ * Decode data from local browser storage.
482
+ */
483
+ Canvas.prototype.decode = function (code) {
484
+ return code.split('.').map(function (item) { return JSON.parse(decodeURIComponent(atob(item))); });
485
+ };
412
486
  /**
413
487
  * Log a message to the debug console.
414
488
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphico",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
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
@@ -1,4 +1,4 @@
1
- import { Drawable, Options } from '.';
1
+ import { Drawable, Options, StoreData } from '.';
2
2
  /**
3
3
  * Represents a canvas for drawing and animating
4
4
  */
@@ -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 extends StoreData>(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 extends StoreData>(name?: string): Partial<T>;
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
  */
package/types/types.d.ts CHANGED
@@ -93,3 +93,7 @@ export interface Drawable {
93
93
  */
94
94
  draw(graphics: CanvasRenderingContext2D): void;
95
95
  }
96
+ /**
97
+ * An arbitrary object which can be saved or loaded from browser storage.
98
+ */
99
+ export type StoreData = Record<string, unknown>;