为什么easyljs stage.getBounds()返回null? [英] Why does easeljs stage.getBounds() return null?

查看:103
本文介绍了为什么easyljs stage.getBounds()返回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(); 返回null。为什么返回null?根据 docs ,它应该返回一个矩形对象表示舞台边界框的尺寸。

stage.getBounds(); is returning null. Why does it return null? According to the docs, it's supposed to return a rectangle object representing the dimensions of the bounding box of the stage.

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

If this usage is incorrect, what is the correct usage to get the dimensions of the object?

推荐答案

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

It looks like the stage object can't calculate their own bounds.

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

In the same getBounds() documentation it appears the following:


并非所有显示对象都可以计算自己的边界(例如Shape)。对于这些对象,可以使用 setBounds ,以便

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

If it's the case, as the documentation says, you can use the setBounds() method (documentation) to set manually the bounds of the object:

setBounds(x,y,width,height)

对于您的代码:

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

这篇关于为什么easyljs stage.getBounds()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