Skip to content

Commit 0ba77e5

Browse files
committed
Swapped to using the math const and tidied up eslint errors.
1 parent ed88c8e commit 0ba77e5

3 files changed

Lines changed: 113 additions & 58 deletions

File tree

v3/src/gameobjects/graphics/Graphics.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var GameObject = require('../GameObject');
33
var Components = require('../../components');
44
var Render = require('./GraphicsRender');
55
var Commands = require('./Commands');
6-
var PI2 = 2 * Math.PI;
6+
var MATH_CONST = require('../../math/const');
77

88
var Graphics = new Class({
99

@@ -20,11 +20,13 @@ var Graphics = new Class({
2020
function Graphics (state, x, y)
2121
{
2222
GameObject.call(this, state);
23-
this.commandBuffer = [];
23+
2424
this.setPosition(x, y);
25+
26+
this.commandBuffer = [];
2527
},
2628

27-
arc: function (x, y, radius, startAngle, endAngle, anticlockwise)
29+
arc: function (x, y, radius, startAngle, endAngle, anticlockwise)
2830
{
2931
this.commandBuffer.push(
3032
Commands.ARC,
@@ -42,21 +44,22 @@ var Graphics = new Class({
4244

4345
fillStyle: function (color, alpha)
4446
{
45-
alpha = (alpha !== undefined ? alpha : 1);
47+
if (alpha === undefined) { alpha = 1; }
48+
4649
this.commandBuffer.push(
4750
Commands.FILL_STYLE,
4851
color, alpha
4952
);
5053
},
5154

52-
beginPath: function ()
55+
beginPath: function ()
5356
{
5457
this.commandBuffer.push(
5558
Commands.BEGIN_PATH
5659
);
5760
},
5861

59-
closePath: function ()
62+
closePath: function ()
6063
{
6164
this.commandBuffer.push(
6265
Commands.CLOSE_PATH
@@ -77,31 +80,31 @@ var Graphics = new Class({
7780
);
7881
},
7982

80-
fillCircle: function (x, y, radius)
83+
fillCircle: function (x, y, radius)
8184
{
8285
this.beginPath();
83-
this.arc(x, y, radius, 0, PI2);
86+
this.arc(x, y, radius, 0, MATH_CONST.PI2);
8487
this.fillPath();
8588
this.closePath();
8689
},
8790

88-
fillRect: function (x, y, width, height)
91+
fillRect: function (x, y, width, height)
8992
{
9093
this.commandBuffer.push(
9194
Commands.FILL_RECT,
9295
x, y, width, height
9396
);
9497
},
9598

96-
strokeCircle: function (x, y, radius)
99+
strokeCircle: function (x, y, radius)
97100
{
98101
this.beginPath();
99-
this.arc(x, y, radius, 0, PI2);
102+
this.arc(x, y, radius, 0, MATH_CONST.PI2);
100103
this.strokePath();
101104
this.closePath();
102105
},
103106

104-
strokeRect: function (x, y, width, height)
107+
strokeRect: function (x, y, width, height)
105108
{
106109
this.beginPath();
107110
this.moveTo(x, y);
@@ -113,26 +116,27 @@ var Graphics = new Class({
113116
this.closePath();
114117
},
115118

116-
lineTo: function (x, y)
119+
lineTo: function (x, y)
117120
{
118121
this.commandBuffer.push(
119122
Commands.LINE_TO,
120123
x, y
121124
);
122125
},
123126

124-
moveTo: function (x, y)
127+
moveTo: function (x, y)
125128
{
126129
this.commandBuffer.push(
127130
Commands.MOVE_TO,
128131
x, y
129132
);
130133
},
131134

132-
clear: function ()
135+
clear: function ()
133136
{
134-
commandBuffer.length = 0;
137+
this.commandBuffer.length = 0;
135138
}
139+
136140
});
137141

138142
module.exports = Graphics;

v3/src/gameobjects/graphics/GraphicsCanvasRenderer.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
var Commands = require('./Commands');
2-
var PI2 = 2 * Math.PI;
2+
var MATH_CONST = require('../../math/const');
3+
34
var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
45
{
56
if (this.renderMask !== this.renderFlags)
67
{
78
return;
89
}
10+
911
var cameraScrollX = camera.scrollX;
1012
var cameraScrollY = camera.scrollY;
11-
var srcX = src.x;
13+
var srcX = src.x;
1214
var srcY = src.y;
1315
var srcScaleX = src.scaleX;
1416
var srcScaleY = src.scaleY;
@@ -56,36 +58,41 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
5658
{
5759
var commandID = commandBuffer[index];
5860

59-
switch(commandID)
61+
switch (commandID)
6062
{
6163
case Commands.ARC:
6264
ctx.arc(
63-
commandBuffer[index + 1],
64-
commandBuffer[index + 2],
65-
commandBuffer[index + 3],
66-
commandBuffer[index + 4],
67-
commandBuffer[index + 5],
65+
commandBuffer[index + 1],
66+
commandBuffer[index + 2],
67+
commandBuffer[index + 3],
68+
commandBuffer[index + 4],
69+
commandBuffer[index + 5],
6870
commandBuffer[index + 6]
6971
);
7072
index += 6;
7173
break;
74+
7275
case Commands.LINE_STYLE:
7376
lineWidth = commandBuffer[index + 1];
74-
lineColor = commandBuffer[index + 2]
77+
lineColor = commandBuffer[index + 2];
7578
lineAlpha = commandBuffer[index + 3];
7679
index += 3;
7780
break;
81+
7882
case Commands.FILL_STYLE:
7983
fillColor = commandBuffer[index + 1];
8084
fillAlpha = commandBuffer[index + 2];
8185
index += 2;
8286
break;
87+
8388
case Commands.BEGIN_PATH:
8489
ctx.beginPath();
8590
break;
91+
8692
case Commands.CLOSE_PATH:
8793
ctx.closePath();
8894
break;
95+
8996
case Commands.FILL_PATH:
9097
red = ((fillColor & 0xFF0000) >>> 16);
9198
green = ((fillColor & 0xFF00) >>> 8);
@@ -94,6 +101,7 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
94101
ctx.globalAlpha = fillAlpha;
95102
ctx.fill();
96103
break;
104+
97105
case Commands.STROKE_PATH:
98106
red = ((lineColor & 0xFF0000) >>> 16);
99107
green = ((lineColor & 0xFF00) >>> 8);
@@ -103,6 +111,7 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
103111
ctx.lineWidth = lineWidth;
104112
ctx.stroke();
105113
break;
114+
106115
case Commands.FILL_RECT:
107116
ctx.fillRect(
108117
commandBuffer[index + 1],
@@ -119,6 +128,7 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
119128
);
120129
index += 2;
121130
break;
131+
122132
case Commands.MOVE_TO:
123133
ctx.moveTo(
124134
commandBuffer[index + 1],
@@ -134,7 +144,6 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
134144
}
135145

136146
ctx.restore();
137-
ctx.globalAlpha = 1.0;
138-
}
147+
};
139148

140149
module.exports = GraphicsCanvasRenderer;

0 commit comments

Comments
 (0)