Skip to content

Commit a1b502f

Browse files
committed
Stage.smoothed allows you to set if sprites will be smoothed when rendered. Set to false if you're using pixel art in your game. Default is true. Works in Canvas and WebGL. Setting the game anti-aliased parameter now works properly too.
Sprite.smoothed and Image.smoothed allows you to set per-Sprite smoothing, perfect if you just want to keep a few sprites smoothed (or not). Fixes phaserjs#381.
1 parent b255fea commit a1b502f

8 files changed

Lines changed: 128 additions & 11 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ New features:
112112
* TileSprites can now receive full Input events, dragging, etc and be positioned in-world and fixed to cameras.
113113
* The StateManager now looks for a function called 'resumed' which is called when a game un-pauses.
114114
* Key.onHold added. This event is dispatched every time the browser sends a keydown event and the key is already being held down.
115+
* Stage.smoothed allows you to set if sprites will be smoothed when rendered. Set to false if you're using pixel art in your game. Default is true. Works in Canvas and WebGL.
116+
* Sprite.smoothed and Image.smoothed allows you to set per-Sprite smoothing, perfect if you just want to keep a few sprites smoothed (or not)
115117

116118

117119
Updates:

examples/display/render crisp.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
33

44
function preload() {
55

66
game.load.image('boss', 'assets/misc/boss1.png');
7+
game.load.image('melon', 'assets/sprites/melon.png');
78
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
89

910
}
1011

1112
var boss;
13+
var melon;
1214
var button;
1315

1416
function create() {
1517

16-
// For browsers that support it, this keeps our pixel art looking crisp
17-
// This only works when you use Phaser.CANVAS as the renderer
18-
Phaser.Canvas.setSmoothingEnabled(game.context, false);
19-
2018
boss = game.add.sprite(game.world.centerX, game.world.centerY, 'boss');
2119
boss.anchor.setTo(0.5, 0.5);
2220

21+
melon = game.add.sprite(500, game.world.centerY, 'melon');
22+
melon.anchor.setTo(0.5, 0.5);
23+
24+
// For browsers that support it, this keeps our pixel art looking crisp (works across Canvas and WebGL)
25+
26+
// You can either set smoothing on a specific sprite, like this:
27+
// boss.smoothed = false;
28+
29+
// Or across the whole stage, like this:
30+
game.stage.smoothed = false;
31+
2332
// Zoom in each time we press the button
2433
button = game.add.button(32, 32, 'button', clickedIt, this, 2, 1, 0);
2534
}
@@ -28,5 +37,8 @@ function clickedIt() {
2837

2938
boss.scale.x += 0.5;
3039
boss.scale.y += 0.5;
40+
41+
melon.scale.x += 0.5;
42+
melon.scale.y += 0.5;
3143

3244
}

src/Phaser.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ var Phaser = Phaser || {
4848
STATIC: 2,
4949
KINEMATIC: 4,
5050

51-
CANVAS_PX_ROUND: false,
52-
CANVAS_CLEAR_RECT: true,
53-
5451
// the various blend modes supported by pixi / phaser
5552
blendModes: {
5653
NORMAL:0,
@@ -70,6 +67,13 @@ var Phaser = Phaser || {
7067
SATURATION:14,
7168
COLOR:15,
7269
LUMINOSITY:16
70+
},
71+
72+
// the scale modes
73+
scaleModes: {
74+
DEFAULT:0,
75+
LINEAR:0,
76+
NEAREST:1
7377
}
7478

7579
};

src/core/Game.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @param {string|HTMLElement} [parent=''] - The DOM element into which this games canvas will be injected. Either a DOM ID (string) or the element itself.
2121
* @param {object} [state=null] - The default state object. A object consisting of Phaser.State functions (preload, create, update, render) or null.
2222
* @param {boolean} [transparent=false] - Use a transparent canvas background or not.
23-
* @param {boolean} [antialias=true] - Anti-alias graphics.
23+
* @param {boolean} [antialias=true] - Draw all image textures anti-aliased or not. The default is for smooth textures, but disable if your game features pixel art.
2424
* @param {object} [physicsConfig=null] - A physics configuration object to pass to the Physics world on creation.
2525
*/
2626
Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias, physicsConfig) {
@@ -564,7 +564,6 @@ Phaser.Game.prototype = {
564564
}
565565

566566
this.renderer = new PIXI.CanvasRenderer(this.width, this.height, this.canvas, this.transparent);
567-
Phaser.Canvas.setSmoothingEnabled(this.renderer.context, this.antialias);
568567
this.context = this.renderer.context;
569568
}
570569
else
@@ -580,6 +579,11 @@ Phaser.Game.prototype = {
580579
this.context = null;
581580
}
582581

