HTML5 中的逐帧动画与画布
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
如果您只有 4 个图像,另一种方法是创建一个巨大的图像,其中所有四个图像都位于“纹理图集”中,然后使用
setTimeout
或setInterval
使用不同的参数调用drawImage()
,将图像的不同子集绘制到画布上。Try this:
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
orsetInterval
to calldrawImage()
with different parameters to draw different subsets of the image to the canvas.这对我也有用!由于某种原因,当我使用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.