调整Fabric.js中的对象大小

Sek*_*nda 3 javascript fabricjs

我打算使用Frabic.js来构建房间布局.它基于网格布局(网格大小:25),对象捕捉到位.

我试图覆盖调整大小事件,使宽度成为网格大小的倍数(25).然而,它似乎随机调整元素大小.

   var canvas = new fabric.Canvas('section', { selection: true }); fabric.Object.prototype.transparentCorners = false;
   var canvasWidth = 600;
   var canvasGrid = 25;

   canvas.on('object:modified', function(options) {
        options.target.set({opacity: 1}); // Return the opacity back to 1 after moving

        var newWidth = (Math.round(options.target.getWidth() / canvasGrid)) * canvasGrid;
        console.log("Width:" + options.target.getWidth());
        console.log("Desired width: " + newWidth);

        if(options.target.getWidth() !== newWidth) {
            console.log("alter the width");
            options.target.set({ width: newWidth });
            console.log("Width changed to: " + options.target.getWidth());
        }

        console.log("Final width: " + options.target.getWidth());
    });
Run Code Online (Sandbox Code Playgroud)

最终用户即时添加对象,其代码如下.

    $("#addBlock").click(function(e){
        e.preventDefault();

        var block = new fabric.Rect({
            left: canvasGrid,
            top: canvasGrid,
            width: canvasGrid,
            height: canvasGrid,
            fill: 'rgb(127, 140, 141)',
            originX: 'left',
            originY: 'top',
            centeredRotation: false,
            lockScalingX: false,
            lockScalingY: false,
            lockRotation: false,
            hasControls: true,
            cornerSize: 8,
            hasBorders: false,
            padding: 0
        });

        canvas.add(block);
    });
Run Code Online (Sandbox Code Playgroud)

控制台输出

Width:170
Desired width: 175
alter the width
Width changed to: 238.00000000000003
Final width: 238.00000000000003
Run Code Online (Sandbox Code Playgroud)

所需的宽度是我想要调整大小/块的大小,而不是它的最终宽度.

ale*_*dro 5

您可以解决重置已修改对象的比例因子的问题:

options.target.set({ width: newWidth, height: newHeight, scaleX: 1, scaleY: 1, });
Run Code Online (Sandbox Code Playgroud)

如果能满足您的需求,请尝试下面的代码段:

$(function () {
      var canvas = new fabric.Canvas('canvas', { selection: true }); fabric.Object.prototype.transparentCorners = false;
      var canvasWidth = 600;
      var canvasGrid = 50;

      canvas.on('object:modified', function (options) {
        options.target.set({ opacity: 1 }); 
        var newWidth = (Math.round(options.target.getWidth() / canvasGrid)) * canvasGrid;
        var newHeight = (Math.round(options.target.getHeight() / canvasGrid)) * canvasGrid;

        if (options.target.getWidth() !== newWidth) {
          options.target.set({ width: newWidth, height: newHeight, scaleX: 1, scaleY: 1, });
        }

      });


      $("#addBlock").click(function (e) {
        e.preventDefault();

        var block = new fabric.Rect({
          left: canvasGrid,
          top: canvasGrid,
          width: canvasGrid,
          height: canvasGrid,
          fill: 'rgb(127, 140, 141)',
          originX: 'left',
          originY: 'top',
          centeredRotation: false,
          lockScalingX: false,
          lockScalingY: false,
          lockRotation: false,
          hasControls: true,
          cornerSize: 8,
          hasBorders: false,
          padding: 0
        });

        canvas.add(block);
      });
    });
Run Code Online (Sandbox Code Playgroud)
canvas {
  border: 1px dashed #333;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<canvas id="canvas" width="500" height="500"></canvas>
<input type="button" id="addBlock" value="add" />
Run Code Online (Sandbox Code Playgroud)