Skip to content

Instantly share code, notes, and snippets.

@eugenserbanescu
Created July 3, 2018 16:31
Show Gist options
  • Save eugenserbanescu/1f2b8a3fdf19772117d725f49cb4ad30 to your computer and use it in GitHub Desktop.
Save eugenserbanescu/1f2b8a3fdf19772117d725f49cb4ad30 to your computer and use it in GitHub Desktop.
X-Y svg path mover
const PAIR_MATCHER = /([A-Z]?)([^A-Za-z,]+),(.*)/
function moveSvg(path, x=0, y=0) {
return path.split(' ').map(pair => {
const matches = pair.match(PAIR_MATCHER)
return matches === null ?
pair :
`${matches[1]}${parseFloat(matches[2]) + x},${parseFloat(matches[3]) + y}`
}).join(' ')
}
const path = "M7.3880597,0.222222222 C6.51008894,0.241763929 5.64450119,0.432020826 4.84029851,0.782222222 C3.23687226,1.48049066 1.97883981,2.78276634 1.34328358,4.40222222 C1.03175068,5.19410281 0.879628734,6.03892052 0.895522388,6.88888889 L0.447761194,6.88888889 C0.483905828,5.07722641 1.2432707,3.35393549 2.55895522,2.09777778 C3.85435382,0.861626498 5.59146364,0.186957731 7.3880597,0.222222222 Z"
console.log(moveSvg(path, 0.5, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment