@al8b/image 0.1.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.
Files changed (94) hide show
  1. package/README.md +30 -0
  2. package/dist/blending.d.mts +3 -0
  3. package/dist/blending.d.ts +3 -0
  4. package/dist/blending.js +65 -0
  5. package/dist/blending.js.map +1 -0
  6. package/dist/blending.mjs +40 -0
  7. package/dist/blending.mjs.map +1 -0
  8. package/dist/color-DJyRSbpZ.d.mts +16 -0
  9. package/dist/color-DJyRSbpZ.d.ts +16 -0
  10. package/dist/core/color.d.mts +1 -0
  11. package/dist/core/color.d.ts +1 -0
  12. package/dist/core/color.js +61 -0
  13. package/dist/core/color.js.map +1 -0
  14. package/dist/core/color.mjs +36 -0
  15. package/dist/core/color.mjs.map +1 -0
  16. package/dist/core/context.d.mts +25 -0
  17. package/dist/core/context.d.ts +25 -0
  18. package/dist/core/context.js +77 -0
  19. package/dist/core/context.js.map +1 -0
  20. package/dist/core/context.mjs +53 -0
  21. package/dist/core/context.mjs.map +1 -0
  22. package/dist/core/drawing.d.mts +2 -0
  23. package/dist/core/drawing.d.ts +2 -0
  24. package/dist/core/drawing.js +165 -0
  25. package/dist/core/drawing.js.map +1 -0
  26. package/dist/core/drawing.mjs +134 -0
  27. package/dist/core/drawing.mjs.map +1 -0
  28. package/dist/core/font.d.mts +19 -0
  29. package/dist/core/font.d.ts +19 -0
  30. package/dist/core/font.js +80 -0
  31. package/dist/core/font.js.map +1 -0
  32. package/dist/core/font.mjs +55 -0
  33. package/dist/core/font.mjs.map +1 -0
  34. package/dist/core/image.d.mts +84 -0
  35. package/dist/core/image.d.ts +84 -0
  36. package/dist/core/image.js +1018 -0
  37. package/dist/core/image.js.map +1 -0
  38. package/dist/core/image.mjs +993 -0
  39. package/dist/core/image.mjs.map +1 -0
  40. package/dist/core/map-rendering.d.mts +2 -0
  41. package/dist/core/map-rendering.d.ts +2 -0
  42. package/dist/core/map-rendering.js +95 -0
  43. package/dist/core/map-rendering.js.map +1 -0
  44. package/dist/core/map-rendering.mjs +70 -0
  45. package/dist/core/map-rendering.mjs.map +1 -0
  46. package/dist/core/pixel.d.mts +18 -0
  47. package/dist/core/pixel.d.ts +18 -0
  48. package/dist/core/pixel.js +99 -0
  49. package/dist/core/pixel.js.map +1 -0
  50. package/dist/core/pixel.mjs +73 -0
  51. package/dist/core/pixel.mjs.map +1 -0
  52. package/dist/core/shapes.d.mts +2 -0
  53. package/dist/core/shapes.d.ts +2 -0
  54. package/dist/core/shapes.js +218 -0
  55. package/dist/core/shapes.js.map +1 -0
  56. package/dist/core/shapes.mjs +187 -0
  57. package/dist/core/shapes.mjs.map +1 -0
  58. package/dist/core/sprite-rendering.d.mts +2 -0
  59. package/dist/core/sprite-rendering.d.ts +2 -0
  60. package/dist/core/sprite-rendering.js +135 -0
  61. package/dist/core/sprite-rendering.js.map +1 -0
  62. package/dist/core/sprite-rendering.mjs +108 -0
  63. package/dist/core/sprite-rendering.mjs.map +1 -0
  64. package/dist/core/text.d.mts +2 -0
  65. package/dist/core/text.d.ts +2 -0
  66. package/dist/core/text.js +105 -0
  67. package/dist/core/text.js.map +1 -0
  68. package/dist/core/text.mjs +78 -0
  69. package/dist/core/text.mjs.map +1 -0
  70. package/dist/core/transform.d.mts +2 -0
  71. package/dist/core/transform.d.ts +2 -0
  72. package/dist/core/transform.js +101 -0
  73. package/dist/core/transform.js.map +1 -0
  74. package/dist/core/transform.mjs +71 -0
  75. package/dist/core/transform.mjs.map +1 -0
  76. package/dist/drawing-Bo2U4IUE.d.mts +31 -0
  77. package/dist/drawing-e7iVDl14.d.ts +31 -0
  78. package/dist/index.d.mts +12 -0
  79. package/dist/index.d.ts +12 -0
  80. package/dist/index.js +1098 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/index.mjs +1066 -0
  83. package/dist/index.mjs.map +1 -0
  84. package/dist/map-rendering-CzeC061I.d.mts +20 -0
  85. package/dist/map-rendering-DcldCBtY.d.ts +20 -0
  86. package/dist/shapes-BY37IuhJ.d.ts +27 -0
  87. package/dist/shapes-CpCwX9Rp.d.mts +27 -0
  88. package/dist/sprite-rendering-Dac9FQ2x.d.ts +30 -0
  89. package/dist/sprite-rendering-DfQuLVXT.d.mts +30 -0
  90. package/dist/text-CIoUSRc-.d.mts +19 -0
  91. package/dist/text-HW25Qq3e.d.ts +19 -0
  92. package/dist/transform-Dqo6bY4r.d.mts +25 -0
  93. package/dist/transform-iwq8WdqR.d.ts +25 -0
  94. package/package.json +43 -0
