I have svg stroke animation https://codesandbox.io/s/magical-hill-r92ong
But it's starts from bottom-right position, can i start it from upper center like on screenshot (red dot) ?
I try to set stroke-dashoffset with negative value it's helps to set start point, but stroke animation is not going to the end
Shift
M(starting point)Shifting the starting point is actually not too complicated – provided you're using absolute commands and your path doesn't contain any shorthand commands (more details below):
The upper center command would be the 16nth or 17nth command:
Reordered path
However, this approach won't work if your path contains relative (lowercase commands) or shorthand commands such as
H,V(horizontal/vertical linetos ),S(cubic curveto),T(quadratic bézier curves).JS aproach 1: Shift starting points using
getPathData()(polyfilled)getPathData()andsetPathData()methods are based the w3c working draft of the SVG Paths specification to provide a standardized way of parsing<path>dattributes to an array of commands as well as applying the manipulated data once again viasvgelement.setPathData(pathData)– so it's "kinda official" (as a successor/replacement forpathSegList())Still (2023) not natively supported by major browsers you can use Jarek Foksa's polyfill
How it works
<path>dattribute to an array of commandsgetPathData({normalize:true})v,h,sand quadratic commands to cubicq,tas well as arcto commandsa! So it's a rather "aggresive/lossy" conversion.Mcommands)JS aproach 2: Retain
Q,Acommands (also based ongetPathdata())In this case you'll need a more advanced normalizing.
s=>c,t=>q,v,h=>lYou might try my codepen example path direction and starting point sanitizer
Alternative: stroke-dashoffset
Besides, you could also use a
stroke-dashoffsetas described here:"How to change start point of svg line animation"