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