gb-image-decoder 0.1.2 → 0.1.3

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/dist/index.cjs CHANGED
@@ -481,11 +481,11 @@ class RGBNDecoder {
481
481
  const paletteChanged = this.setPalette(palette);
482
482
  const lockFrameChanged = this.setLockFrame(lockFrame);
483
483
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
484
+ const canvases = this.setTiles(tiles);
485
+ const newHeight = this.getHeight();
484
486
  if (!shouldUpdate) {
485
487
  return;
486
488
  }
487
- const canvases = this.setTiles(tiles);
488
- const newHeight = this.getHeight();
489
489
  if (!this.canvas) {
490
490
  return;
491
491
  }
@@ -496,10 +496,11 @@ class RGBNDecoder {
496
496
  if (this.canvas.height !== newHeight) {
497
497
  this.canvas.height = newHeight;
498
498
  }
499
- if (!this.canvas) {
499
+ const context = this.canvas?.getContext("2d");
500
+ if (!context) {
500
501
  return;
501
502
  }
502
- this.blendCanvases(this.canvas, canvases);
503
+ this.blendCanvases(context, canvases);
503
504
  }
504
505
  setPalette(palette) {
505
506
  if (!palette) {
@@ -553,17 +554,7 @@ class RGBNDecoder {
553
554
  };
554
555
  }, {});
555
556
  }
556
- blendCanvases(targetCanvas, sourceCanvases) {
557
- const { width, height } = Object.values(sourceCanvases).reduce((acc, current) => ({
558
- width: Math.max(current.width, acc.width),
559
- height: Math.max(current.width, acc.height)
560
- }), { width: 0, height: 0 });
561
- targetCanvas.width = width;
562
- targetCanvas.height = height;
563
- const targetContext = targetCanvas.getContext("2d");
564
- if (!targetContext) {
565
- throw new Error("no canvas context");
566
- }
557
+ blendCanvases(targetContext, sourceCanvases) {
567
558
  channels.forEach((key) => {
568
559
  const sourceCanvas = sourceCanvases[key];
569
560
  if (sourceCanvas && sourceCanvas.width && sourceCanvas.height) {
@@ -589,7 +580,17 @@ class RGBNDecoder {
589
580
  [key]: channelCanvas
590
581
  };
591
582
  }, {});
592
- this.blendCanvases(canvas, canvases);
583
+ const { width, height } = Object.values(canvases).reduce((acc, current) => ({
584
+ width: Math.max(current.width, acc.width),
585
+ height: Math.max(current.height, acc.height)
586
+ }), { width: 0, height: 0 });
587
+ canvas.width = width;
588
+ canvas.height = height;
589
+ const context = canvas.getContext("2d");
590
+ if (!context) {
591
+ throw new Error("no canvas context");
592
+ }
593
+ this.blendCanvases(context, canvases);
593
594
  return canvas;
594
595
  }
595
596
  maxTiles() {
package/dist/index.mjs CHANGED
@@ -479,11 +479,11 @@ class RGBNDecoder {
479
479
  const paletteChanged = this.setPalette(palette);
480
480
  const lockFrameChanged = this.setLockFrame(lockFrame);
481
481
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
482
+ const canvases = this.setTiles(tiles);
483
+ const newHeight = this.getHeight();
482
484
  if (!shouldUpdate) {
483
485
  return;
484
486
  }
485
- const canvases = this.setTiles(tiles);
486
- const newHeight = this.getHeight();
487
487
  if (!this.canvas) {
488
488
  return;
489
489
  }
@@ -494,10 +494,11 @@ class RGBNDecoder {
494
494
  if (this.canvas.height !== newHeight) {
495
495
  this.canvas.height = newHeight;
496
496
  }
497
- if (!this.canvas) {
497
+ const context = this.canvas?.getContext("2d");
498
+ if (!context) {
498
499
  return;
499
500
  }
500
- this.blendCanvases(this.canvas, canvases);
501
+ this.blendCanvases(context, canvases);
501
502
  }
502
503
  setPalette(palette) {
503
504
  if (!palette) {
@@ -551,17 +552,7 @@ class RGBNDecoder {
551
552
  };
552
553
  }, {});
553
554
  }
554
- blendCanvases(targetCanvas, sourceCanvases) {
555
- const { width, height } = Object.values(sourceCanvases).reduce((acc, current) => ({
556
- width: Math.max(current.width, acc.width),
557
- height: Math.max(current.width, acc.height)
558
- }), { width: 0, height: 0 });
559
- targetCanvas.width = width;
560
- targetCanvas.height = height;
561
- const targetContext = targetCanvas.getContext("2d");
562
- if (!targetContext) {
563
- throw new Error("no canvas context");
564
- }
555
+ blendCanvases(targetContext, sourceCanvases) {
565
556
  channels.forEach((key) => {
566
557
  const sourceCanvas = sourceCanvases[key];
567
558
  if (sourceCanvas && sourceCanvas.width && sourceCanvas.height) {
@@ -587,7 +578,17 @@ class RGBNDecoder {
587
578
  [key]: channelCanvas
588
579
  };
589
580
  }, {});
590
- this.blendCanvases(canvas, canvases);
581
+ const { width, height } = Object.values(canvases).reduce((acc, current) => ({
582
+ width: Math.max(current.width, acc.width),
583
+ height: Math.max(current.height, acc.height)
584
+ }), { width: 0, height: 0 });
585
+ canvas.width = width;
586
+ canvas.height = height;
587
+ const context = canvas.getContext("2d");
588
+ if (!context) {
589
+ throw new Error("no canvas context");
590
+ }
591
+ this.blendCanvases(context, canvases);
591
592
  return canvas;
592
593
  }
593
594
  maxTiles() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gb-image-decoder",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",