diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 041e39c..69b5b88 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -1,4 +1,5 @@ -var core = require('../core'); +var core = require('../core'), + math = require('../core/math'); /** * A BitmapText object will create a line or multiple lines of text using bitmap font. To @@ -105,6 +106,24 @@ this.maxLineHeight = 0; /** + * The anchor sets the origin point of the text. + * The default is 0,0 this means the text's origin is the top left + * Setting the anchor to 0.5,0.5 means the text's origin is centered + * Setting the anchor to 1,1 would mean the text's origin point will be the bottom right corner + * + * @member {PIXI.Point} + */ + this.anchor = new math.Point(); + + /** + * Private tracker for the anchor. This allows us to check whether there has been a change + * + * @member {PIXI.Point} + * @private + */ + this._anchor = this.anchor.clone(); + + /** * The dirty state of this object. * * @member {boolean} @@ -343,6 +362,16 @@ this.textWidth = maxLineWidth * scale; this.textHeight = (pos.y + data.lineHeight) * scale; + + // apply anchor + if (this.anchor.x !== 0 || this.anchor.y !== 0) + { + for (i = 0; i < lenChars; i++) + { + this._glyphs[i].x -= this.textWidth * this.anchor.x; + this._glyphs[i].y -= this.textHeight * this.anchor.y; + } + } this.maxLineHeight = maxLineHeight * scale; }; @@ -376,6 +405,13 @@ */ BitmapText.prototype.validate = function() { + // Detect whether the anchor has been updated + if (!this.anchor.equals(this._anchor)) + { + this.dirty = true; + this._anchor.copy(this.anchor); + } + if (this.dirty) { this.updateText();