@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/dist/index.mjs CHANGED
@@ -1,397 +1,404 @@
1
- // src/bounds.ts
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
- switch (spec.kind) {
4
- case "circle":
5
- case "ring":
6
- case "ngon":
7
- return spec.r;
8
- case "triangle":
9
- return spec.size;
10
- case "star":
11
- return spec.rOuter;
12
- case "rect":
13
- case "gradient-rect":
14
- return Math.max(spec.w, spec.h) * 0.6;
15
- case "polyline": {
16
- let max = 0;
17
- for (let i = 0; i < spec.points.length; i += 2) {
18
- const px = spec.points[i] ?? 0;
19
- const py = spec.points[i + 1] ?? 0;
20
- const m = Math.hypot(px, py);
21
- if (m > max) max = m;
22
- }
23
- return max + (spec.strokeWidth ?? 1);
24
- }
25
- case "arc":
26
- return spec.r;
27
- case "annulus":
28
- return spec.rOuter;
29
- case "trapezoid":
30
- return Math.max(spec.topW, spec.botW, spec.h) * 0.6;
31
- case "path":
32
- return spec.boundsR;
33
- case "ship":
34
- return singleShapeRadius(spec.hull);
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
- let max = 0;
39
- for (const s of specs) {
40
- const ox = s.ox ?? 0;
41
- const oy = s.oy ?? 0;
42
- const r = boundsR(s);
43
- const reach = Math.hypot(ox, oy) + r + 2;
44
- if (reach > max) max = reach;
45
- }
46
- return Math.max(4, max);
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
- switch (spec.kind) {
50
- case "rect":
51
- case "gradient-rect":
52
- return Math.max(spec.w, spec.h) * 0.75;
53
- case "trapezoid":
54
- return Math.max(spec.topW, spec.botW, spec.h) * 0.75;
55
- case "ship":
56
- return shapeBoundsRadius([spec.hull]);
57
- default:
58
- return singleShapeRadius(spec);
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
- // src/draw.ts
81
+ //#endregion
82
+ //#region src/draw.ts
83
+ /** ARGB for safe #6FA66B at α ≈ 0.25 (0x40 / 255). */
63
84
  function friendlyGlowArgb() {
64
- return 64 << 24 | 7317099;
85
+ return 1081058923;
65
86
  }
87
+ /** ARGB for hazardOrange #E84B2A at α ≈ 0.6 (0x99 / 255). */
66
88
  function hazardGlowArgb() {
67
- return 153 << 24 | 15223594;
89
+ return -1712829654;
68
90
  }
69
91
  function applyFriendlyGlow(_Skia, canvas, paint, cx, cy, r) {
70
- applyStroke(paint, friendlyGlowArgb(), 3);
71
- canvas.drawCircle(cx, cy, r + 4, paint);
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
- const argb = hazardGlowArgb();
75
- applyStroke(paint, argb, 0.5);
76
- canvas.drawCircle(cx, cy, r + 1, paint);
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
- var COLOR = new Float32Array(4);
100
+ const COLOR = /* @__PURE__ */ new Float32Array(4);
80
101
  function argbTo(out, argb) {
81
- out[0] = (argb >>> 16 & 255) / 255;
82
- out[1] = (argb >>> 8 & 255) / 255;
83
- out[2] = (argb & 255) / 255;
84
- out[3] = (argb >>> 24 & 255) / 255;
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
- argbTo(COLOR, argb);
88
- paint.setColor(COLOR);
89
- paint.setStyle(0);
108
+ argbTo(COLOR, argb);
109
+ paint.setColor(COLOR);
110
+ paint.setStyle(0);
90
111
  }
91
112
  function applyStroke(paint, argb, width) {
92
- argbTo(COLOR, argb);
93
- paint.setColor(COLOR);
94
- paint.setStyle(1);
95
- paint.setStrokeWidth(width);
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
- const cx = x + (spec.ox ?? 0);
99
- const cy = y + (spec.oy ?? 0);
100
- const r = rot + (spec.rot ?? 0);
101
- if (spec.glow !== void 0) {
102
- const gr = singleShapeRadius(spec);
103
- if (spec.glow === "friendly") {
104
- applyFriendlyGlow(Skia, canvas, paint, cx, cy, gr);
105
- } else {
106
- applyHazardGlow(Skia, canvas, paint, cx, cy, gr);
107
- }
108
- }
109
- switch (spec.kind) {
110
- case "circle": {
111
- if (spec.fill !== void 0) {
112
- applyFill(paint, spec.fill);
113
- canvas.drawCircle(cx, cy, spec.r, paint);
114
- }
115
- if (spec.stroke !== void 0) {
116
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
117
- canvas.drawCircle(cx, cy, spec.r, paint);
118
- }
119
- return;
120
- }
121
- case "ring": {
122
- applyStroke(paint, spec.stroke ?? 4278190080, spec.strokeWidth ?? 1);
123
- canvas.drawCircle(cx, cy, spec.r, paint);
124
- return;
125
- }
126
- case "rect":
127
- drawRect(Skia, canvas, paint, spec, cx, cy, r);
128
- return;
129
- case "triangle":
130
- drawTriangle(Skia, canvas, paint, spec, cx, cy, r);
131
- return;
132
- case "ngon":
133
- drawNgon(Skia, canvas, paint, spec, cx, cy, r);
134
- return;
135
- case "star":
136
- drawStar(Skia, canvas, paint, spec, cx, cy, r);
137
- return;
138
- case "polyline":
139
- drawPolyline(Skia, canvas, paint, spec, cx, cy, r);
140
- return;
141
- case "gradient-rect":
142
- drawGradientRect(Skia, canvas, paint, spec, cx, cy, r);
143
- return;
144
- case "arc":
145
- drawArc(Skia, canvas, paint, spec, cx, cy, r);
146
- return;
147
- case "annulus":
148
- drawAnnulus(Skia, canvas, paint, spec, cx, cy, r);
149
- return;
150
- case "trapezoid":
151
- drawTrapezoid(Skia, canvas, paint, spec, cx, cy, r);
152
- return;
153
- case "path":
154
- drawPath(Skia, canvas, paint, spec, cx, cy, r);
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
- canvas.save();
163
- canvas.translate(cx, cy);
164
- if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
165
- const rect = Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, spec.h);
166
- if (spec.fill !== void 0) {
167
- applyFill(paint, spec.fill);
168
- canvas.drawRect(rect, paint);
169
- }
170
- if (spec.stroke !== void 0) {
171
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
172
- canvas.drawRect(rect, paint);
173
- }
174
- canvas.restore();
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
- const s = spec.size;
178
- const path = Skia.Path.Make();
179
- path.moveTo(0, -s);
180
- path.lineTo(-s * 0.6, s * 0.4);
181
- path.lineTo(s * 0.6, s * 0.4);
182
- path.close();
183
- canvas.save();
184
- canvas.translate(cx, cy);
185
- if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
186
- if (spec.fill !== void 0) {
187
- applyFill(paint, spec.fill);
188
- canvas.drawPath(path, paint);
189
- }
190
- if (spec.stroke !== void 0) {
191
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
192
- canvas.drawPath(path, paint);
193
- }
194
- canvas.restore();
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
- const path = Skia.Path.Make();
198
- for (let i = 0; i < spec.n; i++) {
199
- const a = i / spec.n * Math.PI * 2 - Math.PI / 2;
200
- const px = Math.cos(a) * spec.r;
201
- const py = Math.sin(a) * spec.r;
202
- if (i === 0) path.moveTo(px, py);
203
- else path.lineTo(px, py);
204
- }
205
- path.close();
206
- canvas.save();
207
- canvas.translate(cx, cy);
208
- if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
209
- if (spec.fill !== void 0) {
210
- applyFill(paint, spec.fill);
211
- canvas.drawPath(path, paint);
212
- }
213
- if (spec.stroke !== void 0) {
214
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
215
- canvas.drawPath(path, paint);
216
- }
217
- canvas.restore();
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
- const path = Skia.Path.Make();
221
- const total = spec.points * 2;
222
- for (let i = 0; i < total; i++) {
223
- const a = i / total * Math.PI * 2 - Math.PI / 2;
224
- const r = i % 2 === 0 ? spec.rOuter : spec.rInner;
225
- const px = Math.cos(a) * r;
226
- const py = Math.sin(a) * r;
227
- if (i === 0) path.moveTo(px, py);
228
- else path.lineTo(px, py);
229
- }
230
- path.close();
231
- canvas.save();
232
- canvas.translate(cx, cy);
233
- if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
234
- if (spec.fill !== void 0) {
235
- applyFill(paint, spec.fill);
236
- canvas.drawPath(path, paint);
237
- }
238
- if (spec.stroke !== void 0) {
239
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
240
- canvas.drawPath(path, paint);
241
- }
242
- canvas.restore();
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
- const pts = spec.points;
246
- if (pts.length < 4) return;
247
- const path = Skia.Path.Make();
248
- path.moveTo(pts[0] ?? 0, pts[1] ?? 0);
249
- for (let i = 2; i < pts.length; i += 2) {
250
- path.lineTo(pts[i] ?? 0, pts[i + 1] ?? 0);
251
- }
252
- canvas.save();
253
- canvas.translate(cx, cy);
254
- if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
255
- applyStroke(paint, spec.stroke ?? 4278190080, spec.strokeWidth ?? 1);
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
- canvas.save();
261
- canvas.translate(cx, cy);
262
- if (rot !== 0) canvas.rotate(rot * 180 / Math.PI, 0, 0);
263
- const half = spec.axis === 1 ? spec.h / 2 : spec.w / 2;
264
- if (spec.axis === 1) {
265
- applyFill(paint, spec.colorA);
266
- canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, half), paint);
267
- applyFill(paint, spec.colorB);
268
- canvas.drawRect(Skia.XYWHRect(-spec.w / 2, 0, spec.w, half), paint);
269
- } else {
270
- applyFill(paint, spec.colorA);
271
- canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, half, spec.h), paint);
272
- applyFill(paint, spec.colorB);
273
- canvas.drawRect(Skia.XYWHRect(0, -spec.h / 2, half, spec.h), paint);
274
- }
275
- canvas.restore();
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
- return rad * 180 / Math.PI;
292
+ return rad * 180 / Math.PI;
279
293
  }
