spx icon indicating copy to clipboard operation
spx copied to clipboard

Sprite `pivot` & Costume `x`, `y`

Open nighca opened this issue 5 months ago • 0 comments

以下是记录

Sprite pivot (x, y)

Sprite 的“定位/缩放/旋转参考点”相对其左上角的偏移量,正的 x 表示向右偏移,正的 y 表示向上偏移。

如设置 pivot: 0, 0,则表示该精灵的“定位/缩放/旋转参考点”在其左上角。

那么 sprite x: 0, y: 0 时,精灵的左上角与舞台中心对齐

Builder 默认会使用默认 costume 的大小来作为 sprite 的大小,并根据该大小将 pivot 设置为 sprite 的视觉中心。

this.setPivot({ x: costumeSize.width / 2, y: -costumeSize.height / 2 })

注意 pivot 的单位是计入(除以)bitmapResolution 后的结果,因此对于一个 sprite,若其 costume 为 100x100 像素的图片、bitmapResolution 为 2 的 costume,那么设置 pivot: { x: 25, y: -25 } 时,其“定位/缩放/旋转参考点”就在其视觉中心

相关 issue: https://github.com/goplus/builder/issues/570

Costume x, y

Costume 渲染时相对所在 Sprite 的偏移量,正的 x 表示向左偏移(P.S. 这里跟上面不一致,可能是实现上的错误?),正的 y 表示向上偏移。

注意 x、y 的单位是未计入(除以)bitmapResolution 的结果。

因此对于一个 x: 0, y: 0, pivot: { 0, 0 } 的 sprite,其左上角与舞台中心对齐;对于其中的一个 x: 20, y: 20, bitmapResolution: 2 的 costume,其渲染时图片左上角相对舞台中心会分别向左、向上偏移 10

另,

Costume 渲染时相对所在 Sprite 的偏移量,正的 x 表示向左偏移(P.S. 这里跟上面不一致,可能是实现上的错误?),正的 y 表示向上偏移。

这个应该是出于跟 scratch rotationCenterX & rotationCenterY 一致的考虑,后者的正负含义与 spx Costume x, y 一致

相关 issue / PR

  • https://github.com/goplus/builder/issues/570
  • https://github.com/goplus/spx/pull/294
  • https://github.com/goplus/spx/issues/297

nighca avatar Jul 18 '25 10:07 nighca