Raphael draw path with mouse

后端 未结 2 826
小鲜肉
小鲜肉 2020-12-29 14:26

I\'m using the raphael javascript library, and I\'d like to draw a straight line using the mouse. I\'d like to let the user click somewhere, place a single point of the path

2条回答
  •  悲哀的现实
    2020-12-29 15:28

    There's actually a better way to do this, using path.attr('path'). path is an array of path part arrays, e.g.

    [
      ['M', 100, 100],
      ['L', 150, 150],
      ['L', 200, 150],
      ['Z']
    ]
    

    If you update it then you don't need to draw the path from scratch each time.

    Raphael.el.addPart = function (point) {
      var pathParts = this.attr('path') || [];
      pathParts.push(point);
      this.attr('path', pathParts);
    };
    
    var path = paper.path();
    path.addPart(['M', 100, 100]); //moveto 100, 100
    path.addPart(['L', 150, 150]); //lineto 150, 150
    path.addPart(['L', 200, 150]); //lineto 200, 150
    path.addPart(['Z']);           //closepath
    

提交回复
热议问题