Skip to content

Instantly share code, notes, and snippets.

@dmitric
Created July 23, 2020 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitric/003be3d5a62d09f92dc19266783d65b7 to your computer and use it in GitHub Desktop.
Save dmitric/003be3d5a62d09f92dc19266783d65b7 to your computer and use it in GitHub Desktop.
import { svgPathProperties } from "svg-path-properties"
calculateConnectionsBetweenPaths (d, lastd, segments) {
// Take in two "d" attribute string and break then down into N segements
// and return a list of those points
const connections = []
// Given a path's value of d, get the total length of the path
const pp = new svgPathProperties(d)
const pLength = pp.getTotalLength()
// Given another path's value of d, get the total length of the path
const lastpp = new svgPathProperties(lastd)
const lastpLength = lastpp.getTotalLength()
// break up each path into N even segments, and connect them at those corresponding intervals
for (let section=0; section <= segments; section++) {
let pPoint = pp.getPointAtLength(pLength*section/segments)
let lastpPoint = lastpp.getPointAtLength(lastpLength*section/segments)
connections.push([ pPoint , lastpPoint ])
}
return connections
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment