-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Expand file tree
/
Copy pathRectangleCanvasRenderer.js
More file actions
185 lines (163 loc) · 4.81 KB
/
RectangleCanvasRenderer.js
File metadata and controls
185 lines (163 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2025 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var FillStyleCanvas = require('../FillStyleCanvas');
var LineStyleCanvas = require('../LineStyleCanvas');
var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
var DrawRoundedRect = function (ctx, x, y, width, height, tlRadius, trRadius, blRadius, brRadius)
{
// Limit radius to half of the smaller dimension
var maxRadius = Math.min(width / 2, height / 2);
var tl = Math.min(tlRadius, maxRadius);
var tr = Math.min(trRadius, maxRadius);
var bl = Math.min(blRadius, maxRadius);
var br = Math.min(brRadius, maxRadius);
if (tl === 0 && tr === 0 && bl === 0 && br === 0)
{
// Fall back to normal rectangle if radius is 0
ctx.rect(x, y, width, height);
return;
}
if (tl === 0)
{
// Start at top-left
ctx.moveTo(x, y);
}
else
{
// Start at top-left, after the corner
ctx.moveTo(x + tl, y);
}
if (tr === 0)
{
// Top edge
ctx.lineTo(x + width, y);
}
else
{
// Top edge and top-right corner
ctx.lineTo(x + width - tr, y);
ctx.arcTo(x + width, y, x + width, y + tr, tr);
}
if (br === 0)
{
// Right edge
ctx.lineTo(x + width, y + height);
}
else
{
// Right edge and bottom-right corner
ctx.lineTo(x + width, y + height - br);
ctx.arcTo(x + width, y + height, x + width - br, y + height, br);
}
if (bl === 0)
{
// Bottom edge
ctx.lineTo(x, y + height);
}
else
{
// Bottom edge and bottom-left corner
ctx.lineTo(x + bl, y + height);
ctx.arcTo(x, y + height, x, y + height - bl, bl);
}
if (tl === 0)
{
// Left edge
ctx.lineTo(x, y);
}
else
{
// Left edge and top-left corner
ctx.lineTo(x, y + tl);
ctx.arcTo(x, y, x + tl, y, tl);
}
ctx.closePath();
};
/**
* Renders this Game Object with the Canvas Renderer to the given Camera.
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
* This method should not be called directly. It is a utility function of the Render module.
*
* @method Phaser.GameObjects.Rectangle#renderCanvas
* @since 3.13.0
* @private
*
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
* @param {Phaser.GameObjects.Rectangle} src - The Game Object being rendered in this call.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
*/
var RectangleCanvasRenderer = function (renderer, src, camera, parentMatrix)
{
camera.addToRenderList(src);
var ctx = renderer.currentContext;
if (SetTransform(renderer, ctx, src, camera, parentMatrix))
{
var dx = src._displayOriginX;
var dy = src._displayOriginY;
if (src.isFilled)
{
FillStyleCanvas(ctx, src);
if (src.isRounded)
{
ctx.beginPath();
DrawRoundedRect(
ctx,
-dx,
-dy,
src.width,
src.height,
src.radiusTopLeft,
src.radiusTopRight,
src.radiusBottomLeft,
src.radiusBottomRight
);
ctx.fill();
}
else
{
ctx.fillRect(
-dx,
-dy,
src.width,
src.height
);
}
}
if (src.isStroked)
{
LineStyleCanvas(ctx, src);
ctx.beginPath();
if (src.isRounded)
{
DrawRoundedRect(
ctx,
-dx,
-dy,
src.width,
src.height,
src.radiusTopLeft,
src.radiusTopRight,
src.radiusBottomLeft,
src.radiusBottomRight
);
}
else
{
ctx.rect(
-dx,
-dy,
src.width,
src.height
);
}
ctx.stroke();
}
// Restore the context saved in SetTransform
ctx.restore();
}
};
module.exports = RectangleCanvasRenderer;