简体   繁体   English

加载和播放图像序列作为html5中的动画的最佳方法?

[英]Best way to load and play an image sequence as an animation in html5?

I just want to hear if anyone has any suggestions on which approach to take when it comes to playing a short animation consisting of an image sequence in hmtl5. 我只想听听是否有人在播放包含hmtl5中图像序列的简短动画时采取哪种方法有建议。 The wider the browser support the better, and it needs to work for iOS. 浏览器支持的范围越广越好,它需要在iOS上运行。 Canvas can be assumed. 可以假设使用画布。

Requirements: 要求:

  1. Be able to start the animation without user interaction 无需用户干预即可开始动画
  2. Have precise control of which frame to show (eg go to a specific frame, or start from the first frame and play up to the n:th frame) 精确控制要显示的帧(例如,转到特定帧,或者从第一帧开始播放直到第n帧)
  3. Be able to scale the animation (to fit the browser window) 能够缩放动画(以适合浏览器窗口)
  4. Be able to play at least 3 seconds at ~30fps (90 frames) 能够以〜30fps(90帧)播放至少3秒
  5. Support resolution of at least 1280x720 支持分辨率至少为1280x720

From a file size/performance point of view having the animation encoded as a video file would be best, but it doesn't fulfill 1) on iOS, and most likely fails to fulfill 2). 从文件大小/性能的角度来看,最好将动画编码为视频文件,但在iOS上不能满足1),很可能无法满足2)。

The solution I have used in the past is to load the frames individually as a series of jpegs and show the frames either using image elements in the DOM or drawing the frames to a canvas. 我过去使用的解决方案是将帧分别作为一系列jpeg加载,并使用DOM中的图像元素或将帧绘制到画布上来显示帧。 This fulfills pretty much all requirements, and it works reasonably well except that it's really slow to load. 这几乎可以满足所有要求,并且除了加载速度真的很慢之外,还算不错。 And I have only used this method for animations of about 20-30 frames. 而且我只将这种方法用于约20-30帧的动画。 You end up with a lot of http requests and quite a lot of data. 您最终会有许多http请求和大量数据。

For some animations, frames containing lots of motion blur have been saved in a lower resolution which are then stretched when displayed to match the full resolution frames. 对于某些动画,包含大量运动模糊的帧已以较低的分辨率保存,然后在显示时进行拉伸以匹配全分辨率的帧。 This saves some file size and isn't really that noticeable. 这样可以节省一些文件大小,但并不是很明显。

One way to reduce the amount of http requests would be to combine several images into a sprite sheet, but when the individual frames are 1280x720 you quickly end up with a sprite sheet image with a very high resolution, which I don't think will play nice with iOS. 减少http请求数量的一种方法是将多个图像组合成一个sprite工作表,但是当单个帧为1280x720时,您很快会得到一个具有非常高分辨率的sprite工作表图像,我认为这不会发挥作用适用于iOS。

Are there any other approaches that I'm missing here? 我在这里还缺少其他方法吗?

You can use CSS3 keyframe animations and control the animation at any point. 您可以使用CSS3关键帧动画并随时控制动画。

@keyframes sunrise {
   0% {
      bottom: 0;
      left: 340px;
      background: #f00;
   }

   33% {
      bottom: 340px;
      left: 340px;
      background: #ffd630;
   }

   66% {
      bottom: 340px;
      left: 40px;
      background: #ffd630;
   }

   100% {
      bottom: 0;
      left: 40px;
      background: #f00;
   }
}


#sun.animate {
    animation-name: sunrise;
    animation-duration: 10s;
    animation-timing-function: ease;
}

You also need to add vendor prefixes but I would just use modernizr for fallback support. 您还需要添加供应商前缀,但我只会将modernizr用于后备支持。

http://www.impressivewebs.com/demo-files/css3-animated-scene/ http://www.impressivewebs.com/demo-files/css3-animated-scene/

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

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