You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Text Bounds 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.
Set the Style properties `boundsAlignH` and `boundsAlignV` or adjust them via the Text setters to change the alignment.
It works by calculating the final position based on the Text.canvas size, which is modified as the text is updated. Some fonts have additional padding around them which you can mitigate by tweaking the Text.padding property.
Setting a textBounds _doesn't_ update the wordWrapWidth, so be aware of the relationship between the two.
Call this method with nothing defined for any of the parameters to reset an existing textBounds.
phaserjs#1824
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -317,6 +317,7 @@ Version 2.4 - "Katar" - in dev
317
317
* 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)
318
318
* 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)
319
319
* 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.
* @param {number} x - X position of the new text object.
20
20
* @param {number} y - Y position of the new text object.
21
21
* @param {string} text - The actual text that will be written.
22
-
* @param {object} style - The style object containing style attributes like font, font size, etc.
22
+
* @param {object} [style] - The style properties to be set on the Text.
23
+
* @param {string} [style.font='bold 20pt Arial'] - The style and size of the font.
24
+
* @param {string} [style.fontStyle=(from font)] - The style of the font (eg. 'italic'): overrides the value in `style.font`.
25
+
* @param {string} [style.fontVariant=(from font)] - The variant of the font (eg. 'small-caps'): overrides the value in `style.font`.
26
+
* @param {string} [style.fontWeight=(from font)] - The weight of the font (eg. 'bold'): overrides the value in `style.font`.
27
+
* @param {string|number} [style.fontSize=(from font)] - The size of the font (eg. 32 or '32px'): overrides the value in `style.font`.
28
+
* @param {string} [style.backgroundColor=null] - A canvas fillstyle that will be used as the background for the whole Text object. Set to `null` to disable.
29
+
* @param {string} [style.fill='black'] - A canvas fillstyle that will be used on the text eg 'red', '#00FF00'.
30
+
* @param {string} [style.align='left'] - Horizontal alignment of each line in multiline text. Can be: 'left', 'center' or 'right'. Does not affect single lines of text (see `textBounds` and `boundsAlignH` for that).
31
+
* @param {string} [style.boundsAlignH='left'] - Horizontal alignment of the text within the `textBounds`. Can be: 'left', 'center' or 'right'.
32
+
* @param {string} [style.boundsAlignV='top'] - Vertical alignment of the text within the `textBounds`. Can be: 'top', 'middle' or 'bottom'.
33
+
* @param {string} [style.stroke='black'] - A canvas stroke style that will be used on the text stroke eg 'blue', '#FCFF00'.
34
+
* @param {number} [style.strokeThickness=0] - A number that represents the thickness of the stroke. Default is 0 (no stroke).
35
+
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
36
+
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
23
37
*/
24
38
Phaser.Text=function(game,x,y,text,style){
25
39
26
40
x=x||0;
27
41
y=y||0;
28
-
text=text||'';
42
+
text=text||'';
29
43
style=style||{};
30
44
31
-
if(text.length===0)
32
-
{
33
-
text=' ';
34
-
}
35
-
else
36
-
{
37
-
text=text.toString();
38
-
}
39
-
40
45
/**
41
46
* @property {number} type - The const type of this object.
42
47
* @default
@@ -56,6 +61,14 @@ Phaser.Text = function (game, x, y, text, style) {
56
61
*/
57
62
this.padding=newPhaser.Point();
58
63
64
+
/**
65
+
* The textBounds property allows you to specify a rectangular region upon which text alignment is based.
66
+
* See `Text.setTextBounds` for more details.
67
+
* @property {Phaser.Rectangle} textBounds
68
+
* @readOnly
69
+
*/
70
+
this.textBounds=null;
71
+
59
72
/**
60
73
* @property {HTMLCanvasElement} canvas - The canvas element that the text is rendered.
61
74
*/
@@ -116,9 +129,11 @@ Phaser.Text = function (game, x, y, text, style) {
@@ -192,6 +207,7 @@ Phaser.Text.prototype.destroy = function (destroyChildren) {
192
207
* @param {number} [blur=0] - The shadowBlur value. Make the shadow softer by applying a Gaussian blur to it. A number from 0 (no blur) up to approx. 10 (depending on scene).
193
208
* @param {boolean} [shadowStroke=true] - Apply the drop shadow to the Text stroke (if set).
194
209
* @param {boolean} [shadowFill=true] - Apply the drop shadow to the Text fill (if set).
@@ -210,6 +226,8 @@ Phaser.Text.prototype.setShadow = function (x, y, color, blur, shadowStroke, sha
210
226
this.style.shadowFill=shadowFill;
211
227
this.dirty=true;
212
228
229
+
returnthis;
230
+
213
231
};
214
232
215
233
/**
@@ -224,11 +242,14 @@ Phaser.Text.prototype.setShadow = function (x, y, color, blur, shadowStroke, sha
224
242
* @param {string|number} [style.fontSize=(from font)] - The size of the font (eg. 32 or '32px'): overrides the value in `style.font`.
225
243
* @param {string} [style.backgroundColor=null] - A canvas fillstyle that will be used as the background for the whole Text object. Set to `null` to disable.
226
244
* @param {string} [style.fill='black'] - A canvas fillstyle that will be used on the text eg 'red', '#00FF00'.
227
-
* @param {string} [style.align='left'] - Alignment for multiline text ('left', 'center' or 'right'), does not affect single line text.
245
+
* @param {string} [style.align='left'] - Horizontal alignment of each line in multiline text. Can be: 'left', 'center' or 'right'. Does not affect single lines of text (see `textBounds` and `boundsAlignH` for that).
246
+
* @param {string} [style.boundsAlignH='left'] - Horizontal alignment of the text within the `textBounds`. Can be: 'left', 'center' or 'right'.
247
+
* @param {string} [style.boundsAlignV='top'] - Vertical alignment of the text within the `textBounds`. Can be: 'top', 'middle' or 'bottom'.
228
248
* @param {string} [style.stroke='black'] - A canvas stroke style that will be used on the text stroke eg 'blue', '#FCFF00'.
229
249
* @param {number} [style.strokeThickness=0] - A number that represents the thickness of the stroke. Default is 0 (no stroke).
230
250
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
231
251
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
252
+
* @return {Phaser.Text} This Text instance.
232
253
*/
233
254
Phaser.Text.prototype.setStyle=function(style){
234
255
@@ -237,6 +258,8 @@ Phaser.Text.prototype.setStyle = function (style) {
0 commit comments