I am writing an open source Falloutish 2 game in PHP + HTML + CSS + JavaScript. Right now, dealing with an engine. I have this gif: https://dl.dropboxusercontent.com/u/4258402/nmwarrgb_e.gif
... which I want to convert to a data URI. I need to do this because browsers cache images, and this one does not loop, therefore, in order to be able to “start it again” when the animation ends and the block goes to another plate, I need to convert it to a data URI. Otherwise, I would have to reload the image again and again, for example. adding a random line to the end of the image file. Which works well, but it transfers the gear too much and causes a lag.
This is the code I tried to use:
var image = new Image(); (..) this.convertImageObjectToDataURI = function (image) { var canvasTemp = document.createElement('canvas'); canvasTemp.width = image.naturalWidth; // or 'width' if you want a special/scaled size canvasTemp.height = image.naturalHeight; // or 'height' if you want a special/scaled size canvasTemp.getContext('2d').drawImage(image, 0, 0); var dataUri = canvasTemp.toDataURL('image/gif'); // Modify Data URI beginning dataUri = "data:image/gif;" + dataUri.substring(15); console.log(dataUri); // Return image as Data URI return dataUri; };
Unfortunately, it only creates data URIs for the first frame. I tried searching for plugins, read about html canvas, but still I just don't know how to convert an animated gif to a data URI . Any help would be very, very appreciated!
source share