如何使用 CreateJS 将图像居中 canvas

How to center an image on canvas with CreateJS

我正在学习 CreateJS,但 运行 遇到了一些困难。 通常我会通过将 canvas 一分为二并将 width/height 的一半减去这些值来居中图像,但不幸的是我不知道如何使用 CreateJS 访问缩小图像的大小。

即使使用 obj.image.width 也无济于事,因为它 returns 0.

有没有办法做到这一点,或者我不知道的某些功能?

非常感谢!

您可以将图像大小乘以其比例 (scaleX and/or scaleY) 以获得 "transformed" 大小。

// Note that the image must be loaded before you can get the width    
bmp.x = (stage.canvas.width - bmp.image.width * bmp.scaleX) / 2;

这是一个快速 fiddle: http://jsfiddle.net/lannymcnie/v6vuwntx/