Skip to content

Commit a67d2df

Browse files
committed
BitmapData.text will render the given string to the BitmapData, with optional font, color and shadow settings.
1 parent 3c2725a commit a67d2df

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Version 2.3.0 - "Tarabon" - in dev
6464

6565
* `Physics.Arcade.isPaused` allows you to toggle Arcade Physics processing on and off. If `true` the `Body.preUpdate` method will be skipped, halting all motion for all bodies. Note that other methods such as `collide` will still work, so be careful not to call them on paused bodies.
6666
* `Arcade.Body.friction` allows you to have more fine-grained control over the amount of velocity passed between bodies on collision.
67+
* BitmapData.text will render the given string to the BitmapData, with optional font, color and shadow settings.
6768

6869
### Updates
6970

src/gameobjects/BitmapData.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,45 @@ Phaser.BitmapData.prototype = {
13541354

13551355
},
13561356

1357+
/**
1358+
* Draws text to the BitmapData in the given font and color.
1359+
* The default font is 14px Courier, so useful for quickly drawing debug text.
1360+
* If you need to do a lot of font work to this BitmapData we'd recommend implementing your own text draw method.
1361+
*
1362+
* @method Phaser.BitmapData#text
1363+
* @param {string} text - The text to write to the BitmapData.
1364+
* @param {number} x - The x coordinate of the top-left of the text string.
1365+
* @param {number} y - The y coordinate of the top-left of the text string.
1366+
* @param {string} [font='14px Courier'] - The font. This is passed directly to Context.font, so anything that can support, this can.
1367+
* @param {string} [color='rgb(255,255,255)'] - The color the text will be drawn in.
1368+
* @param {boolean} [shadow=true] - Draw a single pixel black shadow below the text (offset by text.x/y + 1)
1369+
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
1370+
*/
1371+
text: function (text, x, y, font, color, shadow) {
1372+
1373+
if (typeof x === 'undefined') { x = 0; }
1374+
if (typeof y === 'undefined') { y = 0; }
1375+
if (typeof font === 'undefined') { font = '14px Courier'; }
1376+
if (typeof color === 'undefined') { color = 'rgb(255,255,255)'; }
1377+
if (typeof shadow === 'undefined') { shadow = true; }
1378+
1379+
var prevFont = this.context.font;
1380+
1381+
this.context.font = font;
1382+
1383+
if (shadow)
1384+
{
1385+
this.context.fillStyle = 'rgb(0,0,0)';
1386+
this.context.fillText(text, x + 1, y + 1);
1387+
}
1388+
1389+
this.context.fillStyle = color;
1390+
this.context.fillText(text, x, y);
1391+
1392+
this.context.font = prevFont;
1393+
1394+
},
1395+
13571396
/**
13581397
* Draws a filled Circle to the BitmapData at the given x, y coordinates and radius in size.
13591398
*

0 commit comments

Comments
 (0)