melonjs 15.2.0 → 15.3.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 (165) hide show
  1. package/README.md +5 -5
  2. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +1 -1
  3. package/dist/melonjs.mjs/_virtual/earcut.js +1 -1
  4. package/dist/melonjs.mjs/_virtual/howler.js +1 -1
  5. package/dist/melonjs.mjs/_virtual/index.js +1 -1
  6. package/dist/melonjs.mjs/application/application.js +24 -20
  7. package/dist/melonjs.mjs/application/header.js +1 -1
  8. package/dist/melonjs.mjs/application/resize.js +10 -4
  9. package/dist/melonjs.mjs/application/settings.js +37 -1
  10. package/dist/melonjs.mjs/audio/audio.js +1 -1
  11. package/dist/melonjs.mjs/camera/camera2d.js +1 -1
  12. package/dist/melonjs.mjs/const.js +1 -1
  13. package/dist/melonjs.mjs/entity/entity.js +1 -1
  14. package/dist/melonjs.mjs/geometries/ellipse.js +1 -1
  15. package/dist/melonjs.mjs/geometries/line.js +1 -1
  16. package/dist/melonjs.mjs/geometries/path2d.js +1 -1
  17. package/dist/melonjs.mjs/geometries/point.js +1 -1
  18. package/dist/melonjs.mjs/geometries/poly.js +1 -1
  19. package/dist/melonjs.mjs/geometries/rectangle.js +1 -1
  20. package/dist/melonjs.mjs/geometries/roundrect.js +1 -1
  21. package/dist/melonjs.mjs/index.js +4 -2
  22. package/dist/melonjs.mjs/input/gamepad.js +1 -1
  23. package/dist/melonjs.mjs/input/input.js +1 -1
  24. package/dist/melonjs.mjs/input/keyboard.js +1 -1
  25. package/dist/melonjs.mjs/input/pointer.js +1 -1
  26. package/dist/melonjs.mjs/input/pointerevent.js +1 -1
  27. package/dist/melonjs.mjs/lang/console.js +1 -1
  28. package/dist/melonjs.mjs/lang/deprecated.js +1 -1
  29. package/dist/melonjs.mjs/level/level.js +1 -1
  30. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +1 -1
  31. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +1 -1
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +1 -1
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +1 -1
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +1 -1
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +1 -1
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +1 -1
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +117 -90
  38. package/dist/melonjs.mjs/level/tiled/constants.js +1 -1
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +1 -1
  43. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  44. package/dist/melonjs.mjs/level/tiled/renderer/autodetect.js +1 -1
  45. package/dist/melonjs.mjs/loader/cache.js +1 -1
  46. package/dist/melonjs.mjs/loader/loader.js +1 -1
  47. package/dist/melonjs.mjs/loader/loadingscreen.js +1 -1
  48. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +1 -1
  49. package/dist/melonjs.mjs/loader/parser.js +1 -1
  50. package/dist/melonjs.mjs/loader/settings.js +1 -1
  51. package/dist/melonjs.mjs/math/color.js +4 -4
  52. package/dist/melonjs.mjs/math/math.js +1 -1
  53. package/dist/melonjs.mjs/math/matrix2.js +1 -1
  54. package/dist/melonjs.mjs/math/matrix3.js +1 -1
  55. package/dist/melonjs.mjs/math/observable_vector2.js +1 -1
  56. package/dist/melonjs.mjs/math/observable_vector3.js +1 -1
  57. package/dist/melonjs.mjs/math/vector2.js +1 -1
  58. package/dist/melonjs.mjs/math/vector3.js +1 -1
  59. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/arraymultimap.js +1 -1
  60. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/multimap.js +1 -1
  61. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +1 -1
  62. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +1 -1
  63. package/dist/melonjs.mjs/node_modules/eventemitter3/index2.js +1 -1
  64. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +1 -1
  65. package/dist/melonjs.mjs/particles/emitter.js +1 -1
  66. package/dist/melonjs.mjs/particles/particle.js +1 -1
  67. package/dist/melonjs.mjs/particles/settings.js +1 -1
  68. package/dist/melonjs.mjs/physics/body.js +1 -1
  69. package/dist/melonjs.mjs/physics/bounds.js +1 -1
  70. package/dist/melonjs.mjs/physics/collision.js +1 -1
  71. package/dist/melonjs.mjs/physics/detector.js +1 -1
  72. package/dist/melonjs.mjs/physics/quadtree.js +1 -1
  73. package/dist/melonjs.mjs/physics/response.js +1 -1
  74. package/dist/melonjs.mjs/physics/sat.js +1 -1
  75. package/dist/melonjs.mjs/physics/world.js +1 -1
  76. package/dist/melonjs.mjs/plugin/plugin.js +3 -3
  77. package/dist/melonjs.mjs/renderable/collectable.js +1 -1
  78. package/dist/melonjs.mjs/renderable/colorlayer.js +1 -1
  79. package/dist/melonjs.mjs/renderable/container.js +1 -1
  80. package/dist/melonjs.mjs/renderable/draggable.js +1 -1
  81. package/dist/melonjs.mjs/renderable/dragndrop.js +1 -1
  82. package/dist/melonjs.mjs/renderable/imagelayer.js +1 -1
  83. package/dist/melonjs.mjs/renderable/light2d.js +1 -1
  84. package/dist/melonjs.mjs/renderable/nineslicesprite.js +1 -1
  85. package/dist/melonjs.mjs/renderable/renderable.js +6 -14
  86. package/dist/melonjs.mjs/renderable/sprite.js +1 -1
  87. package/dist/melonjs.mjs/renderable/trigger.js +1 -1
  88. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +1 -1
  89. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +1 -1
  90. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +1 -1
  91. package/dist/melonjs.mjs/state/stage.js +12 -13
  92. package/dist/melonjs.mjs/state/state.js +4 -14
  93. package/dist/melonjs.mjs/system/device.js +3 -1
  94. package/dist/melonjs.mjs/system/dom.js +1 -1
  95. package/dist/melonjs.mjs/system/event.js +24 -2
  96. package/dist/melonjs.mjs/system/platform.js +1 -1
  97. package/dist/melonjs.mjs/system/pooling.js +1 -1
  98. package/dist/melonjs.mjs/system/save.js +1 -1
  99. package/dist/melonjs.mjs/system/timer.js +1 -1
  100. package/dist/melonjs.mjs/text/bitmaptext.js +1 -1
  101. package/dist/melonjs.mjs/text/bitmaptextdata.js +1 -1
  102. package/dist/melonjs.mjs/text/glyph.js +1 -1
  103. package/dist/melonjs.mjs/text/text.js +1 -1
  104. package/dist/melonjs.mjs/text/textmetrics.js +1 -1
  105. package/dist/melonjs.mjs/text/textstyle.js +1 -1
  106. package/dist/melonjs.mjs/tweens/easing.js +1 -1
  107. package/dist/melonjs.mjs/tweens/interpolation.js +1 -1
  108. package/dist/melonjs.mjs/tweens/tween.js +1 -1
  109. package/dist/melonjs.mjs/utils/agent.js +1 -1
  110. package/dist/melonjs.mjs/utils/array.js +1 -1
  111. package/dist/melonjs.mjs/utils/file.js +1 -1
  112. package/dist/melonjs.mjs/utils/function.js +1 -1
  113. package/dist/melonjs.mjs/utils/string.js +1 -1
  114. package/dist/melonjs.mjs/utils/utils.js +1 -1
  115. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +2 -11
  116. package/dist/melonjs.mjs/video/renderer.js +2 -15
  117. package/dist/melonjs.mjs/video/texture/atlas.js +1 -1
  118. package/dist/melonjs.mjs/video/texture/cache.js +1 -1
  119. package/dist/melonjs.mjs/video/texture/canvas_texture.js +1 -1
  120. package/dist/melonjs.mjs/video/utils/autodetect.js +1 -1
  121. package/dist/melonjs.mjs/video/video.js +4 -54
  122. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +1 -1
  123. package/dist/melonjs.mjs/video/webgl/compositors/compositor.js +1 -1
  124. package/dist/melonjs.mjs/video/webgl/compositors/primitive_compositor.js +1 -1
  125. package/dist/melonjs.mjs/video/webgl/compositors/quad_compositor.js +1 -1
  126. package/dist/melonjs.mjs/video/webgl/glshader.js +1 -1
  127. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +1 -1
  128. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +1 -1
  129. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +1 -1
  130. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +1 -1
  131. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +1 -1
  132. package/dist/melonjs.mjs/video/webgl/utils/precision.js +1 -1
  133. package/dist/melonjs.mjs/video/webgl/utils/program.js +1 -1
  134. package/dist/melonjs.mjs/video/webgl/utils/string.js +1 -1
  135. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +1 -1
  136. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +13 -16
  137. package/dist/melonjs.module.js +262 -250
  138. package/dist/types/application/application.d.ts +4 -27
  139. package/dist/types/application/settings.d.ts +64 -0
  140. package/dist/types/index.d.ts +2 -1
  141. package/dist/types/level/tiled/TMXUtils.d.ts +18 -37
  142. package/dist/types/renderable/renderable.d.ts +1 -2
  143. package/dist/types/state/stage.d.ts +3 -2
  144. package/dist/types/system/device.d.ts +1 -0
  145. package/dist/types/system/event.d.ts +20 -0
  146. package/dist/types/video/canvas/canvas_renderer.d.ts +0 -23
  147. package/dist/types/video/renderer.d.ts +3 -30
  148. package/dist/types/video/video.d.ts +3 -57
  149. package/dist/types/video/webgl/webgl_renderer.d.ts +1 -34
  150. package/package.json +7 -7
  151. package/src/application/application.js +22 -18
  152. package/src/application/resize.js +8 -2
  153. package/src/application/settings.js +36 -0
  154. package/src/index.js +2 -0
  155. package/src/level/tiled/TMXUtils.js +116 -92
  156. package/src/math/color.js +3 -3
  157. package/src/renderable/renderable.js +5 -13
  158. package/src/state/stage.js +11 -12
  159. package/src/state/state.js +2 -12
  160. package/src/system/device.js +2 -0
  161. package/src/system/event.js +22 -0
  162. package/src/video/canvas/canvas_renderer.js +1 -10
  163. package/src/video/renderer.js +1 -14
  164. package/src/video/video.js +7 -30
  165. package/src/video/webgl/webgl_renderer.js +12 -15
