Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Create gif-creater class #816

Benjamin King5cd1d4969d4e
diff --git a/web/js/animation/gif-creater.js b/web/js/animation/gif-creater.js
new file mode 100644
index 000000000..aad11d608
--- /dev/null
+++ b/web/js/animation/gif-creater.js
@@ -0,0 +1,179 @@
+import GifWriter from 'gifwriter';
+import NeuQuant from 'neuquant';
+import { ReadableStream } from 'web-streams-polyfill';
+
+export default class gifCreater {
+/**
+ * Add frame contents to canvas
+ *
+ * https://github.com/yahoo/gifshot/blob/master/src/modules/core/AnimatedGIF.js
+ *
+ * @param {Object} ctx 2d Canvas Context
+ * @param {Object} img Image Element
+ * @param {String} frameText Text to be applied to this string
+ *
+ * @return {Object} 2d Canvas Context
+ */
+ addFrameDetails(ctx, img) {
+ const options = this.options;
+ const frameText = img.text;
+ const {
+ fontColor,
+ fontSize,
+ fontFamily,
+ fontWeight,
+ gifHeight,
+ gifWidth,
+ text,
+ textAlign,
+ textBaseline,
+ waterMark,
+ waterMarkHeight,
+ waterMarkWidth,
+ waterMarkXCoordinate,
+ waterMarkYCoordinate
+ } = options;
+ const textXCoordinate = options.textXCoordinate ? options.textXCoordinate : textAlign === 'left' ? 1 : textAlign === 'right' ? gifWidth : gifWidth / 2;
+ const textYCoordinate = options.textYCoordinate ? options.textYCoordinate : textBaseline === 'top' ? 1 : textBaseline === 'center' ? gifHeight / 2 : gifHeight;
+ const font = fontWeight + ' ' + fontSize + ' ' + fontFamily;
+ const textToUse = (frameText && options.showFrameText) ? frameText : text;
+
+ try {
+ ctx.drawImage(img, 0, 0, gifWidth, gifHeight);
+ if (textToUse) {
+ ctx.font = font;
+ ctx.fillStyle = fontColor;
+ ctx.textAlign = textAlign;
+ ctx.textBaseline = textBaseline;
+ if (options.stroke) {
+ const stroke = options.stroke;
+ ctx.strokeStyle = stroke.color;
+ ctx.lineWidth = stroke.pixels * 2;
+ ctx.strokeText(frameText, textXCoordinate, textYCoordinate);
+ }
+ ctx.fillText(textToUse, textXCoordinate, textYCoordinate);
+ }
+ if (waterMark) {
+ ctx.drawImage(waterMark, waterMarkXCoordinate, waterMarkYCoordinate, waterMarkWidth, waterMarkHeight);
+ }
+ return ctx;
+ } catch (e) {
+ return '' + e;
+ }
+ }
+ /**
+ * Create GIF from options
+ *
+ * @param {Object} options GIF options
+ * @param {Function} callback Function to call once GIF is created
+ * @return {void}
+ */
+ createGIF(options, callback) {
+ var cv = document.createElement('canvas');
+ var ctx = cv.getContext('2d');
+ this.options = options;
+ cv.width = options.gifWidth;
+ cv.height = options.gifHeight;
+ var chunks = [];
+ var gifStream = this.getStream(options.images, ctx);
+ var reader = gifStream.getReader();
+
+ var pull = function() {
+ return reader.read().then(function (result) {
+ chunks.push(result.value);
+ return result.done ? chunks : pull();
+ });
+ };
+ pull().then(function (chunks) {
+ callback(new Blob(chunks, { type: 'image/gif' }));
+ });
+ }
+ /**
+ * Generate GIF-creating Stream
+ * @param {Array} frames Array of GIF image objects
+ * @param {Object} ctx 2d Canvas Cntext
+ * @return {Object} Returns GifWriter Stream
+ */
+ getStream(frames, ctx) {
+ var options = this.options;
+ var width = options.gifWidth;
+ var height = options.gifHeight;
+ var pixels = new Uint8Array(width * height);
+ var totalImages = frames.length;
+ var processedImages = 0;
+ var self = this;
+ var rs = new ReadableStream({
+ // Each time pull gets called you should get the pixel data and
+ // enqueue it as if it would be good old gif.addFrame()
+ pull: function pull(controller) {
+ var frame = frames.shift();
+ if (!frame) controller.close();
+ return new Promise((resolve, reject) => {
+ var img = new Image();
+ img.width = width;
+ img.height = height;
+ img.text = frame.text;
+ img.delay = frame.delay;
+ img.crossOrigin = 'Anonymous';
+ img.onload = () => {
+ resolve(img);
+ };
+ img.src = frame.src;
+ }).then((img) => {
+ var data, rgbComponents, paletteRGB, nq, paletteArray, numberPixels;
+ var r, g, b;
+ var k = 0;
+ var delay = (img.delay) ? img.delay / 10 : 100;
+ processedImages++;
+ delay = (processedImages === totalImages && options.extraLastFrameDelay) ? delay + (options.extraLastFrameDelay / 10) : delay;// Add an extra
+ options.progressCallback(Math.round((processedImages / totalImages) * 100));
+ ctx = self.addFrameDetails(ctx, img);
+ data = ctx.getImageData(0, 0, width, height).data;
+ rgbComponents = dataToRGB(data, width, height);
+ nq = new NeuQuant(rgbComponents, rgbComponents.length, 15);
+ paletteRGB = nq.process();
+ paletteArray = new Uint32Array(componentizedPaletteToArray(paletteRGB));
+ numberPixels = width * height;
+ for (var i = 0; i < numberPixels; i++) {
+ r = rgbComponents[k++];
+ g = rgbComponents[k++];
+ b = rgbComponents[k++];
+ pixels[i] = nq.map(r, g, b);
+ }
+ controller.enqueue([0, 0, width, height, pixels, {
+ palette: new Uint32Array(paletteArray),
+ delay: delay
+ }]);
+ });
+ }
+ });
+ return new GifWriter(rs, width, height, {
+ loop: options.loop || 0 // From GIF: 0 = loop forever, null = not looping, n > 0 = loop n times and stop
+ });
+ }
+}
+// part of neuquant conversion
+function componentizedPaletteToArray(paletteRGB) {
+ var paletteArray = [];
+ var r, g, b;
+ for (var i = 0; i < paletteRGB.length; i += 3) {
+ r = paletteRGB[i];
+ g = paletteRGB[i + 1];
+ b = paletteRGB[i + 2];
+ paletteArray.push(r << 16 | g << 8 | b);
+ }
+ return paletteArray;
+};
+// part of neuquant conversion
+function dataToRGB(data, width, height) {
+ var i = 0;
+ var length = width * height * 4;
+ var rgb = [];
+ while (i < length) {
+ rgb.push(data[i++]);
+ rgb.push(data[i++]);
+ rgb.push(data[i++]);
+ i++;
+ }
+ return rgb;
+}