Here's an SVG animation where a 20-pixel-long stroke
moves along a path consisting of a straight line, a
curve, and another straight line. The effect is
achieved using
stroke-dasharray
and
stroke-dashoffset
animation.
xml
The Path
(d="M50 150 L150 100 Q200 50 250 100 L350
150"
)
(50,150)
to
(150,100)
(first straight line).
(200,50)
reaching
(250,100)
.
(350,150)
(second straight line).
Stroke Effect:
stroke-dasharray="20 300"
makes a 20px visible stroke followed by a 300px
gap.
stroke-dashoffset
is animated from
320
to
0
, making the visible stroke travel along the
path.
Animation:
repeatCount="indefinite"
) over
2s
.
This creates the effect of a moving "highlight" along the pipeline/circuit-like path. π
Your SVG is almost perfect! It achieves the effect of
a visible moving stroke along the path. However, if
you want a more refined and smoother movement,
consider adjusting the
stroke-dasharray
and
stroke-dashoffset
values to match the path length better.
Here's an optimized version:
xml
stroke-dasharray="20 600"
keeps a 20px visible stroke.
stroke-dashoffset
is animated from
0
to
-620
to create the motion effect.
Q200,50 200,100
and
Q200,150 250,150
.
This gives the exact "pipeline" motion effect with a small visible section continuously moving along the path. π
To get the
exact length
of an SVG path, you can use JavaScript's
getTotalLength()
method.
javascriptconst path = document.querySelector("path"); console.log(path.getTotalLength());
This will print the exact length of the path.
If you want to display the length directly inside your SVG for reference:
xml
When you open this SVG in a browser, it will log the exact length of the path.
stroke-dasharray
for Debugging
If you want to manually test the length:
stroke-dasharray
to a large value (e.g.,
stroke-dasharray="1000"
)
stroke-dashoffset="500"
and adjust until you see the stroke disappear at the
end.
xml<path d="M10,50 L150,50 Q200,50 200,100 Q200,150 250,150 L390,150" stroke="blue" stroke-width="4" fill="none" stroke-dasharray="1000" stroke-dashoffset="500"/>
By adjusting
stroke-dashoffset
, you can get a close estimate of the path's length.
getTotalLength()
) for precision.
Let me know if you need more refinements! π
stroke-dashoffset
a Negative Value?
In SVG,
stroke-dashoffset
controls the position of the
dashes and gaps
along a stroked path. A
negative value
moves the visible stroke
forward
along the path.
stroke-dashoffset
with
stroke-dasharray
stroke-dasharray
defines a repeating pattern of dashes and gaps.
Example:
stroke-dasharray="20 600"
β 20px visible stroke, 600px gap.
stroke-dashoffset
shifts this pattern
along the path:
stroke-dashoffset="0"
β The stroke starts at the beginning.
stroke-dashoffset="-20"
β The visible stroke moves
20px forward.
stroke-dashoffset="-620"
β The stroke has traveled
620px forward.
xml
stroke-dashoffset
moves the stroke forward
along the path, creating a smooth traveling effect.
This technique is commonly used for circuit-like animations, loading effects, and path-following movements. π
To animate one path
moving along another path, you can use the
<animateMotion>
element inside an
<g>
(group) or directly inside an
<circle>
or
<rect>
. This allows you to
animate an object along a separate path.
<animateMotion>
Hereβs an SVG example where a moving circle follows a second path as its guide:
xml
d="..."
)
β A static path that
stays visible.
id="motionPath"
)
β Used as a guide for animation.
<circle r="5" fill="blue">
)
<animateMotion>
makes the circle move
along the path.
<mpath href="#motionPath"/>
binds the motion to the hidden path.
dur="3s"
to adjust speed.
<circle>
with an
<image>
or
<rect>
for a different effect.
rotate="auto"
inside
<animateMotion>
to make the object
rotate naturally
along curves.
Want the moving stroke and the animated object to follow the same path?
xml
This way, the stroke moves AND the circle follows the path at the same time!
π Now you can animate circuits, pipelines, and motion paths easily!
π Got it!