开发者

How to use putImageData with animated gifs using HTML5 canvas and Javascript?

开发者 https://www.devze.com 2023-02-25 06:48 出处:网络
I want to manipulate the image data of every frame of an animated gif as it plays in the browser, and I want to do it using HTML5 canvas and Javascript.For example, I have an animated gif, and I want

I want to manipulate the image data of every frame of an animated gif as it plays in the browser, and I want to do it using HTML5 canvas and Javascript. For example, I have an animated gif, and I want to convert every frame to grayscale as it plays in the browser. Is this possible? Whenever I manipulate the image data in an animated gif, only the first frame is shown (correctly manipulated) and the gif won't play any further. This would be a very useful technique if there was a solution for this. Thank you.

EDIT: Is there a way to开发者_运维问答 query the current frame of an animated gif in Javascript, so I can just use putImageData into the image as the animation progresses?


As this page points out, you can use drawImage() to draw the GIF on the canvas, and at the moment it'll draw the current frame from the GIF in Gecko and WebKit -- but not in Opera, and also not according to the spec, so it's not a good idea to rely on this behavior. As far as I know there's no way to get individual frame data from the DOM; I looked into this for a while before writing jsgif (which was mentioned in another answer, but isn't really meant for any sort of practical use as it is -- it's very inefficient, for one).

If you can get away with unpacking the frames on the server and then animating them manually (e.g. like Gmail does), that might be your best option.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号