@lijuhong1981/jsgif 1.3.17 → 1.3.18

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/package.json +7 -7
  2. package/src/GIFPlayer.js +24 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lijuhong1981/jsgif",
3
- "version": "1.3.17",
3
+ "version": "1.3.18",
4
4
  "description": "GIF animation loading and playing.",
5
5
  "author": "lijuhong1981 <lijuhong@hotmail.com>",
6
6
  "homepage": "https://github.com/lijuhong1981/jslibs#readme",
@@ -18,12 +18,12 @@
18
18
  "bugs": {
19
19
  "url": "https://github.com/lijuhong1981/jslibs/issues"
20
20
  },
21
- "gitHead": "e968d936633601ce59f5c9aa99c36143b66a1de2",
21
+ "gitHead": "5d4bd57b654be98c46364160070d5dd11600afa6",
22
22
  "dependencies": {
23
- "@lijuhong1981/jscheck": "^1.1.7",
24
- "@lijuhong1981/jsdestroy": "^1.2.7",
25
- "@lijuhong1981/jsevents": "^1.2.13",
26
- "@lijuhong1981/jsload": "^1.3.18",
27
- "@lijuhong1981/jstask": "^1.2.13"
23
+ "@lijuhong1981/jscheck": "^1.1.8",
24
+ "@lijuhong1981/jsdestroy": "^1.2.8",
25
+ "@lijuhong1981/jsevents": "^1.2.14",
26
+ "@lijuhong1981/jsload": "^1.3.19",
27
+ "@lijuhong1981/jstask": "^1.2.14"
28
28
  }
29
29
  }
package/src/GIFPlayer.js CHANGED
@@ -15,7 +15,7 @@ function byteToBitArr(bite) {
15
15
  return byteArr;
16
16
  };
17
17
 
