【问题标题】:Fabric.js: Assigning background color to groupFabric.js:为组分配背景颜色
【发布时间】:2017-08-12 01:47:40
【问题描述】:

我正在尝试为组设置背景颜色而不影响其包含的对象。

到目前为止,我的代码如下所示:

var canvas = new fabric.Canvas('canvas');

var helloText = new fabric.Text('hello', {
  fontSize: 30,
  top: 10, left: 10,
  originX: 0, originY: 0
});

var worldText = new fabric.Text('world!', {
  fontSize: 40,
  top: 50, left: 100,
  originX: 0, originY: 0
});

var group = new fabric.Group([helloText, worldText], {
    selectionBackgroundColor: 'red',
    backgroundColor: 'blue'
});

canvas.add(group);

可以在here找到一个jsFiddle版本。

从我的代码中可以看出,我已经尝试了属性backgroundColor,但它只影响包含的对象。我想实现类似selectionBackgroundColor的效果。

【问题讨论】:

    标签: javascript colors background grouping fabricjs


    【解决方案1】:

    稍作调整,但这应该会为您完成(相关代码):

    var text = new fabric.Group([helloText, worldText], {});
    var textBoundingRect = text.getBoundingRect();
    var background = new fabric.Rect({
      top: textBoundingRect.top,
      left: textBoundingRect.left,
      width: textBoundingRect.width,
      height: textBoundingRect.height,
      fill: 'blue'
    });
    var group = new fabric.Group([background, text], {});
    

    您的 JSFiddle 已更新,https://jsfiddle.net/rekrah/92ss3d86/

    【讨论】:

    • 您的调整是我的后备解决方案;)但是,我没有考虑使用边界矩形大小来定义背景矩形的大小。谢谢!
    • 是的。当涉及到 FabricJS 时,我发现一些关键字有点误导,我想你可以双向选择 backgroundColor 应该指的是组还是组中的单个项目,我猜。但我确实认为,无论您采用哪种方式,selectionBackgroundColor 都应该是一致的——所以因为backgroundColor 指的是组中的单个项目,所以selectionBackgroundColor 也应该是一致的——反之亦然。如果你得到我的漂移?很高兴能帮上忙,伙计!
    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多