Skip to content

Commit 3012e49

Browse files
committed
Text.addColor would incorrectly color the text stroke if set (thanks @llevkin phaserjs#1893
Text.addStrokeColor works in the same way as `Text.addColor` but allows you to define a color stop for the stroke color instead of the fill color.
1 parent 86cac20 commit 3012e49

3 files changed

Lines changed: 52 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ Version 2.4 - "Katar" - in dev
327327
* BitmapData.move(x, y) allows you to shift the contents of the BitmapData horizontally and vertically by the given amounts. The image wraps-around the edges of the BitmapData.
328328
* BitmapData.moveH(distance) allows you to horizontally shift the BitmapData with wrap-around the edges.
329329
* BitmapData.moveV(distance) allows you to vertically shift the BitmapData with wrap-around the edges.
330+
* Text.addStrokeColor works in the same way as `Text.addColor` but allows you to define a color stop for the stroke color instead of the fill color.
330331

331332
### Updates
332333

@@ -420,6 +421,7 @@ Version 2.4 - "Katar" - in dev
420421
* P2.Body.createGroupCallback incorrectly referenced the `_groupCallbackContext` when deleting it (thanks @Langerz82 #1886)
421422
* 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)
422423
* Reading the dimensions of a Text object would reset its resolution property (thanks @joelika #1717)
424+
* Text.addColor would incorrectly color the text stroke if set (thanks @llevkin #1893)
423425

424426
### Deprecated
425427

src/gameobjects/Text.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ Phaser.Text = function (game, x, y, text, style) {
8585
*/
8686
this.colors = [];
8787

88+
/**
89+
* @property {array} strokeColors - An array of the stroke color values as specified by {@link Phaser.Text#addStrokeColor addStrokeColor}.
90+
*/
91+
this.strokeColors = [];
92+
8893
/**
8994
* Should the linePositionX and Y values be automatically rounded before rendering the Text?
9095
* You may wish to enable this if you want to remove the effect of sub-pixel aliasing from text.
@@ -467,7 +472,7 @@ Phaser.Text.prototype.updateText = function () {
467472
linePositionY = Math.round(linePositionY);
468473
}
469474

470-
if (this.colors.length > 0)
475+
if (this.colors.length > 0 || this.strokeColors.length > 0)
471476
{
472477
this.updateLine(lines[i], linePositionX, linePositionY);
473478
}
@@ -598,31 +603,35 @@ Phaser.Text.prototype.updateShadow = function (state) {
598603
};
599604

600605
/**
601-
* Updates a line of text.
606+
* Updates a line of text, applying fill and stroke per-character colors if applicable.
602607
*
603608
* @method Phaser.Text#updateLine
604609
* @private
605610
*/
606611
Phaser.Text.prototype.updateLine = function (line, x, y) {
607612

608-
for (var i = 0; i < line.length; i++)
613+
for (i = 0; i < line.length; i++)
609614
{
610615
var letter = line[i];
611616

612-
if (this.colors[this._charCount])
613-
{
614-
this.context.fillStyle = this.colors[this._charCount];
615-
this.context.strokeStyle = this.colors[this._charCount];
616-
}
617-
618617
if (this.style.stroke && this.style.strokeThickness)
619618
{
619+
if (this.strokeColors[this._charCount])
620+
{
621+
this.context.strokeStyle = this.strokeColors[this._charCount];
622+
}
623+
620624
this.updateShadow(this.style.shadowStroke);
621625
this.context.strokeText(letter, x, y);
622626
}
623627

624628
if (this.style.fill)
625629
{
630+
if (this.colors[this._charCount])
631+
{
632+
this.context.fillStyle = this.colors[this._charCount];
633+
}
634+
626635
this.updateShadow(this.style.shadowFill);
627636
this.context.fillText(letter, x, y);
628637
}
@@ -635,14 +644,15 @@ Phaser.Text.prototype.updateLine = function (line, x, y) {
635644
};
636645

637646
/**
638-
* Clears any previously set color stops.
647+
* Clears any text fill or stroke colors that were set by `addColor` or `addStrokeColor`.
639648
*
640649
* @method Phaser.Text#clearColors
641650
* @return {Phaser.Text} This Text instance.
642651
*/
643652
Phaser.Text.prototype.clearColors = function () {
644653

645654
this.colors = [];
655+
this.strokeColors = [];
646656
this.dirty = true;
647657

648658
return this;
@@ -657,6 +667,8 @@ Phaser.Text.prototype.clearColors = function () {
657667
* Once set the color remains in use until either another color or the end of the string is encountered.
658668
* For example if the Text was `Photon Storm` and you did `Text.addColor('#ffff00', 6)` it would color in the word `Storm` in yellow.
659669
*
670+
* If you wish to change the stroke color see addStrokeColor instead.
671+
*
660672
* @method Phaser.Text#addColor
661673
* @param {string} color - A canvas fillstyle that will be used on the text eg `red`, `#00FF00`, `rgba()`.
662674
* @param {number} position - The index of the character in the string to start applying this color value from.
@@ -671,6 +683,32 @@ Phaser.Text.prototype.addColor = function (color, position) {
671683

672684
};
673685

686+
/**
687+
* Set specific stroke colors for certain characters within the Text.
688+
*
689+
* It works by taking a color value, which is a typical HTML string such as `#ff0000` or `rgb(255,0,0)` and a position.
690+
* The position value is the index of the character in the Text string to start applying this color to.
691+
* Once set the color remains in use until either another color or the end of the string is encountered.
692+
* For example if the Text was `Photon Storm` and you did `Text.addColor('#ffff00', 6)` it would color in the word `Storm` in yellow.
693+
*
694+
* This has no effect if stroke is disabled or has a thickness of 0.
695+
*
696+
* If you wish to change the text fill color see addColor instead.
697+
*
698+
* @method Phaser.Text#addStrokeColor
699+
* @param {string} color - A canvas fillstyle that will be used on the text stroke eg `red`, `#00FF00`, `rgba()`.
700+
* @param {number} position - The index of the character in the string to start applying this color value from.
701+
* @return {Phaser.Text} This Text instance.
702+
*/
703+
Phaser.Text.prototype.addStrokeColor = function (color, position) {
704+
705+
this.strokeColors[position] = color;
706+
this.dirty = true;
707+
708+
return this;
709+
710+
};
711+
674712
/**
675713
* Greedy wrapping algorithm that will wrap words as the line grows longer than its horizontal bounds.
676714
*

typescript/phaser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,6 +4415,7 @@ declare module Phaser {
44154415
shadowOffsetY: number;
44164416
shadowStroke: boolean;
44174417
stroke: string;
4418+
strokeColors: string[];
44184419
strokeThickness: number;
44194420
scale: Phaser.Point;
44204421
tabs: number|number[];
@@ -4427,6 +4428,7 @@ declare module Phaser {
44274428
z: number;
44284429

44294430
addColor(color: string, position: number): Phaser.Text;
4431+
addStrokeColor(color: string, position: number): Phaser.Text;
44304432
clearColors(): Phaser.Text;
44314433
componentsToFont(components: any): string;
44324434
destroy(destroyChildren?: boolean): void;

0 commit comments

Comments
 (0)