简体   繁体   English

带有多个 SVG 的奇怪 Safari 行为<animate>的

[英]Strange Safari behavior with multiple SVG <animate>'s

I am attempting to animate an SVG play-stop button with multiple <animate> tags on a polygon, such that running beginElement() on each <animate> would cause the shape to morph its points.我试图在多边形上使用多个<animate>标签为 SVG 播放停止按钮<animate> ,这样在每个<animate>上运行beginElement()会导致形状改变其点。 This works great in Chrome and Firefox, but, when testing in Safari, I only seem to be able to activate one <animate> tag.这在 Chrome 和 Firefox 中效果很好,但是,在 Safari 中进行测试时,我似乎只能激活一个<animate>标签。

The what I currently have is as follows:我目前所拥有的如下:

 playState = true; function toggle() { playState = !playState; document.querySelector("#to" + (playState ? "Play" : "Stop")).beginElement(); console.log("Changed state to " + (playState ? "play" : "stop") + ".") } document.querySelector("#toggle").addEventListener("click", toggle); toggle();
 #toggle circle { fill: #808080; } #toggle polygon { fill: #DCDCDC; }
 <svg id="toggle" viewBox="0 0 300 300"> <circle cx="150" cy="150" r="100"/> <polygon points="150,150 150,150 150,150 150,150"> <animate fill="freeze" id="toStop" attributeName="points" dur="500ms" to="100,100 100,200 200,200 200,100" begin="indefinite" calcMode = "spline" keySplines = "0 0.75 0.25 1" keyTimes = "0;1"/> <animate fill="freeze" id="toPlay" attributeName="points" dur="500ms" to="120,100 120,200 206.6025403784,150 206.6025403784,150" begin="indefinite" calcMode = "spline" keySplines = "0 0.75 0.25 1" keyTimes = "0;1"/> </polygon> </svg>

Testing in Safari, I can correctly fire #toPlay initially, but, afterwards, running beginElement() on #toPlay flickers between the two states and activating #toStop does nothing.在Safari的测试,我可以正确触发#toPlay最初,但是,事后,运行beginElement()#toPlay闪烁两种状态之间并激活#toStop什么都不做。 It is almost as though the #toStop animation is being delayed, and then is running rapidly when #toPlay is fired again.这几乎就像#toStop动画被延迟,然后在#toPlay再次触发时快速运行。

I figured it out on my own!我自己想出来的! However, I was forced to compromise slightly.然而,我被迫稍微妥协。 Jesus CMD explains that you need to reset the SVG animation on every other call to beginElement() . Jesus CMD 解释说,您需要在每次调用beginElement()重置 SVG 动画。 The downside of this solution is that the animation must be reset to the polygon points' intial value, so my hopes of having three possible polygon shapes (a single point, a square, and a triangle) must be narrowed down to only two states: play and stop.这个解决方案的缺点是动画必须重置为多边形点的初始值,所以我希望拥有三种可能的多边形形状(一个点、一个正方形和一个三角形)必须缩小到只有两种状态:播放和停止。

The changed snippet is below.更改后的片段如下。

 playState = true; svg = document.querySelector("#toggle"); function toggle() { playState = !playState; if (playState) { svg.pauseAnimations(); svg.setCurrentTime(0); svg.unpauseAnimations(); document.querySelector("#toPlay").beginElement(); } else { document.querySelector("#toStop").beginElement(); } //document.querySelector("#to" + (playState ? "Play" : "Stop")).beginElement(); console.log("Changed to " + (playState ? "Play" : "Stop")) } svg.addEventListener("click", toggle); toggle();
 #toggle circle { fill: #808080; } #toggle polygon { fill: #DCDCDC; }
 <svg id="toggle" viewBox="0 0 300 300"> <circle cx="150" cy="150" r="100"/> <polygon points="100,100 100,200 200,200 200,100"> <animate fill="freeze" id="toStop" attributeName="points" dur="500ms" to="100,100 100,200 200,200 200,100" begin="indefinite" calcMode = "spline" keySplines = "0 0.75 0.25 1" keyTimes = "0;1"/> <animate fill="freeze" id="toPlay" attributeName="points" dur="500ms" to="120,100 120,200 206.6025403784,150 206.6025403784,150" begin="indefinite" calcMode = "spline" keySplines = "0 0.75 0.25 1" keyTimes = "0;1"/> </polygon> </svg>

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

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