createjs 创建图片位图并设置中心点为图片中心

public static createBitmap(imgUrl: string): Promise<any> {
    return new Promise<object | number>((resolve, reject) => {
      const image: any = new Image()
      image.src = imgUrl || ''
      image.onload = () => {
        const Bitmap = new createjs.Bitmap(image)
        const { width, height } = Bitmap.image
        // Bitmap.setBounds(0, 0, width, height);
        Bitmap.regX = width / 2
        Bitmap.regY = height / 2
        resolve(Bitmap)
      }
      image.onerror = (error: any) => {
        console.warn(`"image load fail"${imgUrl}`, error)
      }
    })
  }

注意:默认注册中心点(旋转时围绕的点)是在左上角,regX,regY并不是改变注册中心点,而是向左上方向偏移宽高的一半,但只是偏移物体,这个物体的注册中心点是不变的。想改变注册中心点,只能靠x,y来设置位置,注册中心点永远都在左上角,但通过regX,regY可以把物体本身往反方向平移一半(但注册中心点还在之前的位置),这样的话,我们的旋转位置看起来就是围着中间点旋转了。这么说大家可能有点懵,没关系,这里确实有点难理解~有个博客写的很好,大家可以结合我总结的看一下:

Easeljs之regX/regY详解