npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pixi-center

v1.0.11

Published

Center Sprites, Text and DisplayObjects in pixi.js with ease

Downloads

39

Readme

pixi-center

Center PIXI.Text, PIXI.Sprite, & PIXI.Container vertically and horizontally with ease. Actually you can center anything which inherits from PIXI.DisplayObject. It will round to the nearest pixel so your text stays sharp!

Install

npm i pixi-center

Importing

For convenience pixi-center will be automatically added to PIXI.displayObject which almost everything, including PIXI.Sprite and PIXI.Text, inerit from.

common.js

require('pixi.js')
require('pixi-center')

es6 modules

import pixi from 'pixi.js'
import pixiPause from 'pixi-center'

Usage

Methods

centerX

Center an object horizontally inside a container

mySprite.centerX(
    width,      // container width (Optional)
    options     // see options (Optional)
)

centerY

Center an object vertically inside a container

mySprite.centerY(
    height,     // container height (Optional)
    options     // see options (Optional)
)

centerXY

Center an object horizontally & vertically inside a container

https://jsfiddle.net/brenwell/ecad57kb/

mySprite.centerXY(
    width,      // container width (Optional)
    height,     // container height (Optional)
    options     // see options (Optional)
)

Center an object horizontally & vertically at a point

centerAt

https://jsfiddle.net/brenwell/zpk590p6/

mySprite.centerAt(
    coordinates, // Coordinates e.g. {x:10,y:30}
    options     // see options (Optional)
)

###Examples

Using the parent container to center

This is the simplest example. Pixi-center uses the parent's width and/or height if no options are passed. Which means, your object needs to be added to a container before calling centerXY, centerX or centerY.

const label = new PIXI.Text(text,style)
container.addChild(label)

// call after the object has been added to a child
label.centerXY()

Explictly supplying the bounds

This will center the text within a theoretical box of width:200 by height:300. Since you are providing the bounds it is not immportant to add the object to a container before calling centerXY, centerX or centerY.

const label = new PIXI.Text(text,style)
label.centerXY(200,300)

// can be added after
container.addChild(label)

Center one axis only

This will center the text vertically within a theoretical box of height:300, and then horizontally within the container,

const label = new PIXI.Text(text,style)

// center just the x axis within 500px
label.centerY(500)

container.addChild(label)

// center just the y axis with the parent container
label.centerY()

Center at a point

This will center the label at the given coordinate, regardless of anchor values.

const label = new PIXI.Text(text,style)
label.centerAt({x: 10, y: 10})

Options

The possible options are

const opts = {
    round: true, // default is true. Applies to all methods.
    anchorX: 0, // if not set will be taken from object if possible.
    anchorY: 0  // if not set will be taken from object if possible.
}

Round to pixel

By default pixi-center will place objects on whole pixels by rounding the result of the calculation. Which is great for text as it will be crystal clear. However at times this behaviour is unwanted. Passing an options object with round:false as the last argument will disable this.

label.centerX( null, {round: false} )
label.centerY( null, {round: false} )
label.centerXY( null, null, {round: false} )
label.centerAt( {x: 0, y: 0}, {round: false} )

Anchor positions

By default pixi-center will account for an element's anchor points when centering. But this can be overwritten if you wish by passing an options object with anchorX or anchorY set with values between 0-1 as the last argument. This will offset the centering.

label.centerX( undefined, {anchorX: 0} )
label.centerY( undefined, {anchorY: 0} )
label.centerXY( undefined, undefined, {anchorX: 0, anchorY: 0.5} )
label.centerAt( {x: 0, y: 0}, {anchorX: 0, anchorY: 0.5} )

Update on Resize

You can simply call any of the center functions on window.resize

window.onresize = (e) => label.centerXY()

Or for elements with an update method you could re-center when it is fired

mySprite.update = () => mySprite.centerXY()

Gotchas

Not centering in an empty PIXI.Container or my PIXI.Container keeps growing every time I center my object

PIXI.Containers have a width and height of 0 until something is placed inside them. If you want to center the text inside an empty container, it is best to add a PIXI.Graphics element first to give it some bounds

const container = new PIXI.Container()

const rectangle = new PIXI.Graphics()
rectangle.drawRect(0,0,100,100)
rectangle.alpha = 0 // if you dont want to see it
container.addChild(rectangle)

const label = new PIXI.Text('center me')
container.addChild(label)
container.centerXY()

To resize the container it is best to resize the rectangle instead