引导点多边形创建

时间:2018-03-27 16:48:04

标签: javascript fabricjs fabricjs2

我正在尝试使用所述多边形顶点中的圆来帮助我的用户创建多边形。 我知道如何添加圆圈,但我没有看到如何在双击创建多边形结束时消除它们,它总是抛出一个错误告诉我要删除的对象没有定义

Fiddle

var canvas = window._canvas = new fabric.Canvas('c');


drawPoligon(6);

 function drawPoligon (id){
  //I define the variables that I need
      var mode = "add", currentShape;
  var puntos;
  var obj;
      newColor = "#FF0000";

  //I prepare the reading of the event mouse: down, 
  //for when I click, if I am adding the polygon for 
  //the first time, that is created and added to the canvas
  canvas.on("mouse:down", function (event) {
    var pos = canvas.getPointer(event.e);
        if (mode === "add") {
      // console.log(this.getRandomColor);
            currentShape = new fabric.Polygon([{
                            x: pos.x,
                            y: pos.y
                }, {
                  x: pos.x + 1, 
                  y: pos.y + 1
                        }], {
                  fill: "#FF0000",
                            selectable: false,
                      id: id,
                  objectCaching:false,
                  olvidar: "olvidar"
                });
      canvas.add(currentShape);
      var circ = new fabric.Circle({
        id: "guiaPol",
        evented: false,
        top: pos.y-5,
        left: pos.x-5,
        radius: 10,
        fill: "red",
        perPixelTargetFind: true
      });
      canvas.add(circ);
      canvas.renderAll();
            newColor= currentShape.get('fill');
      mode = "edit";
          } else if (mode === "edit" && currentShape && currentShape.type === "polygon") {
      //In the case that I have added the polygon, what I have to do is add the points, as I click
      var points = currentShape.get("points");
      points.push({
        x: pos.x ,
        y: pos.y
      });
      puntos = points;
      currentShape.set({
        points: points
      });
      var circ = new fabric.Circle({
        id: "guiaPol",
        evented: false,
        top: pos.y-5,
        left: pos.x-5,
        radius: 10,
        fill: "red",
        perPixelTargetFind: true
      });
      canvas.add(circ)
              canvas.renderAll();
          }
  });
  //I set up a mouse: move listener that modifies the poligo in real time, 
  //to see where the next point will go, following the position of the mouse
  canvas.on("mouse:move", function (event) {
    //console.log("Hola");
    var pos = canvas.getPointer(event.e);
    //console.log("CurrShape", currentShape);
    if (mode == "edit" && currentShape) {
      var points = currentShape.get("points");

      points[points.length - 1].x = pos.x;
      points[points.length - 1].y = pos.y;
      currentShape.set({
        points: points,
        dirty: true
      });
      currentShape.setCoords();
      canvas.renderAll();
    }
  });

  // <%'
  // 'Descripción: función que nos ayuda a parar la creación del poligono cuando hacemos doble click 
  // 'Inputs: 
  // 'Outputs:
  // 'DFDNSCADA0676
  // %>

  //This function is executed at the end of the creation of the polygon, which is double clicking on the screen
  function pararCreacion(){
    if (mode === 'edit' || mode === 'add') {
      mode = 'normal';
      var obj = currentShape.toObject();
      currentShape = new fabric.Polygon(puntos,{obj});
      currentShape.set({
        id:id,
        originY: "top",
        originX: "left",
        fill: newColor,
        type: 'polygon',
        nombre: 'Objeto_' + id
      });
      canvas._objects.pop();
      canvas.add(currentShape);
      currentShape.set({
        selectable: true,
      });
      //$("#Elemento_186").removeAttr("style");
      canvas.renderAll();
      canvas.forEachObject(function(o){
        if(o.id == "guiaPol"){
        canvas.remove(o);
        }
      });
      // <%' Cuando ya termino con el poligono y refresco el canvas entonces es cuando añado el cambio a mi matriz deshacer %>
      canvas.off("mouse:move");
    }   
    currentShape = null;
        fabric.util.removeListener(fabric.document,'dblclick', pararCreacion);    //de esta forma cuando termina la creación me sale de la función y me anula el evento
 }
     fabric.util.addListener(fabric.document, 'dblclick', pararCreacion);
  };

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

请替换您的代码

canvas.forEachObject(function(o){
    if(o.id == "guiaPol"){
        canvas.remove(o);
    }
});

var objects = canvas.getObjects(),
    i = objects.length;
while (i--) {
    if(objects[i].id == "guiaPol"){
       canvas.remove(objects[i]);
    }
}

请参阅GitHub

上的fabric.js问题

由于FabricJs v1.6.6函数 forEachObject 循环遍历数组中从0到n的所有元素。 删除函数正在拼接该数组,然后当项成功从数组中删除时,它会为您提供未定义的功能。你需要在画布上获取所有对象,然后从最后一个元素循环到第一个元素。

答案 1 :(得分:1)

var circleObjects = [];
canvas.forEachObject(function(o){
  if(o.id == "guiaPol"){
    circleObjects.push(o);
  }
});
canvas.remove(...circleObjects);

您可以获取所有圈子对象并使用canvas.remove()删除。 此处已更新fiddle

使用canvas.remove(currentShape);删除前一个多边形,这样就会删除旧的多边形,然后在dblclick处理程序中添加新的多边形。