简体   繁体   English

从左到右填充svg

[英]Fill svg from left to right

I'm new to svg, I was trying to fill color in a path/circle. 我是svg的新手,我试图在路径/圆圈中填充颜色。 I was able to fill it with transition. 我能够用过渡填充它。

But I was wondering is there a way to fill the svg from left to right of the path/circle. 但我想知道是否有办法从路径/圆圈的左侧到右侧填充svg。

SVG SVG

<svg viewBox="0 0 160 160" width="160" height="160">
  <circle cx="80" cy="80" r="50" />
</svg>

CSS CSS

circle {
  transition: all 3s;
}

JS JS

setTimeout(function(){
  $('circle').css('fill', 'red');
},300);

How to fill in particular direction, like left to right? 如何从左到右填写特定的方向?

Codepen Codepen

As "fill left to right" can mean a lot of things, I made a few examples: 由于“从左到右填充”可能意味着很多事情,我举了几个例子:

Moving a gradient: 移动渐变:

I would suggest using the SVG <linearGradient> and <animate> tags. 我建议使用SVG <linearGradient><animate>标签。 That way you don't need the JS or CSS. 这样你就不需要JS或CSS了。

Here is a guide to animating gradients in SVG: http://designmodo.com/animate-svg-gradients/ And one to SVG animations in general: https://css-tricks.com/guide-svg-animations-smil/ 以下是SVG中渐变动画的指南: http//designmodo.com/animate-svg-gradients/以及一般SVG动画: https//css-tricks.com/guide-svg-animations-smil/

In your case (fill left to right), I would do it like this: 在你的情况下(从左到右填充),我会这样做:

<svg viewBox="0 0 160 160" width="160" height="160">
    <defs>
        <linearGradient id="lightGradient">
            <stop offset="0%" stop-color="red">
                <animate attributeName="stop-color" values="black; red" dur="3s" fill="freeze" /> 
            </stop>
            <stop offset="100%" stop-color="black">
                <animate attributeName="stop-color" values="black; red" dur="3s" fill="freeze" begin="3s"/> 
            </stop>
        </linearGradient>
    </defs>
    <circle cx="80" cy="80" r="50" fill="url(#lightGradient)"/>
</svg>

Technically it's cheating, as the gradient doesn't actually move, but the colours on both sides change, but it looks pretty much the same. 从技术上讲,这是作弊,因为渐变实际上并没有移动,但两侧的颜色都会发生变化,但它看起来几乎一样。 It starts out with a gradient from black to black, then the left side changes to red, giving the illusion that red moves in from the left. 它从黑色到黑色的渐变开始,然后左侧变为红色,从而产生红色从左侧移入的错觉。 After the left side is red, the right side changes from black to red and it looks like the gradient is moving to fill the whole circle. 在左侧为红色后,右侧从黑色变为红色,看起来渐变正在移动以填充整个圆圈。

Clear border between left and right: 左右边界清晰:

Probably best to use a <clip-path> for this: 可能最好使用<clip-path>

<clipPath id="left-to-right">
    <rect x="130" y="30" width="0" height="100" >
        <animate attributeName="width" values="0;100" dur="3s" fill="freeze"/>   
    </rect>
</clipPath>
...
<circle cx="180" cy="80" r="50" fill="black"/>
<circle cx="180" cy="80" r="50" fill="red" clip-path="url(#left-to-right)"/>

This one draws a black circle, then draws a red circle over it inside a growing clipping area. 这个绘制一个黑色圆圈,然后在一个不断增长的剪裁区域内绘制一个红色圆圈。

One circle obscuring the other: 一个圈子掩盖了另一个圈子:

Again, <clip-path> works for this: 同样, <clip-path>适用于此:

<clipPath id="steady">
    <circle cx="280" cy="80" r="50"/>
</clipPath>
...
<circle cx="280" cy="80" r="50" fill="red" clip-path="url(#steady)">
    <animate attributeName="cx" values="180;280" dur="3s" fill="freeze"/>
</circle>

This one uses a circular clipping area and moves the second circle within it. 这个使用圆形剪切区域并在其中移动第二个圆圈。

Here it is in Codepen: http://codepen.io/anon/pen/aOKeYQ 这是Codepen: http ://codepen.io/anon/pen/aOKeYQ

It's possible, but not with just using 'fill'. 这是可能的,但不能只使用'填充'。

You can eg do a linear gradient fill, with two stops, one being the color you want and the other fully transparent. 你可以做一个线性渐变填充,有两个停止点,一个是你想要的颜色,另一个是完全透明的颜色。 Then animate the offset of the second gradient stop. 然后设置第二个渐变停止的offset的动画。 For an example see here . 举个例子,请看这里

Another way is using a clip-path , where the clip shape is a rect that is animated eg from left to right. 另一种方法是使用clip-path ,其中剪辑形状是例如从左到右动画的rect That can then be applied to a copy of the shape that you want to fill like this. 然后可以将其应用于您要填充的形状的副本。 Basically you need the starting shape (unfilled) and an ending shape (filled), where the final shape gets displayed by animating the clip-path. 基本上你需要起始形状(未填充)和结束形状(填充),其中最终形状通过动画剪辑路径显示。

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

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