582+
if (!this.antialias)
583+
{
584+
this.stage.smoothed = false;
585+
}
586+
583587
Phaser.Canvas.addToDOM(this.canvas, this.parent, true);
584588
Phaser.Canvas.setTouchAction(this.canvas);
585589

src/core/Stage.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,31 @@ Object.defineProperty(Phaser.Stage.prototype, "backgroundColor", {
381381
}
382382

383383
});
384+
385+
/**
386+
* Enable or disable texture smoothing for all objects on this Stage. Only works for bitmap/image textures. Smoothing is enabled by default.
387+
*
388+
* @name Phaser.Stage#smoothed
389+
* @property {boolean} smoothed - Set to true to smooth all sprites rendered on this Stage, or false to disable smoothing (great for pixel art)
390+
*/
391+
Object.defineProperty(Phaser.Stage.prototype, "smoothed", {
392+
393+
get: function () {
394+
395+
return !!PIXI.scaleModes.LINEAR;
396+
397+
},
398+
399+
set: function (value) {
400+
401+
if (value)
402+
{
403+
PIXI.scaleModes.LINEAR = 0;
404+
}
405+
else
406+
{
407+
PIXI.scaleModes.LINEAR = 1;
408+
}
409+
}
410+
411+
});

src/gameobjects/Image.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,37 @@ Object.defineProperty(Phaser.Image.prototype, "fixedToCamera", {
698698
}
699699

700700
});
701+
702+
/**
703+
* Enable or disable texture smoothing for this Image. Only works for bitmap/image textures. Smoothing is enabled by default.
704+
*
705+
* @name Phaser.Image#smoothed
706+
* @property {boolean} smoothed - Set to true to smooth the texture of this Image, or false to disable smoothing (great for pixel art)
707+
*/
708+
Object.defineProperty(Phaser.Image.prototype, "smoothed", {
709+
710+
get: function () {
711+
712+
return !!this.texture.baseTexture.scaleMode;
713+
714+
},
715+
716+
set: function (value) {
717+
718+
if (value)
719+
{
720+
if (this.texture)
721+
{
722+
this.texture.baseTexture.scaleMode = 0;
723+
}
724+
}
725+
else
726+
{
727+
if (this.texture)
728+
{
729+
this.texture.baseTexture.scaleMode = 1;
730+
}
731+
}
732+
}
733+
734+
});

src/gameobjects/Sprite.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,37 @@ Object.defineProperty(Phaser.Sprite.prototype, "fixedToCamera", {
951951
}
952952

953953
});
954+
955+
/**
956+
* Enable or disable texture smoothing for this Sprite. Only works for bitmap/image textures. Smoothing is enabled by default.
957+
*
958+
* @name Phaser.Sprite#smoothed
959+
* @property {boolean} smoothed - Set to true to smooth the texture of this Sprite, or false to disable smoothing (great for pixel art)
960+
*/
961+
Object.defineProperty(Phaser.Sprite.prototype, "smoothed", {
962+
963+
get: function () {
964+
965+
return !!this.texture.baseTexture.scaleMode;
966+
967+
},
968+
969+
set: function (value) {
970+
971+
if (value)
972+
{
973+
if (this.texture)
974+
{
975+
this.texture.baseTexture.scaleMode = 0;
976+
}
977+
}
978+
else
979+
{
980+
if (this.texture)
981+
{
982+
this.texture.baseTexture.scaleMode = 1;
983+
}
984+
}
985+
}
986+
987+
});

src/system/Canvas.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Phaser.Canvas = {
2626
width = width || 256;
2727
height = height || 256;
2828

29-
// var canvas = document.createElement('canvas');
3029
var canvas = document.createElement(navigator.isCocoonJS ? 'screencanvas' : 'canvas');
3130

3231
if (typeof id === 'string')

0 commit comments

Comments
 (0)