简体   繁体   English

Javascript:动画GIF到数据URI

[英]Javascript: Animated GIF to Data URI

I am writing open-source Falloutish 2 game in PHP+HTML+CSS+JavaScript. 我正在用PHP + HTML + CSS + JavaScript编写开源的Falloutish 2游戏。 Right now dealing with the engine. 现在正在处理引擎。 I have this gif: https://dl.dropboxusercontent.com/u/4258402/nmwarrgb_e.gif 我有这个gif: https//dl.dropboxusercontent.com/u/4258402/nmwarrgb_e.gif

...which I want to convert to Data URI. ...我想转换为数据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. 我需要这样做,因为浏览器缓存图像并且这个没有循环,所以为了能够在动画结束并且单元转到另一个图块时“再次运行”,我需要将其转换为数据URI。 Otherwise I'd be forced to re-download image over and over again eg 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. 不幸的是,它仅为第一帧生成数据URI。 I've tried searching plugins, reading about html canvas, but still I just don't know how to convert animated gif to data URI . 我试过搜索插件,阅读有关html画布,但仍然不知道如何将动画gif转换为数据URI Any help would be very, very mich welcomed! 任何帮助都会非常,非常欢迎!

It's not possible with javascript to read server files by this mode with compatibility all browsers, but I recommend you to use sprites instead of animated gifs. 使用兼容所有浏览器的javascript无法通过此模式读取服务器文件,但我建议您使用精灵而不是GIF动画。 A sprite is a png file (that allows alpha channel) with all frames drawn separated. 精灵是一个png文件(允许alpha通道),所有帧都是分开绘制的。 Imagine that file: 想象一下这个文件:

点精灵

As you can see, it's a 600x150 image (150x150 each frame, 4 frames). 如您所见,它是一张600x150的图像(每帧150x150,4帧)。 So you can animate with CSS animations like this: 所以你可以用这样的CSS动画制作动画:

 .dots { width: 150px; height: 150px; background-image: url(http://i.stack.imgur.com/bCHFq.png); -webkit-animation: play .4s steps(4) infinite; -moz-animation: play .4s steps(4) infinite; -ms-animation: play .4s steps(4) infinite; -o-animation: play .4s steps(4) infinite; animation: play .4s steps(4) infinite; } @-webkit-keyframes play { from { background-position: 0px; } to { background-position: -600px; } } @-moz-keyframes play { from { background-position: 0px; } to { background-position: -600px; } } @-ms-keyframes play { from { background-position: 0px; } to { background-position: -600px; } } @-o-keyframes play { from { background-position: 0px; } to { background-position: -600px; } } @keyframes play { from { background-position: 0px; } to { background-position: -600px; } } 
 <div class="dots"></div> 

With that, you can adjust the keyframes, the velocity, the number of frames (steps), and the most important: you don't overload CPU and RAM memory in the browser client. 这样,您可以调整关键帧,速度,帧数(步数)和最重要的:您不会在浏览器客户端中过载CPU和RAM内存。

Good luck. 祝好运。

I believe there is no direct way to do that using JavaScript. 我相信使用JavaScript没有直接的方法。 Using a canvas, you'd have to draw each individual frame onto it and encode them into a GIF file. 使用画布,您必须在其上绘制每个帧并将其编码为GIF文件。 This appears to be impossible using the current API as there is no way to extract single frames (except by manually parsing the GIF file). 使用当前API似乎是不可能的,因为无法提取单个帧(除非通过手动解析GIF文件)。

You might succeed by downloading the binary GIF file using JavaScript (remember cross-domain restrictions of HTTP requests) and generate a base64-encoded data URL. 您可以通过使用JavaScript下载二进制GIF文件(记住HTTP请求的跨域限制)并生成base64编码的数据URL来成功。 For example, you could use this example to get an Uint8Array which you can then convert to a base64 string . 例如,您可以使用此示例来获取Uint8Array,然后可以将其转换为base64字符串

You should probably consider generating the data URLs statically, not using JavaScript. 您应该考虑静态生成数据URL,而不是使用JavaScript。 For example, you can use this shell command to generate the data URL for your gif file: 例如,您可以使用此shell命令为您的gif文件生成数据URL:

echo "data:image/gif;base64,"`curl --silent 'https://dl.dropboxusercontent.com/u/4258402/nmwarrgb_e.gif' | base64 --wrap=0`

There is also a number of online services which will generate data URLs from files. 还有许多在线服务将从文件生成数据URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM