【问题标题】:Unable to maintain thickness of strokeWidth while resizing in case of Groups in Fabricjs在 Fabricjs 中的 Groups 的情况下,在调整大小时无法保持 strokeWidth 的厚度
【发布时间】:2016-03-19 20:41:45
【问题描述】:

我正在编写代码以在 Fabricjs 中的所有对象之间保持相同的 strokeWidth。

我能够成功地维护普通 Objects 的 strokeWidth ,让它成为矩形或椭圆(我编写了代码来维护两者)。 但是当我使用 group 时,我无法保持 strokeWidth。

这是fiddle。

重现问题的步骤?
1)打开上面小提琴链接
2)调整圆的大小,检查边框的大小(调整大小时边框的厚度保持不变)
3)现在调整矩形和 text(group) 的大小,预期的结果是边框的粗细必须始终相同,但 不会发生。

代码: `

var gCanvas = new fabric.Canvas('canvDraw');
gCanvas.setWidth(700);
gCanvas.setHeight(700);
var text = new fabric.Text('test', {  fontSize: 30, top: 100, left : 100, fill : 'red'});
// add some shapes - these are examples, others should work too
var myRectangle = new fabric.Rect({
left: 100,
top: 100,
fill: '#999999',
width: 120,
height: 120,
strokeWidth: 3,
fill: '',
stroke: '#000000'
});
var group = new fabric.Group([ myRectangle, text ], {borderColor: 'black', cornerColor: 'green', lockScalingFlip : true});
gCanvas.add(group);

var myEllipse = new fabric.Ellipse({
top: 250,
left: 100,
rx: 75,
ry: 50,
fill: '',
stroke: '#000000',
strokeWidth: 3
});
gCanvas.add(myEllipse);
gCanvas.observe('object:scaling', function (e) {
        e.target.resizeToScale();
    });


fabric.Object.prototype.resizeToScale = function () {
// resizes an object that has been scaled (e.g. by manipulating the handles), setting scale to 1 and recalculating bounding box where necessary
switch (this.type) {
    case "ellipse":
        this.rx *= this.scaleX;
        this.ry *= this.scaleY;
        this.width = this.rx * 2;
        this.height = this.ry * 2;
        this.scaleX = 1;
        this.scaleY = 1;
        break;

    case "rect":
        this.width *= this.scaleX;
        this.height *= this.scaleY;
        this.scaleX = 1;
        this.scaleY = 1;
    default:
        break;
}
}

`

【问题讨论】:

    标签: javascript jquery html5-canvas fabricjs


    【解决方案1】:

    这种方法不适用于所有类型的形状。面对文本或多边形时,您无法重新计算所有内容。

    你可以这样做:

    fabric.Object.prototype.resizeToScale = function () {
      if  (this.type !=='group') {
        this.strokeWidth = this._origStrokeWidth / Math.max(this.scaleX, this.scaleY);
      }
      else {
        this._objects.forEach( function(obj){
          console.log(obj);
          obj.strokeWidth = obj._origStrokeWidth / Math.max(obj.group.scaleX, obj.group.scaleY);
        });
      }
    }
    

    如果您更喜欢解决方案的效果(边框始终是统一的),请将这行代码放在您的 switch 构造的默认情况下,以处理您无法找到调整大小解决方案的所有类型。

    编辑我添加了组处理。 关键是在组/对象缩放时使笔划宽度更小。 因为当您将拥有多边形、折线和路径时,更改宽度/高度/半径将无济于事。

    http://jsfiddle.net/y344vs5s/4/

    【讨论】:

    • 如果我将 rect 代码置于默认值并调整大小,则会发生其他情况,请通过更新的小提琴jsfiddle.net/Subhasish2015/y344vs5s/2
    • 另外,想加一个点,我可以在 jsfiddle 的椭圆中调整大小并保持正确的 strokeWidth。那么,为什么不分组呢? fabricjs 有什么问题吗?
    • 好吧,这不起作用..如果你找到解决方案,请告诉我
    • 这个fiddle 解决了这个问题,但在一个失败的情况下,如果你拖动并调整大小以使其成为一个宽度为 10 和高度为 100 的矩形(只需假设并相应地调整大小),那么 x 轴笔划宽度与 y 轴笔划宽度不同。
    • 是的,正如我所说,您可以保留每个形状的解决方案,并将此解决方案仅用于无法调整大小的文本、多边形、折线、路径和其他形状)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2016-12-18
    • 1970-01-01
    • 2021-07-21
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多