18
- // Generic functions
18
+ // 通用函数
19
19
  function bitsToNum(ba) {
20
20
  return ba.reduce(function (s, n) {
21
21
  return s * 2 + n;
@@ -69,7 +69,10 @@ function lzwDecode(minCodeSize, data) {
69
69
  function readCode(size) {
70
70
  let code = 0;
71
71
  for (let i = 0; i < size; i++) {
72
- if (data.charCodeAt(pos >> 3) & (1 << (pos & 7))) {
72
+ const byteIndex = pos >> 3;
73
+ if (byteIndex >= data.length)
74
+ return -1;
75
+ if (data.charCodeAt(byteIndex) & (1 << (pos & 7))) {
73
76
  code |= 1 << i;
74
77
  }
75
78
  pos++;
@@ -98,6 +101,8 @@ function lzwDecode(minCodeSize, data) {
98
101
  while (true) {
99
102
  last = code;
100
103
  code = readCode(codeSize);
104
+ if (code === -1)
105
+ break;
101
106
 
102
107
  if (code === clearCode) {
103
108
  clear();
@@ -106,6 +111,8 @@ function lzwDecode(minCodeSize, data) {
106
111
  if (code === eoiCode) {
107
112
  break
108
113
  };
114
+ if (last === null)
115
+ break;
109
116
  if (code < dict.length) {
110
117
  if (last !== clearCode) {
111
118
  dict.push(dict[last].concat(dict[code][0]));
@@ -120,7 +127,7 @@ function lzwDecode(minCodeSize, data) {
120
127
  output.push.apply(output, dict[code]);
121
128
 
122
129
  if (dict.length === (1 << codeSize) && codeSize < 12) {
123
- // If we're at the last code and codeSize is 12, the next code will be a clearCode, and it'll be 12 bits long.
130
+ // 如果已经到达最后一个码且 codeSize 为 12,则下一个码将是 clearCode,其长度为 12 位。
124
131
  codeSize++;
125
132
  }
126
133
  }
@@ -135,7 +142,6 @@ const defaultOptions = {
135
142
 
136
143
  function GIFPlayer(options = {}) {
137
144
  options = Object.assign({}, defaultOptions, options);
138
- console.log('options:', options);
139
145
  const CANVAS = options.canvas || document.createElement("canvas");
140
146
  const CANVAS_CTX = CANVAS.getContext('2d');
141
147
  const TEMP_CANVAS = document.createElement("canvas");//用来拿 imageData 的缓存canvas
@@ -180,11 +186,11 @@ function GIFPlayer(options = {}) {
180
186
  if (currIdx > 0) {
181
187
  // 这块不要动
182
188
  if (LAST_DISPOSA_METHOD === 3) {
183
- // Restore to previous
184
- // If we disposed every TEMP_CANVAS_CTX including first TEMP_CANVAS_CTX up to this point, then we have
185
- // no composited TEMP_CANVAS_CTX to restore to. In this case, restore to background instead.
189
+ // 恢复到前一帧
190
+ // 如果到此为止已释放了包括第一个 TEMP_CANVAS_CTX 在内的所有画布,则没有可恢复的合成画布。
191
+ // 此时改为恢复到背景。
186
192
  if (CURRENT_FRAME_INDEX > -1) {
187
- TEMP_CANVAS_CTX.putImageData(FRAME_LIST[CURRENT_FRAME_INDEX].data, 0, 0);
193
+ TEMP_CANVAS_CTX.putImageData(FRAME_LIST[CURRENT_FRAME_INDEX].imgData, 0, 0);
188
194
  } else {
189
195
  TEMP_CANVAS_CTX.clearRect(0, 0, TEMP_CANVAS.width, TEMP_CANVAS.height);
190
196
  }
@@ -193,8 +199,8 @@ function GIFPlayer(options = {}) {
193
199
  }
194
200
 
195
201
  if (LAST_DISPOSA_METHOD === 2) {
196
- // Restore to background color
197
- // Browser implementations historically restore to transparent; we do the same.
202
+ // 恢复到背景色
203
+ // 浏览器实现历史上会恢复到透明;我们保持一致。
198
204
  // http://www.wizards-toolkit.org/discourse-server/viewtopic.php?f=1&t=21172#p86079
199
205
  TEMP_CANVAS_CTX.clearRect(0, 0, TEMP_CANVAS.width, TEMP_CANVAS.height);
200
206
  }
@@ -211,10 +217,10 @@ function GIFPlayer(options = {}) {
211
217
 
212
218
  TEMP_CANVAS_CTX.putImageData(imgData, img.leftPos, img.topPos);
213
219
 
214
- if (FRAME_LIST[CURRENT_FRAME_INDEX]) {
215
- FRAME_LIST[CURRENT_FRAME_INDEX].imgData = imgData;
216
- FRAME_LIST[CURRENT_FRAME_INDEX].leftPos = img.leftPos;
217
- FRAME_LIST[CURRENT_FRAME_INDEX].topPos = img.topPos;
220
+ if (FRAME_LIST[currIdx - 1]) {
221
+ FRAME_LIST[currIdx - 1].imgData = TEMP_CANVAS_CTX.getImageData(0, 0, TEMP_CANVAS.width, TEMP_CANVAS.height);
222
+ FRAME_LIST[currIdx - 1].leftPos = img.leftPos;
223
+ FRAME_LIST[currIdx - 1].topPos = img.topPos;
218
224
  // console.log(CURRENT_FRAME_INDEX, FRAME_LIST[CURRENT_FRAME_INDEX]);
219
225
  }
220
226
  };
@@ -252,7 +258,7 @@ function GIFPlayer(options = {}) {
252
258
  };
253
259
 
254
260
  function parsePTExt(block) {
255
- // No one *ever* uses this. If you use it, deal with parsing it yourself.
261
+ // 几乎没有人会使用它。如果你要用,请自行处理解析。
256
262
  STREAM.readByte(); // Always 12 这个必须得这样执行一次
257
263
  block.ptHeader = STREAM.readBytes(12);
258
264
  block.ptData = readSubBlocks();
@@ -319,8 +325,8 @@ function GIFPlayer(options = {}) {
319
325
  function parseImg(img) {
320
326
 
321
327
  function deinterlace(pixels, width) {
322
- // Of course this defeats the purpose of interlacing. And it's *probably*
323
- // the least efficient way it's ever been implemented. But nevertheless...
328
+ // 当然这会破坏隔行扫描的初衷。而且这*可能*
329
+ // 是有史以来效率最低的实现方式。但无论如何……
324
330
  let newPixels = new Array(pixels.length);
325
331
  const rows = pixels.length / width;
326
332
 
@@ -329,7 +335,7 @@ function GIFPlayer(options = {}) {
329
335
  newPixels.splice.apply(newPixels, [toRow * width, width].concat(fromPixels));
330
336
  };
331
337
 
332
- // See appendix E.
338
+ // 参见附录 E。
333
339
  const offsets = [0, 4, 2, 1],
334
340
  steps = [8, 8, 4, 2];
335
341