Update fabric.js Path points dynamically

前端 未结 4 792
栀梦
栀梦 2021-01-20 04:05

I\'m trying to add points to a path object dynamically. When I do, the path renders correctly, but the bounding rectangle never gets updated, making it nearly impossible for

4条回答
  •  终归单人心
    2021-01-20 05:03

    In fabric 3.3.2 I solved it combining the answers above:

    var dims = path._calcDimensions()
    path.set({
      width: dims.width,
      height: dims.height,
      left: dims.left,
      top: dims.top,
      pathOffset: {
        x: dims.width / 2 + dims.left,
        y: dims.height / 2 + dims.top
      },
      dirty: true
    })
    path.setCoords()
    

    This correctly updates my path bounding box after adding points like:

    path.set({path: points})
    

    I am not sure though, if this works with negative top and left values, but I didn't need that in my case. I guess the main thing is that the _parseDimensions() method was renamed to _calcDimensions().

提交回复
热议问题