【问题标题】:SVG Path Overlay and Animate Out Another PathSVG 路径叠加和动画出另一条路径
【发布时间】:2015-06-28 00:01:55
【问题描述】:

我有一个灰色虚线的 SVG。我想要做的是将它覆盖在绿色 SVG 虚线的顶部,然后将灰色动画化以显示绿色。有点像从右到左移动的米。

我看到了这个如何制作虚线的例子:

http://jsfiddle.net/ehan4/2/

并且能够做到,但我的线已经虚线了。

我最终这样做了:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         viewBox="0 0 666.9 123.8" enable-background="new 0 0 666.9 123.8" xml:space="preserve">
    <path  opacity="0.4" stroke-width="3" fill="none" stroke="#66CD00" stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="1,6" d="
        M656.2,118.5c0,0-320.4-251-645.9-0.7" />
        <path id="top"  opacity="0.4" fill="none" stroke="#AEAEAE" stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="1,6" d="
    M656.2,118.5c0,0-320.4-251-645.9-0.7"/>

</svg>

var path = document.querySelector('#top');
var length = path.getTotalLength();
// Clear any previous transition
path.style.transition = path.style.WebkitTransition =
  'none';
// Set up the starting positions
path.style.strokeDasharray = 1  + ' ' + 6;
path.style.strokeDashoffset = length;

// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
path.getBoundingClientRect();
// Define our transition
path.style.transition = path.style.WebkitTransition =
  'stroke-dashoffset 20s linear';
// Go!
path.style.strokeDashoffset = '0';

https://jsfiddle.net/ps5yLyab/

如何叠加两条虚线并为灰色设置动画?

【问题讨论】:

  • 很多来自教程。我目前正试图通过动画灰线来解决这个问题。
  • 你不是很清楚你想要达到什么。你是说你想要一条从右向左收缩的动画灰色虚线,显示一条动画绿色虚线?
  • 是的,我希望灰色虚线消失并显示绿色虚线。

标签: javascript html svg svg-animate


【解决方案1】:

您可以使用剪辑路径来做到这一点。

首先我们向 SVG 添加一个 clipPath。

<defs>
    <clipPath id="myclip">
        <rect id="cliprect" x="100%" y="0%" width="100%" height="100%"/>
    </clipPath>
</defs>

此剪辑路径的大小与 SVG 相同(宽度和高度 100%),并从 SVG 最右侧的 x 位置开始 (100%)。所以一开始它并没有透露任何东西。

然后每 10 毫秒我们将它的 x 坐标减少 1%(即 100% -> 99% -> 98% 等)。直到达到零。

var cliprect = document.getElementById("cliprect");
var offsetX = 100;
var speed = 10;

function clipAdjust()
{
    cliprect.setAttribute("x", offsetX+"%");
    offsetX -= 1;
    if (offsetX >= 0) {
        window.setTimeout(clipAdjust, speed);
    }
}

window.setTimeout(clipAdjust, speed);

下面的工作演示:

var path = document.querySelector('#top');
		var length = path.getTotalLength();
		// Clear any previous transition
		path.style.transition = path.style.WebkitTransition =
		  'none';
		// Set up the starting positions
		path.style.strokeDasharray = 1  + ' ' + 6;
		path.style.strokeDashoffset = length;

		// Trigger a layout so styles are calculated & the browser
		// picks up the starting position before animating
		path.getBoundingClientRect();
		// Define our transition
		path.style.transition = path.style.WebkitTransition =
		  'stroke-dashoffset 20s linear';
		// Go!
		path.style.strokeDashoffset = '0';

var cliprect = document.getElementById("cliprect");
var offsetX = 100;
var speed = 10;

function clipAdjust()
{
    cliprect.setAttribute("x", offsetX+"%");
    offsetX -= 1;
    if (offsetX >= 0) {
        window.setTimeout(clipAdjust, speed);
    }
}

window.setTimeout(clipAdjust, speed);
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
		 viewBox="0 0 666.9 123.8" enable-background="new 0 0 666.9 123.8" xml:space="preserve">

    <defs>
        <clipPath id="myclip">
            <rect id="cliprect" x="100%" y="0%" width="100%" height="100%"/>
        </clipPath>
    </defs>

    <path opacity="0.4" fill="none" stroke="#AEAEAE" stroke-linecap="round"
          stroke-miterlimit="5" stroke-dasharray="1,6" stroke-width="2"
          d="M656.2,118.5c0,0-320.4-251-645.9-0.7"/>

    <g clip-path="url(#myclip)">
        <path stroke-width="3" fill="none" stroke="white"
              stroke-linecap="round" stroke-miterlimit="5"
              d="M656.2,118.5c0,0-320.4-251-645.9-0.7" />
        <path id="top"  opacity="0.4" stroke-width="3" fill="none" stroke="#66CD00"
              stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="6,6"
              d="M656.2,118.5c0,0-320.4-251-645.9-0.7" />
    </g>
  
</svg>

【讨论】:

  • 好吧,我会试试这个,并在我有机会的时候发布这里发生的事情。听起来像是什么工作。我只是剪掉灰色并在剪下时显示绿色。
  • 其实恰恰相反。随着剪辑矩形向左移动,我们正在剪掉绿色并显示越来越多的绿色。
  • 这完美地移动了绿线。现在我想我可以在绿线在屏幕上移动时隐藏灰线。感谢您的所有帮助!
  • 啊,是的。我没有正确隐藏灰线,是吗?现在已经解决了。我在绿色后面用白线隐藏它。但是还有其他方法,例如有两个剪辑路径。一种绿色,一种灰色。
  • 漂亮,我以前从没搞过 svg。图形现在似乎有了更多的可能性。
猜你喜欢
  • 1970-01-01
  • 2018-04-01
  • 2016-02-14
  • 2016-01-06
  • 2014-02-05
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多