在Fabric.js中设置对象拖动限制

MAN*_*NGA 12 fabricjs

我是织物js的新手想要设置阻力限制在此输入图像描述

我也试过https://github.com/kangax/fabric.js/wiki/Working-with-events

无法得到解决方案.

请检查附图,对象可以移动任何软件,但它应该只显示在红色区域.我想要这个.帮帮我...提前谢谢!!

Fel*_*ung 12

虽然Orangepill的答案是正确的,但是当你的物体碰到物体边界时它会产生"口吃".如果您有一个矩形边界框(而不是复杂的边界对象),则可以选择沿着边界拖动对象并沿着边界框"滑动".您可以通过限制坐标值并让其他维度像往常一样移动来完成此操作.示例代码段如下所示:

var canvas = new fabric.Canvas("bounded");

var boundingBox = new fabric.Rect({
  fill: "none",
  width: 600,
  height: 400,
  hasBorders: false,
  hasControls: false,
  lockMovementX: true,
  lockMovementY: true,
  evented: false,
  stroke: "red"
});

var movingBox = new fabric.Rect({
  width: 100,
  height: 100,
  hasBorders: false,
  hasControls: false
});

canvas.on("object:moving", function() {
  var top = movingBox.top;
  var bottom = top + movingBox.height;
  var left = movingBox.left;
  var right = left + movingBox.width;

  var topBound = boundingBox.top;
  var bottomBound = topBound + boundingBox.height;
  var leftBound = boundingBox.left;
  var rightBound = leftBound + boundingBox.width;

  // capping logic here
  movingBox.setLeft(Math.min(Math.max(left, leftBound), rightBound - movingBox.width));
  movingBox.setTop(Math.min(Math.max(top, topBound), bottomBound - movingBox.height));
});

canvas.add(boundingBox);
canvas.add(movingBox);
Run Code Online (Sandbox Code Playgroud)

在JSFiddle中查看此示例


Mic*_*ton 11

Felix Fung的答案是一个起点,但有许多事情需要考虑.这是一个版本,其中包括其中一些.

它处理具有视口变换(即,缩放/平移)的画布以及中心原点而不是左/上原点的对象.它还将对象的宽度/高度限制为视口到顶部/左侧而不是底部/右侧.

canvas.on("object:moving", function(e) {
  var obj = e.target;
  var canvas = obj.canvas;
  var top = obj.top;
  var left = obj.left;
  var zoom = canvas.getZoom();
  var pan_x = canvas.viewportTransform[4];
  var pan_y = canvas.viewportTransform[5];

  // width & height we are constraining to must be calculated by applying the inverse of the current viewportTransform
  var c_width = canvas.width / zoom;
  var c_height = canvas.height / zoom;


  var w = obj.width * obj.scaleX
  var left_adjust, right_adjust
  if(obj.originX == "center") {
    left_adjust = right_adjust = w / 2;
  } else {
    left_adjust = 0;
    right_adjust = w;
  }

  var h = obj.height * obj.scaleY;
  var top_adjust, bottom_adjust;
  if(obj.originY == "center") {
    top_adjust = bottom_adjust = h / 2;
  } else {
    top_adjust = 0;
    bottom_adjust = h;
  }

  // if you need margins set them here
  var top_margin = 0;
  var bottom_margin = 0;
  var left_margin = 0;
  var right_margin = 0;


  var top_bound = top_margin + top_adjust - pan_y;
  var bottom_bound = c_height - bottom_adjust - bottom_margin - pan_y;
  var left_bound = left_margin + left_adjust - pan_x;
  var right_bound = c_width - right_adjust - right_margin - pan_x;

  if( w > c_width ) {
    obj.setLeft(left_bound);
  } else {
    obj.setLeft(Math.min(Math.max(left, left_bound), right_bound));          
  }

  if( h > c_height ) {
    obj.setTop(top_bound);
  } else {
    obj.setTop(Math.min(Math.max(top, top_bound), bottom_bound));          
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 完美运作。在v。2.0中,需要将setLeft(x)和setTop(y)更改为set('left',x)和set('top',y)。 (2认同)

Ora*_*ill 8

对我有用的是为事件创建一个事件监听器object:moving.当移动发生时,你更新了goodtop和goodleft变量,一旦你超出界限就把对象重新定位到最后的好点.

var goodtop, goodleft, boundingObject;

canvas.on("object:moving", function(){
    var obj = this.relatedTarget;
    var bounds = boundingObject;
    obj.setCoords();
    if(!obj.isContainedWithinObject(bounds)){
        obj.setTop(goodtop);
        obj.setLeft(goodleft);
        canvas.refresh();    
    } else {
        goodtop = obj.top;
        goodleft = obj.left;
    }  
});
Run Code Online (Sandbox Code Playgroud)