@@ -0,0 +1,993 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/blending.ts
5
+ var BLENDING_MODES = {
6
+ normal: "source-over",
7
+ additive: "lighter"
8
+ };
9
+ var MODES = [
10
+ "source-over",
11
+ "source-in",
12
+ "source-out",
13
+ "source-atop",
14
+ "destination-over",
15
+ "destination-in",
16
+ "destination-out",
17
+ "destination-atop",
18
+ "lighter",
19
+ "copy",
20
+ "xor",
21
+ "multiply",
22
+ "screen",
23
+ "overlay",
24
+ "darken",
25
+ "lighten",
26
+ "color-dodge",
27
+ "color-burn",
28
+ "hard-light",
29
+ "soft-light",
30
+ "difference",
31
+ "exclusion",
32
+ "hue",
33
+ "saturation",
34
+ "color",
35
+ "luminosity"
36
+ ];
37
+ for (const mode of MODES) {
38
+ BLENDING_MODES[mode] = mode;
39
+ }
40
+
41
+ // src/core/color.ts
42
+ function setColor(context, color) {
43
+ if (color == null) {
44
+ return;
45
+ }
46
+ if (typeof color === "string") {
47
+ context.fillStyle = color;
48
+ context.strokeStyle = color;
49
+ }
50
+ }
51
+ __name(setColor, "setColor");
52
+ function setLinearGradient(context, x1, y1, x2, y2, c1, c2) {
53
+ const grd = context.createLinearGradient(x1, y1, x2, y2);
54
+ grd.addColorStop(0, c1);
55
+ grd.addColorStop(1, c2);
56
+ context.fillStyle = grd;
57
+ context.strokeStyle = grd;
58
+ }
59
+ __name(setLinearGradient, "setLinearGradient");
60
+ function setRadialGradient(context, x, y, radius, c1, c2) {
61
+ const grd = context.createRadialGradient(x, y, 0, x, y, radius);
62
+ grd.addColorStop(0, c1);
63
+ grd.addColorStop(1, c2);
64
+ context.fillStyle = grd;
65
+ context.strokeStyle = grd;
66
+ }
67
+ __name(setRadialGradient, "setRadialGradient");
68
+
69
+ // src/core/context.ts
70
+ function createDefaultContextState(centered) {
71
+ if (centered) {
72
+ return {
73
+ alpha: 1,
74
+ pixelated: 1,
75
+ line_width: 1,
76
+ translation_x: 0,
77
+ translation_y: 0,
78
+ rotation: 0,
79
+ scale_x: 1,
80
+ scale_y: -1,
81
+ image_transform: true,
82
+ anchor_x: 0,
83
+ anchor_y: 0,
84
+ object_rotation: 0,
85
+ object_scale_x: 1,
86
+ object_scale_y: -1,
87
+ font: "BitCell"
88
+ };
89
+ } else {
90
+ return {
91
+ alpha: 1,
92
+ pixelated: 1,
93
+ line_width: 1,
94
+ translation_x: 0,
95
+ translation_y: 0,
96
+ rotation: 0,
97
+ scale_x: 1,
98
+ scale_y: 1,
99
+ image_transform: false,
100
+ anchor_x: -1,
101
+ anchor_y: 1,
102
+ object_rotation: 0,
103
+ object_scale_x: 1,
104
+ object_scale_y: 1,
105
+ font: "BitCell"
106
+ };
107
+ }
108
+ }
109
+ __name(createDefaultContextState, "createDefaultContextState");
110
+ function updateImageTransform(state) {
111
+ state.image_transform = state.translation_x !== 0 || state.translation_y !== 0 || state.scale_x !== 1 || state.scale_y !== 1 || state.rotation !== 0;
112
+ }
113
+ __name(updateImageTransform, "updateImageTransform");
114
+
115
+ // src/core/drawing.ts
116
+ function initDrawOp(context, state, x, y, object_transform = true) {
117
+ let res = false;
118
+ if (state.image_transform) {
119
+ context.save();
120
+ res = true;
121
+ context.translate(state.translation_x, state.translation_y);
122
+ context.scale(state.scale_x, state.scale_y);
123
+ context.rotate(state.rotation / 180 * Math.PI);
124
+ context.translate(x, y);
125
+ }
126
+ if (object_transform && (state.object_rotation !== 0 || state.object_scale_x !== 1 || state.object_scale_y !== 1)) {
127
+ if (!res) {
128
+ context.save();
129
+ res = true;
130
+ context.translate(x, y);
131
+ }
132
+ if (state.object_rotation !== 0) {
133
+ context.rotate(state.object_rotation / 180 * Math.PI);
134
+ }
135
+ if (state.object_scale_x !== 1 || state.object_scale_y !== 1) {
136
+ context.scale(state.object_scale_x, state.object_scale_y);
137
+ }
138
+ }
139
+ return res;
140
+ }
141
+ __name(initDrawOp, "initDrawOp");
142
+ function closeDrawOp(context) {
143
+ context.restore();
144
+ }
145
+ __name(closeDrawOp, "closeDrawOp");
146
+ function fillRect(context, state, x, y, w, h) {
147
+ context.globalAlpha = state.alpha;
148
+ if (initDrawOp(context, state, x, y)) {
149
+ context.fillRect(-w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h);
150
+ closeDrawOp(context);
151
+ } else {
152
+ context.fillRect(x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h);
153
+ }
154
+ }
155
+ __name(fillRect, "fillRect");
156
+ function fillRoundRect(context, state, x, y, w, h, round) {
157
+ context.globalAlpha = state.alpha;
158
+ if (initDrawOp(context, state, x, y)) {
159
+ context.fillRoundRect(-w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h, round);
160
+ closeDrawOp(context);
161
+ } else {
162
+ context.fillRoundRect(x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h, round);
163
+ }
164
+ }
165
+ __name(fillRoundRect, "fillRoundRect");
166
+ function fillRound(context, state, x, y, w, h) {
167
+ context.globalAlpha = state.alpha;
168
+ w = Math.abs(w);
169
+ h = Math.abs(h);
170
+ if (initDrawOp(context, state, x, y)) {
171
+ context.beginPath();
172
+ context.ellipse(-state.anchor_x * w / 2, 0 + state.anchor_y * h / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);
173
+ context.fill();
174
+ closeDrawOp(context);
175
+ } else {
176
+ context.beginPath();
177
+ context.ellipse(x - state.anchor_x * w / 2, y + state.anchor_y * h / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);
178
+ context.fill();
179
+ }
180
+ }
181
+ __name(fillRound, "fillRound");
182
+ function drawRect(context, state, x, y, w, h) {
183
+ context.globalAlpha = state.alpha;
184
+ context.lineWidth = state.line_width;
185
+ if (initDrawOp(context, state, x, y)) {
186
+ context.strokeRect(-w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h);
187
+ closeDrawOp(context);
188
+ } else {
189
+ context.strokeRect(x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h);
190
+ }
191
+ }
192
+ __name(drawRect, "drawRect");
193
+ function drawRoundRect(context, state, x, y, w, h, round) {
194
+ context.globalAlpha = state.alpha;
195
+ context.lineWidth = state.line_width;
196
+ if (initDrawOp(context, state, x, y)) {
197
+ context.strokeRoundRect(-w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h, round);
198
+ closeDrawOp(context);
199
+ } else {
200
+ context.strokeRoundRect(x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h, round);
201
+ }
202
+ }
203
+ __name(drawRoundRect, "drawRoundRect");
204
+ function drawRound(context, state, x, y, w, h) {
205
+ context.globalAlpha = state.alpha;
206
+ context.lineWidth = state.line_width;
207
+ w = Math.abs(w);
208
+ h = Math.abs(h);
209
+ if (initDrawOp(context, state, x, y)) {
210
+ context.beginPath();
211
+ context.ellipse(0 - state.anchor_x * w / 2, 0 + state.anchor_y * h / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);
212
+ context.stroke();
213
+ closeDrawOp(context);
214
+ } else {
215
+ context.beginPath();
216
+ context.ellipse(x - state.anchor_x * w / 2, y + state.anchor_y * h / 2, w / 2, h / 2, 0, 0, Math.PI * 2, false);
217
+ context.stroke();
218
+ }
219
+ }
220
+ __name(drawRound, "drawRound");
221
+ function drawLine(context, state, x1, y1, x2, y2) {
222
+ context.globalAlpha = state.alpha;
223
+ context.lineWidth = state.line_width;
224
+ const transform = initDrawOp(context, state, 0, 0, false);
225
+ context.beginPath();
226
+ context.moveTo(x1, y1);
227
+ context.lineTo(x2, y2);
228
+ context.stroke();
229
+ if (transform) {
230
+ closeDrawOp(context);
231
+ }
232
+ }
233
+ __name(drawLine, "drawLine");
234
+
235
+ // src/core/font.ts
236
+ var loadedFonts = /* @__PURE__ */ new Set();
237
+ var loadingFonts = /* @__PURE__ */ new Map();
238
+ function loadFont(font) {
239
+ if (!font) {
240
+ return;
241
+ }
242
+ if (loadedFonts.has(font)) {
243
+ return Promise.resolve();
244
+ }
245
+ if (loadingFonts.has(font)) {
246
+ return loadingFonts.get(font);
247
+ }
248
+ if (typeof document !== "undefined" && "fonts" in document) {
249
+ const promise = document.fonts.load(`12pt ${font}`).then(() => {
250
+ loadedFonts.add(font);
251
+ loadingFonts.delete(font);
252
+ }).catch(() => {
253
+ loadedFonts.add(font);
254
+ loadingFonts.delete(font);
255
+ });
256
+ loadingFonts.set(font, promise);
257
+ return promise;
258
+ }
259
+ loadedFonts.add(font);
260
+ return Promise.resolve();
261
+ }
262
+ __name(loadFont, "loadFont");
263
+ function isFontReady(font) {
264
+ if (!font) {
265
+ return true;
266
+ }
267
+ if (loadedFonts.has(font)) {
268
+ return true;
269
+ }
270
+ if (typeof document !== "undefined" && "fonts" in document) {
271
+ return document.fonts.check(`12pt ${font}`);
272
+ }
273
+ return true;
274
+ }
275
+ __name(isFontReady, "isFontReady");
276
+
277
+ // src/core/map-rendering.ts
278
+ var resolveStringMap = /* @__PURE__ */ __name((name) => {
279
+ const playerRuntime = globalThis?.player?.runtime;
280
+ if (!playerRuntime) {
281
+ return null;
282
+ }
283
+ const directMaps = playerRuntime.maps;
284
+ if (directMaps && directMaps[name]) {
285
+ return directMaps[name];
286
+ }
287
+ const screenMaps = playerRuntime.screen?.runtime?.maps;
288
+ if (screenMaps && screenMaps[name]) {
289
+ return screenMaps[name];
290
+ }
291
+ return null;
292
+ }, "resolveStringMap");
293
+ function drawMap(context, state, map, x, y, w, h) {
294
+ const resolvedMap = typeof map === "string" ? resolveStringMap(map) : map ?? null;
295
+ if (!resolvedMap || !resolvedMap.ready || !resolvedMap.draw) {
296
+ return;
297
+ }
298
+ context.globalAlpha = state.alpha;
299
+ context.imageSmoothingEnabled = !state.pixelated;
300
+ if (initDrawOp(context, state, x, y)) {
301
+ resolvedMap.draw(context, -w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h);
302
+ closeDrawOp(context);
303
+ } else {
304
+ resolvedMap.draw(context, x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h);
305
+ }
306
+ }
307
+ __name(drawMap, "drawMap");
308
+
309
+ // src/core/pixel.ts
310
+ var pixelDataCache = null;
311
+ function setRGB(context, x, y, r, g, b) {
312
+ if (pixelDataCache == null) {
313
+ pixelDataCache = context.createImageData(1, 1);
314
+ }
315
+ if (typeof r === "object") {
316
+ pixelDataCache.data[0] = r.R;
317
+ pixelDataCache.data[1] = r.G;
318
+ pixelDataCache.data[2] = r.B;
319
+ } else {
320
+ pixelDataCache.data[0] = r;
321
+ pixelDataCache.data[1] = g || 0;
322
+ pixelDataCache.data[2] = b || 0;
323
+ }
324
+ pixelDataCache.data[3] = 255;
325
+ context.putImageData(pixelDataCache, x, y);
326
+ }
327
+ __name(setRGB, "setRGB");
328
+ function setRGBA(context, x, y, r, g, b, a) {
329
+ if (pixelDataCache == null) {
330
+ pixelDataCache = context.createImageData(1, 1);
331
+ }
332
+ if (typeof r === "object") {
333
+ pixelDataCache.data[0] = r.R;
334
+ pixelDataCache.data[1] = r.G;
335
+ pixelDataCache.data[2] = r.B;
336
+ pixelDataCache.data[3] = r.A != null ? r.A : 255;
337
+ } else {
338
+ pixelDataCache.data[0] = r;
339
+ pixelDataCache.data[1] = g || 0;
340
+ pixelDataCache.data[2] = b || 0;
341
+ pixelDataCache.data[3] = a ?? 255;
342
+ }
343
+ context.putImageData(pixelDataCache, x, y);
344
+ }
345
+ __name(setRGBA, "setRGBA");
346
+ function getRGB(context, x, y, result = {
347
+ R: 0,
348
+ G: 0,
349
+ B: 0
350
+ }) {
351
+ const d = context.getImageData(x, y, 1, 1);
352
+ result.R = d.data[0];
353
+ result.G = d.data[1];
354
+ result.B = d.data[2];
355
+ return result;
356
+ }
357
+ __name(getRGB, "getRGB");
358
+ function getRGBA(context, x, y, result = {
359
+ R: 0,
360
+ G: 0,
361
+ B: 0,
362
+ A: 255
363
+ }) {
364
+ const d = context.getImageData(x, y, 1, 1);
365
+ result.R = d.data[0];
366
+ result.G = d.data[1];
367
+ result.B = d.data[2];
368
+ result.A = d.data[3];
369
+ return result;
370
+ }
371
+ __name(getRGBA, "getRGBA");
372
+
373
+ // src/core/shapes.ts
374
+ function drawPolyline(context, state, args) {
375
+ if (Array.isArray(args[0])) {
376
+ args = args[0];
377
+ }
378
+ context.globalAlpha = state.alpha;
379
+ context.lineWidth = state.line_width;
380
+ if (args.length < 4) {
381
+ return;
382
+ }
383
+ const len = Math.floor(args.length / 2);
384
+ const transform = initDrawOp(context, state, 0, 0, false);
385
+ context.beginPath();
386
+ context.moveTo(args[0], args[1]);
387
+ for (let i = 1; i <= len - 1; i++) {
388
+ context.lineTo(args[i * 2], args[i * 2 + 1]);
389
+ }
390
+ context.stroke();
391
+ if (transform) {
392
+ closeDrawOp(context);
393
+ }
394
+ }
395
+ __name(drawPolyline, "drawPolyline");
396
+ function drawPolygon(context, state, args) {
397
+ if (Array.isArray(args[0])) {
398
+ args = args[0];
399
+ }
400
+ context.globalAlpha = state.alpha;
401
+ context.lineWidth = state.line_width;
402
+ if (args.length < 4) {
403
+ return;
404
+ }
405
+ const len = Math.floor(args.length / 2);
406
+ const transform = initDrawOp(context, state, 0, 0, false);
407
+ context.beginPath();
408
+ context.moveTo(args[0], args[1]);
409
+ for (let i = 1; i <= len - 1; i++) {
410
+ context.lineTo(args[i * 2], args[i * 2 + 1]);
411
+ }
412
+ context.closePath();
413
+ context.stroke();
414
+ if (transform) {
415
+ closeDrawOp(context);
416
+ }
417
+ }
418
+ __name(drawPolygon, "drawPolygon");
419
+ function fillPolygon(context, state, args) {
420
+ if (Array.isArray(args[0])) {
421
+ args = args[0];
422
+ }
423
+ context.globalAlpha = state.alpha;
424
+ if (args.length < 4) {
425
+ return;
426
+ }
427
+ const len = Math.floor(args.length / 2);
428
+ const transform = initDrawOp(context, state, 0, 0, false);
429
+ context.beginPath();
430
+ context.moveTo(args[0], args[1]);
431
+ for (let i = 1; i <= len - 1; i++) {
432
+ context.lineTo(args[i * 2], args[i * 2 + 1]);
433
+ }
434
+ context.fill();
435
+ if (transform) {
436
+ closeDrawOp(context);
437
+ }
438
+ }
439
+ __name(fillPolygon, "fillPolygon");
440
+ function drawQuadCurve(context, state, args) {
441
+ if (Array.isArray(args[0])) {
442
+ args = args[0];
443
+ }
444
+ context.globalAlpha = state.alpha;
445
+ context.lineWidth = state.line_width;
446
+ if (args.length < 4) {
447
+ return;
448
+ }
449
+ const transform = initDrawOp(context, state, 0, 0, false);
450
+ context.beginPath();
451
+ context.moveTo(args[0], args[1]);
452
+ let index = 2;
453
+ while (index <= args.length - 4) {
454
+ context.quadraticCurveTo(args[index], args[index + 1], args[index + 2], args[index + 3]);
455
+ index += 4;
456
+ }
457
+ context.stroke();
458
+ if (transform) {
459
+ closeDrawOp(context);
460
+ }
461
+ }
462
+ __name(drawQuadCurve, "drawQuadCurve");
463
+ function drawBezierCurve(context, state, args) {
464
+ if (Array.isArray(args[0])) {
465
+ args = args[0];
466
+ }
467
+ context.globalAlpha = state.alpha;
468
+ context.lineWidth = state.line_width;
469
+ if (args.length < 4) {
470
+ return;
471
+ }
472
+ const transform = initDrawOp(context, state, 0, 0, false);
473
+ context.beginPath();
474
+ context.moveTo(args[0], args[1]);
475
+ let index = 2;
476
+ while (index <= args.length - 6) {
477
+ context.bezierCurveTo(args[index], args[index + 1], args[index + 2], args[index + 3], args[index + 4], args[index + 5]);
478
+ index += 6;
479
+ }
480
+ context.stroke();
481
+ if (transform) {
482
+ closeDrawOp(context);
483
+ }
484
+ }
485
+ __name(drawBezierCurve, "drawBezierCurve");
486
+ function drawArc(context, state, x, y, radius, angle1, angle2, ccw) {
487
+ context.globalAlpha = state.alpha;
488
+ context.lineWidth = state.line_width;
489
+ if (initDrawOp(context, state, x, y)) {
490
+ context.beginPath();
491
+ context.arc(0, 0, radius, angle1 / 180 * Math.PI, angle2 / 180 * Math.PI, ccw);
492
+ context.stroke();
493
+ closeDrawOp(context);
494
+ } else {
495
+ context.beginPath();
496
+ context.arc(x, y, radius, angle1 / 180 * Math.PI, angle2 / 180 * Math.PI, ccw);
497
+ context.stroke();
498
+ }
499
+ }
500
+ __name(drawArc, "drawArc");
501
+ function fillArc(context, state, x, y, radius, angle1, angle2, ccw) {
502
+ context.globalAlpha = state.alpha;
503
+ if (initDrawOp(context, state, x, y)) {
504
+ context.beginPath();
505
+ context.arc(0, 0, radius, angle1 / 180 * Math.PI, angle2 / 180 * Math.PI, ccw);
506
+ context.fill();
507
+ closeDrawOp(context);
508
+ } else {
509
+ context.beginPath();
510
+ context.arc(x, y, radius, angle1 / 180 * Math.PI, angle2 / 180 * Math.PI, ccw);
511
+ context.fill();
512
+ }
513
+ }
514
+ __name(fillArc, "fillArc");
515
+
516
+ // src/core/sprite-rendering.ts
517
+ function getSpriteFrame(sprite) {
518
+ if (typeof sprite === "object" && "canvas" in sprite && !("frames" in sprite)) {
519
+ return sprite.canvas || sprite.image || null;
520
+ }
521
+ if (typeof sprite === "object" && sprite.frames && sprite.frames.length > 0) {
522
+ if (!sprite.ready) {
523
+ return null;
524
+ }
525
+ if (sprite.frames.length > 1) {
526
+ if (!sprite.animation_start || sprite.animation_start === 0) {
527
+ sprite.animation_start = Date.now();
528
+ }
529
+ const dt = 1e3 / (sprite.fps || 5);
530
+ const frame = Math.floor((Date.now() - sprite.animation_start) / dt) % sprite.frames.length;
531
+ if (frame >= 0 && frame < sprite.frames.length) {
532
+ return sprite.frames[frame].canvas;
533
+ }
534
+ return sprite.frames[0].canvas;
535
+ }
536
+ return sprite.frames[0].canvas;
537
+ }
538
+ return null;
539
+ }
540
+ __name(getSpriteFrame, "getSpriteFrame");
541
+ function drawSprite(context, state, sprite, x, y, w, h) {
542
+ const canvas = getSpriteFrame(sprite);
543
+ if (canvas == null) {
544
+ return;
545
+ }
546
+ if (w == null) {
547
+ w = canvas.width;
548
+ }
549
+ if (!h) {
550
+ h = w / canvas.width * canvas.height;
551
+ }
552
+ context.globalAlpha = state.alpha;
553
+ context.imageSmoothingEnabled = !state.pixelated;
554
+ if (initDrawOp(context, state, x, y)) {
555
+ context.drawImage(canvas, -w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h);
556
+ closeDrawOp(context);
557
+ } else {
558
+ context.drawImage(canvas, x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h);
559
+ }
560
+ }
561
+ __name(drawSprite, "drawSprite");
562
+ function drawSpritePart(context, state, sprite, sx, sy, sw, sh, x, y, w, h) {
563
+ const canvas = getSpriteFrame(sprite);
564
+ if (canvas == null) {
565
+ return;
566
+ }
567
+ if (w == null) {
568
+ w = canvas.width;
569
+ }
570
+ if (!h) {
571
+ h = w / sw * sh;
572
+ }
573
+ context.globalAlpha = state.alpha;
574
+ context.imageSmoothingEnabled = !state.pixelated;
575
+ if (initDrawOp(context, state, x, y)) {
576
+ context.drawImage(canvas, sx, sy, sw, sh, -w / 2 - state.anchor_x * w / 2, -h / 2 + state.anchor_y * h / 2, w, h);
577
+ closeDrawOp(context);
578
+ } else {
579
+ context.drawImage(canvas, sx, sy, sw, sh, x - w / 2 - state.anchor_x * w / 2, y - h / 2 + state.anchor_y * h / 2, w, h);
580
+ }
581
+ }
582
+ __name(drawSpritePart, "drawSpritePart");
583
+
584
+ // src/core/text.ts
585
+ function textWidth(context, state, text, size) {
586
+ context.font = `${size}pt ${state.font}`;
587
+ return context.measureText(text).width;
588
+ }
589
+ __name(textWidth, "textWidth");
590
+ function drawText(context, state, text, x, y, size) {
591
+ context.globalAlpha = state.alpha;
592
+ context.font = `${size}pt ${state.font}`;
593
+ context.textAlign = "center";
594
+ context.textBaseline = "middle";
595
+ const w = context.measureText(text).width;
596
+ const h = size;
597
+ if (initDrawOp(context, state, x, y)) {
598
+ context.fillText(text, 0 - state.anchor_x * w / 2, 0 + state.anchor_y * h / 2);
599
+ closeDrawOp(context);
600
+ } else {
601
+ context.fillText(text, x - state.anchor_x * w / 2, y + state.anchor_y * h / 2);
602
+ }
603
+ }
604
+ __name(drawText, "drawText");
605
+ function drawTextOutline(context, state, text, x, y, size) {
606
+ context.globalAlpha = state.alpha;
607
+ context.font = `${size}pt ${state.font}`;
608
+ context.lineWidth = state.line_width;
609
+ context.textAlign = "center";
610
+ context.textBaseline = "middle";
611
+ const w = context.measureText(text).width;
612
+ const h = size;
613
+ if (initDrawOp(context, state, x, y)) {
614
+ context.strokeText(text, 0 - state.anchor_x * w / 2, 0 + state.anchor_y * h / 2);
615
+ closeDrawOp(context);
616
+ } else {
617
+ context.strokeText(text, x - state.anchor_x * w / 2, y + state.anchor_y * h / 2);
618
+ }
619
+ }
620
+ __name(drawTextOutline, "drawTextOutline");
621
+
622
+ // src/core/transform.ts
623
+ function setTranslation(state, translation_x, translation_y) {
624
+ state.translation_x = translation_x;
625
+ state.translation_y = translation_y;
626
+ if (!isFinite(state.translation_x)) {
627
+ state.translation_x = 0;
628
+ }
629
+ if (!isFinite(state.translation_y)) {
630
+ state.translation_y = 0;
631
+ }
632
+ updateImageTransform(state);
633
+ }
634
+ __name(setTranslation, "setTranslation");
635
+ function setScale(state, scale_x, scale_y) {
636
+ state.scale_x = scale_x;
637
+ state.scale_y = scale_y;
638
+ if (!isFinite(state.scale_x) || state.scale_x === 0) {
639
+ state.scale_x = 1;
640
+ }
641
+ if (!isFinite(state.scale_y) || state.scale_y === 0) {
642
+ state.scale_y = 1;
643
+ }
644
+ updateImageTransform(state);
645
+ }
646
+ __name(setScale, "setScale");
647
+ function setRotation(state, rotation) {
648
+ state.rotation = rotation;
649
+ if (!isFinite(state.rotation)) {
650
+ state.rotation = 0;
651
+ }
652
+ updateImageTransform(state);
653
+ }
654
+ __name(setRotation, "setRotation");
655
+ function setDrawAnchor(state, anchor_x, anchor_y) {
656
+ state.anchor_x = anchor_x;
657
+ state.anchor_y = anchor_y;
658
+ if (typeof state.anchor_x !== "number") {
659
+ state.anchor_x = 0;
660
+ }
661
+ if (typeof state.anchor_y !== "number") {
662
+ state.anchor_y = 0;
663
+ }
664
+ }
665
+ __name(setDrawAnchor, "setDrawAnchor");
666
+ function setDrawRotation(state, object_rotation) {
667
+ state.object_rotation = object_rotation;
668
+ }
669
+ __name(setDrawRotation, "setDrawRotation");
670
+ function setDrawScale(state, object_scale_x, object_scale_y) {
671
+ state.object_scale_x = object_scale_x;
672
+ state.object_scale_y = object_scale_y;
673
+ }
674
+ __name(setDrawScale, "setDrawScale");
675
+
676
+ // src/core/image.ts
677
+ function splitColorArg(args) {
678
+ if (args.length > 0 && args.length % 2 === 1 && typeof args[args.length - 1] === "string") {
679
+ return {
680
+ points: args.slice(0, -1),
681
+ color: args[args.length - 1]
682
+ };
683
+ }
684
+ return {
685
+ points: args,
686
+ color: void 0
687
+ };
688
+ }
689
+ __name(splitColorArg, "splitColorArg");
690
+ var Image = class {
691
+ static {
692
+ __name(this, "Image");
693
+ }
694
+ _canvas;
695
+ _context = null;
696
+ _image = null;
697
+ _state;
698
+ _centered;
699
+ width;
700
+ height;
701
+ constructor(widthOrImage, height, centered = false) {
702
+ this._centered = centered;
703
+ if (typeof widthOrImage === "object" && "naturalWidth" in widthOrImage) {
704
+ this._image = widthOrImage;
705
+ this.width = this._image.width;
706
+ this.height = this._image.height;
707
+ } else if (typeof widthOrImage === "object" && "getContext" in widthOrImage) {
708
+ this._canvas = widthOrImage;
709
+ this.width = this._canvas.width;
710
+ this.height = this._canvas.height;
711
+ } else {
712
+ this._canvas = document.createElement("canvas");
713
+ this.width = Math.round(widthOrImage);
714
+ this.height = Math.round(height || widthOrImage);
715
+ this._canvas.width = this.width;
716
+ this._canvas.height = this.height;
717
+ }
718
+ }
719
+ /**
720
+ * Initialize or get the canvas context
721
+ * Matches msImage initContext behavior exactly
722
+ */
723
+ initContext() {
724
+ if (this._context) {
725
+ return this._context;
726
+ }
727
+ if (!this._canvas && this._image) {
728
+ this._canvas = document.createElement("canvas");
729
+ this._canvas.width = this._image.width;
730
+ this._canvas.height = this._image.height;
731
+ this._context = this._canvas.getContext("2d");
732
+ this._context.drawImage(this._image, 0, 0);
733
+ this._image = null;
734
+ }
735
+ this._context = this._canvas.getContext("2d");
736
+ if (this._context) {
737
+ this._context.lineCap = "round";
738
+ }
739
+ if (!this._state) {
740
+ this._state = createDefaultContextState(this._centered);
741
+ if (this._centered) {
742
+ this._state.translation_x = this.width / 2;
743
+ this._state.translation_y = this.height / 2;
744
+ }
745
+ }
746
+ return this._context;
747
+ }
748
+ /**
749
+ * Get the canvas element
750
+ */
751
+ get canvas() {
752
+ this.initContext();
753
+ return this._canvas;
754
+ }
755
+ /**
756
+ * Get the image element (if still available)
757
+ */
758
+ get image() {
759
+ return this._image;
760
+ }
761
+ /**
762
+ * Get the rendering context
763
+ */
764
+ get context() {
765
+ return this._context;
766
+ }
767
+ // ===== PIXEL OPERATIONS =====
768
+ setRGB(x, y, r, g, b) {
769
+ this.initContext();
770
+ setRGB(this._context, x, y, r, g, b);
771
+ }
772
+ setRGBA(x, y, r, g, b, a) {
773
+ this.initContext();
774
+ setRGBA(this._context, x, y, r, g, b, a);
775
+ }
776
+ getRGB(x, y, result) {
777
+ this.initContext();
778
+ return getRGB(this._context, x, y, result);
779
+ }
780
+ getRGBA(x, y, result) {
781
+ this.initContext();
782
+ return getRGBA(this._context, x, y, result);
783
+ }
784
+ // ===== CANVAS OPERATIONS =====
785
+ clear(color) {
786
+ this.initContext();
787
+ const c = this._context.fillStyle;
788
+ const s = this._context.strokeStyle;
789
+ const blending_save = this._context.globalCompositeOperation;
790
+ this._context.globalAlpha = 1;
791
+ this._context.globalCompositeOperation = "source-over";
792
+ if (color != null) {
793
+ this.setColor(color);
794
+ } else {
795
+ this._context.fillStyle = "#000";
796
+ }
797
+ this._context.fillRect(0, 0, this.width, this.height);
798
+ this._context.fillStyle = c;
799
+ this._context.strokeStyle = s;
800
+ this._context.globalCompositeOperation = blending_save;
801
+ }
802
+ // ===== COLOR OPERATIONS =====
803
+ setColor(color) {
804
+ this.initContext();
805
+ setColor(this._context, color);
806
+ }
807
+ setAlpha(alpha) {
808
+ this.initContext();
809
+ this._state.alpha = alpha;
810
+ }
811
+ setPixelated(pixelated) {
812
+ this.initContext();
813
+ this._state.pixelated = pixelated;
814
+ }
815
+ setBlending(blending) {
816
+ this.initContext();
817
+ const mode = BLENDING_MODES[blending || "normal"] || "source-over";
818
+ this._context.globalCompositeOperation = mode;
819
+ }
820
+ setLineWidth(line_width) {
821
+ this.initContext();
822
+ this._state.line_width = line_width;
823
+ }
824
+ setLineDash(dash) {
825
+ this.initContext();
826
+ if (!Array.isArray(dash)) {
827
+ this._context.setLineDash([]);
828
+ } else {
829
+ this._context.setLineDash(dash);
830
+ }
831
+ }
832
+ setLinearGradient(x1, y1, x2, y2, c1, c2) {
833
+ this.initContext();
834
+ setLinearGradient(this._context, x1, y1, x2, y2, c1, c2);
835
+ }
836
+ setRadialGradient(x, y, radius, c1, c2) {
837
+ this.initContext();
838
+ setRadialGradient(this._context, x, y, radius, c1, c2);
839
+ }
840
+ // ===== FONT OPERATIONS =====
841
+ setFont(font) {
842
+ this.initContext();
843
+ this._state.font = font || "Verdana";
844
+ this.loadFont(this._state.font);
845
+ }
846
+ loadFont(font) {
847
+ return loadFont(font);
848
+ }
849
+ isFontReady(font) {
850
+ return isFontReady(font || this._state?.font || "Verdana");
851
+ }
852
+ // ===== TRANSFORM OPERATIONS =====
853
+ setTranslation(translation_x, translation_y) {
854
+ this.initContext();
855
+ setTranslation(this._state, translation_x, translation_y);
856
+ }
857
+ setScale(scale_x, scale_y) {
858
+ this.initContext();
859
+ setScale(this._state, scale_x, scale_y);
860
+ }
861
+ setRotation(rotation) {
862
+ this.initContext();
863
+ setRotation(this._state, rotation);
864
+ }
865
+ setDrawAnchor(anchor_x, anchor_y) {
866
+ this.initContext();
867
+ setDrawAnchor(this._state, anchor_x, anchor_y);
868
+ }
869
+ setDrawRotation(object_rotation) {
870
+ this.initContext();
871
+ setDrawRotation(this._state, object_rotation);
872
+ }
873
+ setDrawScale(object_scale_x, object_scale_y = object_scale_x) {
874
+ this.initContext();
875
+ setDrawScale(this._state, object_scale_x, object_scale_y);
876
+ }
877
+ // ===== DRAWING OPERATIONS =====
878
+ fillRect(x, y, w, h, color) {
879
+ this.initContext();
880
+ this.setColor(color);
881
+ fillRect(this._context, this._state, x, y, w, h);
882
+ }
883
+ fillRoundRect(x, y, w, h, round = 10, color) {
884
+ this.initContext();
885
+ this.setColor(color);
886
+ fillRoundRect(this._context, this._state, x, y, w, h, round);
887
+ }
888
+ fillRound(x, y, w, h, color) {
889
+ this.initContext();
890
+ this.setColor(color);
891
+ fillRound(this._context, this._state, x, y, w, h);
892
+ }
893
+ drawRect(x, y, w, h, color) {
894
+ this.initContext();
895
+ this.setColor(color);
896
+ drawRect(this._context, this._state, x, y, w, h);
897
+ }
898
+ drawRoundRect(x, y, w, h, round = 10, color) {
899
+ this.initContext();
900
+ this.setColor(color);
901
+ drawRoundRect(this._context, this._state, x, y, w, h, round);
902
+ }
903
+ drawRound(x, y, w, h, color) {
904
+ this.initContext();
905
+ this.setColor(color);
906
+ drawRound(this._context, this._state, x, y, w, h);
907
+ }
908
+ drawLine(x1, y1, x2, y2, color) {
909
+ this.initContext();
910
+ this.setColor(color);
911
+ drawLine(this._context, this._state, x1, y1, x2, y2);
912
+ }
913
+ // ===== SHAPE OPERATIONS =====
914
+ drawPolyline(...args) {
915
+ this.initContext();
916
+ const { points, color } = splitColorArg(args);
917
+ this.setColor(color);
918
+ drawPolyline(this._context, this._state, points);
919
+ }
920
+ drawPolygon(...args) {
921
+ this.initContext();
922
+ const { points, color } = splitColorArg(args);
923
+ this.setColor(color);
924
+ drawPolygon(this._context, this._state, points);
925
+ }
926
+ fillPolygon(...args) {
927
+ this.initContext();
928
+ const { points, color } = splitColorArg(args);
929
+ this.setColor(color);
930
+ fillPolygon(this._context, this._state, points);
931
+ }
932
+ drawQuadCurve(...args) {
933
+ this.initContext();
934
+ const { points, color } = splitColorArg(args);
935
+ this.setColor(color);
936
+ drawQuadCurve(this._context, this._state, points);
937
+ }
938
+ drawBezierCurve(...args) {
939
+ this.initContext();
940
+ const { points, color } = splitColorArg(args);
941
+ this.setColor(color);
942
+ drawBezierCurve(this._context, this._state, points);
943
+ }
944
+ drawArc(x, y, radius, angle1, angle2, ccw, color) {
945
+ this.initContext();
946
+ this.setColor(color);
947
+ drawArc(this._context, this._state, x, y, radius, angle1, angle2, ccw);
948
+ }
949
+ fillArc(x, y, radius, angle1, angle2, ccw, color) {
950
+ this.initContext();
951
+ this.setColor(color);
952
+ fillArc(this._context, this._state, x, y, radius, angle1, angle2, ccw);
953
+ }
954
+ // ===== TEXT OPERATIONS =====
955
+ textWidth(text, size) {
956
+ this.initContext();
957
+ return textWidth(this._context, this._state, text, size);
958
+ }
959
+ drawText(text, x, y, size, color) {
960
+ this.initContext();
961
+ this.setColor(color);
962
+ drawText(this._context, this._state, text, x, y, size);
963
+ }
964
+ drawTextOutline(text, x, y, size, color) {
965
+ this.initContext();
966
+ this.setColor(color);
967
+ drawTextOutline(this._context, this._state, text, x, y, size);
968
+ }
969
+ // ===== SPRITE RENDERING =====
970
+ drawSprite(sprite, x, y, w, h) {
971
+ this.initContext();
972
+ drawSprite(this._context, this._state, sprite, x, y, w, h);
973
+ }
974
+ drawImage(sprite, x, y, w, h) {
975
+ this.drawSprite(sprite, x, y, w, h);
976
+ }
977
+ drawSpritePart(sprite, sx, sy, sw, sh, x, y, w, h) {
978
+ this.initContext();
979
+ drawSpritePart(this._context, this._state, sprite, sx, sy, sw, sh, x, y, w, h);
980
+ }
981
+ drawImagePart(sprite, sx, sy, sw, sh, x, y, w, h) {
982
+ this.drawSpritePart(sprite, sx, sy, sw, sh, x, y, w, h);
983
+ }
984
+ // ===== MAP RENDERING =====
985
+ drawMap(map, x, y, w, h) {
986
+ this.initContext();
987
+ drawMap(this._context, this._state, map, x, y, w, h);
988
+ }
989
+ };
990
+ export {
991
+ Image
992
+ };
993
+ //# sourceMappingURL=image.mjs.map