Fabric.js:为组分配背景颜色

Ste*_*amp 6 javascript grouping background colors fabricjs

我试图为一个组设置背景颜色而不影响其包含的对象。

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

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);
Run Code Online (Sandbox Code Playgroud)

可以在此处找到 jsFiddle 版本。

正如您从我的代码中看到的,我已经尝试过该属性backgroundColor,但它只影响包含的对象。我想达到类似的效果 selectionBackgroundColor

Tim*_*ker 4

稍微调整一下,但这应该适合你(相关代码):

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], {});
Run Code Online (Sandbox Code Playgroud)

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