@lijuhong1981/jsgif 1.0.10 → 1.1.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.
Files changed (2) hide show
  1. package/index.js +24 -20
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import Check from "@lijuhong1981/jscheck";
2
- import { Destroyable, destroyHTMLElement, destroyObject } from "@lijuhong1981/jsdestroy";
2
+ import { defineDestroyProperties, Destroyable, destroyHTMLElement, destroyObject } from "@lijuhong1981/jsdestroy";
3
+ import { EventRaiser } from "@lijuhong1981/events";
3
4
  import { fetchArrayBuffer } from "@lijuhong1981/jsload";
4
5
 
5
6
  // 转流数组
@@ -145,6 +146,7 @@ function GifPlayer(options = {}) {
145
146
  let TRANSPARENCY = null;
146
147
  let isPlaying = false;
147
148
  let currentFrameIndex = 0;
149
+ const frameChanged = new EventRaiser();
148
150
  const player = {};
149
151
 
150
152
  function reset() {
@@ -464,9 +466,7 @@ function GifPlayer(options = {}) {
464
466
  function drawCurrentFrame() {
465
467
  const frame = FRAME_LIST[currentFrameIndex];
466
468
  drawFrame(frame);
467
- if (typeof options.onFrameChanged === 'function') {
468
- options.onFrameChanged(currentFrameIndex, frame);
469
- }
469
+ frameChanged.raiseEvent(currentFrameIndex, frame);
470
470
  return frame;
471
471
  }
472
472
 
@@ -507,7 +507,7 @@ function GifPlayer(options = {}) {
507
507
  return player;
508
508
  }
509
509
 
510
- function loadData(data) {
510
+ function loadData(data, onLoad, onError) {
511
511
  Check.defined('data', data);
512
512
 
513
513
  return new Promise(function (resolve, reject) {
@@ -522,25 +522,37 @@ function GifPlayer(options = {}) {
522
522
  stopGif();
523
523
  if (options.autoPlay)
524
524
  playGif();
525
+ if (typeof onLoad === 'function')
526
+ onLoad(player);
525
527
  resolve(player);
526
528
  } catch (error) {
529
+ if (typeof onError === 'function')
530
+ onError(error);
527
531
  reject(error);
528
532
  }
529
533
  });
530
534
  }
531
535
 
532
536
  // 用xhr请求本地文件
533
- function loadUrl(url, requestOptions) {
537
+ function loadUrl(url, requestOptions, onLoad, onError) {
534
538
  Check.typeOf.string('url', url);
535
539
 
536
540
  return new Promise(function (resolve, reject) {
541
+
542
+ function rejectError() {
543
+ if (typeof onError === 'function')
544
+ onError(error);
545
+ reject(error);
546
+ }
547
+
537
548
  fetchArrayBuffer(url, requestOptions).then(function (arraybuffer) {
538
- loadData(arraybuffer).then(resolve).catch(reject);
539
- }).catch(reject);
549
+ loadData(arraybuffer, onLoad, onError).then(resolve).catch(rejectError);
550
+ }).catch(rejectError);
540
551
  });
541
552
  }
542
553
 
543
554
  function destroyGif() {
555
+ frameChanged.clear();
544
556
  stopGif();
545
557
  destroyHTMLElement(CANVAS);
546
558
  destroyHTMLElement(TEMP_CANVAS);
@@ -623,22 +635,14 @@ function GifPlayer(options = {}) {
623
635
  return options.timeScale;
624
636
  }
625
637
  },
626
- onFrameChanged: {
627
- set: function (value) {
628
- options.onFrameChanged = value;
629
- },
638
+ frameChanged: {
630
639
  get: function () {
631
- return options.onFrameChanged;
640
+ return frameChanged;
632
641
  }
633
- },
634
- destroy: {
635
- value: destroyGif,
636
- },
642
+ }
637
643
  });
638
644
 
639
- player.isDestroyed = function () {
640
- return false;
641
- };
645
+ defineDestroyProperties(player, destroyGif);
642
646
 
643
647
  return player;
644
648
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lijuhong1981/jsgif",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "> TODO: description",
5
5
  "author": "lijuhong1981",
6
6
  "homepage": "https://github.com/lijuhong1981/jslibs#readme",