简体   繁体   English

从HTML5中的SVG图像播放动画

[英]Play animation from SVG Images in HTML5

Suppose I have, say, 120 SVG images that are set up in such a way that they resemble the frames of a video. 假设我有120个SVG图像,这些图像的设置方式类似于视频帧。 Suppose I want to embed these SVG images into an HTML webpage in so that they play back at a rate that I have full control over (such as 30 or 60 fps, for example). 假设我想将这些SVG图像嵌入到HTML网页中,以便它们以我完全控制的速率播放(例如30或60 fps)。 Furthermore, the goal would be to make sure that they are still displayed on screen as vector graphics on the webpage so that no matter how far the user has zoomed in, they still stay looking crisp. 此外,目标是确保它们仍然在屏幕上显示为网页上的矢量图形,这样无论用户放大了多远,它们仍然保持清晰。 So essentially I would have a really short video that is vector as opposed to raster. 所以基本上我会有一个非常短的视频,它是矢量而不是光栅。

So on to the real question; 那么关于真正的问题; is there any way to efficiently accomplish this in HTML? 有没有办法在HTML中有效地实现这一点? Or would switching between so many different vector graphics so quickly cause some serious lag issues? 或者会在如此多的不同矢量图形之间切换,从而导致一些严重的滞后问题? If there is a good way to do this, would you please provide an example? 如果有一个很好的方法,请你提供一个例子吗?

Depending on how complex your SVG is you may find replacing the DOM object fast enough is going to be a challenge without a lot of optimization ( this answer contains a lot of useful detail and comparisons) 根据您的SVG的复杂程度,您可能会发现在没有大量优化的情况下快速替换DOM对象将是一项挑战( 此答案包含许多有用的细节和比较)

A very simple Javascript example of how to achieve this would be to step through an array of SVG items and swap them out on a setInterval timer aligned to your framerate (I'd used the code this is based on for a background loading animation on an AndroidTV app and it worked pretty reliably) 一个非常简单的Javascript示例如何实现这一点就是逐步执行SVG项目数组,并在与帧速率对齐的setInterval计时器上交换它们(我使用的代码是基于背景加载动画的AndroidTV应用程序,它的工作非常可靠)

<svg height="200" width="200" id="svg">

</svg>


<script>

var fps = 30
var f = 0
var svgA = [
'<circle cx="50" cy="60" r="40" stroke="black"stroke-width="3" fill="red" />',
'<circle cx="55" cy="62" r="42" stroke="black"stroke-width="4" fill="red" />',
'<circle cx="60" cy="64" r="44" stroke="black"stroke-width="5" fill="red" />',
'<circle cx="65" cy="66" r="46" stroke="black"stroke-width="6" fill="red" />',
'<circle cx="70" cy="68" r="48" stroke="black"stroke-width="7" fill="red" />',
'<circle cx="74" cy="70" r="50" stroke="black"stroke-width="8" fill="red" />',
'<circle cx="77" cy="72" r="48" stroke="black"stroke-width="9" fill="red" />',
'<circle cx="79" cy="74" r="46" stroke="black"stroke-width="8" fill="red" />',
'<circle cx="80" cy="76" r="44" stroke="black"stroke-width="7" fill="red" />',
'<circle cx="79" cy="74" r="46" stroke="black"stroke-width="6" fill="red" />',
'<circle cx="77" cy="72" r="48" stroke="black"stroke-width="5" fill="red" />',
'<circle cx="74" cy="70" r="46" stroke="black"stroke-width="4" fill="red" />',
'<circle cx="70" cy="68" r="44" stroke="black"stroke-width="3" fill="red" />',
'<circle cx="65" cy="64" r="42" stroke="black"stroke-width="2" fill="red" />',
'<circle cx="55" cy="62" r="40" stroke="black"stroke-width="3" fill="red" />'
]

var i = setInterval(function() {nextFrame();}, 1000 / fps);


function nextFrame() {
    document.getElementById("svg").innerHTML = svgA[f];
    f=(f+1)%svgA.length
    if (f==0) {
        clearInterval(i)
    }
}

</script>

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

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