I am writing open-source Falloutish 2 game in PHP+HTML+CSS+JavaScript. Right now dealing with the engine. I have this gif: https://dl.dropboxusercontent.com/u/4258402/nmwarrgb_e.gif
...which I want to convert to Data URI. I need to do this because browsers cache images and this one is not looped, so in order to be able to "run it again" when animation ends and unit goes to another tile, I need to convert it to data URI. Otherwise I'd be forced to re-download image over and over again e.g. by adding random string to the end of image file. Which works fine, but takes way too much transfer and causes 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 produces Data URI only for the first frame. I've tried searching plugins, reading about html canvas, but still I just don't know how to convert animated gif to data URI. Any help would be very, very mich welcomed!