280
294
  function drawArc(Skia, canvas, paint, spec, cx, cy, rot) {
281
- const path = Skia.Path.Make();
282
- const oval = Skia.XYWHRect(-spec.r, -spec.r, spec.r * 2, spec.r * 2);
283
- const startDeg = radToDeg(spec.startAngle);
284
- const sweepDeg = radToDeg(spec.endAngle - spec.startAngle);
285
- if (spec.sector === true) {
286
- path.moveTo(0, 0);
287
- path.addArc(oval, startDeg, sweepDeg);
288
- path.close();
289
- } else {
290
- path.addArc(oval, startDeg, sweepDeg);
291
- }
292
- canvas.save();
293
- canvas.translate(cx, cy);
294
- if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
295
- if (spec.fill !== void 0) {
296
- applyFill(paint, spec.fill);
297
- canvas.drawPath(path, paint);
298
- }
299
- if (spec.stroke !== void 0) {
300
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
301
- canvas.drawPath(path, paint);
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
- const midR = (spec.rOuter + spec.rInner) / 2;
307
- const thickness = spec.rOuter - spec.rInner;
308
- const ringColor = spec.fill ?? spec.stroke ?? 4278190080;
309
- canvas.save();
310
- canvas.translate(cx, cy);
311
- if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
312
- if (spec.startAngle !== void 0 && spec.endAngle !== void 0) {
313
- const path = Skia.Path.Make();
314
- const oval = Skia.XYWHRect(-midR, -midR, midR * 2, midR * 2);
315
- path.addArc(oval, radToDeg(spec.startAngle), radToDeg(spec.endAngle - spec.startAngle));
316
- applyStroke(paint, ringColor, thickness);
317
- canvas.drawPath(path, paint);
318
- } else {
319
- applyStroke(paint, ringColor, thickness);
320
- canvas.drawCircle(0, 0, midR, paint);
321
- }
322
- canvas.restore();
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
- const path = Skia.Path.Make();
326
- const halfTopW = spec.topW / 2;
327
- const halfBotW = spec.botW / 2;
328
- const halfH = spec.h / 2;
329
- path.moveTo(-halfTopW, -halfH);
330
- path.lineTo(halfTopW, -halfH);
331
- path.lineTo(halfBotW, halfH);
332
- path.lineTo(-halfBotW, halfH);
333
- path.close();
334
- canvas.save();
335
- canvas.translate(cx, cy);
336
- if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
337
- if (spec.fill !== void 0) {
338
- applyFill(paint, spec.fill);
339
- canvas.drawPath(path, paint);
340
- }
341
- if (spec.stroke !== void 0) {
342
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
343
- canvas.drawPath(path, paint);
344
- }
345
- canvas.restore();
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
- const path = Skia.Path.MakeFromSVGString(spec.d);
349
- if (path === null) return;
350
- canvas.save();
351
- canvas.translate(cx, cy);
352
- if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);
353
- if (spec.fill !== void 0) {
354
- applyFill(paint, spec.fill);
355
- canvas.drawPath(path, paint);
356
- }
357
- if (spec.stroke !== void 0) {
358
- applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);
359
- canvas.drawPath(path, paint);
360
- }
361
- canvas.restore();
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
- const rectSpec = {
365
- kind: "rect",
366
- w: spec.size,
367
- h: spec.size,
368
- fill: spec.fill,
369
- stroke: spec.stroke,
370
- strokeWidth: spec.strokeWidth,
371
- ox: spec.ox,
372
- oy: spec.oy,
373
- rot: spec.rot
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
- drawSquare(Skia, canvas, paint, spec.engine, cx, cy, rot);
379
- drawShape(Skia, canvas, paint, spec.hull, cx, cy, rot);
380
- for (let i = 0; i < spec.weapons.length; i++) {
381
- const w = spec.weapons[i];
382
- if (w !== void 0) {
383
- drawShape(Skia, canvas, paint, w, cx, cy, rot);
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
- export {
388
- applyFriendlyGlow,
389
- applyHazardGlow,
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