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