Skip to content

Commit 4b1c0eb

Browse files
committed
Added Canvas rendering functions
1 parent dfa40b8 commit 4b1c0eb

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Sets the fillStyle on the target context based on the given Shape.
9+
*
10+
* @method Phaser.GameObjects.Shape#FillStyleCanvas
11+
* @since 3.13.0
12+
* @private
13+
*
14+
* @param {CanvasRenderingContext2D} ctx - The context to set the fill style on.
15+
* @param {Phaser.GameObjects.Shape} src - The Game Object to set the fill style from.
16+
*/
17+
var FillStyleCanvas = function (ctx, src)
18+
{
19+
var fillColor = src.fillColor;
20+
var fillAlpha = src.fillAlpha;
21+
22+
var red = ((fillColor & 0xFF0000) >>> 16);
23+
var green = ((fillColor & 0xFF00) >>> 8);
24+
var blue = (fillColor & 0xFF);
25+
26+
ctx.fillStyle = 'rgba(' + red + ',' + green + ',' + blue + ',' + fillAlpha + ')';
27+
};
28+
29+
module.exports = FillStyleCanvas;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Sets the strokeStyle and lineWidth on the target context based on the given Shape.
9+
*
10+
* @method Phaser.GameObjects.Shape#LineStyleCanvas
11+
* @since 3.13.0
12+
* @private
13+
*
14+
* @param {CanvasRenderingContext2D} ctx - The context to set the stroke style on.
15+
* @param {Phaser.GameObjects.Shape} src - The Game Object to set the stroke style from.
16+
*/
17+
var LineStyleCanvas = function (ctx, src)
18+
{
19+
var strokeColor = src.strokeColor;
20+
var strokeAlpha = src.strokeAlpha;
21+
22+
var red = ((strokeColor & 0xFF0000) >>> 16);
23+
var green = ((strokeColor & 0xFF00) >>> 8);
24+
var blue = (strokeColor & 0xFF);
25+
26+
ctx.strokeStyle = 'rgba(' + red + ',' + green + ',' + blue + ',' + strokeAlpha + ')';
27+
ctx.lineWidth = src.lineWidth;
28+
};
29+
30+
module.exports = LineStyleCanvas;

src/gameobjects/shape/rectangle/RectangleCanvasRenderer.js

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

7+
var FillStyleCanvas = require('../FillStyleCanvas');
8+
var LineStyleCanvas = require('../LineStyleCanvas');
9+
var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
10+
711
/**
812
* Renders this Game Object with the Canvas Renderer to the given Camera.
913
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@@ -21,6 +25,41 @@
2125
*/
2226
var RectangleCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2327
{
28+
var ctx = renderer.currentContext;
29+
30+
if (SetTransform(renderer, ctx, src, camera, parentMatrix))
31+
{
32+
var dx = src._displayOriginX;
33+
var dy = src._displayOriginY;
34+
35+
if (src.isFilled)
36+
{
37+
FillStyleCanvas(ctx, src);
38+
39+
ctx.fillRect(
40+
-dx,
41+
-dy,
42+
src.width,
43+
src.height
44+
);
45+
}
46+
47+
if (src.isStroked)
48+
{
49+
LineStyleCanvas(ctx, src);
50+
51+
ctx.beginPath();
52+
53+
ctx.rect(
54+
-dx,
55+
-dy,
56+
src.width,
57+
src.height
58+
);
59+
60+
ctx.stroke();
61+
}
62+
}
2463
};
2564

2665
module.exports = RectangleCanvasRenderer;

src/gameobjects/shape/triangle/TriangleCanvasRenderer.js

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

7+
var FillStyleCanvas = require('../FillStyleCanvas');
8+
var LineStyleCanvas = require('../LineStyleCanvas');
9+
var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
10+
711
/**
812
* Renders this Game Object with the Canvas Renderer to the given Camera.
913
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@@ -21,6 +25,42 @@
2125
*/
2226
var TriangleCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2327
{
28+
var ctx = renderer.currentContext;
29+
30+
if (SetTransform(renderer, ctx, src, camera, parentMatrix))
31+
{
32+
var dx = src._displayOriginX;
33+
var dy = src._displayOriginY;
34+
35+
var x1 = src.data.x1 - dx;
36+
var y1 = src.data.y1 - dy;
37+
var x2 = src.data.x2 - dx;
38+
var y2 = src.data.y2 - dy;
39+
var x3 = src.data.x3 - dx;
40+
var y3 = src.data.y3 - dy;
41+
42+
ctx.beginPath();
43+
44+
ctx.moveTo(x1, y1);
45+
ctx.lineTo(x2, y2);
46+
ctx.lineTo(x3, y3);
47+
48+
ctx.closePath();
49+
50+
if (src.isFilled)
51+
{
52+
FillStyleCanvas(ctx, src);
53+
54+
ctx.fill();
55+
}
56+
57+
if (src.isStroked)
58+
{
59+
LineStyleCanvas(ctx, src);
60+
61+
ctx.stroke();
62+
}
63+
}
2464
};
2565

2666
module.exports = TriangleCanvasRenderer;

0 commit comments

Comments
 (0)