@@ -43,6 +43,7 @@ export function onresize(game) {
43
43
  let renderer = game.renderer;
44
44
  let settings = renderer.settings;
45
45
  let scaleX = 1, scaleY = 1;
46
+ let nodeBounds;
46
47
 
47
48
  if (settings.autoScale) {
48
49
 
@@ -56,8 +57,13 @@ export function onresize(game) {
56
57
  canvasMaxHeight = parseInt(style.maxHeight, 10) || Infinity;
57
58
  }
58
59
 
59
- // get the maximum canvas size within the parent div containing the canvas container
60
- let nodeBounds = device.getParentBounds(game.getParentElement());
60
+ if (typeof game.settings.scaleTarget !== "undefined") {
61
+ // get the bounds of the given scale target
62
+ nodeBounds = device.getElementBounds(game.settings.scaleTarget);
63
+ } else {
64
+ // get the maximum canvas size within the parent div containing the canvas container
65
+ nodeBounds = device.getParentBounds(game.getParentElement());
66
+ }
61
67
 
62
68
  let _max_width = Math.min(canvasMaxWidth, nodeBounds.width);
63
69
  let _max_height = Math.min(canvasMaxHeight, nodeBounds.height);
@@ -5,6 +5,7 @@ export const defaultSettings = {
5
5
  autoScale : false,
6
6
  scale : 1.0,
7
7
  scaleMethod : "manual",
8
+ scaleTarget : undefined,
8
9
  transparent : false,
9
10
  premultipliedAlpha: true,
10
11
  blendMode : "normal",
@@ -17,3 +18,38 @@ export const defaultSettings = {
17
18
  consoleHeader : true,
18
19
  legacy : false
19
20
  };
21
+
22
+ /**
23
+ * Application & Renderer Settings definition.
24
+ * @typedef {Object} Settings
25
+ * @property {string|HTMLElement} [parent=document.body] - the DOM parent element to hold the canvas in the HTML file
26
+ * @property {number|Renderer} [renderer=AUTO] - renderer to use (CANVAS, WEBGL, AUTO), or a custom renderer class
27
+ * @property {number|string} [scale=1.0] - enable scaling of the canvas ('auto' for automatic scaling)
28
+ * @property {string} [scaleMethod="fit"] - screen scaling modes ('fit','fill-min','fill-max','flex','flex-width','flex-height','stretch') : <br>
29
+ * - <i><b>`fit`</b></i> : Letterboxed; content is scaled to design aspect ratio <br>
30
+ * <center><img src="images/scale-fit.png"/></center><br>
31
+ * - <i><b>`fill-min`</b></i> : Canvas is resized to fit minimum design resolution; content is scaled to design aspect ratio <br>
32
+ * <center><img src="images/scale-fill-min.png"/></center><br>
33
+ * - <i><b>`fill-max`</b></i> : Canvas is resized to fit maximum design resolution; content is scaled to design aspect ratio <br>
34
+ * <center><img src="images/scale-fill-max.png"/></center><br>
35
+ * - <i><b>`flex`</b><</i> : Canvas width & height is resized to fit; content is scaled to design aspect ratio <br>
36
+ * <center><img src="images/scale-flex.png"/></center><br>
37
+ * - <i><b>`flex-width`</b></i> : Canvas width is resized to fit; content is scaled to design aspect ratio <br>
38
+ * <center><img src="images/scale-flex-width.png"/></center><br>
39
+ * - <i><b>`flex-height`</b></i> : Canvas height is resized to fit; content is scaled to design aspect ratio <br>
40
+ * <center><img src="images/scale-flex-height.png"/></center><br>
41
+ * - <i><b>`stretch`</b></i> : Canvas is resized to fit; content is scaled to screen aspect ratio <br>
42
+ * <center><img src="images/scale-stretch.png"/></center>
43
+ * @property {string|HTMLElement} [scaleTarget] - the HTML Element to be used as the reference target when using automatic scaling (by default melonJS will use the parent container of the div element containing the canvas)
44
+ * @property {boolean} [preferWebGL1=false] - if true the renderer will only use WebGL 1
45
+ * @property {boolean} [depthTest="sorting"] - ~Experimental~ the default method to sort object on the z axis in WebGL ("sorting", "z-buffer")
46
+ * @property {string} [powerPreference="default"] - a hint to the user agent indicating what configuration of GPU is suitable for the WebGL context ("default", "high-performance", "low-power"). To be noted that Safari and Chrome (since version 80) both default to "low-power" to save battery life and improve the user experience on these dual-GPU machines.
47
+ * @property {boolean} [transparent=false] - whether to allow transparent pixels in the front buffer (screen).
48
+ * @property {boolean} [antiAlias=false] - whether to enable or not video scaling interpolation
49
+ * @property {boolean} [consoleHeader=true] - whether to display melonJS version and basic device information in the console
50
+ * @param {number} [options.zoomX=width] - The actual width of the canvas with scaling applied
51
+ * @param {number} [options.zoomY=height] - The actual height of the canvas with scaling applied
52
+ * @param {Compositor} [options.compositor] - a custom compositor class (WebGL only)
53
+ * @see Application
54
+ * @memberof Application
55
+ */
package/src/index.js CHANGED
@@ -54,6 +54,7 @@ import TMXTileset from "./level/tiled/TMXTileset.js";
54
54
  import TMXTilesetGroup from "./level/tiled/TMXTilesetGroup.js";
55
55
  import TMXTileMap from "./level/tiled/TMXTileMap.js";
56
56
  import TMXLayer from "./level/tiled/TMXLayer.js";
57
+ import * as TMXUtils from "./level/tiled/TMXUtils.js";
57
58
  import Pointer from "./input/pointer.js";
58
59
  import Stage from "./state/stage.js";
59
60
  import Camera2d from "./camera/camera2d.js";
@@ -167,6 +168,7 @@ export {
167
168
  TMXTilesetGroup,
168
169
  TMXTileMap,
169
170
  TMXLayer,
171
+ TMXUtils,
170
172
  Pointer,
171
173
  Stage,
172
174
  Camera2d,
@@ -1,5 +1,10 @@
1
1
  import { isBoolean, isNumeric } from "../../utils/string.js";
2
- import * as utils from "../../utils/utils.js";
2
+
3
+ /**
4
+ * the function used to decompress zlib/gzip data
5
+ * @ignore
6
+ */
7
+ let inflateFunction;
3
8
 
4
9
  /**
5
10
  * set and interpret a TMX property value
@@ -96,99 +101,11 @@ function parseAttributes(obj, elt) {
96
101
  }
97
102
  }
98
103
 
99
- /**
100
- * decompress and decode zlib/gzip data
101
- * @ignore
102
- * @name decompress
103
- * @param {string} input - Base64 encoded and compressed data
104
- * @param {string} format - compressed data format ("gzip","zlib", "zstd")
105
- * @returns {Uint32Array} Decoded and decompress data
106
- */
107
- export function decompress(data, format) {
108
- if (typeof utils.inflateb64 === "function") {
109
- return utils.inflateb64(data, format);
110
- } else {
111
- throw new Error("GZIP/ZLIB compressed TMX Tile Map not supported!");
112
- }
113
- }
114
-
115
- /**
116
- * Decode a CSV encoded array into a binary array
117
- * @ignore
118
- * @name decodeCSV
119
- * @param {string} input- - CSV formatted data (only numbers, everything else will be converted to NaN)
120
- * @returns {number[]} Decoded data
121
- */
122
- export function decodeCSV(input) {
123
- let entries = input.replace("\n", "").trim().split(",");
124
-
125
- let result = [];
126
- for (let i = 0; i < entries.length; i++) {
127
- result.push(+entries[i]);
128
- }
129
- return result;
130
- }
131
-
132
- /**
133
- * Decode a base64 encoded string into a byte array
134
- * @ignore
135
- * @name decodeBase64AsArray
136
- * @param {string} input - Base64 encoded data
137
- * @param {number} [bytes] - number of bytes per array entry
138
- * @returns {Uint32Array} Decoded data
139
- */
140
- export function decodeBase64AsArray(input, bytes) {
141
- bytes = bytes || 1;
142
-
143
- let i, j, len;
144
- let dec = globalThis.atob(input.replace(/[^A-Za-z0-9\+\/\=]/g, ""));
145
- let ar = new Uint32Array(dec.length / bytes);
146
-
147
- for (i = 0, len = dec.length / bytes; i < len; i++) {
148
- ar[i] = 0;
149
- for (j = bytes - 1; j >= 0; --j) {
150
- ar[i] += dec.charCodeAt((i * bytes) + j) << (j << 3);
151
- }
152
- }
153
- return ar;
154
- }
155
-
156
- /**
157
- * Decode the given data
158
- * @ignore
159
- */
160
- export function decode(data, encoding, compression) {
161
- compression = compression || "none";
162
- encoding = encoding || "none";
163
-
164
- switch (encoding) {
165
- case "csv":
166
- return decodeCSV(data);
167
-
168
- case "base64":
169
- if (compression !== "none") {
170
- data = decompress(data, compression);
171
- } else {
172
- data = decodeBase64AsArray(data, 4);
173
- }
174
- return data;
175
-
176
- case "none":
177
- return data;
178
-
179
- case "xml":
180
- throw new Error("XML encoding is deprecated, use base64 instead");
181
-
182
- default:
183
- throw new Error("Unknown layer encoding: " + encoding);
184
- }
185
- }
186
-
187
104
  /**
188
105
  * Normalize TMX format to Tiled JSON format
189
106
  * @ignore
190
107
  */
191
- export function normalize(obj, item) {
108
+ function normalize(obj, item) {
192
109
  let nodeName = item.nodeName;
193
110
 
194
111
  switch (nodeName) {
@@ -316,9 +233,113 @@ export function normalize(obj, item) {
316
233
  }
317
234
  }
318
235
 
236
+ /**
237
+ * a collection of utility functions for parsing TMX maps
238
+ * @namespace TMXUtils
239
+ */
240
+
241
+ /**
242
+ * decompress and decode zlib/gzip data
243
+ * @memberof TMXUtils
244
+ * @param {string} input - Base64 encoded and compressed data
245
+ * @param {string} format - compressed data format ("gzip","zlib", "zstd")
246
+ * @returns {Uint32Array} Decoded and decompress data
247
+ */
248
+ function decompress(data, format) {
249
+ if (typeof inflateFunction === "function") {
250
+ return inflateFunction(data, format);
251
+ } else {
252
+ throw new Error("GZIP/ZLIB compressed TMX Tile Map not supported!");
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Decode a CSV encoded array into a binary array
258
+ * @memberof TMXUtils
259
+ * @param {string} input- - CSV formatted data (only numbers, everything else will be converted to NaN)
260
+ * @returns {number[]} Decoded data
261
+ */
262
+ function decodeCSV(input) {
263
+ let entries = input.replace("\n", "").trim().split(",");
264
+
265
+ let result = [];
266
+ for (let i = 0; i < entries.length; i++) {
267
+ result.push(+entries[i]);
268
+ }
269
+ return result;
270
+ }
271
+
272
+ /**
273
+ * Decode a base64 encoded string into a byte array
274
+ * @memberof TMXUtils
275
+ * @param {string} input - Base64 encoded data
276
+ * @param {number} [bytes] - number of bytes per array entry
277
+ * @returns {Uint32Array} Decoded data
278
+ */
279
+ function decodeBase64AsArray(input, bytes) {
280
+ bytes = bytes || 1;
281
+
282
+ let i, j, len;
283
+ let dec = globalThis.atob(input.replace(/[^A-Za-z0-9\+\/\=]/g, ""));
284
+ let ar = new Uint32Array(dec.length / bytes);
285
+
286
+ for (i = 0, len = dec.length / bytes; i < len; i++) {
287
+ ar[i] = 0;
288
+ for (j = bytes - 1; j >= 0; --j) {
289
+ ar[i] += dec.charCodeAt((i * bytes) + j) << (j << 3);
290
+ }
291
+ }
292
+ return ar;
293
+ }
294
+
295
+ /**
296
+ * set the function used to inflate gzip/zlib data
297
+ * @memberof TMXUtils
298
+ * @param {Func} fn - inflate function
299
+ */
300
+ export function setInflateFunction(fn) {
301
+ inflateFunction = fn;
302
+ }
303
+
304
+ /**
305
+ * Decode a encoded array into a binary array
306
+ * @memberof TMXUtils
307
+ * @param {string} data - data to be decoded
308
+ * @param {string} [encoding="none"] - data encoding ("csv", "base64", "xml")
309
+ * @returns {number[]} Decoded data
310
+ */
311
+ export function decode(data, encoding, compression) {
312
+ compression = compression || "none";
313
+ encoding = encoding || "none";
314
+
315
+ switch (encoding) {
316
+ case "csv":
317
+ return decodeCSV(data);
318
+
319
+ case "base64":
320
+ if (compression !== "none") {
321
+ data = decompress(data, compression);
322
+ } else {
323
+ data = decodeBase64AsArray(data, 4);
324
+ }
325
+ return data;
326
+
327
+ case "none":
328
+ return data;
329
+
330
+ case "xml":
331
+ throw new Error("XML encoding is deprecated, use base64 instead");
332
+
333
+ default:
334
+ throw new Error("Unknown layer encoding: " + encoding);
335
+ }
336
+ }
337
+
319
338
  /**
320
339
  * Parse a XML TMX object and returns the corresponding javascript object
321
- * @ignore
340
+ * @memberof TMXUtils
341
+ * @param {Document} xml - XML TMX object
342
+ * @returns {object} Javascript object
322
343
  */
323
344
  export function parse(xml) {
324
345
  // Create the return object
@@ -356,7 +377,10 @@ export function parse(xml) {
356
377
 
357
378
  /**
358
379
  * Apply TMX Properties to the given object
359
- * @ignore
380
+ * @memberof TMXUtils
381
+ * @param {object} obj - object to apply the properties to
382
+ * @param {object} data - TMX data object
383
+ * @returns {object} obj
360
384
  */
361
385
  export function applyTMXProperties(obj, data) {
362
386
  let properties = data.properties;
package/src/math/color.js CHANGED
@@ -336,12 +336,12 @@ let cssToRGB = new Map();
336
336
  * @returns {Color} Reference to this object for method chaining
337
337
  */
338
338
  copy(color) {
339
- if (color instanceof Color) {
339
+ if (typeof color === "string") {
340
+ return this.parseCSS(color);
341
+ } else {
340
342
  this.glArray.set(color.glArray);
341
343
  return this;
342
344
  }
343
-
344
- return this.parseCSS(color);
345
345
  }
346
346
 
347
347
  /**
@@ -4,7 +4,6 @@ import Rect from "./../geometries/rectangle.js";
4
4
  import pool from "./../system/pooling.js";
5
5
  import { releaseAllPointerEvents } from "./../input/input.js";
6
6
  import { clamp } from "./../math/math.js";
7
- import Color from "./../math/color.js";
8
7
 
9
8
  /**
10
9
  * @classdesc
@@ -258,6 +257,9 @@ import Color from "./../math/color.js";
258
257
  // viewport flag
259
258
  this._inViewport = false;
260
259
 
260
+ // renderable cache tint value used by the getter/setter
261
+ this._tint = pool.pull("Color", 255, 255, 255, 1.0);
262
+
261
263
  // ensure it's fully opaque by default
262
264
  this.setOpacity(1.0);
263
265
  }
@@ -282,21 +284,11 @@ import Color from "./../math/color.js";
282
284
  * this.tint.setColor(255, 255, 255);
283
285
  */
284
286
  get tint() {
285
- if (typeof this._tint === "undefined") {
286
- this._tint = pool.pull("Color", 255, 255, 255, 1.0);
287
- }
288
287
  return this._tint;
289
288
  }
290
289
  set tint(value) {
291
- if (typeof this._tint === "undefined") {
292
- this._tint = pool.pull("Color", 255, 255, 255, 1.0);
293
- }
294
- if (value instanceof Color) {
295
- this._tint.copy(value);
296
- } else {
297
- // string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
298
- this._tint.parseCSS(value);
299
- }
290
+ this._tint.copy(value);
291
+ this.isDirty = true;
300
292
  }
301
293
 
302
294
  /**
@@ -1,5 +1,5 @@
1
1
  import { renderer } from "./../video/video.js";
2
- import { game } from "../index.js";
2
+ import * as event from "./../system/event.js";
3
3
  import Camera2d from "./../camera/camera2d.js";
4
4
  import Color from "./../math/color.js";
5
5
 
@@ -87,7 +87,6 @@ let default_settings = {
87
87
  * @ignore
88
88
  */
89
89
  reset() {
90
-
91
90
  // add all defined cameras
92
91
  this.settings.cameras.forEach((camera) => {
93
92
  this.cameras.set(camera.name, camera);
@@ -105,7 +104,7 @@ let default_settings = {
105
104
  }
106
105
 
107
106
  // reset the game
108
- game.reset();
107
+ event.emit(event.STAGE_RESET, this);
109
108
 
110
109
  // call the onReset Function
111
110
  this.onResetEvent.apply(this, arguments);
@@ -120,8 +119,7 @@ let default_settings = {
120
119
  * @returns {boolean}
121
120
  */
122
121
  update(dt) {
123
- // update all objects (and pass the elapsed time since last frame)
124
- let isDirty = game.world.update(dt);
122
+ let isDirty = false;
125
123
 
126
124
  // update the camera/viewport
127
125
  // iterate through all cameras
@@ -146,13 +144,15 @@ let default_settings = {
146
144
  * @name draw
147
145
  * @memberof Stage
148
146
  * @ignore
149
- * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer object
147
+ * @param {Renderer} renderer - the renderer object to draw with
148
+ * @param {World} world - the world object to draw
150
149
  */
151
- draw(renderer) {
150
+ draw(renderer, world) {
151
+
152
152
  // iterate through all cameras
153
153
  this.cameras.forEach((camera) => {
154
154
  // render the root container
155
- camera.draw(renderer, game.world);
155
+ camera.draw(renderer, world);
156
156
 
157
157
  // render the ambient light
158
158
  if (this.ambientLight.alpha !== 0) {
@@ -172,9 +172,9 @@ let default_settings = {
172
172
 
173
173
  // render all lights
174
174
  this.lights.forEach((light) => {
175
- light.preDraw(renderer, game.world);
176
- light.draw(renderer, game.world);
177
- light.postDraw(renderer, game.world);
175
+ light.preDraw(renderer, world);
176
+ light.draw(renderer, world);
177
+ light.postDraw(renderer, world);
178
178
  });
179
179
  });
180
180
  }
@@ -209,7 +209,6 @@ let default_settings = {
209
209
  if (typeof this.settings.onResetEvent === "function") {
210
210
  this.settings.onResetEvent.apply(this, arguments);
211
211
  }
212
-
213
212
  }
214
213
 
215
214
  /**
@@ -72,11 +72,7 @@ function _pauseRunLoop() {
72
72
  * @ignore
73
73
  */
74
74
  function _renderFrame(time) {
75
- let stage = _stages[_state].stage;
76
- // update all game objects
77
- game.update(time, stage);
78
- // render all game objects
79
- game.draw(stage);
75
+ event.emit(event.TICK, time);
80
76
  // schedule the next frame update
81
77
  if (_animFrameId !== -1) {
82
78
  _animFrameId = globalThis.requestAnimationFrame(_renderFrame);
@@ -125,9 +121,6 @@ function _switchState(state) {
125
121
  if (_onSwitchComplete) {
126
122
  _onSwitchComplete();
127
123
  }
128
-
129
- // force repaint
130
- game.repaint();
131
124
  }
132
125
  }
133
126
 
@@ -273,7 +266,7 @@ let state = {
273
266
  * default state ID for the default Stage
274
267
  * (the default stage is the one running as soon as melonJS is started)
275
268
  * @constant
276
- * @name SETTINGS
269
+ * @name DEFAULT
277
270
  * @memberof state
278
271
  */
279
272
  DEFAULT : 9,
@@ -361,9 +354,6 @@ let state = {
361
354
  // calculate the elpased time
362
355
  _pauseTime = globalThis.performance.now() - _pauseTime;
363
356
 
364
- // force repaint
365
- game.repaint();
366
-
367
357
  // publish the restart notification
368
358
  event.emit(event.STATE_RESTART, _pauseTime);
369
359
  }
@@ -45,6 +45,7 @@ function hasOffscreenCanvas() {
45
45
 
46
46
  /**
47
47
  * used by [un]watchAccelerometer()
48
+ * @ignore
48
49
  */
49
50
  function onDeviceMotion(e) {
50
51
  // Accelerometer information
@@ -55,6 +56,7 @@ function onDeviceMotion(e) {
55
56
 
56
57
  /**
57
58
  * used by [un]watchDeviceOrientation()
59
+ * @ignore
58
60
  */
59
61
  export function onDeviceRotate(e) {
60
62
  gamma = e.gamma;
@@ -30,6 +30,17 @@ export const DOM_READY = "dom_ready";
30
30
  */
31
31
  export const BOOT = "me.boot";
32
32
 
33
+ /**
34
+ * event generated when the system update the engine and the renderer by one step
35
+ * @public
36
+ * @constant
37
+ * @type {string}
38
+ * @name TICK
39
+ * @memberof event
40
+ * @see event.on
41
+ */
42
+ export const TICK = "me.tick";
43
+
33
44
  /**
34
45
  * event when the game is paused <br>
35
46
  * Data passed : none <br>
@@ -89,6 +100,17 @@ export const STATE_RESTART = "me.state.onRestart";
89
100
  */
90
101
  export const STATE_CHANGE = "me.state.onChange";
91
102
 
103
+ /**
104
+ * event for when a stage is resetted
105
+ * @public
106
+ * @constant
107
+ * @type {string}
108
+ * @name STAGE_RESET
109
+ * @memberof event
110
+ * @see event.on
111
+ */
112
+ export const STAGE_RESET = "me.stage.onReset";
113
+
92
114
  /**
93
115
  * event for when the video is initialized<br>
94
116
  * Data passed : none <br>
@@ -14,16 +14,7 @@ import * as event from "./../../system/event.js";
14
14
  */
15
15
  export default class CanvasRenderer extends Renderer {
16
16
  /**
17
- * @param {object} options - The renderer parameters
18
- * @param {number} options.width - The width of the canvas without scaling
19
- * @param {number} options.height - The height of the canvas without scaling
20
- * @param {HTMLCanvasElement} [options.canvas] - The html canvas to draw to on screen
21
- * @param {boolean} [options.antiAlias=false] - Whether to enable anti-aliasing
22
- * @param {boolean} [options.transparent=false] - Whether to enable transparency on the canvas (performance hit when enabled)
23
- * @param {boolean} [options.subPixel=false] - Whether to enable subpixel renderering (performance hit when enabled)
24
- * @param {boolean} [options.textureSeamFix=true] - enable the texture seam fix when rendering Tile when antiAlias is off for the canvasRenderer
25
- * @param {number} [options.zoomX=width] - The actual width of the canvas with scaling applied
26
- * @param {number} [options.zoomY=height] - The actual height of the canvas with scaling applied
17
+ * @param {Application.Settings} [options] - optional parameters for the renderer
27
18
  */
28
19
  constructor(options) {
29
20
  // parent constructor
@@ -20,20 +20,7 @@ import Point from "../geometries/point.js";
20
20
  */
21
21
  export default class Renderer {
22
22
  /**
23
- * @param {object} options - The renderer parameters
24
- * @param {number} options.width - The width of the canvas without scaling
25
- * @param {number} options.height - The height of the canvas without scaling
26
- * @param {HTMLCanvasElement} [options.canvas] - The html canvas to draw to on screen
27
- * @param {boolean} [options.antiAlias=false] - Whether to enable anti-aliasing, use false (default) for a pixelated effect.
28
- * @param {boolean} [options.failIfMajorPerformanceCaveat=true] - If true, the renderer will switch to CANVAS mode if the performances of a WebGL context would be dramatically lower than that of a native application making equivalent OpenGL calls.
29
- * @param {boolean} [options.transparent=false] - Whether to enable transparency on the canvas
30
- * @param {boolean} [options.premultipliedAlpha=true] - in WebGL, whether the renderer will assume that colors have premultiplied alpha when canvas transparency is enabled
31
- * @param {boolean} [options.blendMode="normal"] - the default blend mode to use ("normal", "multiply")
32
- * @param {boolean} [options.depthBuffer="sorting"] - ~Experimental~ the default method to sort object on the z axis in WebGL ("sorting", "z-buffer")
33
- * @param {boolean} [options.subPixel=false] - Whether to enable subpixel rendering (performance hit when enabled)
34
- * @param {boolean} [options.verbose=false] - Enable the verbose mode that provides additional details as to what the renderer is doing
35
- * @param {number} [options.zoomX=width] - The actual width of the canvas with scaling applied
36
- * @param {number} [options.zoomY=height] - The actual height of the canvas with scaling applied
23
+ * @param {Application.Settings} [options] - optional parameters for the renderer
37
24
  */
38
25
  constructor(options) {
39
26
  /**
@@ -2,33 +2,34 @@ import * as event from "./../system/event.js";
2
2
  import { initialized, game } from "./../index.js";
3
3
  import * as device from "./../system/device.js";
4
4
  import { throttle } from "../utils/function.js";
5
- import * as vc from "../const";
6
5
 
7
6
  /**
8
7
  * @namespace video
9
8
  */
10
9
 
11
-
12
10
  /**
13
11
  * Select the HTML5 Canvas renderer
14
12
  * @memberof video
13
+ * @name CANVAS
15
14
  * @static
16
15
  */
17
- export const CANVAS = vc.CANVAS;
16
+ export { CANVAS } from "../const";
18
17
 
19
18
  /**
20
19
  * Select the WebGL renderer
21
20
  * @memberof video
21
+ * @name WEBGL
22
22
  * @static
23
23
  */
24
- export const WEBGL = vc.WEBGL;
24
+ export { WEBGL } from "../const";
25
25
 
26
26
  /**
27
27
  * Auto-select the renderer (Attempt WebGL first, with fallback to Canvas)
28
28
  * @memberof video
29
+ * @name AUTO
29
30
  * @static
30
31
  */
31
- export const AUTO = vc.AUTO;
32
+ export { AUTO } from "../const";
32
33
 
33
34
  /**
34
35
  * A reference to the active Canvas or WebGL active renderer renderer
@@ -39,34 +40,10 @@ export let renderer = null;
39
40
 
40
41
  /**
41
42
  * Initialize the "video" system (create a canvas based on the given arguments, and the related renderer). <br>
42
- * melonJS support various scaling mode, that can be enabled <u>once the scale option is set to <b>`auto`</b></u> : <br>
43
- * - <i><b>`fit`</b></i> : Letterboxed; content is scaled to design aspect ratio <br>
44
- * <center><img src="images/scale-fit.png"/></center><br>
45
- * - <i><b>`fill-min`</b></i> : Canvas is resized to fit minimum design resolution; content is scaled to design aspect ratio <br>
46
- * <center><img src="images/scale-fill-min.png"/></center><br>
47
- * - <i><b>`fill-max`</b></i> : Canvas is resized to fit maximum design resolution; content is scaled to design aspect ratio <br>
48
- * <center><img src="images/scale-fill-max.png"/></center><br>
49
- * - <i><b>`flex`</b><</i> : Canvas width & height is resized to fit; content is scaled to design aspect ratio <br>
50
- * <center><img src="images/scale-flex.png"/></center><br>
51
- * - <i><b>`flex-width`</b></i> : Canvas width is resized to fit; content is scaled to design aspect ratio <br>
52
- * <center><img src="images/scale-flex-width.png"/></center><br>
53
- * - <i><b>`flex-height`</b></i> : Canvas height is resized to fit; content is scaled to design aspect ratio <br>
54
- * <center><img src="images/scale-flex-height.png"/></center><br>
55
- * - <i><b>`stretch`</b></i> : Canvas is resized to fit; content is scaled to screen aspect ratio
56
- * <center><img src="images/scale-stretch.png"/></center><br>
57
43
  * @memberof video
58
44
  * @param {number} width - The width of the canvas viewport
59
45
  * @param {number} height - The height of the canvas viewport
60
- * @param {object} [options] - The optional video/renderer parameters.<br> (see Renderer(s) documentation for further specific options)
61
- * @param {string|HTMLElement} [options.parent=document.body] - the DOM parent element to hold the canvas in the HTML file
62
- * @param {number|Renderer} [options.renderer=video.AUTO] - renderer to use (me.video.CANVAS, me.video.WEBGL, me.video.AUTO), or a custom renderer class
63
- * @param {number|string} [options.scale=1.0] - enable scaling of the canvas ('auto' for automatic scaling)
64
- * @param {string} [options.scaleMethod="fit"] - screen scaling modes ('fit','fill-min','fill-max','flex','flex-width','flex-height','stretch')
65
- * @param {boolean} [options.preferWebGL1=false] - if true the renderer will only use WebGL 1
66
- * @param {string} [options.powerPreference="default"] - a hint to the user agent indicating what configuration of GPU is suitable for the WebGL context ("default", "high-performance", "low-power"). To be noted that Safari and Chrome (since version 80) both default to "low-power" to save battery life and improve the user experience on these dual-GPU machines.
67
- * @param {boolean} [options.transparent=false] - whether to allow transparent pixels in the front buffer (screen).
68
- * @param {boolean} [options.antiAlias=false] - whether to enable or not video scaling interpolation
69
- * @param {boolean} [options.consoleHeader=true] - whether to display melonJS version and basic device information in the console
46
+ * @param {Application.Settings} [options] - optional parameters for the renderer
70
47
  * @returns {boolean} false if initialization failed (canvas not supported)
71
48
  * @example
72
49
  * // init the video with a 640x480 canvas