@flare-engine/shapes 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +29 -16
- package/dist/index.d.mts +92 -99
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +92 -99
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +363 -382
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +355 -348
- package/dist/index.mjs.map +1 -1
- package/package.json +21 -8
package/dist/index.mjs
CHANGED
|
@@ -1,397 +1,404 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/bounds.ts
|
|
2
|
+
/**
|
|
3
|
+
* Approximate bounding radius for a single primitive. Used to size glow
|
|
4
|
+
* rings and atlas tiles. Returned values are deliberately a touch generous
|
|
5
|
+
* to avoid clipping.
|
|
6
|
+
*/
|
|
2
7
|
function singleShapeRadius(spec) {
|
|
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
|
-
|
|
8
|
+
switch (spec.kind) {
|
|
9
|
+
case "circle":
|
|
10
|
+
case "ring":
|
|
11
|
+
case "ngon": return spec.r;
|
|
12
|
+
case "triangle": return spec.size;
|
|
13
|
+
case "star": return spec.rOuter;
|
|
14
|
+
case "rect":
|
|
15
|
+
case "gradient-rect": return Math.max(spec.w, spec.h) * .6;
|
|
16
|
+
case "polyline": {
|
|
17
|
+
let max = 0;
|
|
18
|
+
for (let i = 0; i < spec.points.length; i += 2) {
|
|
19
|
+
const px = spec.points[i] ?? 0;
|
|
20
|
+
const py = spec.points[i + 1] ?? 0;
|
|
21
|
+
const m = Math.hypot(px, py);
|
|
22
|
+
if (m > max) max = m;
|
|
23
|
+
}
|
|
24
|
+
return max + (spec.strokeWidth ?? 1);
|
|
25
|
+
}
|
|
26
|
+
case "arc": return spec.r;
|
|
27
|
+
case "annulus": return spec.rOuter;
|
|
28
|
+
case "trapezoid": return Math.max(spec.topW, spec.botW, spec.h) * .6;
|
|
29
|
+
case "path": return spec.boundsR;
|
|
30
|
+
case "ship": {
|
|
31
|
+
const { hull, weapons, engine } = spec;
|
|
32
|
+
let max = Math.hypot(hull.ox ?? 0, hull.oy ?? 0) + singleShapeRadius(hull);
|
|
33
|
+
for (let i = 0; i < weapons.length; i++) {
|
|
34
|
+
const w = weapons[i];
|
|
35
|
+
if (w === void 0) continue;
|
|
36
|
+
const reach = Math.hypot(w.ox ?? 0, w.oy ?? 0) + singleShapeRadius(w);
|
|
37
|
+
if (reach > max) max = reach;
|
|
38
|
+
}
|
|
39
|
+
const engineReach = Math.hypot(engine.ox ?? 0, engine.oy ?? 0) + engine.size * .6;
|
|
40
|
+
if (engineReach > max) max = engineReach;
|
|
41
|
+
return max;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
36
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Compute axis-aligned bounding radius for a shape composition. Used to
|
|
47
|
+
* size atlas tiles. Returns the max half-extent from origin across all
|
|
48
|
+
* shapes (including their `ox`/`oy` offsets and stroke widths).
|
|
49
|
+
*/
|
|
37
50
|
function shapeBoundsRadius(specs) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
let max = 0;
|
|
52
|
+
for (const s of specs) {
|
|
53
|
+
const ox = s.ox ?? 0;
|
|
54
|
+
const oy = s.oy ?? 0;
|
|
55
|
+
const r = boundsR(s);
|
|
56
|
+
const reach = Math.hypot(ox, oy) + r + 2;
|
|
57
|
+
if (reach > max) max = reach;
|
|
58
|
+
}
|
|
59
|
+
return Math.max(4, max);
|
|
47
60
|
}
|
|
61
|
+
/** Tighter atlas-tile-sizing radius (rect/gradient-rect use 0.75 to fit corners). */
|
|
48
62
|
function boundsR(spec) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
switch (spec.kind) {
|
|
64
|
+
case "rect":
|
|
65
|
+
case "gradient-rect": return Math.max(spec.w, spec.h) * .75;
|
|
66
|
+
case "trapezoid": return Math.max(spec.topW, spec.botW, spec.h) * .75;
|
|
67
|
+
case "ship": return shapeBoundsRadius([
|
|
68
|
+
spec.hull,
|
|
69
|
+
...spec.weapons,
|
|
70
|
+
{
|
|
71
|
+
kind: "rect",
|
|
72
|
+
w: spec.engine.size,
|
|
73
|
+
h: spec.engine.size,
|
|
74
|
+
ox: spec.engine.ox,
|
|
75
|
+
oy: spec.engine.oy
|
|
76
|
+
}
|
|
77
|
+
]);
|
|
78
|
+
default: return singleShapeRadius(spec);
|
|
79
|
+
}
|
|
60
80
|
}
|
|
61
|
-
|
|
62
|
-
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/draw.ts
|
|
83
|
+
/** ARGB for safe #6FA66B at α ≈ 0.25 (0x40 / 255). */
|
|
63
84
|
function friendlyGlowArgb() {
|
|
64
|
-
|
|
85
|
+
return 1081058923;
|
|
65
86
|
}
|
|
87
|
+
/** ARGB for hazardOrange #E84B2A at α ≈ 0.6 (0x99 / 255). */
|
|
66
88
|
function hazardGlowArgb() {
|
|
67
|
-
|
|
89
|
+
return -1712829654;
|
|
68
90
|
}
|
|
69
91
|
function applyFriendlyGlow(_Skia, canvas, paint, cx, cy, r) {
|
|
70
|
-
|
|
71
|
-
|
|
92
|
+
applyStroke(paint, friendlyGlowArgb(), 3);
|
|
93
|
+
canvas.drawCircle(cx, cy, r + 4, paint);
|
|
72
94
|
}
|
|
73
95
|
function applyHazardGlow(_Skia, canvas, paint, cx, cy, r) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
canvas.drawCircle(cx, cy, r + 2, paint);
|
|
96
|
+
applyStroke(paint, hazardGlowArgb(), .5);
|
|
97
|
+
canvas.drawCircle(cx, cy, r + 1, paint);
|
|
98
|
+
canvas.drawCircle(cx, cy, r + 2, paint);
|
|
78
99
|
}
|
|
79
|
-
|
|
100
|
+
const COLOR = /* @__PURE__ */ new Float32Array(4);
|
|
80
101
|
function argbTo(out, argb) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
102
|
+
out[0] = (argb >>> 16 & 255) / 255;
|
|
103
|
+
out[1] = (argb >>> 8 & 255) / 255;
|
|
104
|
+
out[2] = (argb & 255) / 255;
|
|
105
|
+
out[3] = (argb >>> 24 & 255) / 255;
|
|
85
106
|
}
|
|
86
107
|
function applyFill(paint, argb) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
108
|
+
argbTo(COLOR, argb);
|
|
109
|
+
paint.setColor(COLOR);
|
|
110
|
+
paint.setStyle(0);
|
|
90
111
|
}
|
|
91
112
|
function applyStroke(paint, argb, width) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
113
|
+
argbTo(COLOR, argb);
|
|
114
|
+
paint.setColor(COLOR);
|
|
115
|
+
paint.setStyle(1);
|
|
116
|
+
paint.setStrokeWidth(width);
|
|
96
117
|
}
|
|
97
118
|
function drawShape(Skia, canvas, paint, spec, x, y, rot) {
|
|
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
|
-
return;
|
|
156
|
-
case "ship":
|
|
157
|
-
drawShip(Skia, canvas, paint, spec, cx, cy, r);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
119
|
+
const cx = x + (spec.ox ?? 0);
|
|
120
|
+
const cy = y + (spec.oy ?? 0);
|
|
121
|
+
const r = rot + (spec.rot ?? 0);
|
|
122
|
+
if (spec.glow !== void 0) {
|
|
123
|
+
const gr = singleShapeRadius(spec);
|
|
124
|
+
if (spec.glow === "friendly") applyFriendlyGlow(Skia, canvas, paint, cx, cy, gr);
|
|
125
|
+
else applyHazardGlow(Skia, canvas, paint, cx, cy, gr);
|
|
126
|
+
}
|
|
127
|
+
switch (spec.kind) {
|
|
128
|
+
case "circle":
|
|
129
|
+
if (spec.fill !== void 0) {
|
|
130
|
+
applyFill(paint, spec.fill);
|
|
131
|
+
canvas.drawCircle(cx, cy, spec.r, paint);
|
|
132
|
+
}
|
|
133
|
+
if (spec.stroke !== void 0) {
|
|
134
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
135
|
+
canvas.drawCircle(cx, cy, spec.r, paint);
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
case "ring":
|
|
139
|
+
applyStroke(paint, spec.stroke ?? 4278190080, spec.strokeWidth ?? 1);
|
|
140
|
+
canvas.drawCircle(cx, cy, spec.r, paint);
|
|
141
|
+
return;
|
|
142
|
+
case "rect":
|
|
143
|
+
drawRect(Skia, canvas, paint, spec, cx, cy, r);
|
|
144
|
+
return;
|
|
145
|
+
case "triangle":
|
|
146
|
+
drawTriangle(Skia, canvas, paint, spec, cx, cy, r);
|
|
147
|
+
return;
|
|
148
|
+
case "ngon":
|
|
149
|
+
drawNgon(Skia, canvas, paint, spec, cx, cy, r);
|
|
150
|
+
return;
|
|
151
|
+
case "star":
|
|
152
|
+
drawStar(Skia, canvas, paint, spec, cx, cy, r);
|
|
153
|
+
return;
|
|
154
|
+
case "polyline":
|
|
155
|
+
drawPolyline(Skia, canvas, paint, spec, cx, cy, r);
|
|
156
|
+
return;
|
|
157
|
+
case "gradient-rect":
|
|
158
|
+
drawGradientRect(Skia, canvas, paint, spec, cx, cy, r);
|
|
159
|
+
return;
|
|
160
|
+
case "arc":
|
|
161
|
+
drawArc(Skia, canvas, paint, spec, cx, cy, r);
|
|
162
|
+
return;
|
|
163
|
+
case "annulus":
|
|
164
|
+
drawAnnulus(Skia, canvas, paint, spec, cx, cy, r);
|
|
165
|
+
return;
|
|
166
|
+
case "trapezoid":
|
|
167
|
+
drawTrapezoid(Skia, canvas, paint, spec, cx, cy, r);
|
|
168
|
+
return;
|
|
169
|
+
case "path":
|
|
170
|
+
drawPath(Skia, canvas, paint, spec, cx, cy, r);
|
|
171
|
+
return;
|
|
172
|
+
case "ship":
|
|
173
|
+
drawShip(Skia, canvas, paint, spec, cx, cy, r);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
160
176
|
}
|
|
161
177
|
function drawRect(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
178
|
+
canvas.save();
|
|
179
|
+
canvas.translate(cx, cy);
|
|
180
|
+
if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
|
|
181
|
+
const rect = Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, spec.h);
|
|
182
|
+
if (spec.fill !== void 0) {
|
|
183
|
+
applyFill(paint, spec.fill);
|
|
184
|
+
canvas.drawRect(rect, paint);
|
|
185
|
+
}
|
|
186
|
+
if (spec.stroke !== void 0) {
|
|
187
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
188
|
+
canvas.drawRect(rect, paint);
|
|
189
|
+
}
|
|
190
|
+
canvas.restore();
|
|
175
191
|
}
|
|
176
192
|
function drawTriangle(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
const s = spec.size;
|
|
194
|
+
const path = Skia.Path.Make();
|
|
195
|
+
path.moveTo(0, -s);
|
|
196
|
+
path.lineTo(-s * .6, s * .4);
|
|
197
|
+
path.lineTo(s * .6, s * .4);
|
|
198
|
+
path.close();
|
|
199
|
+
canvas.save();
|
|
200
|
+
canvas.translate(cx, cy);
|
|
201
|
+
if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
|
|
202
|
+
if (spec.fill !== void 0) {
|
|
203
|
+
applyFill(paint, spec.fill);
|
|
204
|
+
canvas.drawPath(path, paint);
|
|
205
|
+
}
|
|
206
|
+
if (spec.stroke !== void 0) {
|
|
207
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
208
|
+
canvas.drawPath(path, paint);
|
|
209
|
+
}
|
|
210
|
+
canvas.restore();
|
|
195
211
|
}
|
|
196
212
|
function drawNgon(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
213
|
+
const path = Skia.Path.Make();
|
|
214
|
+
for (let i = 0; i < spec.n; i++) {
|
|
215
|
+
const a = i / spec.n * Math.PI * 2 - Math.PI / 2;
|
|
216
|
+
const px = Math.cos(a) * spec.r;
|
|
217
|
+
const py = Math.sin(a) * spec.r;
|
|
218
|
+
if (i === 0) path.moveTo(px, py);
|
|
219
|
+
else path.lineTo(px, py);
|
|
220
|
+
}
|
|
221
|
+
path.close();
|
|
222
|
+
canvas.save();
|
|
223
|
+
canvas.translate(cx, cy);
|
|
224
|
+
if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
|
|
225
|
+
if (spec.fill !== void 0) {
|
|
226
|
+
applyFill(paint, spec.fill);
|
|
227
|
+
canvas.drawPath(path, paint);
|
|
228
|
+
}
|
|
229
|
+
if (spec.stroke !== void 0) {
|
|
230
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
231
|
+
canvas.drawPath(path, paint);
|
|
232
|
+
}
|
|
233
|
+
canvas.restore();
|
|
218
234
|
}
|
|
219
235
|
function drawStar(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
236
|
+
const path = Skia.Path.Make();
|
|
237
|
+
const total = spec.points * 2;
|
|
238
|
+
for (let i = 0; i < total; i++) {
|
|
239
|
+
const a = i / total * Math.PI * 2 - Math.PI / 2;
|
|
240
|
+
const r = i % 2 === 0 ? spec.rOuter : spec.rInner;
|
|
241
|
+
const px = Math.cos(a) * r;
|
|
242
|
+
const py = Math.sin(a) * r;
|
|
243
|
+
if (i === 0) path.moveTo(px, py);
|
|
244
|
+
else path.lineTo(px, py);
|
|
245
|
+
}
|
|
246
|
+
path.close();
|
|
247
|
+
canvas.save();
|
|
248
|
+
canvas.translate(cx, cy);
|
|
249
|
+
if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
|
|
250
|
+
if (spec.fill !== void 0) {
|
|
251
|
+
applyFill(paint, spec.fill);
|
|
252
|
+
canvas.drawPath(path, paint);
|
|
253
|
+
}
|
|
254
|
+
if (spec.stroke !== void 0) {
|
|
255
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
256
|
+
canvas.drawPath(path, paint);
|
|
257
|
+
}
|
|
258
|
+
canvas.restore();
|
|
243
259
|
}
|
|
244
260
|
function drawPolyline(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
canvas.drawPath(path, paint);
|
|
257
|
-
canvas.restore();
|
|
261
|
+
const pts = spec.points;
|
|
262
|
+
if (pts.length < 4) return;
|
|
263
|
+
const path = Skia.Path.Make();
|
|
264
|
+
path.moveTo(pts[0] ?? 0, pts[1] ?? 0);
|
|
265
|
+
for (let i = 2; i < pts.length; i += 2) path.lineTo(pts[i] ?? 0, pts[i + 1] ?? 0);
|
|
266
|
+
canvas.save();
|
|
267
|
+
canvas.translate(cx, cy);
|
|
268
|
+
if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
|
|
269
|
+
applyStroke(paint, spec.stroke ?? 4278190080, spec.strokeWidth ?? 1);
|
|
270
|
+
canvas.drawPath(path, paint);
|
|
271
|
+
canvas.restore();
|
|
258
272
|
}
|
|
259
273
|
function drawGradientRect(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
274
|
+
canvas.save();
|
|
275
|
+
canvas.translate(cx, cy);
|
|
276
|
+
if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
|
|
277
|
+
const half = spec.axis === 1 ? spec.h / 2 : spec.w / 2;
|
|
278
|
+
if (spec.axis === 1) {
|
|
279
|
+
applyFill(paint, spec.colorA);
|
|
280
|
+
canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, half), paint);
|
|
281
|
+
applyFill(paint, spec.colorB);
|
|
282
|
+
canvas.drawRect(Skia.XYWHRect(-spec.w / 2, 0, spec.w, half), paint);
|
|
283
|
+
} else {
|
|
284
|
+
applyFill(paint, spec.colorA);
|
|
285
|
+
canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, half, spec.h), paint);
|
|
286
|
+
applyFill(paint, spec.colorB);
|
|
287
|
+
canvas.drawRect(Skia.XYWHRect(0, -spec.h / 2, half, spec.h), paint);
|
|
288
|
+
}
|
|
289
|
+
canvas.restore();
|
|
276
290
|
}
|
|
277
291
|
function radToDeg(rad) {
|
|
278
|
-
|
|
292
|
+
return rad * 180 / Math.PI;
|
|
279
293
|
}
|
|
280
294
|
function drawArc(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
canvas.restore();
|
|
295
|
+
const path = Skia.Path.Make();
|
|
296
|
+
const oval = Skia.XYWHRect(-spec.r, -spec.r, spec.r * 2, spec.r * 2);
|
|
297
|
+
const startDeg = radToDeg(spec.startAngle);
|
|
298
|
+
const sweepDeg = radToDeg(spec.endAngle - spec.startAngle);
|
|
299
|
+
if (spec.sector === true) {
|
|
300
|
+
path.moveTo(0, 0);
|
|
301
|
+
path.addArc(oval, startDeg, sweepDeg);
|
|
302
|
+
path.close();
|
|
303
|
+
} else path.addArc(oval, startDeg, sweepDeg);
|
|
304
|
+
canvas.save();
|
|
305
|
+
canvas.translate(cx, cy);
|
|
306
|
+
if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
|
|
307
|
+
if (spec.fill !== void 0) {
|
|
308
|
+
applyFill(paint, spec.fill);
|
|
309
|
+
canvas.drawPath(path, paint);
|
|
310
|
+
}
|
|
311
|
+
if (spec.stroke !== void 0) {
|
|
312
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
313
|
+
canvas.drawPath(path, paint);
|
|
314
|
+
}
|
|
315
|
+
canvas.restore();
|
|
304
316
|
}
|
|
305
317
|
function drawAnnulus(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
318
|
+
const midR = (spec.rOuter + spec.rInner) / 2;
|
|
319
|
+
const thickness = spec.rOuter - spec.rInner;
|
|
320
|
+
const ringColor = spec.fill ?? spec.stroke ?? 4278190080;
|
|
321
|
+
canvas.save();
|
|
322
|
+
canvas.translate(cx, cy);
|
|
323
|
+
if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
|
|
324
|
+
if (spec.startAngle !== void 0 && spec.endAngle !== void 0) {
|
|
325
|
+
const path = Skia.Path.Make();
|
|
326
|
+
const oval = Skia.XYWHRect(-midR, -midR, midR * 2, midR * 2);
|
|
327
|
+
path.addArc(oval, radToDeg(spec.startAngle), radToDeg(spec.endAngle - spec.startAngle));
|
|
328
|
+
applyStroke(paint, ringColor, thickness);
|
|
329
|
+
canvas.drawPath(path, paint);
|
|
330
|
+
} else {
|
|
331
|
+
applyStroke(paint, ringColor, thickness);
|
|
332
|
+
canvas.drawCircle(0, 0, midR, paint);
|
|
333
|
+
}
|
|
334
|
+
canvas.restore();
|
|
323
335
|
}
|
|
324
336
|
function drawTrapezoid(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
337
|
+
const path = Skia.Path.Make();
|
|
338
|
+
const halfTopW = spec.topW / 2;
|
|
339
|
+
const halfBotW = spec.botW / 2;
|
|
340
|
+
const halfH = spec.h / 2;
|
|
341
|
+
path.moveTo(-halfTopW, -halfH);
|
|
342
|
+
path.lineTo(halfTopW, -halfH);
|
|
343
|
+
path.lineTo(halfBotW, halfH);
|
|
344
|
+
path.lineTo(-halfBotW, halfH);
|
|
345
|
+
path.close();
|
|
346
|
+
canvas.save();
|
|
347
|
+
canvas.translate(cx, cy);
|
|
348
|
+
if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
|
|
349
|
+
if (spec.fill !== void 0) {
|
|
350
|
+
applyFill(paint, spec.fill);
|
|
351
|
+
canvas.drawPath(path, paint);
|
|
352
|
+
}
|
|
353
|
+
if (spec.stroke !== void 0) {
|
|
354
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
355
|
+
canvas.drawPath(path, paint);
|
|
356
|
+
}
|
|
357
|
+
canvas.restore();
|
|
346
358
|
}
|
|
347
359
|
function drawPath(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
360
|
+
const path = Skia.Path.MakeFromSVGString(spec.d);
|
|
361
|
+
if (path === null) return;
|
|
362
|
+
canvas.save();
|
|
363
|
+
canvas.translate(cx, cy);
|
|
364
|
+
if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
|
|
365
|
+
if (spec.fill !== void 0) {
|
|
366
|
+
applyFill(paint, spec.fill);
|
|
367
|
+
canvas.drawPath(path, paint);
|
|
368
|
+
}
|
|
369
|
+
if (spec.stroke !== void 0) {
|
|
370
|
+
applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
|
|
371
|
+
canvas.drawPath(path, paint);
|
|
372
|
+
}
|
|
373
|
+
canvas.restore();
|
|
362
374
|
}
|
|
363
375
|
function drawSquare(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
drawRect(Skia, canvas, paint, rectSpec, cx, cy, rot);
|
|
376
|
+
drawRect(Skia, canvas, paint, {
|
|
377
|
+
kind: "rect",
|
|
378
|
+
w: spec.size,
|
|
379
|
+
h: spec.size,
|
|
380
|
+
fill: spec.fill,
|
|
381
|
+
stroke: spec.stroke,
|
|
382
|
+
strokeWidth: spec.strokeWidth,
|
|
383
|
+
ox: spec.ox,
|
|
384
|
+
oy: spec.oy,
|
|
385
|
+
rot: spec.rot
|
|
386
|
+
}, cx, cy, rot);
|
|
376
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* Draw a ShipSpec by painting engine → hull → weapons (engine deepest,
|
|
390
|
+
* weapons foremost — painter's algorithm). Zero allocation in the weapons
|
|
391
|
+
* loop: classic indexed for-loop over the readonly array.
|
|
392
|
+
*/
|
|
377
393
|
function drawShip(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
}
|
|
385
|
-
}
|
|
394
|
+
drawSquare(Skia, canvas, paint, spec.engine, cx, cy, rot);
|
|
395
|
+
drawShape(Skia, canvas, paint, spec.hull, cx, cy, rot);
|
|
396
|
+
for (let i = 0; i < spec.weapons.length; i++) {
|
|
397
|
+
const w = spec.weapons[i];
|
|
398
|
+
if (w !== void 0) drawShape(Skia, canvas, paint, w, cx, cy, rot);
|
|
399
|
+
}
|
|
386
400
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
argbTo,
|
|
391
|
-
drawShape,
|
|
392
|
-
friendlyGlowArgb,
|
|
393
|
-
hazardGlowArgb,
|
|
394
|
-
shapeBoundsRadius,
|
|
395
|
-
singleShapeRadius
|
|
396
|
-
};
|
|
401
|
+
//#endregion
|
|
402
|
+
export { applyFriendlyGlow, applyHazardGlow, argbTo, drawShape, friendlyGlowArgb, hazardGlowArgb, shapeBoundsRadius, singleShapeRadius };
|
|
403
|
+
|
|
397
404
|
//# sourceMappingURL=index.mjs.map
|