Skip to content

Commit a89d48b

Browse files
committed
Text.autoRound allows you to control if the text is allowed to render at sub-pixel coordinates or not. Set to true to round the coordinates, often eliminating anti-aliasing from certain font types (phaserjs#1867)
1 parent ce405ac commit a89d48b

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ Version 2.4 - "Katar" - in dev
318318
* 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)
319319
* 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.
320320
* 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.
321+
* Text.autoRound allows you to control if the text is allowed to render at sub-pixel coordinates or not. Set to `true` to round the coordinates, often eliminating anti-aliasing from certain font types (#1867)
321322

322323
### Updates
323324

src/gameobjects/Text.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ Phaser.Text = function (game, x, y, text, style) {
9191
*/
9292
this.colors = [];
9393

94+
/**
95+
* Should the linePositionX and Y values be automatically rounded before rendering the Text?
96+
* You may wish to enable this if you want to remove the effect of sub-pixel aliasing from text.
97+
* @property {boolean} autoRound
98+
* @default
99+
*/
100+
this.autoRound = false;
101+
94102
/**
95103
* @property {string} _text - Internal cache var.
96104
* @private
@@ -410,6 +418,12 @@ Phaser.Text.prototype.updateText = function () {
410418
linePositionX += (maxLineWidth - lineWidths[i]) / 2;
411419
}
412420

421+
if (this.autoRound)
422+
{
423+
linePositionX = Math.round(linePositionX);
424+
linePositionY = Math.round(linePositionY);
425+
}
426+
413427
if (this.colors.length > 0)
414428
{
415429
this.updateLine(lines[i], linePositionX, linePositionY);

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4339,6 +4339,7 @@ declare module Phaser {
43394339

43404340
align: string;
43414341
angle: number;
4342+
autoRound: boolean;
43424343
boundsAlignH: string;
43434344
boundsAlignV: string;
43444345
cameraOffset: Phaser.Point;

0 commit comments

Comments
 (0)