Skip to content

Commit

Permalink
Add a centerPivot function
Browse files Browse the repository at this point in the history
  • Loading branch information
sajmoni committed Nov 19, 2022
1 parent 6abb53c commit 56c1985
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
})

0 comments on commit 56c1985

Please sign in to comment.