Last active
February 20, 2020 00:44
-
-
Save JakeTrock/23b400ddbc3d230606b8aeb17013042d to your computer and use it in GitHub Desktop.
A simple gif processing extension for pdfkit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GIF from 'gifuct-js/dist/gifuct-js.js' | |
class GIFImage { | |
constructor(data, label) { | |
var oReq = new XMLHttpRequest(); | |
oReq.open("GET", gifURL, true); | |
oReq.responseType = "arraybuffer"; | |
oReq.onload = function (oEvent) { | |
var arrayBuffer = oReq.response; // Note: not oReq.responseText | |
if (arrayBuffer) { | |
var gif = new GIF(arrayBuffer); | |
var frames = gif.decompressFrames(true); | |
this.label = label; | |
this.image = frames[0]; | |
this.width = this.image.dims.width; | |
this.height = this.image.dims.height; | |
this.imgData = this.image.pixels; | |
this.obj = null; | |
} | |
}; | |
oReq.send(null); | |
} | |
embed(document) { | |
if (this.obj) { | |
return; | |
} | |
this.obj = document.ref({ | |
Type: 'XObject', | |
Subtype: 'Image', | |
BitsPerComponent: 8,// I guessed 8 bpp would be accurate according to this: https://en.wikipedia.org/wiki/GIF | |
Width: this.width, | |
Height: this.height, | |
Filter: ''//not sure what to put here | |
}); | |
this.obj.end(this.imgData); | |
// free memory | |
return (this.data = null); | |
} | |
} | |
export default GIFImage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment