Update fabric.js Path points dynamically

前端 未结 4 821
栀梦
栀梦 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 04:37

    I couldn't find any new way to do this. But I figured out something like below;

    1. create an SVG path string with modifications.
    2. create a new fabric path object.
    3. replace path, width, height and pathOffset properties of the original path object with the properties of the new path object.
    4. setCoords. renderAll etc...

    It may not be much efficient. But it was the solution for me.

        var pathObject = new fabric.Path("M0,0 L100,100 ~ Z");
    
        var updatedPath =  new fabric.Path("M50,100 L120,46 ~ Z");
    
        pathObject.set({
          path : updatedPath.path,
          width : updatedPath.width,
          height : updatedPath.height,
          pathOffset: updatedPath.pathOffset
        });
        pathObject.setCoords();
    

    On my setup, it says path._parseDimensions is not a function. I didn't try to solve it. I have to change all path content. So my solution seems better for me :)

提交回复
热议问题