【问题标题】:Resizing Objects in Fabric.js在 Fabric.js 中调整对象大小
【发布时间】:2015-09-04 03:05:33
【问题描述】:

我打算使用 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());
    });

对象由最终用户动态添加,其代码如下。

    $("#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);
    });

控制台输出

Width:170
Desired width: 175
alter the width
Width changed to: 238.00000000000003
Final width: 238.00000000000003

所需的宽度是我希望将形状/块调整到的大小,而不是最终宽度。

【问题讨论】:

    标签: javascript fabricjs


    【解决方案1】:

    也许您可以解决重置修改对象的比例因子:

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

    如果可以满足您的需求,请在下面的 sn-p 中尝试自己:

    $(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);
          });
        });
    canvas {
      border: 1px dashed #333;
    }
    <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" />

    【讨论】:

    • 你是对的,在解决问题后重置比例。感谢您的帮助!
    猜你喜欢
    • 2019-04-25
    • 2014-02-15
    • 2016-04-10
    • 2014-11-06
    • 2021-11-18
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多