@flare-engine/shapes 0.2.1 → 0.3.1

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