diff --git a/src/helpers.ts b/src/helpers.ts index a7bf017..9ac0302 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -52,6 +52,10 @@ export const getHeight = (object: Container): number => { return object.height } +export const centerPivot = (container: Container): void => { + container.pivot.set(container.width / 2, container.height / 2) +} + export const centerX = (container: Container, xPosition: number): void => { container.x = xPosition container.pivot.x = container.width / 2 diff --git a/test/test.ts b/test/test.ts index c976de2..b63e077 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,6 +1,6 @@ import test from 'ava' import * as ex from 'pixi-ex' -import { Container, Graphics, Assets } from 'pixi.js' +import { Container, Graphics, Assets, Sprite } from 'pixi.js' import * as internal from '../src/internal' @@ -106,3 +106,17 @@ test('centerY', (t) => { t.is(displayObject.y, 500) t.is(displayObject.pivot.y, 50) }) + +test('centerPivot', (t) => { + const container = new Container() + const sprite = new Sprite() + sprite.height = 200 + sprite.width = 300 + container.addChild(sprite) + + t.is(container.pivot.x, 0) + t.is(container.pivot.y, 0) + ex.centerPivot(container) + t.is(container.pivot.x, 150) + t.is(container.pivot.y, 100) +})