Simplifying SVG path strings by reducing number of nodes

前端 未结 1 1820
终归单人心
终归单人心 2021-02-09 22:10

I am generating a large SVG path string that represents a line chart.

Beneath the chart I have a slider for selecting a time range slice. Behind the slider is a mini pr

相关标签:
1条回答
  • 2021-02-09 22:42

    Simplify.js is probably what you're looking after.

    Given your line chart consists of straight line segments only (which by definition it should), you can use it like this:

      var tolerance = 3
      var pathSegArray = []
      for (var i=0; i<path.pathSegList.numberOfItems; i++) {
        pathSegArray.push(path.pathSegList.getItem(i))
      }
      var newPathArray = simplify(pathSegArray,tolerance)
      var newD = "M";
      for (i=0; i<newPathArray.length; i++) {
        newD += newPathArray[i].x + " " + newPathArray[i].y + " "
      }
      path.setAttribute("d",newD)
    
    0 讨论(0)
提交回复
热议问题