@jsenv/terminal-recorder 1.0.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 +51 -0
- package/dist/css/xterm.css +177 -0
- package/dist/js/terminal_recorder.js +1470 -0
- package/dist/xterm.html +26 -0
- package/package.json +47 -0
- package/src/client/gif_encoder.js +398 -0
- package/src/client/gif_encoder_dev/gif_encoder.js +567 -0
- package/src/client/gif_encoder_dev/gif_encoder_dev.html +130 -0
- package/src/client/gif_encoder_dev/lzwencoder.js +275 -0
- package/src/client/gif_encoder_dev/neuquant.js +510 -0
- package/src/client/gif_encoder_jsenv_dev/gif_encoder_jsenv_dev.html +94 -0
- package/src/client/lzw_encoder.js +253 -0
- package/src/client/neuquant.js +498 -0
- package/src/client/terminal_recorder.js +317 -0
- package/src/client/xterm.html +26 -0
- package/src/client/xterm_dev.html +48 -0
- package/src/main.js +4 -0
- package/src/svg/parse_ansi.js +375 -0
- package/src/svg/render_terminal_html.js +5 -0
- package/src/svg/render_terminal_svg.js +300 -0
- package/src/svg/xml_generator.js +149 -0
- package/src/terminal_recording.js +205 -0
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This class lets you encode animated GIF files
|
|
5
|
+
* Base class : http://www.java2s.com/Code/Java/2D-Graphics-GUI/AnimatedGifEncoder.htm
|
|
6
|
+
* @author Kevin Weiner (original Java version - kweiner@fmsware.com)
|
|
7
|
+
* @author Thibault Imbert (AS3 version - bytearray.org)
|
|
8
|
+
* @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
|
|
9
|
+
* @version 0.1 AS3 implementation
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
GIFEncoder = function () {
|
|
13
|
+
for (var i = 0, chr = {}; i < 256; i++) chr[i] = String.fromCharCode(i);
|
|
14
|
+
|
|
15
|
+
function ByteArray() {
|
|
16
|
+
this.bin = [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
ByteArray.prototype.getData = function () {
|
|
20
|
+
console.log(this.bin);
|
|
21
|
+
for (var v = "", l = this.bin.length, i = 0; i < l; i++)
|
|
22
|
+
v += chr[this.bin[i]];
|
|
23
|
+
return v;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
ByteArray.prototype.writeByte = function (val) {
|
|
27
|
+
this.bin.push(val);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
ByteArray.prototype.writeUTFBytes = function (string) {
|
|
31
|
+
for (var l = string.length, i = 0; i < l; i++)
|
|
32
|
+
this.writeByte(string.charCodeAt(i));
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
ByteArray.prototype.writeBytes = function (array, offset, length) {
|
|
36
|
+
for (var l = length || array.length, i = offset || 0; i < l; i++)
|
|
37
|
+
this.writeByte(array[i]);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
var exports = {};
|
|
41
|
+
var width; // image size
|
|
42
|
+
var height;
|
|
43
|
+
var transparent = null; // transparent color if given
|
|
44
|
+
var transIndex; // transparent index in color table
|
|
45
|
+
var repeat = -1; // no repeat
|
|
46
|
+
var delay = 0; // frame delay (hundredths)
|
|
47
|
+
var started = false; // ready to output frames
|
|
48
|
+
var out;
|
|
49
|
+
var image; // current frame
|
|
50
|
+
var pixels; // BGR byte array from frame
|
|
51
|
+
var indexedPixels; // converted frame indexed to palette
|
|
52
|
+
var colorDepth; // number of bit planes
|
|
53
|
+
var colorTab; // RGB palette
|
|
54
|
+
var usedEntry = []; // active palette entries
|
|
55
|
+
var palSize = 7; // color table size (bits-1)
|
|
56
|
+
var dispose = -1; // disposal code (-1 = use default)
|
|
57
|
+
var closeStream = false; // close stream when finished
|
|
58
|
+
var firstFrame = true;
|
|
59
|
+
var sizeSet = false; // if false, get size from first frame
|
|
60
|
+
var sample = 10; // default sample interval for quantizer
|
|
61
|
+
var comment = ""; // "Generated by jsgif (https://github.com/antimatter15/jsgif/)"; // default comment for generated gif
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Sets the delay time between each frame, or changes it for subsequent frames
|
|
65
|
+
* (applies to last frame added)
|
|
66
|
+
* int delay time in milliseconds
|
|
67
|
+
* @param ms
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
var setDelay = (exports.setDelay = function setDelay(ms) {
|
|
71
|
+
delay = Math.round(ms / 10);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Sets the GIF frame disposal code for the last added frame and any
|
|
76
|
+
*
|
|
77
|
+
* subsequent frames. Default is 0 if no transparent color has been set,
|
|
78
|
+
* otherwise 2.
|
|
79
|
+
* @param code
|
|
80
|
+
* int disposal code.
|
|
81
|
+
*/
|
|
82
|
+
var setDispose = (exports.setDispose = function setDispose(code) {
|
|
83
|
+
if (code >= 0) dispose = code;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Sets the number of times the set of GIF frames should be played. Default is
|
|
88
|
+
* 1; 0 means play indefinitely. Must be invoked before the first image is
|
|
89
|
+
* added.
|
|
90
|
+
*
|
|
91
|
+
* @param iter
|
|
92
|
+
* int number of iterations.
|
|
93
|
+
* @return
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
var setRepeat = (exports.setRepeat = function setRepeat(iter) {
|
|
97
|
+
if (iter >= 0) repeat = iter;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Sets the transparent color for the last added frame and any subsequent
|
|
102
|
+
* frames. Since all colors are subject to modification in the quantization
|
|
103
|
+
* process, the color in the final palette for each frame closest to the given
|
|
104
|
+
* color becomes the transparent color for that frame. May be set to null to
|
|
105
|
+
* indicate no transparent color.
|
|
106
|
+
* @param
|
|
107
|
+
* Color to be treated as transparent on display.
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
var setTransparent = (exports.setTransparent = function setTransparent(c) {
|
|
111
|
+
transparent = c;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Sets the comment for the block comment
|
|
116
|
+
* @param
|
|
117
|
+
* string to be insterted as comment
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
var setComment = (exports.setComment = function setComment(c) {
|
|
121
|
+
comment = c;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The addFrame method takes an incoming BitmapData object to create each frames
|
|
126
|
+
* @param
|
|
127
|
+
* BitmapData object to be treated as a GIF's frame
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
var addFrame = (exports.addFrame = function addFrame(im, is_imageData) {
|
|
131
|
+
if (im === null || !started || out === null) {
|
|
132
|
+
throw new Error("Please call start method before calling addFrame");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
var ok = true;
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
if (!is_imageData) {
|
|
139
|
+
image = im.getImageData(0, 0, im.canvas.width, im.canvas.height).data;
|
|
140
|
+
if (!sizeSet) setSize(im.canvas.width, im.canvas.height);
|
|
141
|
+
} else {
|
|
142
|
+
if (im instanceof ImageData) {
|
|
143
|
+
image = im.data;
|
|
144
|
+
if (!sizeset || width != im.width || height != im.height) {
|
|
145
|
+
setSize(im.width, im.height);
|
|
146
|
+
} else {
|
|
147
|
+
}
|
|
148
|
+
} else if (im instanceof Uint8ClampedArray) {
|
|
149
|
+
if (im.length == width * height * 4) {
|
|
150
|
+
image = im;
|
|
151
|
+
} else {
|
|
152
|
+
console.log(
|
|
153
|
+
"Please set the correct size: ImageData length mismatch",
|
|
154
|
+
);
|
|
155
|
+
ok = false;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
console.log("Please provide correct input");
|
|
159
|
+
ok = false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
getImagePixels(); // convert to correct format if necessary
|
|
163
|
+
analyzePixels(); // build color table & map pixels
|
|
164
|
+
|
|
165
|
+
if (firstFrame) {
|
|
166
|
+
writeLSD(); // logical screen descriptior
|
|
167
|
+
writePalette(); // global color table
|
|
168
|
+
if (repeat >= 0) {
|
|
169
|
+
// use NS app extension to indicate reps
|
|
170
|
+
writeNetscapeExt();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
writeGraphicCtrlExt(); // write graphic control extension
|
|
175
|
+
if (comment !== "") {
|
|
176
|
+
writeCommentExt(); // write comment extension
|
|
177
|
+
}
|
|
178
|
+
writeImageDesc(); // image descriptor
|
|
179
|
+
if (!firstFrame) writePalette(); // local color table
|
|
180
|
+
writePixels(); // encode and write pixel data
|
|
181
|
+
firstFrame = false;
|
|
182
|
+
} catch (e) {
|
|
183
|
+
ok = false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return ok;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @description: Downloads the encoded gif with the given name
|
|
191
|
+
* No need of any conversion from the stream data (out) to base64
|
|
192
|
+
* Solves the issue of large file sizes when there are more frames
|
|
193
|
+
* and does not involve in creation of any temporary data in the process
|
|
194
|
+
* so no wastage of memory, and speeds up the process of downloading
|
|
195
|
+
* to just calling this function.
|
|
196
|
+
* @parameter {String} filename filename used for downloading the gif
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
var download = (exports.download = function download(filename) {
|
|
200
|
+
if (out === null || closeStream == false) {
|
|
201
|
+
console.log(
|
|
202
|
+
"Please call start method and add frames and call finish method before calling download",
|
|
203
|
+
);
|
|
204
|
+
} else {
|
|
205
|
+
filename =
|
|
206
|
+
filename !== undefined
|
|
207
|
+
? filename.endsWith(".gif")
|
|
208
|
+
? filename
|
|
209
|
+
: filename + ".gif"
|
|
210
|
+
: "download.gif";
|
|
211
|
+
var templink = document.createElement("a");
|
|
212
|
+
templink.download = filename;
|
|
213
|
+
templink.href = URL.createObjectURL(
|
|
214
|
+
new Blob([new Uint8Array(out.bin)], { type: "image/gif" }),
|
|
215
|
+
);
|
|
216
|
+
templink.click();
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Adds final trailer to the GIF stream, if you don't call the finish method
|
|
222
|
+
* the GIF stream will not be valid.
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
var finish = (exports.finish = function finish() {
|
|
226
|
+
if (!started) return false;
|
|
227
|
+
|
|
228
|
+
var ok = true;
|
|
229
|
+
started = false;
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
out.writeByte(0x3b); // gif trailer
|
|
233
|
+
closeStream = true;
|
|
234
|
+
} catch (e) {
|
|
235
|
+
ok = false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return ok;
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Resets some members so that a new stream can be started.
|
|
243
|
+
* This method is actually called by the start method
|
|
244
|
+
*/
|
|
245
|
+
|
|
246
|
+
var reset = function reset() {
|
|
247
|
+
// reset for subsequent use
|
|
248
|
+
transIndex = 0;
|
|
249
|
+
image = null;
|
|
250
|
+
pixels = null;
|
|
251
|
+
indexedPixels = null;
|
|
252
|
+
colorTab = null;
|
|
253
|
+
closeStream = false;
|
|
254
|
+
firstFrame = true;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* * Sets frame rate in frames per second. Equivalent to
|
|
259
|
+
* <code>setDelay(1000/fps)</code>.
|
|
260
|
+
* @param fps
|
|
261
|
+
* float frame rate (frames per second)
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
var setFrameRate = (exports.setFrameRate = function setFrameRate(fps) {
|
|
265
|
+
if (fps != 0xf) delay = Math.round(100 / fps);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Sets quality of color quantization (conversion of images to the maximum 256
|
|
270
|
+
* colors allowed by the GIF specification). Lower values (minimum = 1)
|
|
271
|
+
* produce better colors, but slow processing significantly. 10 is the
|
|
272
|
+
* default, and produces good color mapping at reasonable speeds. Values
|
|
273
|
+
* greater than 20 do not yield significant improvements in speed.
|
|
274
|
+
* @param quality
|
|
275
|
+
* int greater than 0.
|
|
276
|
+
* @return
|
|
277
|
+
*/
|
|
278
|
+
|
|
279
|
+
var setQuality = (exports.setQuality = function setQuality(quality) {
|
|
280
|
+
if (quality < 1) quality = 1;
|
|
281
|
+
sample = quality;
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Sets the GIF frame size. The default size is the size of the first frame
|
|
286
|
+
* added if this method is not invoked.
|
|
287
|
+
* @param w
|
|
288
|
+
* int frame width.
|
|
289
|
+
* @param h
|
|
290
|
+
* int frame width.
|
|
291
|
+
*/
|
|
292
|
+
|
|
293
|
+
var setSize = (exports.setSize = function setSize(w, h) {
|
|
294
|
+
if (started && !firstFrame) return;
|
|
295
|
+
width = w;
|
|
296
|
+
height = h;
|
|
297
|
+
if (width < 1) width = 320;
|
|
298
|
+
if (height < 1) height = 240;
|
|
299
|
+
sizeSet = true;
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Initiates GIF file creation on the given stream.
|
|
304
|
+
* @param os
|
|
305
|
+
* OutputStream on which GIF images are written.
|
|
306
|
+
* @return false if initial write failed.
|
|
307
|
+
*/
|
|
308
|
+
|
|
309
|
+
var start = (exports.start = function start() {
|
|
310
|
+
reset();
|
|
311
|
+
var ok = true;
|
|
312
|
+
closeStream = false;
|
|
313
|
+
out = new ByteArray();
|
|
314
|
+
try {
|
|
315
|
+
out.writeUTFBytes("GIF89a"); // header
|
|
316
|
+
} catch (e) {
|
|
317
|
+
ok = false;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return (started = ok);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
var cont = (exports.cont = function cont() {
|
|
324
|
+
reset();
|
|
325
|
+
var ok = true;
|
|
326
|
+
closeStream = false;
|
|
327
|
+
out = new ByteArray();
|
|
328
|
+
|
|
329
|
+
return (started = ok);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Analyzes image colors and creates color map.
|
|
334
|
+
*/
|
|
335
|
+
|
|
336
|
+
var analyzePixels = function analyzePixels() {
|
|
337
|
+
var len = pixels.length;
|
|
338
|
+
var nPix = len / 3;
|
|
339
|
+
indexedPixels = [];
|
|
340
|
+
console.log("analyze", pixels);
|
|
341
|
+
var nq = new NeuQuant(pixels, len, sample);
|
|
342
|
+
|
|
343
|
+
// initialize quantizer
|
|
344
|
+
colorTab = nq.process(); // create reduced palette
|
|
345
|
+
console.log("colorTab", colorTab);
|
|
346
|
+
|
|
347
|
+
// map image pixels to new palette
|
|
348
|
+
var k = 0;
|
|
349
|
+
for (var j = 0; j < nPix; j++) {
|
|
350
|
+
var index = nq.map(
|
|
351
|
+
pixels[k++] & 0xff,
|
|
352
|
+
pixels[k++] & 0xff,
|
|
353
|
+
pixels[k++] & 0xff,
|
|
354
|
+
);
|
|
355
|
+
usedEntry[index] = true;
|
|
356
|
+
indexedPixels[j] = index;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
pixels = null;
|
|
360
|
+
colorDepth = 8;
|
|
361
|
+
palSize = 7;
|
|
362
|
+
|
|
363
|
+
// get closest match to transparent color if specified
|
|
364
|
+
if (transparent !== null) {
|
|
365
|
+
transIndex = findClosest(transparent);
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Returns index of palette color closest to c
|
|
371
|
+
*/
|
|
372
|
+
|
|
373
|
+
var findClosest = function findClosest(c) {
|
|
374
|
+
if (colorTab === null) return -1;
|
|
375
|
+
var r = (c & 0xff0000) >> 16;
|
|
376
|
+
var g = (c & 0x00ff00) >> 8;
|
|
377
|
+
var b = c & 0x0000ff;
|
|
378
|
+
var minpos = 0;
|
|
379
|
+
var dmin = 256 * 256 * 256;
|
|
380
|
+
var len = colorTab.length;
|
|
381
|
+
|
|
382
|
+
for (var i = 0; i < len; ) {
|
|
383
|
+
var dr = r - (colorTab[i++] & 0xff);
|
|
384
|
+
var dg = g - (colorTab[i++] & 0xff);
|
|
385
|
+
var db = b - (colorTab[i] & 0xff);
|
|
386
|
+
var d = dr * dr + dg * dg + db * db;
|
|
387
|
+
var index = i / 3;
|
|
388
|
+
if (usedEntry[index] && d < dmin) {
|
|
389
|
+
dmin = d;
|
|
390
|
+
minpos = index;
|
|
391
|
+
}
|
|
392
|
+
i++;
|
|
393
|
+
}
|
|
394
|
+
return minpos;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Extracts image pixels into byte array "pixels
|
|
399
|
+
*/
|
|
400
|
+
|
|
401
|
+
var getImagePixels = function getImagePixels() {
|
|
402
|
+
var w = width;
|
|
403
|
+
var h = height;
|
|
404
|
+
pixels = [];
|
|
405
|
+
var data = image;
|
|
406
|
+
var count = 0;
|
|
407
|
+
|
|
408
|
+
for (var i = 0; i < h; i++) {
|
|
409
|
+
for (var j = 0; j < w; j++) {
|
|
410
|
+
var b = i * w * 4 + j * 4;
|
|
411
|
+
pixels[count++] = data[b];
|
|
412
|
+
pixels[count++] = data[b + 1];
|
|
413
|
+
pixels[count++] = data[b + 2];
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Writes Graphic Control Extension
|
|
420
|
+
*/
|
|
421
|
+
|
|
422
|
+
var writeGraphicCtrlExt = function writeGraphicCtrlExt() {
|
|
423
|
+
out.writeByte(0x21); // extension introducer
|
|
424
|
+
out.writeByte(0xf9); // GCE label
|
|
425
|
+
out.writeByte(4); // data block size
|
|
426
|
+
var transp;
|
|
427
|
+
var disp;
|
|
428
|
+
if (transparent === null) {
|
|
429
|
+
transp = 0;
|
|
430
|
+
disp = 0; // dispose = no action
|
|
431
|
+
} else {
|
|
432
|
+
transp = 1;
|
|
433
|
+
disp = 2; // force clear if using transparent color
|
|
434
|
+
}
|
|
435
|
+
if (dispose >= 0) {
|
|
436
|
+
disp = dispose & 7; // user override
|
|
437
|
+
}
|
|
438
|
+
disp <<= 2;
|
|
439
|
+
// packed fields
|
|
440
|
+
out.writeByte(
|
|
441
|
+
0 | // 1:3 reserved
|
|
442
|
+
disp | // 4:6 disposal
|
|
443
|
+
0 | // 7 user input - 0 = none
|
|
444
|
+
transp,
|
|
445
|
+
); // 8 transparency flag
|
|
446
|
+
|
|
447
|
+
WriteShort(delay); // delay x 1/100 sec
|
|
448
|
+
out.writeByte(transIndex); // transparent color index
|
|
449
|
+
out.writeByte(0); // block terminator
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Writes Comment Extention
|
|
454
|
+
*/
|
|
455
|
+
|
|
456
|
+
var writeCommentExt = function writeCommentExt() {
|
|
457
|
+
out.writeByte(0x21); // extension introducer
|
|
458
|
+
out.writeByte(0xfe); // comment label
|
|
459
|
+
out.writeByte(comment.length); // Block Size (s)
|
|
460
|
+
out.writeUTFBytes(comment);
|
|
461
|
+
out.writeByte(0); // block terminator
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Writes Image Descriptor
|
|
466
|
+
*/
|
|
467
|
+
|
|
468
|
+
var writeImageDesc = function writeImageDesc() {
|
|
469
|
+
out.writeByte(0x2c); // image separator
|
|
470
|
+
WriteShort(0); // image position x,y = 0,0
|
|
471
|
+
WriteShort(0);
|
|
472
|
+
WriteShort(width); // image size
|
|
473
|
+
WriteShort(height);
|
|
474
|
+
|
|
475
|
+
// packed fields
|
|
476
|
+
if (firstFrame) {
|
|
477
|
+
// no LCT - GCT is used for first (or only) frame
|
|
478
|
+
out.writeByte(0);
|
|
479
|
+
} else {
|
|
480
|
+
// specify normal LCT
|
|
481
|
+
out.writeByte(
|
|
482
|
+
0x80 | // 1 local color table 1=yes
|
|
483
|
+
0 | // 2 interlace - 0=no
|
|
484
|
+
0 | // 3 sorted - 0=no
|
|
485
|
+
0 | // 4-5 reserved
|
|
486
|
+
palSize,
|
|
487
|
+
); // 6-8 size of color table
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Writes Logical Screen Descriptor
|
|
493
|
+
*/
|
|
494
|
+
|
|
495
|
+
var writeLSD = function writeLSD() {
|
|
496
|
+
// logical screen size
|
|
497
|
+
WriteShort(width);
|
|
498
|
+
WriteShort(height);
|
|
499
|
+
// packed fields
|
|
500
|
+
out.writeByte(
|
|
501
|
+
0x80 | // 1 : global color table flag = 1 (gct used)
|
|
502
|
+
0x70 | // 2-4 : color resolution = 7
|
|
503
|
+
0x00 | // 5 : gct sort flag = 0
|
|
504
|
+
palSize,
|
|
505
|
+
); // 6-8 : gct size
|
|
506
|
+
|
|
507
|
+
out.writeByte(0); // background color index
|
|
508
|
+
out.writeByte(0); // pixel aspect ratio - assume 1:1
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Writes Netscape application extension to define repeat count.
|
|
513
|
+
*/
|
|
514
|
+
|
|
515
|
+
var writeNetscapeExt = function writeNetscapeExt() {
|
|
516
|
+
out.writeByte(0x21); // extension introducer
|
|
517
|
+
out.writeByte(0xff); // app extension label
|
|
518
|
+
out.writeByte(11); // block size
|
|
519
|
+
out.writeUTFBytes("NETSCAPE" + "2.0"); // app id + auth code
|
|
520
|
+
out.writeByte(3); // sub-block size
|
|
521
|
+
out.writeByte(1); // loop sub-block id
|
|
522
|
+
WriteShort(repeat); // loop count (extra iterations, 0=repeat forever)
|
|
523
|
+
out.writeByte(0); // block terminator
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Writes color table
|
|
528
|
+
*/
|
|
529
|
+
|
|
530
|
+
var writePalette = function writePalette() {
|
|
531
|
+
out.writeBytes(colorTab);
|
|
532
|
+
var n = 3 * 256 - colorTab.length;
|
|
533
|
+
for (var i = 0; i < n; i++) out.writeByte(0);
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
var WriteShort = function WriteShort(pValue) {
|
|
537
|
+
out.writeByte(pValue & 0xff);
|
|
538
|
+
out.writeByte((pValue >> 8) & 0xff);
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Encodes and writes pixel data
|
|
543
|
+
*/
|
|
544
|
+
|
|
545
|
+
var writePixels = function writePixels() {
|
|
546
|
+
var myencoder = new LZWEncoder(width, height, indexedPixels, colorDepth);
|
|
547
|
+
myencoder.encode(out);
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Retrieves the GIF stream
|
|
552
|
+
*/
|
|
553
|
+
|
|
554
|
+
var stream = (exports.stream = function stream() {
|
|
555
|
+
return out;
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
var setProperties = (exports.setProperties = function setProperties(
|
|
559
|
+
has_start,
|
|
560
|
+
is_first,
|
|
561
|
+
) {
|
|
562
|
+
started = has_start;
|
|
563
|
+
firstFrame = is_first;
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
return exports;
|
|
567
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
TODO: a canvas to gif encoding using video to gif source code
|
|
3
|
+
https://github.com/incubated-geek-cc/video-to-GIF/tree/main?tab=readme-ov-fil
|
|
4
|
+
-->
|
|
5
|
+
|
|
6
|
+
<!doctype html>
|
|
7
|
+
<html>
|
|
8
|
+
<head>
|
|
9
|
+
<title>Title</title>
|
|
10
|
+
<meta charset="utf-8" />
|
|
11
|
+
<link rel="icon" href="data:," />
|
|
12
|
+
</head>
|
|
13
|
+
|
|
14
|
+
<body>
|
|
15
|
+
<div style="display: flex">
|
|
16
|
+
<canvas id="canvas"></canvas>
|
|
17
|
+
<img src="data:," />
|
|
18
|
+
</div>
|
|
19
|
+
<script type="text/javascript" src="./lzwencoder.js"></script>
|
|
20
|
+
<script type="text/javascript" src="./neuquant.js"></script>
|
|
21
|
+
<script type="text/javascript" src="./gif_encoder.js"></script>
|
|
22
|
+
<script type="module">
|
|
23
|
+
/* globals GIFEncoder */
|
|
24
|
+
const width = 100;
|
|
25
|
+
const height = 200;
|
|
26
|
+
const canvas = document.querySelector("canvas");
|
|
27
|
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
28
|
+
canvas.width = width;
|
|
29
|
+
canvas.height = height;
|
|
30
|
+
|
|
31
|
+
const encoder = new GIFEncoder(canvas.width, canvas.height);
|
|
32
|
+
encoder.setRepeat(0); // 0 for repeat, -1 for no-repeat
|
|
33
|
+
encoder.setDelay(0); // frame delay in ms // 500
|
|
34
|
+
encoder.setQuality(16); // [1,30] | Best=1 | >20 not much speed improvement. 10 is default.
|
|
35
|
+
encoder.start();
|
|
36
|
+
|
|
37
|
+
let previousMs = Date.now();
|
|
38
|
+
|
|
39
|
+
const updateCanvasColor = (color) => {
|
|
40
|
+
drawRectangle(context, {
|
|
41
|
+
x: 0,
|
|
42
|
+
y: 0,
|
|
43
|
+
width: canvas.width,
|
|
44
|
+
height: canvas.height,
|
|
45
|
+
fill: color,
|
|
46
|
+
});
|
|
47
|
+
const delay = Date.now() - previousMs;
|
|
48
|
+
encoder.setDelay(delay);
|
|
49
|
+
encoder.addFrame(context);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const drawRectangle = (
|
|
53
|
+
context,
|
|
54
|
+
{ x, y, width, height, radius, fill, stroke, strokeWidth },
|
|
55
|
+
) => {
|
|
56
|
+
if (radius) {
|
|
57
|
+
context.beginPath();
|
|
58
|
+
context.roundRect(x, y, width, height, [radius]);
|
|
59
|
+
if (fill) {
|
|
60
|
+
context.fillStyle = fill;
|
|
61
|
+
context.fill();
|
|
62
|
+
}
|
|
63
|
+
if (stroke) {
|
|
64
|
+
context.strokeWidth = strokeWidth;
|
|
65
|
+
context.strokeStyle = stroke;
|
|
66
|
+
context.stroke();
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
context.beginPath();
|
|
71
|
+
context.rect(x, y, width, height);
|
|
72
|
+
if (fill) {
|
|
73
|
+
context.fillStyle = fill;
|
|
74
|
+
context.fill();
|
|
75
|
+
}
|
|
76
|
+
if (stroke) {
|
|
77
|
+
context.strokeWidth = strokeWidth;
|
|
78
|
+
context.strokeStyle = stroke;
|
|
79
|
+
context.stroke();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function encode64(input) {
|
|
84
|
+
var output = "";
|
|
85
|
+
var i = 0;
|
|
86
|
+
var l = input.length;
|
|
87
|
+
var key =
|
|
88
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
89
|
+
var chr1;
|
|
90
|
+
var chr2;
|
|
91
|
+
var chr3;
|
|
92
|
+
var enc1;
|
|
93
|
+
var enc2;
|
|
94
|
+
var enc3;
|
|
95
|
+
var enc4;
|
|
96
|
+
while (i < l) {
|
|
97
|
+
chr1 = input.charCodeAt(i++);
|
|
98
|
+
chr2 = input.charCodeAt(i++);
|
|
99
|
+
chr3 = input.charCodeAt(i++);
|
|
100
|
+
enc1 = chr1 >> 2;
|
|
101
|
+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
102
|
+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
103
|
+
enc4 = chr3 & 63;
|
|
104
|
+
if (isNaN(chr2)) enc3 = enc4 = 64;
|
|
105
|
+
else if (isNaN(chr3)) enc4 = 64;
|
|
106
|
+
output =
|
|
107
|
+
output +
|
|
108
|
+
key.charAt(enc1) +
|
|
109
|
+
key.charAt(enc2) +
|
|
110
|
+
key.charAt(enc3) +
|
|
111
|
+
key.charAt(enc4);
|
|
112
|
+
}
|
|
113
|
+
return output;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
updateCanvasColor("red");
|
|
117
|
+
await new Promise((resolve) => {
|
|
118
|
+
setTimeout(resolve, 400);
|
|
119
|
+
});
|
|
120
|
+
updateCanvasColor("blue");
|
|
121
|
+
encoder.finish();
|
|
122
|
+
|
|
123
|
+
const readableStream = encoder.stream();
|
|
124
|
+
const binary_gif = readableStream.getData();
|
|
125
|
+
console.log(binary_gif);
|
|
126
|
+
const b64Str = `data:image/gif;base64,${encode64(binary_gif)}`;
|
|
127
|
+
document.querySelector("img").src = b64Str;
|
|
128
|
+
</script>
|
|
129
|
+
</body>
|
|
130
|
+
</html>
|