HTML5 画布中的动画 GIF

发布于 2024-09-05 21:40:55 字数 70 浏览 2 评论 0原文

在 HTML5 中,如何在画布中轻松绘制(请不要太多复杂的代码)动画 GIF(使用 drawImage 仅在画布中显示第一帧)

In HTML5, how can I draw easily (no too much complex code please) an animated GIF in a canvas that works (with drawImage only first frame is shown in the canvas)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小姐丶请自重 2024-09-12 21:40:55

如果 gif 文件的自动画布动画不可用,您可以尝试使用 sprite-sheets,但这确实需要更多代码。

var img_obj = {
    'source': null,
    'current': 0,
    'total_frames': 16,
    'width': 16,
    'height': 16
};

var img = new Image();
img.onload = function () { // Triggered when image has finished loading.
    img_obj.source = img;  // we set the image source for our object.
}
img.src = 'img/filename.png'; // contains an image of size 256x16
                              // with 16 frames of size 16x16

function draw_anim(context, x, y, iobj) { // context is the canvas 2d context.
    if (iobj.source != null)
        context.drawImage(iobj.source, iobj.current * iobj.width, 0,
                          iobj.width, iobj.height,
                          x, y, iobj.width, iobj.height);
    iobj.current = (iobj.current + 1) % iobj.total_frames;
                   // incrementing the current frame and assuring animation loop
}

function on_body_load() { // <body onload='on_body_load()'>...
    var canvas = document.getElementById('canvasElement');
                 // <canvas id='canvasElement' width='320' height='200'/>
    var context = canvas.getContext("2d");

    setInterval((function (c, i) {
                return function () {
                    draw_anim(c, 10, 10, i);
                };
    })(context, img_obj), 100);
}

这就是我解决问题的方法。希望这对您有所帮助。 (已测试)

Well if automated canvas animation of gif files isn't available, you could try using sprite-sheets, but that indeed would require a bit more code.

var img_obj = {
    'source': null,
    'current': 0,
    'total_frames': 16,
    'width': 16,
    'height': 16
};

var img = new Image();
img.onload = function () { // Triggered when image has finished loading.
    img_obj.source = img;  // we set the image source for our object.
}
img.src = 'img/filename.png'; // contains an image of size 256x16
                              // with 16 frames of size 16x16

function draw_anim(context, x, y, iobj) { // context is the canvas 2d context.
    if (iobj.source != null)
        context.drawImage(iobj.source, iobj.current * iobj.width, 0,
                          iobj.width, iobj.height,
                          x, y, iobj.width, iobj.height);
    iobj.current = (iobj.current + 1) % iobj.total_frames;
                   // incrementing the current frame and assuring animation loop
}

function on_body_load() { // <body onload='on_body_load()'>...
    var canvas = document.getElementById('canvasElement');
                 // <canvas id='canvasElement' width='320' height='200'/>
    var context = canvas.getContext("2d");

    setInterval((function (c, i) {
                return function () {
                    draw_anim(c, 10, 10, i);
                };
    })(context, img_obj), 100);
}

This is how I'd tackle the problem. Hope this has been helpful. (tested)

怪我闹别瞎闹 2024-09-12 21:40:55

我相信您正在寻找 http://slbkbs.org/jsgif

不幸的是,DOM 不公开 GIF 的各个帧,因此这是通过下载 GIF(使用 XMLHttpRequest)、解析它并在 上绘制它来完成的。

I believe you are looking for http://slbkbs.org/jsgif

Unfortunately, the DOM doesn't expose individual frames of a GIF, so this is done by downloading the GIF (with XMLHttpRequest), parsing it, and drawing it on a <canvas>.

风柔一江水 2024-09-12 21:40:55

对于仍然遇到此问题的人,或者那些不想动态加载图像或弄清楚需要使用多少帧的人;

将动画 GIF 保留在 HTML 页面上,在 CSS 中设置为 display:block、visibility:visible 和position:relative。使用负的 margin-top/z-index (或者你有的)动态地将它定位在画布下。然后设置一个 JavaScript 计时器,根据您需要的速度重复调用drawImage,以便正确刷新图像。

如果图像在 DOM 中不可见,则 drawImage 例程无法获取动画的后续帧。如果图像绝对位于画布下方,则渲染会滞后。

For anyone still having trouble with this, or those who don't want to load their images dynamically or figure out how many frames they need to use;

Leave the animated GIF on the HTML page, set to display:block, visibility:visible, and position:relative in the CSS. Dynamically position it under the canvas with a negative margin-top/z-index (or what have you). Then set a JavaScript timer that calls drawImage repeatedly, as fast as you need in order to refresh the image properly.

If the image is not visible in the DOM, the drawImage routine cannot acquire subsequent frames of the animation. If the image is absolutely positioned under the canvas, the rendering lags.

故事与诗 2024-09-12 21:40:55

好吧,正如其他人已经说过的,你必须将 pe 动画分割成帧。我解决这个问题的方法更多的是个人喜好,但我在 GIMP 中打开 GIF 并使用插件将所有显示为单独图层的帧转换为精灵表。然后我只需要为画布中的精灵设置动画,这要容易得多。

这是插件的链接:D

Well, as other people said already, you have to split pe animation in frames. My way of tackling the problem is more of a personal preference, but I open the GIF in GIMP and using a plugin I convert all the frames which are displayed as individual layers into a sprite sheet. Then I only have to animate the sprite in the canvas which is much easier.

Here is the link for the plugin :D

半城柳色半声笛 2024-09-12 21:40:55

如果您已经在画布中编程了某种帧速率,那么将 gif 导出为图像序列可能是一个不错的策略。

在 Photoshop 中,您可以打开 gif(打开时间轴窗口),然后“文件 > 导出 > 渲染视频”并选择“Photoshop 图像序列”。

预加载它们,添加一个计数器,然后使用drawImage根据计数器绘制正确的图像。

If you already have programmed some kind of framerate into your canvas, it could be a good strategy to export the gif as a sequence of images.

In Photoshop you can open the gif (turn on timeline window) and then "File > Export > Render Video" and select "Photoshop Image Sequence".

Preload them all, add a counter and then just use drawImage to draw the correct image according to the counter.

白馒头 2024-09-12 21:40:55

Canvas 动画不是 HTML5 规范的一部分。要么恢复到 GIF,要么考虑使用基于 JavaScript 的库来呈现 SVG 或回退到 Canvas/VML:http://raphaeljs.com /

Canvas animation is not part of the HTML5 spec. Either revert to GIF or consider a JavaScript-based library that renders SVG or falls back to Canvas/VML: http://raphaeljs.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文