Skip to content

Commit 9548fb9

Browse files
committed
Final textBounds updates.
1 parent 3f7f998 commit 9548fb9

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Version 2.4 - "Katar" - in dev
317317
* When loading a BitmapText you can now specify either an XML file or a JSON file for the font data. This is useful in environments such as Cocoon where you don't have a native XML parser. If you wish to use JSON the formatting should be equal to the result of running a valid XML file through X2JS (thanks @Feenposhleen #1837)
318318
* Game Objects that have the Health component (such as Sprites) now have a new method: `heal` which adds the given amount to the health property, i.e. is the opposite of `damage` (thanks @stephandesouza #1794)
319319
* maxHealth is a new property that Game Objects with the Health component receive and works in combination with the `heal` method to ensure a health limit cap.
320-
* Text.setTextBounds is a rectangular region that allows you to align your text within it, regardless of the number of lines of text or position within the world. For example in an 800x600 sized game if you set the textBounds to be 0,0,800,600 and text alignment to 'left' and vertical alignment to 'bottom' then the text will render in the bottom-right hand corner of the game, regardless of the size of font you're using or the number of lines in the text itself.
320+
* Text.setTextBounds is a rectangular region that allows you to align your text within it, regardless of the number of lines of text or position within the world. For example in an 800x600 sized game if you set the textBounds to be 0,0,800,600 and text alignment to 'left' and vertical alignment to 'bottom' then the text will render in the bottom-right hand corner of the game, regardless of the size of font you're using or the number of lines in the text itself (thanks @boostermedia for the idea #1824)
321321
* Text.autoRound allows you to control if the text is allowed to render at sub-pixel coordinates or not. Set to `true` to round the coordinates, often eliminating anti-aliasing from certain font types (#1867)
322322
* Tiled Image Collection support is now available and has been added to the TilemapParser and Tilemap classes (thanks @asyed94 #1879)
323323
* Keyboard.addKeys is a practical way to create an object containing user selected hotkeys. For example: `addKeys( [Phaser.Keyboard.W, Phaser.Keyboard.S, Phaser.Keyboard.A, Phaser.Keyboard.D], [ 'up', 'down', 'left', 'right' ] );` would return an object containing the properties `up`, `down`, `left` and `right` that you could poll just like a Phaser.Key object. (thanks @Mourtz #1857)
@@ -414,6 +414,7 @@ Version 2.4 - "Katar" - in dev
414414
* Setting mute to false on Sound that was never muted caused its volume to be set to zero (thanks @brianbunch #1870)
415415
* P2.Body.createGroupCallback incorrectly referenced the `_groupCallbackContext` when deleting it (thanks @Langerz82 #1886)
416416
* When reusing a Tween created with an array of properties the values would get exponentially added to the TweenData internal array each time the tween was re-run (thanks @SBCGames #1747)
417+
* Reading the dimensions of a Text object would reset its resolution property (thanks @joelika #1717)
417418

418419
### Deprecated
419420

src/gameobjects/Text.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ Phaser.Text = function (game, x, y, text, style) {
8080
this.context = this.canvas.getContext('2d');
8181

8282
/**
83-
*
84-
* @property {number} resolution - The resolution of the canvas.
83+
* The resolution of the canvas used for rendering the Text.
84+
* If you change this you need to call `Text.updateText` after doing so.
85+
* @property {number} resolution
8586
* @default
8687
*/
8788
this.resolution = 1;
@@ -137,8 +138,6 @@ Phaser.Text = function (game, x, y, text, style) {
137138

138139
Phaser.Sprite.call(this, game, x, y, PIXI.Texture.fromCanvas(this.canvas));
139140

140-
this.texture.trim = new Phaser.Rectangle();
141-
142141
this.setStyle(style);
143142

144143
if (text !== '')
@@ -730,13 +729,15 @@ Phaser.Text.prototype.setText = function (text) {
730729
*
731730
* This is especially powerful when you need to align text against specific coordinates in your game, but the actual text dimensions
732731
* may vary based on font (say for multi-lingual games).
733-
*
734-
* It works by calculating the final position based on the Text.canvas size, which is modified as the text is updated. Some fonts
735-
* have additional padding around them which you can mitigate by tweaking the Text.padding property.
736732
*
737733
* If `Text.wordWrapWidth` is greater than the width of the text bounds it is clamped to match the bounds width.
738734
*
739735
* Call this method with no arguments given to reset an existing textBounds.
736+
*
737+
* It works by calculating the final position based on the Text.canvas size, which is modified as the text is updated. Some fonts
738+
* have additional padding around them which you can mitigate by tweaking the Text.padding property. It then adjusts the `pivot`
739+
* property based on the given bounds and canvas size. This means if you need to set the pivot property directly in your game then
740+
* you either cannot use `setTextBounds` or you must place the Text object inside another DisplayObject on which you set the pivot.
740741
*
741742
* @method Phaser.Text#setTextBounds
742743
* @param {number} [x] - The x coordinate of the Text Bounds region.
@@ -784,7 +785,6 @@ Phaser.Text.prototype.updateTexture = function () {
784785

785786
var base = this.texture.baseTexture;
786787
var crop = this.texture.crop;
787-
var trim = this.texture.trim;
788788
var frame = this.texture.frame;
789789

790790
var w = this.canvas.width;
@@ -796,9 +796,6 @@ Phaser.Text.prototype.updateTexture = function () {
796796
crop.width = w;
797797
crop.height = h;
798798

799-
trim.width = w;
800-
trim.height = h;
801-
802799
frame.width = w;
803800
frame.height = h;
804801

@@ -832,8 +829,8 @@ Phaser.Text.prototype.updateTexture = function () {
832829
y = this.textBounds.halfHeight - (this.canvas.height / 2);
833830
}
834831

835-
trim.x = x;
836-
trim.y = y;
832+
this.pivot.x = -x;
833+
this.pivot.y = -y;
837834
}
838835

839836
this.texture.baseTexture.dirty();

0 commit comments

Comments
 (0)