I am creating a dynamic path using my SVG. I now wish to add gradient to my path but I am stuck. The way I am trying, my gradient is coming along the path as shown in image 2 while I require it to be the kind in image 1.
Current
My gradient and stroke definitions are as follows :
<defs>
<linearGradient id = "grad1" spreadMethod="reflect">
<stop offset="0%" style="stop-color: lightcoral;" />
<stop offset="50%" style="stop-color: #ffffff;" />
<stop offset="100%" style="stop-color: lightcoral;" />
</linearGradient>
</defs>
</svg>
script :
svgPath.setAttribute("stroke", "url(#grad1");`
svgPath.setAttribute("fill", "none");
svgPath.setAttribute("stroke-linejoin", "round");`
svgPath.setAttribute("stroke-width", "10");
});
You can't make a gradient run along the stroke of a path, turning at the corners etc., if that's what you mean.
If instead you just want to make it so the gradient is oriented vertically, then you need to use the
x
,y1
,x2
andy2
attributes to set the line along which the gradient runs. If you don't specify these attributes, the gradient is oriented horizontally as per your second image.If you want to have a "pipe" like gradient effect, then the simplest method is to layer multiple paths of different stroke widths.
Here's a simple example to demonstrate.
Answer @Paul LeBeau inspires, very interesting technique - immitations of the gradient along the curvilinear path.
This technique can be used to animate the movement of fluid along the pipes, filling the vessels.
I tried to make such an example of animation. I hope someone will come in handy.
Steps to implement the animation:
polyline
command from the example above, with thepath
command.class="poly"
stroke-dasharray
,stroke-dashoffset