HTML5 中的逐帧动画与画布

发布于 2024-10-13 06:26:30 字数 743 浏览 2 评论 0原文

我有一个 Flash 动画,我正在尝试将其转换为 HTML5。现在我已经把所有的图片都拿出来了。例如,在手部动画中,我拍摄了所有手部图像。我已经用基础绘图制作了画布,但我不知道如何逐帧替换这些图像。

function draw(){
    var canvas = document.getElementById('canvas');
    if(canvas.getContext){
        // canvas animation code here:
        var ctx = canvas.getContext('2d');

        var lhs = new Image();

        lhs.src = "images/left_hnd_1.png";

        lhs.onload = function(){
            ctx.drawImage(lhs, 293, 137);
        }

    } else {
        // canvas unsupported code here:
        document.getElementById('girl').style.display = "block";
    }
}

现在我为该图像添加了三个框架。 left_hnd_2.png、left_hnd_3.png 和left_hnd_4.png。我本来会使用一张图像,但帧之间的差异太大,无法用一张图像来完成。我怎样才能用我想要的时差来动画化它。

任何想法将不胜感激。谢谢!

I have a flash animation I am trying to convert to HTML5. Now I have taken out all the images. For example in the hand animation, I have taken images of all hand images. I have made the canvas with the base drawing but I don't know how to replace those images frame by frame.

function draw(){
    var canvas = document.getElementById('canvas');
    if(canvas.getContext){
        // canvas animation code here:
        var ctx = canvas.getContext('2d');

        var lhs = new Image();

        lhs.src = "images/left_hnd_1.png";

        lhs.onload = function(){
            ctx.drawImage(lhs, 293, 137);
        }

    } else {
        // canvas unsupported code here:
        document.getElementById('girl').style.display = "block";
    }
}

Now I have three more frame for this image. left_hnd_2.png, left_hnd_3.png & left_hnd_4.png. I would've used one image but the difference in frames is way too much for it to be done with one image. How can I animate this with the time differences I want.

Any ideas would be greatly appreciated. Thanks!

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

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

发布评论

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

评论(2

半﹌身腐败 2024-10-20 06:26:30

试试这个:

var imgNumber = 1;
var lastImgNumber = 4;

var ctx = canvas.getContext('2d');
var img = new Image;
img.onload = function(){
  ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height );
  ctx.drawImage( img, 0, 0 );
};
var timer = setInterval( function(){
  if (imgNumber>lastImgNumber){
    clearInterval( timer );
  }else{
    img.src = "images/left_hnd_"+( imgNumber++ )+".png";
  }
}, 1000/15 ); //Draw at 15 frames per second

如果您只有 4 个图像,另一种方法是创建一个巨大的图像,其中所有四个图像都位于“纹理图集”中,然后使用 setTimeoutsetInterval 使用不同的参数调用drawImage(),将图像的不同子集绘制到画布上。

Try this:

var imgNumber = 1;
var lastImgNumber = 4;

var ctx = canvas.getContext('2d');
var img = new Image;
img.onload = function(){
  ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height );
  ctx.drawImage( img, 0, 0 );
};
var timer = setInterval( function(){
  if (imgNumber>lastImgNumber){
    clearInterval( timer );
  }else{
    img.src = "images/left_hnd_"+( imgNumber++ )+".png";
  }
}, 1000/15 ); //Draw at 15 frames per second

An alternative, if you only have 4 images, would be to create a single huge image with all four in a 'texture atlas', and then use setTimeout or setInterval to call drawImage() with different parameters to draw different subsets of the image to the canvas.

2024-10-20 06:26:30

这对我也有用!由于某种原因,当我使用OP的打开代码时它不起作用:functiondraw(){

但是当我使用时:window.onload = functiondraw(){ > 动画在画布上播放。我还使用了大约 150 个带有 Alpha 通道的 PNG 图像,因此这是将“视频”或创建合成到 iPad/iPhone 的好方法。我确认它可以在 iPad iOS 4.3 上运行。

This worked for me as well! For some reason, it didn't work when I had used the OP's opening code: function draw(){

However when I used: window.onload = function draw() { the animation plays on the canvas. I'm also using about 150 PNG images with an Alpha channel so this is a great way to bring 'video' or create composites to the iPad/iPhone. I confirm that it does work on iPad iOS 4.3.

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