Skip to content

Commit 8290e8c

Browse files
committed
Text.setShadow has two new optional parameters: shadowStroke and shadowFill. These allow you to set if the drop shadow is applied to the Text stroke, the Text fill or both of them (thanks @qdrj phaserjs#1766)
Text.shadowStroke and Text.shadowFill allow you to toggle if the drop shadow is applied to the Text stroke or fill independently.
1 parent d943424 commit 8290e8c

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ Version 2.4 - "Katar" - in dev
268268
* Phaser.Matrix.copyFrom and copyTo allow you to copy Matrix values from and to other Matrix objects.
269269
* Phaser.Matrix.setTo allows you to set all properties of a Matrix in a single call.
270270
* The Phaser.Matrix constructor now allows you to optionally set all Matrix properties on instantiation.
271+
* Text.setShadow has two new optional parameters: `shadowStroke` and `shadowFill`. These allow you to set if the drop shadow is applied to the Text stroke, the Text fill or both of them (thanks @qdrj #1766)
272+
* Text.shadowStroke and Text.shadowFill allow you to toggle if the drop shadow is applied to the Text stroke or fill independently.
271273

272274
### Updates
273275

src/gameobjects/Text.js

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,24 @@ Phaser.Text.prototype.destroy = function (destroyChildren) {
198198
* @param {number} [y=0] - The shadowOffsetY value in pixels. This is how far offset vertically the shadow effect will be.
199199
* @param {string} [color='rgba(0,0,0,1)'] - The color of the shadow, as given in CSS rgba or hex format. Set the alpha component to 0 to disable the shadow.
200200
* @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).
201+
* @param {boolean} [shadowStroke=true] - Apply the drop shadow to the Text stroke (if set).
202+
* @param {boolean} [shadowFill=true] - Apply the drop shadow to the Text fill (if set).
201203
*/
202-
Phaser.Text.prototype.setShadow = function (x, y, color, blur) {
204+
Phaser.Text.prototype.setShadow = function (x, y, color, blur, shadowStroke, shadowFill) {
203205

204206
if (typeof x === 'undefined') { x = 0; }
205207
if (typeof y === 'undefined') { y = 0; }
206208
if (typeof color === 'undefined') { color = 'rgba(0, 0, 0, 1)'; }
207209
if (typeof blur === 'undefined') { blur = 0; }
210+
if (typeof shadowStroke === 'undefined') { shadowStroke = true; }
211+
if (typeof shadowFill === 'undefined') { shadowFill = true; }
208212

209213
this.style.shadowOffsetX = x;
210214
this.style.shadowOffsetY = y;
211215
this.style.shadowColor = color;
212216
this.style.shadowBlur = blur;
217+
this.style.shadowStroke = shadowStroke;
218+
this.style.shadowFill = shadowFill;
213219
this.dirty = true;
214220

215221
};
@@ -302,10 +308,10 @@ Phaser.Text.prototype.updateText = function () {
302308
outputText = this.runWordWrap(this.text);
303309
}
304310

305-
//split text into lines
311+
// Split text into lines
306312
var lines = outputText.split(/(?:\r\n|\r|\n)/);
307313

308-
//calculate text width
314+
// Calculate text width
309315
var lineWidths = [];
310316
var maxLineWidth = 0;
311317
var fontProperties = this.determineFontProperties(this.style.font);
@@ -321,7 +327,7 @@ Phaser.Text.prototype.updateText = function () {
321327

322328
this.canvas.width = width * this.resolution;
323329

324-
//calculate text height
330+
// Calculate text height
325331
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this.padding.y;
326332
var height = lineHeight * lines.length;
327333
var lineSpacing = this._lineSpacing;
@@ -357,10 +363,7 @@ Phaser.Text.prototype.updateText = function () {
357363
this.context.font = this.style.font;
358364
this.context.strokeStyle = this.style.stroke;
359365
this.context.textBaseline = 'alphabetic';
360-
this.context.shadowOffsetX = this.style.shadowOffsetX;
361-
this.context.shadowOffsetY = this.style.shadowOffsetY;
362-
this.context.shadowColor = this.style.shadowColor;
363-
this.context.shadowBlur = this.style.shadowBlur;
366+
364367
this.context.lineWidth = this.style.strokeThickness;
365368
this.context.lineCap = 'round';
366369
this.context.lineJoin = 'round';
@@ -398,21 +401,48 @@ Phaser.Text.prototype.updateText = function () {
398401
{
399402
if (this.style.stroke && this.style.strokeThickness)
400403
{
404+
this.updateShadow(this.style.shadowStroke);
401405
this.context.strokeText(lines[i], linePositionX, linePositionY);
402406
}
403407

404408
if (this.style.fill)
405409
{
410+
this.updateShadow(this.style.shadowFill);
406411
this.context.fillText(lines[i], linePositionX, linePositionY);
407412
}
408413
}
409-
410414
}
411415

412416
this.updateTexture();
413417

414418
};
415419

420+
/**
421+
* Sets the Shadow on the Text.context based on the Style settings, or disables it if not enabled.
422+
* This is called automatically by Text.updateText.
423+
*
424+
* @method Phaser.Text#updateShadow
425+
* @param {boolean} state - If true the shadow will be set to the Style values, otherwise it will be set to zero.
426+
*/
427+
Phaser.Text.prototype.updateShadow = function (state) {
428+
429+
if (state)
430+
{
431+
this.context.shadowOffsetX = this.style.shadowOffsetX;
432+
this.context.shadowOffsetY = this.style.shadowOffsetY;
433+
this.context.shadowColor = this.style.shadowColor;
434+
this.context.shadowBlur = this.style.shadowBlur;
435+
}
436+
else
437+
{
438+
this.context.shadowOffsetX = 0;
439+
this.context.shadowOffsetY = 0;
440+
this.context.shadowColor = 0;
441+
this.context.shadowBlur = 0;
442+
}
443+
444+
};
445+
416446
/**
417447
* Updates a line of text.
418448
*
@@ -433,11 +463,13 @@ Phaser.Text.prototype.updateLine = function (line, x, y) {
433463

434464
if (this.style.stroke && this.style.strokeThickness)
435465
{
466+
this.updateShadow(this.style.shadowStroke);
436467
this.context.strokeText(letter, x, y);
437468
}
438469

439470
if (this.style.fill)
440471
{
472+
this.updateShadow(this.style.shadowFill);
441473
this.context.fillText(letter, x, y);
442474
}
443475

@@ -1070,3 +1102,47 @@ Object.defineProperty(Phaser.Text.prototype, 'shadowBlur', {
10701102
}
10711103

10721104
});
1105+
1106+
/**
1107+
* @name Phaser.Text#shadowStroke
1108+
* @property {boolean} shadowStroke - Sets if the drop shadow is applied to the Text stroke.
1109+
*/
1110+
Object.defineProperty(Phaser.Text.prototype, 'shadowStroke', {
1111+
1112+
get: function() {
1113+
return this.style.shadowStroke;
1114+
},
1115+
1116+
set: function(value) {
1117+
1118+
if (value !== this.style.shadowStroke)
1119+
{
1120+
this.style.shadowStroke = value;
1121+
this.dirty = true;
1122+
}
1123+
1124+
}
1125+
1126+
});
1127+
1128+
/**
1129+
* @name Phaser.Text#shadowFill
1130+
* @property {boolean} shadowFill - Sets if the drop shadow is applied to the Text fill.
1131+
*/
1132+
Object.defineProperty(Phaser.Text.prototype, 'shadowFill', {
1133+
1134+
get: function() {
1135+
return this.style.shadowFill;
1136+
},
1137+
1138+
set: function(value) {
1139+
1140+
if (value !== this.style.shadowFill)
1141+
{
1142+
this.style.shadowFill = value;
1143+
this.dirty = true;
1144+
}
1145+
1146+
}
1147+
1148+
});

0 commit comments

Comments
 (0)