Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-3995 Frontend Gif Rendering (Firefox & Safari fix) (#7002)

vicgonzalez25687b9b81e31a
diff --git a/web/js/containers/gif.js b/web/js/containers/gif.js
index de2389455..9376a360b 100644
--- a/web/js/containers/gif.js
+++ b/web/js/containers/gif.js
@@ -25,7 +25,7 @@ import {
import { TIME_SCALE_FROM_NUMBER } from '../modules/date/constants';
import GifResults from '../components/animation-widget/gif-post-creation';
import getAnimationFrames from '../modules/animation/selectors';
-import { getStampProps, svgToPng, getNumberOfSteps } from '../modules/animation/util';
+import { getStampProps, svgToCanvas, getNumberOfSteps } from '../modules/animation/util';
import { changeCropBounds } from '../modules/animation/actions';
import { selectDate as selectDateAction } from '../modules/date/actions';
import { promiseImageryForTime } from '../modules/map/util';
@@ -213,7 +213,7 @@ class GIF extends Component {
height,
);
- const stamp = svgToPng('brand/images/wv-logo-w-shadow.svg', stampHeight);
+ const stamp = svgToCanvas('brand/images/wv-logo-w-shadow.svg', stampHeight);
gifStream.createGIF(
{
diff --git a/web/js/containers/gif.test.js b/web/js/containers/gif.test.js
index e7c56a420..572139e0b 100644
--- a/web/js/containers/gif.test.js
+++ b/web/js/containers/gif.test.js
@@ -116,7 +116,7 @@ jest.mock('../modules/animation/util', () => ({
fontSize: 20, x: 5, y: 6, align: 'left',
},
})),
- svgToPng: jest.fn(() => ({ width: 100, height: 30 })),
+ svgToCanvas: jest.fn(() => ({ width: 100, height: 30 })),
getNumberOfSteps: jest.fn(() => 12),
}));
@@ -136,7 +136,7 @@ const Gif = require('./gif').default;
const getAnimationFrames = require('../modules/animation/selectors').default;
const { captureAnimationFrames, captureMapBackdrop } = require('../modules/image-download/util');
const { promiseImageryForTime } = require('../modules/map/util');
-const { getStampProps, svgToPng, getNumberOfSteps } = require('../modules/animation/util');
+const { getStampProps, svgToCanvas, getNumberOfSteps } = require('../modules/animation/util');
const { changeCropBounds } = require('../modules/animation/actions');
const defaultProps = {
@@ -296,7 +296,7 @@ describe('GIF creation flow', () => {
}),
);
expect(getStampProps).toHaveBeenCalled();
- expect(svgToPng).toHaveBeenCalled();
+ expect(svgToCanvas).toHaveBeenCalled();
expect(mockCreateGIF).toHaveBeenCalled();
const [options, onComplete] = mockCreateGIF.mock.calls[0];
diff --git a/web/js/modules/animation/gifstream.js b/web/js/modules/animation/gifstream.js
index 42892fe51..1132dea46 100644
--- a/web/js/modules/animation/gifstream.js
+++ b/web/js/modules/animation/gifstream.js
@@ -258,6 +258,8 @@ export default class GifStream {
const height = options.gifHeight;
const totalImages = frames.length;
let processedImages = 0;
+ let nq;
+ let paletteArray;
const self = this;
const rs = new ReadableStream({
pull: function pull(controller) {
@@ -275,9 +277,12 @@ export default class GifStream {
ctx = self.addFrameDetails(ctx, frame);
const imgData = ctx.getImageData(0, 0, width, height);
const rgbComponents = dataToRGB(imgData.data, imgData.width, imgData.height);
- const nq = new NeuQuant(rgbComponents, rgbComponents.length, 15);
- const paletteRGB = nq.process();
- const paletteArray = new Uint32Array(componentizedPaletteToArray(paletteRGB));
+ if (!nq) {
+ // cache the color palette so the NASA swish don't change color
+ // and keep color consistant across all frames..
+ nq = new NeuQuant(rgbComponents, rgbComponents.length, 1);
+ paletteArray = new Uint32Array(componentizedPaletteToArray(nq.process()));
+ }
const numberPixels = imgData.height * imgData.width;
const pixels = new Uint8Array(imgData.height * imgData.width);
for (let i = 0; i < numberPixels; i++) {
diff --git a/web/js/modules/animation/util.js b/web/js/modules/animation/util.js
index 8f4f73f70..55acedb59 100644
--- a/web/js/modules/animation/util.js
+++ b/web/js/modules/animation/util.js
@@ -131,7 +131,7 @@ export function getStampProps(
return { stampHeight, dateStamp };
}
-export function svgToPng(svgURL, stampHeight) {
+export function svgToCanvas(svgURL, stampHeight) {
const canvasEl = document.createElement('canvas');
const canvgOptions = {
log: false,
@@ -139,12 +139,7 @@ export function svgToPng(svgURL, stampHeight) {
scaleHeight: stampHeight,
};
canvg(canvasEl, svgURL, canvgOptions);
- const newImage = new Image();
- newImage.src = canvasEl.toDataURL('image/png');
- newImage.width = canvasEl.width;
- newImage.height = canvasEl.height;
-
- return newImage;
+ return canvasEl;
}
export function mapLocationToAnimationState(