【问题标题】:Draw Poligon FabricJS 2.0绘制多边形织物 JS 2.0
【发布时间】:2018-07-23 18:23:27
【问题描述】:

在以前的fabricjs版本中,我有以下功能可以帮助我在单击鼠标时绘制一个多边形,并且它是正确完成的。

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

  //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,
                  olvidar: "olvidar"
                });
      canvas.add(currentShape);
      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
      });
              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({
        originY: "top",
        originX: "left",
        fill: newColor,
        type: 'polygon'
      });
      canvas._objects.pop();
      canvas.add(currentShape);
      currentShape.set({
        selectable: true,
      });
      $("#Elemento_186").removeAttr("style");
      canvas.renderAll();
      // <%' 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);
  };

这个函数的问题是它会生成一个不可见的矩形或类似的东西,其大小小于画布的大小。或者这就是我在画布上看到的,正因为如此,我看到了那个盒子里的人物。

我不知道如何更好地解释它

Creating the object

Final result of the creation of the object

Fiddle Working Example

【问题讨论】:

    标签: javascript canvas fabricjs fabricjs2


    【解决方案1】:

    在绘制时设置为多边形对象,如果启用缓存,它不会更新缓存画布的宽度/高度,因此无法绘制多边形。

    objectCaching:false
    

    jsfiddle

    【讨论】:

    • 感谢 Druga,我还没有完全理解 Fabricjs 中 object caching 的概念
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2011-12-30
    • 2018-05-14
    • 1970-01-01
    • 2023-04-08
    • 2014-08-04
    相关资源
    最近更新 更多