Skip to content

Commit 2c10ac3

Browse files
committed
Added roundPixels support for the Canvas renderer
1 parent 252a76f commit 2c10ac3

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

src/renderer/canvas/CanvasRenderer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ var CanvasRenderer = new Class({
8888
pixelArt: game.config.pixelArt,
8989
backgroundColor: game.config.backgroundColor,
9090
resolution: game.config.resolution,
91-
autoResize: game.config.autoResize
91+
autoResize: game.config.autoResize,
92+
roundPixels: game.config.roundPixels
9293
};
9394

9495
/**
@@ -134,7 +135,7 @@ var CanvasRenderer = new Class({
134135
* @type {function}
135136
* @since 3.0.0
136137
*/
137-
this.drawImage = DrawImage;
138+
this.drawImage = DrawImage(this.config.roundPixels);
138139

139140
/**
140141
* [description]
@@ -143,7 +144,7 @@ var CanvasRenderer = new Class({
143144
* @type {function}
144145
* @since 3.0.0
145146
*/
146-
this.blitImage = BlitImage;
147+
this.blitImage = BlitImage(this.config.roundPixels);
147148

148149
/**
149150
* [description]

src/renderer/canvas/utils/BlitImage.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var roundPixels = false;
8+
79
/**
810
* No scaling, anchor, rotation or effects, literally draws the frame directly to the canvas.
911
*
@@ -19,6 +21,12 @@ var BlitImage = function (dx, dy, frame)
1921
var ctx = this.currentContext;
2022
var cd = frame.canvasData;
2123

24+
if (roundPixels)
25+
{
26+
dx |= 0;
27+
dy |= 0;
28+
}
29+
2230
ctx.drawImage(
2331
frame.source.image,
2432
cd.sx,
@@ -32,4 +40,11 @@ var BlitImage = function (dx, dy, frame)
3240
);
3341
};
3442

35-
module.exports = BlitImage;
43+
// Special return so we can store the config value locally
44+
45+
module.exports = function (configRoundPixels)
46+
{
47+
roundPixels = configRoundPixels;
48+
49+
return BlitImage;
50+
};

src/renderer/canvas/utils/DrawImage.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var roundPixels = false;
8+
79
/**
810
* [description]
911
*
@@ -70,11 +72,22 @@ var DrawImage = function (src, camera)
7072
dy -= src.displayOriginY;
7173
}
7274

75+
var tx = src.x - camera.scrollX * src.scrollFactorX;
76+
var ty = src.y - camera.scrollY * src.scrollFactorY;
77+
78+
if (roundPixels)
79+
{
80+
tx |= 0;
81+
ty |= 0;
82+
dx |= 0;
83+
dy |= 0;
84+
}
85+
7386
// Perform Matrix ITRS
7487

7588
ctx.save();
7689

77-
ctx.translate(src.x - camera.scrollX * src.scrollFactorX, src.y - camera.scrollY * src.scrollFactorY);
90+
ctx.translate(tx, ty);
7891

7992
ctx.rotate(src.rotation);
8093

@@ -86,4 +99,11 @@ var DrawImage = function (src, camera)
8699
ctx.restore();
87100
};
88101

89-
module.exports = DrawImage;
102+
// Special return so we can store the config value locally
103+
104+
module.exports = function (configRoundPixels)
105+
{
106+
roundPixels = configRoundPixels;
107+
108+
return DrawImage;
109+
};

0 commit comments

Comments
 (0)