简体   繁体   English

如何增加Fabric对象的选择区域?

[英]How to increase the selection area of a fabric Object?

Consider the following code: 请考虑以下代码:

var lineObj = new fabric.Line([120,200,320,200],{
  stroke: '#000000',
  strokeWidth: 1
});
canvas.add(lineObj);

This 1 pixel line is very hard to select owing to its very small width. 由于其非常小的宽度,这1像素线很难选择。 What I wish to do here is to increase its selection area. 我想在这里做的是增加其选择范围。 Like this: 像这样:

Line Corners

Is there any way in which I can accomplish this? 有什么方法可以实现这个目标吗?

Yes, we have padding value for that. 是的,我们有padding值。

var lineObj = new fabric.Line([120,200,320,200], {
  stroke: '#000000',
  strokeWidth: 1,
  padding: 20
});
canvas.add(lineObj);

在此输入图像描述

Did I mention Fabric is flexible and powerful? 我提到Fabric是否灵活而强大? :) :)

@markE Nice solution! @markE不错的解决方案! Glad you're finding it easy to walk through source code. 很高兴您发现浏览源代码很容易。

Bad news--no solution in API / Good news--you can code your solution 坏消息 - API /好消息中没有解决方案 - 您可以编写解决方案代码

The API doesn't provide a way to expand the boundingbox of a line, so there's no API way to get a bigger selection area for lines. API没有提供扩展行的边界框的方法,因此没有API方法来获得更大的行选择区域。

FabricJS is open-source and well-organized and the source code itself has useful comments. FabricJS是开源的,组织良好,源代码本身也有很多有用的注释。 Here's what I found... 这是我发现的......

"Line" objects extend the "Object" object. “Line”对象扩展“Object”对象。 The "Object" has helper methods and the most pertinent is in the file object_geometry.mixin.js. “对象”有辅助方法,最相关的是文件object_geometry.mixin.js。 In there, I found that the boundingbox for any object is being generated using the method getBoundingRect(). 在那里,我发现使用方法getBoundingRect()生成任何对象的边界框。

You did ask "Is there ANY way...", so here's that way: 你确实问过“有什么方法......”,所以就是这样:

So to solve your issue, you must over-ride the getBoundingRect() for lines and make it slightly wider. 因此,要解决您的问题,您必须覆盖线路的getBoundingRect()并使其稍微宽一些。 This will automatically make the selection area of your lines wider and more easily clicked. 这将自动使您的线条选择区域更宽,更容易点击。 @Kangax, feel free to indicate any simpler solution! @Kangax,随意指出任何更简单的解决方案!

To get you started, here is the source for getBoundingRect() from the source file object_geometry.mixin.js: 为了帮助您入门,以下是源文件object_geometry.mixin.js中getBoundingRect()的源代码:

getBoundingRect: function() {
  this.oCoords || this.setCoords();

  var xCoords = [this.oCoords.tl.x, this.oCoords.tr.x, this.oCoords.br.x, this.oCoords.bl.x];
  var minX = fabric.util.array.min(xCoords);
  var maxX = fabric.util.array.max(xCoords);
  var width = Math.abs(minX - maxX);

  var yCoords = [this.oCoords.tl.y, this.oCoords.tr.y, this.oCoords.br.y, this.oCoords.bl.y];
  var minY = fabric.util.array.min(yCoords);
  var maxY = fabric.util.array.max(yCoords);
  var height = Math.abs(minY - maxY);

  return {
    left: minX,
    top: minY,
    width: width,
    height: height
  };
},

/**
 * Returns width of an object
 * @method getWidth
 * @return {Number} width value
 */
getWidth: function() {
  return this.width * this.scaleX;
},

/**
 * Returns height of an object
 * @method getHeight
 * @return {Number} height value
 */
getHeight: function() {
  return this.height * this.scaleY;
},

/**
 * Makes sure the scale is valid and modifies it if necessary
 * @private
 * @method _constrainScale
 * @param {Number} value
 * @return {Number}
 */
_constrainScale: function(value) {
  if (Math.abs(value) < this.minScaleLimit) {
    if (value < 0)
      return -this.minScaleLimit;
    else
      return this.minScaleLimit;
  }

  return value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM