melonjs 15.14.0 → 16.0.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 (187) hide show
  1. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +2 -2
  2. package/dist/melonjs.mjs/_virtual/earcut.js +2 -2
  3. package/dist/melonjs.mjs/_virtual/howler.js +2 -2
  4. package/dist/melonjs.mjs/_virtual/index.js +2 -2
  5. package/dist/melonjs.mjs/application/application.js +2 -2
  6. package/dist/melonjs.mjs/application/header.js +2 -2
  7. package/dist/melonjs.mjs/application/resize.js +2 -2
  8. package/dist/melonjs.mjs/application/settings.js +5 -3
  9. package/dist/melonjs.mjs/audio/audio.js +2 -2
  10. package/dist/melonjs.mjs/camera/camera2d.js +2 -2
  11. package/dist/melonjs.mjs/const.js +2 -2
  12. package/dist/melonjs.mjs/geometries/ellipse.js +6 -8
  13. package/dist/melonjs.mjs/geometries/line.js +2 -2
  14. package/dist/melonjs.mjs/geometries/path2d.js +131 -5
  15. package/dist/melonjs.mjs/geometries/point.js +7 -9
  16. package/dist/melonjs.mjs/geometries/poly.js +4 -14
  17. package/dist/melonjs.mjs/geometries/rectangle.js +2 -2
  18. package/dist/melonjs.mjs/geometries/roundrect.js +2 -2
  19. package/dist/melonjs.mjs/geometries/toarccanvas.js +103 -0
  20. package/dist/melonjs.mjs/index.js +3 -3
  21. package/dist/melonjs.mjs/input/gamepad.js +2 -2
  22. package/dist/melonjs.mjs/input/input.js +2 -2
  23. package/dist/melonjs.mjs/input/keyboard.js +2 -2
  24. package/dist/melonjs.mjs/input/pointer.js +2 -2
  25. package/dist/melonjs.mjs/input/pointerevent.js +2 -2
  26. package/dist/melonjs.mjs/lang/console.js +2 -2
  27. package/dist/melonjs.mjs/lang/deprecated.js +2 -2
  28. package/dist/melonjs.mjs/level/level.js +2 -2
  29. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +2 -2
  30. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +2 -2
  31. package/dist/melonjs.mjs/level/tiled/TMXObject.js +2 -2
  32. package/dist/melonjs.mjs/level/tiled/TMXTile.js +2 -2
  33. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +2 -2
  34. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +2 -2
  35. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +2 -2
  36. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +2 -2
  37. package/dist/melonjs.mjs/level/tiled/constants.js +2 -2
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -2
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +2 -2
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +2 -2
  43. package/dist/melonjs.mjs/level/tiled/renderer/autodetect.js +2 -2
  44. package/dist/melonjs.mjs/loader/cache.js +2 -2
  45. package/dist/melonjs.mjs/loader/loader.js +2 -2
  46. package/dist/melonjs.mjs/loader/loadingscreen.js +4 -4
  47. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +3 -4
  48. package/dist/melonjs.mjs/loader/parsers/binary.js +15 -22
  49. package/dist/melonjs.mjs/loader/parsers/fetchdata.js +62 -0
  50. package/dist/melonjs.mjs/loader/parsers/fontface.js +2 -2
  51. package/dist/melonjs.mjs/loader/parsers/image.js +47 -11
  52. package/dist/melonjs.mjs/loader/parsers/json.js +14 -31
  53. package/dist/melonjs.mjs/loader/parsers/script.js +2 -2
  54. package/dist/melonjs.mjs/loader/parsers/tmx.js +48 -74
  55. package/dist/melonjs.mjs/loader/settings.js +2 -2
  56. package/dist/melonjs.mjs/math/color.js +2 -2
  57. package/dist/melonjs.mjs/math/math.js +13 -3
  58. package/dist/melonjs.mjs/math/matrix2.js +2 -2
  59. package/dist/melonjs.mjs/math/matrix3.js +2 -2
  60. package/dist/melonjs.mjs/math/observable_vector2.js +2 -2
  61. package/dist/melonjs.mjs/math/observable_vector3.js +2 -2
  62. package/dist/melonjs.mjs/math/vector2.js +7 -9
  63. package/dist/melonjs.mjs/math/vector3.js +6 -10
  64. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/arraymultimap.js +2 -2
  65. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/multimap.js +2 -2
  66. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +2 -2
  67. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +2 -2
  68. package/dist/melonjs.mjs/node_modules/eventemitter3/index2.js +2 -2
  69. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +2 -2
  70. package/dist/melonjs.mjs/particles/emitter.js +2 -2
  71. package/dist/melonjs.mjs/particles/particle.js +2 -2
  72. package/dist/melonjs.mjs/particles/settings.js +3 -5
  73. package/dist/melonjs.mjs/physics/body.js +6 -8
  74. package/dist/melonjs.mjs/physics/bounds.js +2 -2
  75. package/dist/melonjs.mjs/physics/collision.js +3 -5
  76. package/dist/melonjs.mjs/physics/detector.js +2 -2
  77. package/dist/melonjs.mjs/physics/quadtree.js +2 -2
  78. package/dist/melonjs.mjs/physics/response.js +2 -2
  79. package/dist/melonjs.mjs/physics/sat.js +2 -2
  80. package/dist/melonjs.mjs/physics/world.js +2 -2
  81. package/dist/melonjs.mjs/plugin/plugin.js +5 -5
  82. package/dist/melonjs.mjs/renderable/collectable.js +2 -2
  83. package/dist/melonjs.mjs/renderable/colorlayer.js +2 -2
  84. package/dist/melonjs.mjs/renderable/container.js +2 -2
  85. package/dist/melonjs.mjs/renderable/draggable.js +2 -2
  86. package/dist/melonjs.mjs/renderable/dragndrop.js +2 -2
  87. package/dist/melonjs.mjs/renderable/entity/entity.js +2 -2
  88. package/dist/melonjs.mjs/renderable/imagelayer.js +13 -12
  89. package/dist/melonjs.mjs/renderable/light2d.js +2 -2
  90. package/dist/melonjs.mjs/renderable/nineslicesprite.js +2 -2
  91. package/dist/melonjs.mjs/renderable/renderable.js +20 -2
  92. package/dist/melonjs.mjs/renderable/sprite.js +2 -2
  93. package/dist/melonjs.mjs/renderable/text/bitmaptext.js +2 -2
  94. package/dist/melonjs.mjs/renderable/text/bitmaptextdata.js +2 -2
  95. package/dist/melonjs.mjs/renderable/text/glyph.js +2 -2
  96. package/dist/melonjs.mjs/renderable/text/text.js +2 -2
  97. package/dist/melonjs.mjs/renderable/text/textmetrics.js +2 -2
  98. package/dist/melonjs.mjs/renderable/text/textstyle.js +2 -2
  99. package/dist/melonjs.mjs/renderable/trigger.js +2 -2
  100. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +2 -2
  101. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +2 -2
  102. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +2 -2
  103. package/dist/melonjs.mjs/state/stage.js +2 -2
  104. package/dist/melonjs.mjs/state/state.js +3 -4
  105. package/dist/melonjs.mjs/system/device.js +2 -2
  106. package/dist/melonjs.mjs/system/dom.js +2 -2
  107. package/dist/melonjs.mjs/system/event.js +2 -2
  108. package/dist/melonjs.mjs/system/platform.js +2 -2
  109. package/dist/melonjs.mjs/system/pooling.js +3 -20
  110. package/dist/melonjs.mjs/system/save.js +2 -2
  111. package/dist/melonjs.mjs/system/timer.js +3 -19
  112. package/dist/melonjs.mjs/tweens/easing.js +2 -2
  113. package/dist/melonjs.mjs/tweens/interpolation.js +2 -2
  114. package/dist/melonjs.mjs/tweens/tween.js +2 -2
  115. package/dist/melonjs.mjs/utils/agent.js +2 -2
  116. package/dist/melonjs.mjs/utils/array.js +2 -2
  117. package/dist/melonjs.mjs/utils/file.js +2 -2
  118. package/dist/melonjs.mjs/utils/function.js +2 -2
  119. package/dist/melonjs.mjs/utils/string.js +2 -2
  120. package/dist/melonjs.mjs/utils/utils.js +2 -2
  121. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +2 -2
  122. package/dist/melonjs.mjs/video/renderer.js +14 -17
  123. package/dist/melonjs.mjs/video/texture/atlas.js +28 -14
  124. package/dist/melonjs.mjs/video/texture/cache.js +3 -4
  125. package/dist/melonjs.mjs/video/texture/canvas_texture.js +16 -19
  126. package/dist/melonjs.mjs/video/utils/autodetect.js +2 -2
  127. package/dist/melonjs.mjs/video/video.js +2 -2
  128. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +2 -2
  129. package/dist/melonjs.mjs/video/webgl/compositors/compositor.js +2 -2
  130. package/dist/melonjs.mjs/video/webgl/compositors/primitive_compositor.js +2 -2
  131. package/dist/melonjs.mjs/video/webgl/compositors/quad_compositor.js +5 -3
  132. package/dist/melonjs.mjs/video/webgl/glshader.js +2 -2
  133. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +2 -2
  134. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +2 -2
  135. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +2 -2
  136. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +2 -2
  137. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +2 -2
  138. package/dist/melonjs.mjs/video/webgl/utils/precision.js +2 -2
  139. package/dist/melonjs.mjs/video/webgl/utils/program.js +2 -2
  140. package/dist/melonjs.mjs/video/webgl/utils/string.js +2 -2
  141. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +2 -2
  142. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +2 -2
  143. package/dist/melonjs.module.js +1187 -325
  144. package/dist/types/application/application.d.ts +1 -1
  145. package/dist/types/application/settings.d.ts +5 -0
  146. package/dist/types/geometries/path2d.d.ts +24 -0
  147. package/dist/types/geometries/rectangle.d.ts +4 -4
  148. package/dist/types/geometries/roundrect.d.ts +1 -1
  149. package/dist/types/geometries/toarccanvas.d.ts +10 -0
  150. package/dist/types/loader/parsers/binary.d.ts +0 -8
  151. package/dist/types/loader/parsers/fetchdata.d.ts +15 -0
  152. package/dist/types/loader/parsers/tmx.d.ts +1 -0
  153. package/dist/types/math/color.d.ts +4 -4
  154. package/dist/types/math/math.d.ts +6 -0
  155. package/dist/types/math/observable_vector2.d.ts +2 -2
  156. package/dist/types/math/observable_vector3.d.ts +3 -3
  157. package/dist/types/physics/bounds.d.ts +4 -4
  158. package/dist/types/renderable/entity/entity.d.ts +1 -1
  159. package/dist/types/renderable/imagelayer.d.ts +2 -2
  160. package/dist/types/renderable/renderable.d.ts +9 -3
  161. package/dist/types/renderable/text/bitmaptext.d.ts +1 -1
  162. package/dist/types/video/renderer.d.ts +11 -23
  163. package/dist/types/video/texture/canvas_texture.d.ts +11 -23
  164. package/package.json +18 -17
  165. package/src/application/settings.js +3 -1
  166. package/src/geometries/ellipse.js +4 -6
  167. package/src/geometries/path2d.js +129 -4
  168. package/src/geometries/point.js +5 -7
  169. package/src/geometries/poly.js +2 -12
  170. package/src/geometries/toarccanvas.js +94 -0
  171. package/src/loader/parsers/binary.js +13 -56
  172. package/src/loader/parsers/fetchdata.js +53 -0
  173. package/src/loader/parsers/image.js +45 -12
  174. package/src/loader/parsers/json.js +12 -30
  175. package/src/loader/parsers/tmx.js +46 -72
  176. package/src/math/math.js +10 -0
  177. package/src/math/vector2.js +5 -7
  178. package/src/math/vector3.js +4 -8
  179. package/src/physics/body.js +4 -6
  180. package/src/plugin/plugin.js +1 -1
  181. package/src/polyfill/index.js +3 -0
  182. package/src/renderable/imagelayer.js +10 -9
  183. package/src/renderable/renderable.js +18 -0
  184. package/src/video/renderer.js +12 -15
  185. package/src/video/texture/atlas.js +26 -12
  186. package/src/video/texture/canvas_texture.js +14 -17
  187. package/src/video/webgl/compositors/quad_compositor.js +3 -1
@@ -1,9 +1,8 @@
1
1
  import * as fileUtil from "../../utils/file.js";
2
- import { ua } from "../../system/platform.js";
3
2
  import level from "../../level/level.js";
4
3
  import * as TMXUtils from "../../level/tiled/TMXUtils.js";
5
4
  import { tmxList } from "../cache.js";
6
- import { nocache, withCredentials } from "../settings.js";
5
+ import { fetchData } from "./fetchdata.js";
7
6
 
8
7
 
9
8
  /**
@@ -11,6 +10,7 @@ import { nocache, withCredentials } from "../settings.js";
11
10
  * @param {loader.Asset} data - asset data
12
11
  * @param {Function} [onload] - function to be called when the asset is loaded
13
12
  * @param {Function} [onerror] - function to be called in case of error
13
+ * @param {Function} [fetchData] - function to use instead of default window.fetch, has some error handling and things
14
14
  * @returns {number} the amount of corresponding resource parsed/preloaded
15
15
  * @ignore
16
16
  */
@@ -33,7 +33,6 @@ export function preloadTMX(tmxData, onload, onerror) {
33
33
  }
34
34
  }
35
35
 
36
-
37
36
  //if the data is in the tmxData object, don't get it via a XMLHTTPRequest
38
37
  if (tmxData.data) {
39
38
  addToTMXList(tmxData.data);
@@ -43,85 +42,60 @@ export function preloadTMX(tmxData, onload, onerror) {
43
42
  return;
44
43
  }
45
44
 
46
- let xmlhttp = new XMLHttpRequest();
47
- // check the data format ('tmx', 'json')
48
- let format = fileUtil.getExtension(tmxData.src);
49
-
50
- if (xmlhttp.overrideMimeType) {
51
- if (format === "json") {
52
- xmlhttp.overrideMimeType("application/json");
53
- }
54
- else {
55
- xmlhttp.overrideMimeType("text/xml");
56
- }
57
- }
45
+ fetchData(tmxData.src, "text")
46
+ .then(response => {
47
+ if (typeof response !== "string") {
48
+ throw new Error("Invalid response type");
49
+ }
58
50
 
59
- xmlhttp.open("GET", tmxData.src + nocache, true);
60
- xmlhttp.withCredentials = withCredentials;
61
- // set the callbacks
62
- xmlhttp.ontimeout = onerror;
63
- xmlhttp.onreadystatechange = function () {
64
- if (xmlhttp.readyState === 4) {
65
- // status = 0 when file protocol is used, or cross-domain origin,
66
- // (With Chrome use "--allow-file-access-from-files --disable-web-security")
67
- if ((xmlhttp.status === 200) || ((xmlhttp.status === 0) && xmlhttp.responseText)) {
68
- let result = null;
51
+ let result;
69
52
 
70
- // parse response
71
- switch (format) {
72
- case "xml":
73
- case "tmx":
74
- case "tsx": {
75
- // ie9 does not fully implement the responseXML
76
- if (ua.match(/msie/i) || !xmlhttp.responseXML) {
77
- if (globalThis.DOMParser) {
78
- // manually create the XML DOM
79
- result = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
80
- } else {
81
- throw new Error("XML file format loading not supported, use the JSON file format instead");
82
- }
83
- }
84
- else {
85
- result = xmlhttp.responseXML;
86
- }
87
- // converts to a JS object
88
- const data = TMXUtils.parse(result);
89
- switch (format) {
90
- case "tmx":
91
- result = data.map;
92
- break;
53
+ switch (fileUtil.getExtension(tmxData.src)) {
54
+ case "xml":
55
+ case "tmx":
56
+ case "tsx": {
57
+ const parser = new DOMParser();
93
58
 
94
- case "tsx":
95
- result = data.tilesets[0];
96
- break;
97
- }
98
- break;
59
+ if (typeof parser.parseFromString === "undefined") {
60
+ throw new Error(
61
+ "XML file format loading supported, use the JSON file format instead"
62
+ );
99
63
  }
100
- case "json":
101
- case "tmj":
102
- case "tsj":
103
- result = JSON.parse(xmlhttp.responseText);
104
- break;
105
64
 
106
- default:
107
- throw new Error("TMX file format " + format + " not supported !");
108
- }
65
+ const xmlDoc = parser.parseFromString(response, "text/xml");
66
+ const data = TMXUtils.parse(xmlDoc);
109
67
 
110
- //set the TMX content
111
- addToTMXList(result);
68
+ switch (fileUtil.getExtension(tmxData.src)) {
69
+ case "tmx":
70
+ result = data.map;
71
+ break;
112
72
 
113
- // fire the callback
114
- if (typeof onload === "function") {
115
- onload();
73
+ case "tsx":
74
+ result = data.tilesets[0];
75
+ break;
76
+ }
77
+ break;
116
78
  }
79
+ case "json":
80
+ case "tmj":
81
+ case "tsj":
82
+ result = JSON.parse(response);
83
+ break;
84
+ default:
85
+ throw new Error(`TMX file format not supported: ${fileUtil.getExtension(tmxData.src)}`);
86
+ }
87
+
88
+ addToTMXList(result);
89
+
90
+ if (typeof onload === "function") {
91
+ onload();
117
92
  }
118
- else if (typeof onerror === "function") {
119
- onerror(tmxData.name);
93
+ })
94
+ .catch(error => {
95
+ if (typeof onerror === "function") {
96
+ onerror(error);
120
97
  }
121
- }
122
- };
123
- // send the request
124
- xmlhttp.send();
98
+ });
125
99
 
126
100
  return 1;
127
101
  }
package/src/math/math.js CHANGED
@@ -207,3 +207,13 @@ export function round(num, dec = 0) {
207
207
  export function toBeCloseTo(expected, actual, precision = 2) {
208
208
  return Math.abs(expected - actual) < (Math.pow(10, -precision) / 2);
209
209
  }
210
+
211
+
212
+ /**
213
+ * Calculates the power of a number.
214
+ * @param {number} n - The number to be raised to the power of 2.
215
+ * @returns {number} The result of raising the number to the power of 2.
216
+ */
217
+ export function pow(n) {
218
+ return Math.pow(n, 2);
219
+ }
@@ -246,18 +246,16 @@ export default class Vector2d {
246
246
  * @param {number} [y]
247
247
  * @returns {boolean}
248
248
  */
249
- equals() {
249
+ equals(...args) {
250
250
  let _x, _y;
251
- if (arguments.length === 2) {
251
+ if (args.length === 2) {
252
252
  // x, y
253
- _x = arguments[0];
254
- _y = arguments[1];
253
+ [_x, _y] = args;
255
254
  } else {
256
255
  // vector
257
- _x = arguments[0].x;
258
- _y = arguments[0].y;
256
+ [_x, _y] = [args[0].x, args[0].y];
259
257
  }
260
- return ((this.x === _x) && (this.y === _y));
258
+ return this.x === _x && this.y === _y;
261
259
  }
262
260
 
263
261
  /**
@@ -262,18 +262,14 @@ export default class Vector3d {
262
262
  * @param {number} [z]
263
263
  * @returns {boolean}
264
264
  */
265
- equals() {
265
+ equals(...args) {
266
266
  let _x, _y, _z;
267
- if (arguments.length >= 2) {
267
+ if (args.length >= 2) {
268
268
  // x, y, z
269
- _x = arguments[0];
270
- _y = arguments[1];
271
- _z = arguments[2];
269
+ [_x, _y, _z] = args;
272
270
  } else {
273
271
  // vector
274
- _x = arguments[0].x;
275
- _y = arguments[0].y;
276
- _z = arguments[0].z;
272
+ [_x, _y, _z] = [args[0].x, args[0].y, args[0].z];
277
273
  }
278
274
 
279
275
  if (typeof _z === "undefined") {
@@ -528,17 +528,15 @@ export default class Body {
528
528
  * // do something
529
529
  * }
530
530
  */
531
- contains() {
531
+ contains(...args) {
532
532
  let _x, _y;
533
533
 
534
- if (arguments.length === 2) {
534
+ if (args.length === 2) {
535
535
  // x, y
536
- _x = arguments[0];
537
- _y = arguments[1];
536
+ [_x, _y] = args;
538
537
  } else {
539
538
  // vector
540
- _x = arguments[0].x;
541
- _y = arguments[0].y;
539
+ [_x, _y] = [args[0].x, args[0].y];
542
540
  }
543
541
 
544
542
  if (this.getBounds().contains(_x, _y)) {
@@ -135,7 +135,7 @@ export function register(plugin, name = plugin.toString().match(/ (\w+)/)[1]) {
135
135
  }
136
136
 
137
137
  // compatibility testing
138
- if (checkVersion(instance.version, version) > 0) {
138
+ if (checkVersion(instance.version, version) < 0) {
139
139
  throw new Error("Plugin version mismatch, expected: " + instance.version + ", got: " + version);
140
140
  }
141
141
 
@@ -8,3 +8,6 @@ import "core-js/es/string/trim-end";
8
8
  // "built-in" polyfills
9
9
  import "./console.js";
10
10
  import "./roundrect.js";
11
+
12
+ // fetch polyfill
13
+ import "whatwg-fetch";
@@ -124,14 +124,12 @@ export default class ImageLayer extends Sprite {
124
124
  event.on(event.VIEWPORT_ONCHANGE, this.updateLayer, this);
125
125
  event.on(event.VIEWPORT_ONRESIZE, this.resize, this);
126
126
  // force a first refresh when the level is loaded
127
- event.once(event.LEVEL_LOADED, () => {
128
- this.updateLayer(game.viewport.pos);
129
- });
127
+ event.on(event.LEVEL_LOADED, this.updateLayer, this);
130
128
  // in case the level is not added to the root container,
131
129
  // the onActivateEvent call happens after the LEVEL_LOADED event
132
130
  // so we need to force a first update
133
131
  if (this.ancestor.root !== true) {
134
- this.updateLayer(game.viewport.pos);
132
+ this.updateLayer();
135
133
  }
136
134
  }
137
135
 
@@ -159,10 +157,12 @@ export default class ImageLayer extends Sprite {
159
157
  * updateLayer function
160
158
  * @ignore
161
159
  */
162
- updateLayer(vpos) {
160
+ updateLayer() {
163
161
  const rx = this.ratio.x,
164
162
  ry = this.ratio.y;
165
163
 
164
+ const viewport = game.viewport;
165
+
166
166
  if (rx === 0 && ry === 0) {
167
167
  // static image
168
168
  return;
@@ -170,8 +170,8 @@ export default class ImageLayer extends Sprite {
170
170
 
171
171
  const width = this.width,
172
172
  height = this.height,
173
- bw = game.viewport.bounds.width,
174
- bh = game.viewport.bounds.height,
173
+ bw = viewport.bounds.width,
174
+ bh = viewport.bounds.height,
175
175
  ax = this.anchorPoint.x,
176
176
  ay = this.anchorPoint.y,
177
177
 
@@ -181,8 +181,8 @@ export default class ImageLayer extends Sprite {
181
181
  * See https://github.com/melonjs/melonJS/issues/741#issuecomment-138431532
182
182
  * for a thorough description of how this works.
183
183
  */
184
- x = ax * (rx - 1) * (bw - game.viewport.width) + this.offset.x - rx * vpos.x,
185
- y = ay * (ry - 1) * (bh - game.viewport.height) + this.offset.y - ry * vpos.y;
184
+ x = ax * (rx - 1) * (bw - viewport.width) + this.offset.x - rx * viewport.pos.x,
185
+ y = ay * (ry - 1) * (bh - viewport.height) + this.offset.y - ry * viewport.pos.y;
186
186
 
187
187
 
188
188
  // Repeat horizontally; start drawing from left boundary
@@ -263,6 +263,7 @@ export default class ImageLayer extends Sprite {
263
263
  // cancel all event subscriptions
264
264
  event.off(event.VIEWPORT_ONCHANGE, this.updateLayer);
265
265
  event.off(event.VIEWPORT_ONRESIZE, this.resize);
266
+ event.off(event.LEVEL_LOADED, this.updateLayer);
266
267
  }
267
268
 
268
269
  /**
@@ -258,6 +258,9 @@ export default class Renderable extends Rect {
258
258
  // viewport flag
259
259
  this._inViewport = false;
260
260
 
261
+ // cache value for the parentApp
262
+ this._parentApp = undefined;
263
+
261
264
  // renderable cache tint value used by the getter/setter
262
265
  this._tint = pool.pull("Color", 255, 255, 255, 1.0);
263
266
 
@@ -265,6 +268,20 @@ export default class Renderable extends Rect {
265
268
  this.setOpacity(1.0);
266
269
  }
267
270
 
271
+ /**
272
+ * returns the parent application (or game) to which this renderable is attached to
273
+ * @return {Application} the parent application or undefined if not attached to any container/app
274
+ */
275
+ get parentApp() {
276
+ if (typeof this._parentApp === "undefined") {
277
+ if (typeof this.ancestor !== "undefined" && typeof this.ancestor.getRootAncestor === "function") {
278
+ // the `app` property is only defined in the world "root" container
279
+ this._parentApp = this.ancestor.getRootAncestor().app;
280
+ }
281
+ }
282
+ return this._parentApp;
283
+ }
284
+
268
285
  /**
269
286
  * Whether the renderable object is floating (i.e. used screen coordinates), or contained in a floating parent container
270
287
  * @see Renderable#floating
@@ -790,6 +807,7 @@ export default class Renderable extends Rect {
790
807
  }
791
808
 
792
809
  this.ancestor = undefined;
810
+ this._parentApp = undefined;
793
811
 
794
812
  // destroy the physic body if defined and is a builtin body object
795
813
  if ((typeof this.body !== "undefined") && (typeof this.body.destroy === "function")) {
@@ -409,19 +409,18 @@ export default class Renderer {
409
409
 
410
410
  /**
411
411
  * creates a Blob object representing the last rendered frame
412
- * @param {object} [options] - An object with the following properties:
413
- * @param {string} [options.type="image/png"] - A string indicating the image format
414
- * @param {number} [options.quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
412
+ * @param {string} [type="image/png"] - A string indicating the image format
413
+ * @param {number} [quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
415
414
  * @returns {Promise} A Promise returning a Blob object representing the last rendered frame
416
415
  * @example
417
416
  * renderer.convertToBlob().then((blob) => console.log(blob));
418
417
  */
419
- toBlob(options) {
418
+ toBlob(type = "image/png", quality) {
420
419
  return new Promise((resolve) => {
421
420
  event.once(event.GAME_AFTER_DRAW, () => {
422
421
  this.canvas.toBlob((blob) => {
423
422
  resolve(blob);
424
- }, options ? options.type : undefined, options ? options.quality : undefined);
423
+ }, type, quality);
425
424
  });
426
425
  });
427
426
  }
@@ -429,18 +428,17 @@ export default class Renderer {
429
428
  /**
430
429
  * creates an ImageBitmap object of the last frame rendered
431
430
  * (not supported by standard Canvas)
432
- * @param {object} [options] - An object with the following properties:
433
- * @param {string} [options.type="image/png"] - A string indicating the image format
434
- * @param {number} [options.quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
431
+ * @param {string} [type="image/png"] - A string indicating the image format
432
+ * @param {number} [quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
435
433
  * @returns {Promise} A Promise returning an ImageBitmap.
436
434
  * @example
437
435
  * renderer.transferToImageBitmap().then((image) => console.log(image));
438
436
  */
439
- toImageBitmap(options) {
437
+ toImageBitmap(type = "image/png", quality) {
440
438
  return new Promise((resolve) => {
441
439
  event.once(event.GAME_AFTER_DRAW, () => {
442
440
  let image = new Image();
443
- image.src = this.canvas.toDataURL(options);
441
+ image.src = this.canvas.toDataURL(type, quality);
444
442
  image.onload = () => {
445
443
  createImageBitmap(image).then((bitmap) => resolve(bitmap));
446
444
  };
@@ -450,17 +448,16 @@ export default class Renderer {
450
448
 
451
449
  /**
452
450
  * returns a data URL containing a representation of the last frame rendered
453
- * @param {object} [options] - An object with the following properties:
454
- * @param {string} [options.type="image/png"] - A string indicating the image format
455
- * @param {number} [options.quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
451
+ * @param {string} [type="image/png"] - A string indicating the image format
452
+ * @param {number} [quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
456
453
  * @returns {Promise} A Promise returning a string containing the requested data URL.
457
454
  * @example
458
455
  * renderer.toDataURL().then((dataURL) => console.log(dataURL));
459
456
  */
460
- toDataURL(options) {
457
+ toDataURL(type = "image/png", quality) {
461
458
  return new Promise((resolve) => {
462
459
  event.once(event.GAME_AFTER_DRAW, () => {
463
- resolve(this.canvas.toDataURL(options));
460
+ resolve(this.canvas.toDataURL(type, quality));
464
461
  });
465
462
  });
466
463
  }
@@ -174,13 +174,25 @@ export class TextureAtlas {
174
174
  if (frame.hasOwnProperty("filename")) {
175
175
  // Source coordinates
176
176
  let s = frame.frame;
177
+ let trimmed = !!frame.trimmed;
178
+
179
+ let trim;
180
+
181
+ if (trimmed) {
182
+ trim = {
183
+ x : frame.spriteSourceSize.x,
184
+ y : frame.spriteSourceSize.y,
185
+ w : frame.spriteSourceSize.w,
186
+ h : frame.spriteSourceSize.h
187
+ };
188
+ }
177
189
 
178
190
  let originX, originY;
179
191
  // Pixel-based offset origin from the top-left of the source frame
180
- let hasTextureAnchorPoint = (frame.spriteSourceSize && frame.sourceSize && frame.pivot);
192
+ let hasTextureAnchorPoint = (frame.sourceSize && frame.pivot);
181
193
  if (hasTextureAnchorPoint) {
182
- originX = (frame.sourceSize.w * frame.pivot.x) - ((frame.trimmed) ? frame.spriteSourceSize.x : 0);
183
- originY = (frame.sourceSize.h * frame.pivot.y) - ((frame.trimmed) ? frame.spriteSourceSize.y : 0);
194
+ originX = (frame.sourceSize.w * frame.pivot.x) - ((trimmed) ? trim.x : 0);
195
+ originY = (frame.sourceSize.h * frame.pivot.y) - ((trimmed) ? trim.y : 0);
184
196
  }
185
197
 
186
198
  atlas[frame.filename] = {
@@ -188,7 +200,8 @@ export class TextureAtlas {
188
200
  texture : data.meta.image || "default", // the source texture
189
201
  offset : new Vector2d(s.x, s.y),
190
202
  anchorPoint : (hasTextureAnchorPoint) ? new Vector2d(originX / s.w, originY / s.h) : null,
191
- trimmed : !!frame.trimmed,
203
+ trimmed : trimmed,
204
+ trim : trim,
192
205
  width : s.w,
193
206
  height : s.h,
194
207
  angle : (frame.rotated === true) ? -ETA : 0
@@ -241,17 +254,18 @@ export class TextureAtlas {
241
254
  for (let frame = 0, count = spritecount.x * spritecount.y; frame < count; frame++) {
242
255
  let name = "" + frame;
243
256
  atlas[name] = {
244
- name : name,
245
- texture : "default", // the source texture
246
- offset : new Vector2d(
257
+ name : name,
258
+ texture : "default", // the source texture
259
+ offset : new Vector2d(
247
260
  margin + (spacing + data.framewidth) * (frame % spritecount.x),
248
261
  margin + (spacing + data.frameheight) * ~~(frame / spritecount.x)
249
262
  ),
250
- anchorPoint : (data.anchorPoint || null),
251
- trimmed : false,
252
- width : data.framewidth,
253
- height : data.frameheight,
254
- angle : 0
263
+ anchorPoint : (data.anchorPoint || null),
264
+ trimmed : false,
265
+ trim : undefined,
266
+ width : data.framewidth,
267
+ height : data.frameheight,
268
+ angle : 0
255
269
  };
256
270
  this.addUVs(atlas, name, width, height);
257
271
  }
@@ -120,43 +120,41 @@ class CanvasTexture {
120
120
 
121
121
  /**
122
122
  * creates a Blob object representing the image contained in this canvas texture
123
- * @param {object} [options] - An object with the following properties:
124
- * @param {string} [options.type="image/png"] - A string indicating the image format
125
- * @param {number} [options.quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
123
+ * @param {string} [type="image/png"] - A string indicating the image format
124
+ * @param {number} [quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
126
125
  * @returns {Promise} A Promise returning a Blob object representing the image contained in this canvas texture
127
126
  * @example
128
127
  * canvasTexture.convertToBlob().then((blob) => console.log(blob));
129
128
  */
130
- toBlob(options) {
129
+ toBlob(type = "image/png", quality) {
131
130
  if (typeof this.canvas.convertToBlob === "function") {
132
- return this.canvas.convertToBlob(options);
131
+ return this.canvas.convertToBlob(type, quality);
133
132
  } else {
134
133
  return new Promise(function(resolve) {
135
134
  this.canvas.toBlob((blob) => {
136
135
  resolve(blob);
137
- }, options ? options.type : undefined, options ? options.quality : undefined);
136
+ }, type, quality);
138
137
  });
139
138
  }
140
139
  }
141
140
 
142
141
  /**
143
142
  * creates an ImageBitmap object from the most recently rendered image of this canvas texture
144
- * @param {object} [options] - An object with the following properties:
145
- * @param {string} [options.type="image/png"] - A string indicating the image format
146
- * @param {number} [options.quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
143
+ * @param {string} [type="image/png"] - A string indicating the image format
144
+ * @param {number} [quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
147
145
  * @returns {Promise} A Promise returning an ImageBitmap.
148
146
  * @example
149
147
  * canvasTexture.transferToImageBitmap().then((bitmap) => console.log(bitmap));
150
148
  */
151
- toImageBitmap(options) {
149
+ toImageBitmap(type = "image/png", quality) {
152
150
  return new Promise((resolve) => {
153
151
  if (typeof this.canvas.transferToImageBitmap === "function") {
154
152
  resolve(this.canvas.transferToImageBitmap());
155
153
  } else {
156
154
  let image = new Image();
157
- image.src = this.canvas.toDataURL(options);
155
+ image.src = this.canvas.toDataURL(type, quality);
158
156
  image.onload = () => {
159
- createImageBitmap(image).then((bitmap) => resolve(bitmap));
157
+ globalThis.createImageBitmap(image).then((bitmap) => resolve(bitmap));
160
158
  };
161
159
  }
162
160
  });
@@ -165,16 +163,15 @@ class CanvasTexture {
165
163
  /**
166
164
  * returns a data URL containing a representation of the most recently rendered image of this canvas texture
167
165
  * (not supported by OffscreenCanvas)
168
- * @param {object} [options] - An object with the following properties:
169
- * @param {string} [options.type="image/png"] - A string indicating the image format
170
- * @param {number} [options.quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
166
+ * @param {string} [type="image/png"] - A string indicating the image format
167
+ * @param {number} [quality] - A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.
171
168
  * @returns {Promise} A Promise returning a string containing the requested data URL.
172
169
  * @example
173
170
  * renderer.toDataURL().then((dataURL) => console.log(dataURL));
174
171
  */
175
- toDataURL(options) {
172
+ toDataURL(type = "image/png", quality) {
176
173
  return new Promise((resolve) => {
177
- resolve(this.canvas.toDataURL(options));
174
+ resolve(this.canvas.toDataURL(type, quality));
178
175
  });
179
176
  }
180
177
 
@@ -92,7 +92,9 @@ export default class QuadCompositor extends Compositor {
92
92
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels, 0);
93
93
  } else if (typeof globalThis.OffscreenCanvas !== "undefined" && pixels instanceof globalThis.OffscreenCanvas) {
94
94
  // convert to ImageBitmap first (else Safari 16.4 and higher will throw an TypeError exception)
95
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, pixels.transferToImageBitmap());
95
+ const imageBitmap = pixels.transferToImageBitmap();
96
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imageBitmap);
97
+ imageBitmap.close();
96
98
  } else {
97
99
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
98
100
  }