@lijuhong1981/jsgif 1.0.5 → 1.0.7

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 (2) hide show
  1. package/index.js +56 -5
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import Check from "@lijuhong1981/jscheck";
2
+ import { Destroyable, destroyHTMLElement, destroyObject } from "@lijuhong1981/jsdestroy";
2
3
  import { fetchArrayBuffer } from "@lijuhong1981/jsload";
3
4
 
4
5
  // 转流数组
@@ -17,8 +18,9 @@ function bitsToNum(ba) {
17
18
  }, 0);
18
19
  };
19
20
 
20
- class Stream {
21
+ class Stream extends Destroyable {
21
22
  constructor(data) {
23
+ super();
22
24
  this.data = data;
23
25
  this.len = data.length;
24
26
  this.pos = 0;
@@ -151,6 +153,7 @@ function GifPlayer(options = {}) {
151
153
  LAST_DISPOSA_METHOD = null;
152
154
  CURRENT_FRAME_INDEX = -1;
153
155
  TRANSPARENCY = null;
156
+ STREAM && STREAM.destroy();
154
157
  };
155
158
 
156
159
  function readSubBlocks() {
@@ -458,9 +461,13 @@ function GifPlayer(options = {}) {
458
461
  CANVAS_CTX.drawImage(TEMP_CANVAS, 0, 0);
459
462
  }
460
463
 
461
- function doCurrentFrame() {
464
+ function drawCurrentFrame() {
462
465
  const frame = FRAME_LIST[currentFrameIndex];
463
466
  drawFrame(frame);
467
+ }
468
+
469
+ function doFrame() {
470
+ drawCurrentFrame();
464
471
  if (typeof options.onFrameChanged === 'function') {
465
472
  options.onFrameChanged(currentFrameIndex, frame);
466
473
  }
@@ -468,7 +475,7 @@ function GifPlayer(options = {}) {
468
475
  if (!isPlaying)
469
476
  return;
470
477
  currentFrameIndex = getNextFrameIndex();
471
- doCurrentFrame();
478
+ doFrame();
472
479
  }, frame.delayTime * options.timeScale * 10);
473
480
  }
474
481
 
@@ -479,7 +486,7 @@ function GifPlayer(options = {}) {
479
486
 
480
487
  isPlaying = true;
481
488
 
482
- doCurrentFrame();
489
+ doFrame();
483
490
 
484
491
  return player;
485
492
  }
@@ -494,6 +501,7 @@ function GifPlayer(options = {}) {
494
501
  isPlaying = false;
495
502
 
496
503
  currentFrameIndex = 0;
504
+ drawCurrentFrame();
497
505
 
498
506
  return player;
499
507
  }
@@ -505,8 +513,8 @@ function GifPlayer(options = {}) {
505
513
  try {
506
514
  if (data instanceof ArrayBuffer)
507
515
  data = new Uint8Array(data);
508
- STREAM = new Stream(data);
509
516
  reset();
517
+ STREAM = new Stream(data);
510
518
  parseHeader();
511
519
  parseBlock();
512
520
  drawAtlasImage();
@@ -531,48 +539,72 @@ function GifPlayer(options = {}) {
531
539
  });
532
540
  }
533
541
 
542
+ function destroyGif() {
543
+ stopGif();
544
+ destroyHTMLElement(CANVAS);
545
+ destroyHTMLElement(TEMP_CANVAS);
546
+ destroyHTMLElement(ATLAS_CANVAS);
547
+ reset();
548
+ destroyObject(player);
549
+ }
550
+
534
551
  Object.defineProperties(player, {
535
552
  loadData: {
536
553
  value: loadData,
537
554
  },
538
555
  loadUrl: {
556
+ configurable: true,
557
+ writable: true,
539
558
  value: loadUrl,
540
559
  },
541
560
  play: {
561
+ configurable: true,
562
+ writable: true,
542
563
  value: playGif,
543
564
  },
544
565
  pause: {
566
+ configurable: true,
567
+ writable: true,
545
568
  value: pauseGif,
546
569
  },
547
570
  stop: {
571
+ configurable: true,
572
+ writable: true,
548
573
  value: stopGif,
549
574
  },
550
575
  canvas: {
576
+ configurable: true,
551
577
  get: function () {
552
578
  return CANVAS;
553
579
  },
554
580
  },
555
581
  atlasCanvas: {
582
+ configurable: true,
556
583
  get: function () {
557
584
  return ATLAS_CANVAS;
558
585
  },
559
586
  },
560
587
  frameWidth: {
588
+ configurable: true,
561
589
  get: function () {
562
590
  return CANVAS.width;
563
591
  }
564
592
  },
565
593
  frameHeight: {
594
+ configurable: true,
566
595
  get: function () {
567
596
  return CANVAS.height;
568
597
  }
569
598
  },
570
599
  framesNumber: {
600
+ configurable: true,
571
601
  get: function () {
572
602
  return FRAME_LIST.length;
573
603
  }
574
604
  },
575
605
  currentFrameIndex: {
606
+ configurable: true,
607
+ writable: true,
576
608
  set: function (value) {
577
609
  if (!FRAME_LIST[value])
578
610
  throw new Error('illegal index: ' + value);
@@ -583,11 +615,14 @@ function GifPlayer(options = {}) {
583
615
  }
584
616
  },
585
617
  frameList: {
618
+ configurable: true,
586
619
  get: function () {
587
620
  return FRAME_LIST;
588
621
  }
589
622
  },
590
623
  reverse: {
624
+ configurable: true,
625
+ writable: true,
591
626
  set: function (value) {
592
627
  Check.typeOf.bool('reverse', value);
593
628
  options.reverse = value;
@@ -597,6 +632,8 @@ function GifPlayer(options = {}) {
597
632
  }
598
633
  },
599
634
  timeScale: {
635
+ configurable: true,
636
+ writable: true,
600
637
  set: function (value) {
601
638
  Check.typeOf.number.greaterThan('timeScale', value, 0);
602
639
  options.timeScale = value;
@@ -606,6 +643,8 @@ function GifPlayer(options = {}) {
606
643
  }
607
644
  },
608
645
  onFrameChanged: {
646
+ configurable: true,
647
+ writable: true,
609
648
  set: function (value) {
610
649
  options.onFrameChanged = value;
611
650
  },
@@ -613,6 +652,18 @@ function GifPlayer(options = {}) {
613
652
  return options.onFrameChanged;
614
653
  }
615
654
  },
655
+ isDestroyed: {
656
+ configurable: true,
657
+ writable: true,
658
+ value: function () {
659
+ return false;
660
+ },
661
+ },
662
+ destroy: {
663
+ configurable: true,
664
+ writable: true,
665
+ value: destroyGif,
666
+ },
616
667
  });
617
668
 
618
669
  return player;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lijuhong1981/jsgif",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "> TODO: description",
5
5
  "author": "lijuhong1981",
6
6
  "homepage": "https://github.com/lijuhong1981/jslibs#readme",
@@ -17,5 +17,6 @@
17
17
  },
18
18
  "bugs": {
19
19
  "url": "https://github.com/lijuhong1981/jslibs/issues"
20
- }
20
+ },
21
+ "gitHead": "41eaaf2e07770b704c80b27f40b0f033e80dfe9e"
21
22
  }