为什么easeljs stage.getBounds() 返回null?

jus*_*tin 2 easeljs createjs

在这个例子中:

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());
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/EaselJS/0.8.0/easeljs.min.js"></script>
<canvas id="testCanvas" width="600" height="150"></canvas>
Run Code Online (Sandbox Code Playgroud)

stage.getBounds();正在返回空值。为什么它返回null?根据docs,它应该返回一个表示舞台边界框尺寸的矩形对象。

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

cri*_*aos 5

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

在同一getBounds() 文档中,它出现以下内容:

并非所有显示对象都可以计算自己的边界(例如形状)。对于这些对象,您可以使用setBounds以便在计算容器边界时将它们包括在内。

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

setBounds(x,y,width,height)
Run Code Online (Sandbox Code Playgroud)

就您的代码而言:

stage.setBounds(0,0,600,150); // (0,0), at the same size of the canvas
Run Code Online (Sandbox Code Playgroud)