为什么 easeljs stage.getBounds() return 为空?

Why does easeljs stage.getBounds() return null?

在这个例子中:

var canvas = document.getElementById("testCanvas");
var stage = new createjs.Stage(canvas);

function drawRectangle(){
 var rect = new createjs.Shape();
 rect.graphics.beginFill("#000").drawRect(10, 10, 100, 100);
 stage.addChild(rect);
}

function drawShapes(){
  drawRectangle();
 stage.update();
}

drawShapes();
// Why does this call log null?
console.log(stage.getBounds());
<script src="https://cdnjs.cloudflare.com/ajax/libs/EaselJS/0.8.0/easeljs.min.js"></script>
<canvas id="testCanvas" width="600" height="150"></canvas>

stage.getBounds(); 为 returning null。为什么它 return 为空?根据 docs,它应该是 return 一个代表舞台边界框尺寸的矩形对象。

如果这种用法不正确,那么获取对象尺寸的正确用法是什么?

看起来 stage 对象无法计算自己的边界。

在同一个 getBounds() documentation 中出现以下内容:

Not all display objects can calculate their own bounds (ex. Shape). For these objects, you can use setBounds so that they are included when calculating Container bounds.

如果是这种情况,如文档所述,您可以使用 setBounds() 方法 (documentation) 手动设置对象的边界:

setBounds(x,y,width,height)

对于您的代码:

stage.setBounds(0,0,600,150); // (0,0), at the same size of the canvas