简体   繁体   English

沿SVG路径对SVG渐变进行动画处理

[英]Animate SVG Gradient along SVG path

I have created an SVG egg and animated a path and added a gradient fine. 我创建了一个SVG鸡蛋,并对路径进行了动画处理,并添加了渐变效果。 I've added a linear gradient and it goes top to bottom, however i'd like 0% to be the dark colour and 100% be the light colour - so essentially the gradient is following the already existing path and getting lighter as the number increases. 我添加了一个线性渐变,它从上到下,但是我希望0%为深色,而100%为浅色-因此,基本上,渐变沿已存在的路径移动,并且随着数字变浅增加。

Here is a link to my jsFiddle. 这是我的jsFiddle的链接。 It's probably better for me to link you guys there than put code in here (as for some reason the text doesn't render on SO's snippet thing) 对于我来说,将您链接到那里可能比在这里放置代码更好(因为某些原因,文本未呈现在SO的摘要中)

http://jsfiddle.net/andyjh07/Lrywj95t/ http://jsfiddle.net/andyjh07/Lrywj95t/

Here is the SVG code if you need it: 如果需要,这是SVG代码:

<div id="stage">
    <div class="egg-holder">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 104.7 144.3" enable-background="new 0 0 104.7 144.3" xml:space="preserve">

            <defs>
                <linearGradient id="orangeGradient"
                                x1="0%" y1="0%"
                                x2="0%" y2="100%"
                                spreadMethod="pad">
                    <stop offset="0%"   stop-color="#ff9800" stop-opacity="1"/>
                    <stop offset="100%" stop-color="#e65100" stop-opacity="1"/>
                </linearGradient>
          </defs>

            <g id="egg-grey">
                <path fill="#FFFFFF" stroke="#212121" stroke-width="10" stroke-miterlimit="10" d="M99.3,92.2c0,25.7-20.8,46.5-46.5,46.5 S6.3,117.8,6.3,92.2S27.2,5.7,52.8,5.7S99.3,66.5,99.3,92.2z"/>
            </g>
            <g id="egg-orange">
                <path fill="none" stroke="url(#orangeGradient)" stroke-width="10" stroke-miterlimit="10" d="M99.3,92.2c0,25.7-20.8,46.5-46.5,46.5 S6.3,117.8,6.3,92.2S27.2,5.7,52.8,5.7S99.3,66.5,99.3,92.2z"/>
            </g>
        </svg>
        <div class="timer">
            <p></p>
        </div>
    </div>
</div>

If I understand your question correctly you want to have a gradient along an SVG path. 如果我正确理解了您的问题,那么您希望沿SVG路径创建一个渐变。

It looks like from this question , it's not possible to do this without segmenting the path. 这个问题看来,如果不对路径进行分段就不可能做到这一点。

I did however find this reference , it's a pretty complex chunk of code, but based on the result he's able to accomplish what you're after. 但是,我确实找到了这个参考 ,这是一段相当复杂的代码,但是基于结果,他能够完成您所追求的目标。

Good luck! 祝好运!

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

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