【发布时间】:2017-10-11 08:02:56
【问题描述】:
我在 SVG 元素上使用动画(仅路径)(只需使用 JavaScript 频繁切换路径的可见性属性)。 SVG 有一个背景图像。显示的某些路径必须在笔划上有背景图像(看起来好像它们正在擦除路径)。我使用 SVG 的屏蔽功能来做到这一点,如下所示:
var t = 0;
var displayDictionary = {};
function fillDisplayDictionary()
{
var paths = document.querySelectorAll("svg path");
for(var index=0; index < paths.length; index++)
{
var path = paths[index];
var pathDisplayTime = parseInt(path.getAttribute("data-t"));
displayDictionary[pathDisplayTime] = path;
}
}
function displayNextElement()
{
displayDictionary[t].style.visibility = "visible";
t++;
if(t == 5)
clearInterval(interval);
}
fillDisplayDictionary();
interval = setInterval(displayNextElement, 40);
svg path
{
visibility: hidden;
}
<!DOCTYPE html>
<html>
<body>
<svg height="400" width="450">
<!-- this is the background image -->
<image xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450"></image>
<!-- these are ordinary paths -->
<path data-t="0" d="M 100 350 l 150 -300" stroke="red" stroke-width="8" fill="none" />
<path data-t="1" d="M 250 50 l 150 300" stroke="red" stroke-width="8" fill="none" />
<path data-t="2" d="M 175 200 l 150 0" stroke="green" stroke-width="8" fill="none" />
<path data-t="3" d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="8" fill="none" />
<mask id="mask">
<!-- this is the erasing path -->
<path data-t="4" d="M 0 0 L 400 450" stroke="white" stroke-width="20" />
</mask>
<!-- this is an image identical to the background image, and it is masked by the above path-->
<image xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg" width="400" height="450" mask="url(#mask)"></image>
</svg>
</body>
</html>
笔划上有太多带或不带背景图像的路径。这在 Chrome 中运行良好。但是,在 FireFox 中,动画过程在显示擦除路径(即在笔划上带有背景图像的路径)时变得非常缓慢。有没有其他方法可以显示 Firefox 响应良好的擦除路径。
【问题讨论】:
标签: javascript html css svg