本文介绍了在Fabric.js中调整对象大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划使用Frabic.js构建房间布局.它基于网格布局(网格大小:25),其中对象会对齐到适当的位置.

I'm planning on using Frabic.js to build a room layout. It's based around a grid layout (grid size: 25) with the objects snapping into position.

我正在尝试覆盖调整大小事件,以使宽度变为网格大小(25)的倍数.但是,它似乎是在随机调整元素的大小.

I'm trying to override resize events to make the width a multiple of the grid size (25). However it seems to be randomly resizing the the element.

代码

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

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

The objects are added on the fly by the end user, the code for it is below.

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

所需的宽度是我希望将形状/块调整为所需的宽度,而不是最终宽度.

The desired width is what I would like the shapes/blocks to be resized to rather than it's final width.

推荐答案

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

Probably you can solve resetting the scale factor of your modified object:

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

如果可以满足需要,请尝试以下代码段:

try yourself in the snippet below if can fit for your needs:

$(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" />

这篇关于在Fabric.js中调整对象大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 22:07
查看更多