Skip to content

Commit b7b622a

Browse files
committed
Text style has a new optional property: backgroundColor which is a Canvas fill style that is set behind all Text in the Text object. It allows you to set a background color without having to use an additional Graphics object.
Text.lineSpacing can now accept negative values without cutting the bottom of the Text object off. The value can never be less than the height of a single line of text. Text.lineSpacing is no longer applied to the first line of Text, which prevents text from being cut off further down the Text object.
1 parent 7010883 commit b7b622a

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

src/gameobjects/Text.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ Phaser.Text.prototype.setShadow = function (x, y, color, blur) {
226226
* @param {string} [style.fontVariant=(from font)] - The variant of the font (eg. 'small-caps'): overrides the value in `style.font`.
227227
* @param {string} [style.fontWeight=(from font)] - The weight of the font (eg. 'bold'): overrides the value in `style.font`.
228228
* @param {string|number} [style.fontSize=(from font)] - The size of the font (eg. 32 or '32px'): overrides the value in `style.font`.
229+
* @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.
229230
* @param {string} [style.fill='black'] - A canvas fillstyle that will be used on the text eg 'red', '#00FF00'.
230231
* @param {string} [style.align='left'] - Alignment for multiline text ('left', 'center' or 'right'), does not affect single line text.
231232
* @param {string} [style.stroke='black'] - A canvas stroke style that will be used on the text stroke eg 'blue', '#FCFF00'.
@@ -237,6 +238,7 @@ Phaser.Text.prototype.setStyle = function (style) {
237238

238239
style = style || {};
239240
style.font = style.font || 'bold 20pt Arial';
241+
style.backgroundColor = style.backgroundColor || null;
240242
style.fill = style.fill || 'black';
241243
style.align = style.align || 'left';
242244
style.stroke = style.stroke || 'black'; //provide a default, see: https://github.com/GoodBoyDigital/pixi.js/issues/136
@@ -322,9 +324,21 @@ Phaser.Text.prototype.updateText = function () {
322324
this.canvas.width = width * this.resolution;
323325

324326
//calculate text height
325-
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this._lineSpacing + this.padding.y;
327+
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this.padding.y;
328+
var height = lineHeight * lines.length;
329+
var lineSpacing = this._lineSpacing;
326330

327-
var height = (lineHeight + this._lineSpacing) * lines.length;
331+
if (lineSpacing < 0 && Math.abs(lineSpacing) > lineHeight)
332+
{
333+
lineSpacing = -lineHeight;
334+
}
335+
336+
// Adjust for line spacing
337+
if (lineSpacing !== 0)
338+
{
339+
var diff = lineSpacing * (lines.length - 1);
340+
height += diff;
341+
}
328342

329343
this.canvas.height = height * this.resolution;
330344

@@ -334,6 +348,12 @@ Phaser.Text.prototype.updateText = function () {
334348
{
335349
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
336350
}
351+
352+
if (this.style.backgroundColor)
353+
{
354+
this.context.fillStyle = this.style.backgroundColor;
355+
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
356+
}
337357

338358
this.context.fillStyle = this.style.fill;
339359
this.context.font = this.style.font;
@@ -352,12 +372,17 @@ Phaser.Text.prototype.updateText = function () {
352372

353373
this._charCount = 0;
354374

355-
//draw lines line by line
375+
// Draw text line by line
356376
for (i = 0; i < lines.length; i++)
357377
{
358378
linePositionX = this.style.strokeThickness / 2;
359379
linePositionY = (this.style.strokeThickness / 2 + i * lineHeight) + fontProperties.ascent;
360380

381+
if (i > 0)
382+
{
383+
linePositionY += (lineSpacing * i);
384+
}
385+
361386
if (this.style.align === 'right')
362387
{
363388
linePositionX += maxLineWidth - lineWidths[i];
@@ -383,6 +408,7 @@ Phaser.Text.prototype.updateText = function () {
383408
this.context.fillText(lines[i], linePositionX, linePositionY);
384409
}
385410
}
411+
386412
}
387413

388414
this.updateTexture();

0 commit comments

Comments
 (0)