melonjs 14.0.1 → 14.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 (214) hide show
  1. package/README.md +2 -0
  2. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
  3. package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
  4. package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
  5. package/dist/melonjs.mjs/_virtual/howler.js +10 -0
  6. package/dist/melonjs.mjs/_virtual/index.js +10 -0
  7. package/dist/melonjs.mjs/_virtual/index2.js +10 -0
  8. package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
  9. package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
  10. package/dist/melonjs.mjs/application/application.js +240 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +732 -0
  13. package/dist/melonjs.mjs/entity/entity.js +248 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
  16. package/dist/melonjs.mjs/geometries/line.js +116 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +319 -0
  18. package/dist/melonjs.mjs/geometries/point.js +89 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +500 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +168 -0
  22. package/dist/melonjs.mjs/index.js +248 -0
  23. package/dist/melonjs.mjs/input/gamepad.js +501 -0
  24. package/dist/melonjs.mjs/input/input.js +26 -0
  25. package/dist/melonjs.mjs/input/keyboard.js +470 -0
  26. package/dist/melonjs.mjs/input/pointer.js +393 -0
  27. package/dist/melonjs.mjs/input/pointerevent.js +818 -0
  28. package/dist/melonjs.mjs/lang/deprecated.js +157 -0
  29. package/dist/melonjs.mjs/level/level.js +297 -0
  30. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
  31. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +448 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -0
  43. package/dist/melonjs.mjs/loader/loader.js +801 -0
  44. package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
  45. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
  46. package/dist/melonjs.mjs/math/color.js +618 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +503 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +681 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
  52. package/dist/melonjs.mjs/math/vector2.js +528 -0
  53. package/dist/melonjs.mjs/math/vector3.js +569 -0
  54. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
  55. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
  56. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
  57. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
  58. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
  59. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
  60. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
  61. package/dist/melonjs.mjs/particles/emitter.js +267 -0
  62. package/dist/melonjs.mjs/particles/particle.js +188 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +704 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +460 -0
  66. package/dist/melonjs.mjs/physics/collision.js +132 -0
  67. package/dist/melonjs.mjs/physics/detector.js +194 -0
  68. package/dist/melonjs.mjs/physics/quadtree.js +391 -0
  69. package/dist/melonjs.mjs/physics/response.js +57 -0
  70. package/dist/melonjs.mjs/physics/sat.js +483 -0
  71. package/dist/melonjs.mjs/physics/world.js +221 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +62 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1018 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +156 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +783 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +654 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +157 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
  86. package/dist/melonjs.mjs/state/stage.js +237 -0
  87. package/dist/melonjs.mjs/state/state.js +596 -0
  88. package/dist/melonjs.mjs/system/device.js +909 -0
  89. package/dist/melonjs.mjs/system/dom.js +78 -0
  90. package/dist/melonjs.mjs/system/event.js +537 -0
  91. package/dist/melonjs.mjs/system/platform.js +41 -0
  92. package/dist/melonjs.mjs/system/pooling.js +209 -0
  93. package/dist/melonjs.mjs/system/save.js +157 -0
  94. package/dist/melonjs.mjs/system/timer.js +286 -0
  95. package/dist/melonjs.mjs/text/bitmaptext.js +364 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
  97. package/dist/melonjs.mjs/text/glyph.js +66 -0
  98. package/dist/melonjs.mjs/text/text.js +453 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +176 -0
  100. package/dist/melonjs.mjs/text/textstyle.js +23 -0
  101. package/dist/melonjs.mjs/tweens/easing.js +336 -0
  102. package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
  103. package/dist/melonjs.mjs/tweens/tween.js +480 -0
  104. package/dist/melonjs.mjs/utils/agent.js +76 -0
  105. package/dist/melonjs.mjs/utils/array.js +63 -0
  106. package/dist/melonjs.mjs/utils/file.js +42 -0
  107. package/dist/melonjs.mjs/utils/function.js +70 -0
  108. package/dist/melonjs.mjs/utils/string.js +82 -0
  109. package/dist/melonjs.mjs/utils/utils.js +173 -0
  110. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +807 -0
  111. package/dist/melonjs.mjs/video/renderer.js +411 -0
  112. package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
  113. package/dist/melonjs.mjs/video/texture/cache.js +143 -0
  114. package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
  115. package/dist/melonjs.mjs/video/video.js +462 -0
  116. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +143 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +168 -0
  118. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
  119. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
  120. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
  121. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
  122. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
  123. package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
  124. package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
  125. package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
  126. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
  127. package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +495 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
  129. package/dist/melonjs.module.d.ts +1163 -1163
  130. package/dist/melonjs.module.js +1903 -3274
  131. package/package.json +22 -17
  132. package/src/application/application.js +3 -3
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +31 -31
  135. package/src/entity/entity.js +17 -17
  136. package/src/geometries/ellipse.js +16 -16
  137. package/src/geometries/line.js +5 -5
  138. package/src/geometries/path2d.js +32 -32
  139. package/src/geometries/poly.js +15 -15
  140. package/src/geometries/rectangle.js +18 -18
  141. package/src/geometries/roundrect.js +8 -8
  142. package/src/input/gamepad.js +15 -15
  143. package/src/input/keyboard.js +12 -12
  144. package/src/input/pointer.js +6 -6
  145. package/src/input/pointerevent.js +12 -12
  146. package/src/lang/deprecated.js +12 -12
  147. package/src/level/level.js +25 -25
  148. package/src/level/tiled/TMXLayer.js +22 -22
  149. package/src/level/tiled/TMXTile.js +5 -5
  150. package/src/level/tiled/TMXTileMap.js +6 -6
  151. package/src/level/tiled/TMXTileset.js +2 -2
  152. package/src/level/tiled/TMXUtils.js +5 -5
  153. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
  154. package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
  155. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  156. package/src/level/tiled/renderer/TMXRenderer.js +19 -19
  157. package/src/loader/loader.js +20 -20
  158. package/src/math/color.js +20 -20
  159. package/src/math/math.js +16 -16
  160. package/src/math/matrix2.js +16 -16
  161. package/src/math/matrix3.js +25 -25
  162. package/src/math/observable_vector2.js +14 -14
  163. package/src/math/observable_vector3.js +16 -16
  164. package/src/math/vector2.js +9 -9
  165. package/src/math/vector3.js +10 -10
  166. package/src/particles/emitter.js +6 -6
  167. package/src/particles/particle.js +2 -2
  168. package/src/physics/body.js +28 -28
  169. package/src/physics/bounds.js +8 -8
  170. package/src/physics/collision.js +2 -2
  171. package/src/physics/detector.js +6 -6
  172. package/src/physics/quadtree.js +11 -11
  173. package/src/physics/sat.js +31 -31
  174. package/src/physics/world.js +5 -5
  175. package/src/plugin/plugin.js +5 -5
  176. package/src/renderable/collectable.js +3 -3
  177. package/src/renderable/colorlayer.js +5 -5
  178. package/src/renderable/container.js +21 -21
  179. package/src/renderable/dragndrop.js +14 -14
  180. package/src/renderable/imagelayer.js +13 -13
  181. package/src/renderable/light2d.js +3 -3
  182. package/src/renderable/nineslicesprite.js +16 -16
  183. package/src/renderable/renderable.js +23 -23
  184. package/src/renderable/sprite.js +28 -28
  185. package/src/renderable/trigger.js +15 -15
  186. package/src/renderable/ui/uibaseelement.js +7 -7
  187. package/src/renderable/ui/uispriteelement.js +6 -6
  188. package/src/renderable/ui/uitextbutton.js +13 -13
  189. package/src/state/stage.js +7 -7
  190. package/src/state/state.js +17 -17
  191. package/src/system/device.js +11 -11
  192. package/src/system/event.js +10 -10
  193. package/src/system/pooling.js +9 -9
  194. package/src/system/save.js +2 -2
  195. package/src/system/timer.js +10 -10
  196. package/src/text/bitmaptext.js +18 -18
  197. package/src/text/bitmaptextdata.js +2 -2
  198. package/src/text/text.js +23 -23
  199. package/src/text/textmetrics.js +8 -8
  200. package/src/tweens/tween.js +19 -19
  201. package/src/utils/agent.js +5 -5
  202. package/src/utils/array.js +4 -4
  203. package/src/utils/file.js +2 -2
  204. package/src/utils/function.js +6 -6
  205. package/src/utils/string.js +5 -5
  206. package/src/utils/utils.js +4 -4
  207. package/src/video/canvas/canvas_renderer.js +70 -70
  208. package/src/video/renderer.js +26 -26
  209. package/src/video/texture/atlas.js +22 -22
  210. package/src/video/texture/canvas_texture.js +9 -9
  211. package/src/video/video.js +17 -17
  212. package/src/video/webgl/glshader.js +10 -10
  213. package/src/video/webgl/webgl_compositor.js +41 -41
  214. package/src/video/webgl/webgl_renderer.js +75 -75
@@ -0,0 +1,909 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
3
+ * http://www.melonjs.org
4
+ * melonjs is licensed under the MIT License.
5
+ * http://www.opensource.org/licenses/mit-license
6
+ * @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
7
+ */
8
+ import { getParent } from '../video/video.js';
9
+ import save from './save.js';
10
+ import { prefixed } from '../utils/agent.js';
11
+ import { DOMContentLoaded } from './dom.js';
12
+ import * as platform$1 from './platform.js';
13
+
14
+ /**
15
+ * device type and capabilities
16
+ * @namespace device
17
+ */
18
+
19
+ let accelInitialized = false;
20
+ let deviceOrientationInitialized = false;
21
+ // swipe utility fn & flag
22
+ let swipeEnabled = true;
23
+ // a cache DOMRect object
24
+ let domRect = {left: 0, top: 0, x: 0, y: 0, width: 0, height: 0, right: 0, bottom: 0};
25
+
26
+ function disableSwipeFn(e) {
27
+ e.preventDefault();
28
+ if (typeof globalThis.scroll === "function") {
29
+ globalThis.scroll(0, 0);
30
+ }
31
+ return false;
32
+ }
33
+
34
+ function hasLocalStorage() {
35
+ try {
36
+ return !!globalThis.localStorage;
37
+ } catch (e) {
38
+ // the above generates an exception when cookies are blocked
39
+ return false;
40
+ }
41
+ }
42
+
43
+ function hasOffscreenCanvas() {
44
+ try {
45
+ // some browser (e.g. Safari) implements WebGL1 and WebGL2 contexts only
46
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=801176
47
+ return (typeof globalThis.OffscreenCanvas !== "undefined") && ((new globalThis.OffscreenCanvas(0, 0).getContext( "2d" )) !== null);
48
+ } catch (e) {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * used by [un]watchAccelerometer()
55
+ */
56
+ function onDeviceMotion(e) {
57
+ // Accelerometer information
58
+ accelerationX = e.accelerationIncludingGravity.x;
59
+ accelerationY = e.accelerationIncludingGravity.y;
60
+ accelerationZ = e.accelerationIncludingGravity.z;
61
+ }
62
+
63
+ /**
64
+ * used by [un]watchDeviceOrientation()
65
+ */
66
+ function onDeviceRotate(e) {
67
+ gamma = e.gamma;
68
+ beta = e.beta;
69
+ alpha = e.alpha;
70
+ }
71
+
72
+ /**
73
+ * the device platform type
74
+ * @name platform
75
+ * @memberof device
76
+ * @readonly
77
+ * @public
78
+ * @type {device.platform}
79
+ */
80
+ let platform = platform$1;
81
+
82
+ /**
83
+ * True if the browser supports Touch Events
84
+ * @name touchEvent
85
+ * @memberof device
86
+ * @type {boolean}
87
+ * @readonly
88
+ * @public
89
+ */
90
+ const touchEvent = !!("ontouchstart" in globalThis);
91
+
92
+ /**
93
+ * True if the browser supports Pointer Events
94
+ * @name pointerEvent
95
+ * @memberof device
96
+ * @type {boolean}
97
+ * @readonly
98
+ * @public
99
+ */
100
+ const pointerEvent = !!globalThis.PointerEvent;
101
+
102
+ /**
103
+ * Touch capabilities (support either Touch or Pointer events)
104
+ * @name touch
105
+ * @memberof device
106
+ * @type {boolean}
107
+ * @readonly
108
+ * @public
109
+ */
110
+ const touch = touchEvent || pointerEvent;
111
+
112
+ /**
113
+ * the maximum number of simultaneous touch contact points are supported by the current device.
114
+ * @name maxTouchPoints
115
+ * @memberof device
116
+ * @type {number}
117
+ * @readonly
118
+ * @public
119
+ * @example
120
+ * if (me.device.maxTouchPoints > 1) {
121
+ * // device supports multi-touch
122
+ * }
123
+ */
124
+ const maxTouchPoints = touch ? (pointerEvent ? globalThis.navigator.maxTouchPoints || 1 : 10) : 1;
125
+
126
+ /**
127
+ * W3C standard wheel events
128
+ * @name wheel
129
+ * @memberof device
130
+ * @type {boolean}
131
+ * @readonly
132
+ * @public
133
+ */
134
+ const wheel = typeof globalThis.document !== "undefined" && "onwheel" in globalThis.document.createElement("div");
135
+
136
+
137
+ /**
138
+ * Browser pointerlock api support
139
+ * @name hasPointerLockSupport
140
+ * @memberof device
141
+ * @type {boolean}
142
+ * @readonly
143
+ * @public
144
+ */
145
+ const hasPointerLockSupport = typeof globalThis.document !== "undefined" && typeof globalThis.document.pointerLockElement !== "undefined";
146
+
147
+ /**
148
+ * Browser device orientation
149
+ * @name hasDeviceOrientation
150
+ * @memberof device
151
+ * @readonly
152
+ * @public
153
+ * @type {boolean}
154
+ */
155
+ const hasDeviceOrientation = !!globalThis.DeviceOrientationEvent;
156
+
157
+ /**
158
+ * Supports the ScreenOrientation API
159
+ * @name screenOrientation
160
+ * @memberof device
161
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/onchange
162
+ * @type {boolean}
163
+ * @readonly
164
+ * @public
165
+ */
166
+ const screenOrientation = (typeof screen !== "undefined") && (typeof screen.orientation !== "undefined");
167
+
168
+ /**
169
+ * Browser accelerometer capabilities
170
+ * @name hasAccelerometer
171
+ * @memberof device
172
+ * @readonly
173
+ * @public
174
+ * @type {boolean}
175
+ */
176
+ const hasAccelerometer = !!globalThis.DeviceMotionEvent;
177
+
178
+ /**
179
+ * Browser full screen support
180
+ * @name hasFullscreenSupport
181
+ * @memberof device
182
+ * @type {boolean}
183
+ * @readonly
184
+ * @public
185
+ */
186
+ const hasFullscreenSupport = typeof globalThis.document !== "undefined" && (prefixed("fullscreenEnabled", globalThis.document) || globalThis.document.mozFullScreenEnabled);
187
+
188
+ if (hasFullscreenSupport === true) {
189
+ globalThis.document.exitFullscreen = prefixed("cancelFullScreen", globalThis.document) || prefixed("exitFullscreen", globalThis.document);
190
+ }
191
+
192
+ /**
193
+ * Device WebAudio Support
194
+ * @name hasWebAudio
195
+ * @memberof device
196
+ * @type {boolean}
197
+ * @readonly
198
+ * @public
199
+ */
200
+ const hasWebAudio = !!(globalThis.AudioContext || globalThis.webkitAudioContext);
201
+
202
+ /**
203
+ * Device HTML5Audio Support
204
+ * @name hasHTML5Audio
205
+ * @memberof device
206
+ * @type {boolean}
207
+ * @readonly
208
+ * @public
209
+ */
210
+ const hasHTML5Audio = (typeof globalThis.Audio !== "undefined");
211
+
212
+ /**
213
+ * Returns true if the browser/device has audio capabilities.
214
+ * @name sound
215
+ * @memberof device
216
+ * @type {boolean}
217
+ * @readonly
218
+ * @public
219
+ */
220
+ const sound = hasWebAudio || hasHTML5Audio;
221
+
222
+ /**
223
+ * Browser Local Storage capabilities <br>
224
+ * (this flag will be set to false if cookies are blocked)
225
+ * @name localStorage
226
+ * @memberof device
227
+ * @readonly
228
+ * @public
229
+ * @type {boolean}
230
+ */
231
+ const localStorage = hasLocalStorage();
232
+
233
+ /**
234
+ * equals to true if the device browser supports OffScreenCanvas.
235
+ * @name offscreenCanvas
236
+ * @memberof device
237
+ * @type {boolean}
238
+ * @readonly
239
+ * @public
240
+ */
241
+ const offscreenCanvas = hasOffscreenCanvas();
242
+
243
+ /**
244
+ * Browser Base64 decoding capability
245
+ * @name nativeBase64
246
+ * @memberof device
247
+ * @type {boolean}
248
+ * @readonly
249
+ * @public
250
+ */
251
+ const nativeBase64 = (typeof(globalThis.atob) === "function");
252
+
253
+ /**
254
+ * a string representing the preferred language of the user, usually the language of the browser UI.
255
+ * (will default to "en" if the information is not available)
256
+ * @name language
257
+ * @memberof device
258
+ * @type {string}
259
+ * @readonly
260
+ * @public
261
+ * @see http://www.w3schools.com/tags/ref_language_codes.asp
262
+ */
263
+ const language = typeof globalThis.navigator !== "undefined" ? globalThis.navigator.language || globalThis.navigator.browserLanguage || globalThis.navigator.userLanguage || "en" : "en";
264
+
265
+ /**
266
+ * Ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device.
267
+ * @name devicePixelRatio
268
+ * @memberof device
269
+ * @type {number}
270
+ * @readonly
271
+ * @public
272
+ */
273
+ const devicePixelRatio = globalThis.devicePixelRatio || 1;
274
+
275
+ /**
276
+ * equals to true if a mobile device.
277
+ * (Android | iPhone | iPad | iPod | BlackBerry | Windows Phone | Kindle)
278
+ * @name isMobile
279
+ * @memberof device
280
+ * @type {boolean}
281
+ * @readonly
282
+ * @public
283
+ */
284
+ const isMobile = platform.isMobile;
285
+
286
+ /**
287
+ * contains the g-force acceleration along the x-axis.
288
+ * @name accelerationX
289
+ * @memberof device
290
+ * @type {number}
291
+ * @readonly
292
+ * @public
293
+ * @see device.watchAccelerometer
294
+ */
295
+ let accelerationX = 0;
296
+
297
+ /**
298
+ * contains the g-force acceleration along the y-axis.
299
+ * @name accelerationY
300
+ * @memberof device
301
+ * @type {number}
302
+ * @readonly
303
+ * @public
304
+ * @see device.watchAccelerometer
305
+ */
306
+ let accelerationY = 0;
307
+
308
+ /**
309
+ * contains the g-force acceleration along the z-axis.
310
+ * @name accelerationZ
311
+ * @memberof device
312
+ * @type {number}
313
+ * @readonly
314
+ * @public
315
+ * @see device.watchAccelerometer
316
+ */
317
+ let accelerationZ = 0;
318
+
319
+ /**
320
+ * Device orientation Gamma property. Gives angle on tilting a portrait held phone left or right
321
+ * @name gamma
322
+ * @memberof device
323
+ * @type {number}
324
+ * @readonly
325
+ * @public
326
+ * @see device.watchDeviceOrientation
327
+ */
328
+ let gamma = 0;
329
+
330
+ /**
331
+ * Device orientation Beta property. Gives angle on tilting a portrait held phone forward or backward
332
+ * @name beta
333
+ * @memberof device
334
+ * @type {number}
335
+ * @readonly
336
+ * @public
337
+ * @see device.watchDeviceOrientation
338
+ */
339
+ let beta = 0;
340
+
341
+ /**
342
+ * Device orientation Alpha property. Gives angle based on the rotation of the phone around its z axis.
343
+ * The z-axis is perpendicular to the phone, facing out from the center of the screen.
344
+ * @name alpha
345
+ * @memberof device
346
+ * @type {number}
347
+ * @readonly
348
+ * @public
349
+ * @see device.watchDeviceOrientation
350
+ */
351
+ let alpha = 0;
352
+
353
+ /**
354
+ * Specify whether to pause the game when losing focus
355
+ * @name pauseOnBlur
356
+ * @memberof device
357
+ * @type {boolean}
358
+ * @public
359
+ * @default true
360
+ */
361
+ let pauseOnBlur = true;
362
+
363
+ /**
364
+ * Specify whether to unpause the game when gaining focus
365
+ * @name resumeOnFocus
366
+ * @memberof device
367
+ * @type {boolean}
368
+ * @public
369
+ * @default true
370
+ */
371
+ let resumeOnFocus = true;
372
+
373
+ /**
374
+ * Specify whether to automatically bring the window to the front
375
+ * @name autoFocus
376
+ * @memberof device
377
+ * @type {boolean}
378
+ * @public
379
+ * @default true
380
+ */
381
+ let autoFocus = true;
382
+
383
+ /**
384
+ * Specify whether to stop the game when losing focus or not.
385
+ * The engine restarts on focus if this is enabled.
386
+ * @name stopOnBlur
387
+ * @memberof device
388
+ * @type {boolean}
389
+ * @public
390
+ * @default false
391
+ */
392
+ let stopOnBlur = false;
393
+
394
+ /**
395
+ * specify a function to execute when the Device is fully loaded and ready
396
+ * @function onReady
397
+ * @memberof device
398
+ * @public
399
+ * @param {Function} fn - the function to be executed
400
+ * @example
401
+ * // small game skeleton
402
+ * var game = {
403
+ * // called by the me.device.onReady function
404
+ * onload = function () {
405
+ * // init video
406
+ * if (!me.video.init('screen', 640, 480, true)) {
407
+ * alert("Sorry but your browser does not support html 5 canvas.");
408
+ * return;
409
+ * }
410
+ *
411
+ * // initialize the "audio"
412
+ * me.audio.init("mp3,ogg");
413
+ *
414
+ * // set callback for ressources loaded event
415
+ * me.loader.onload = this.loaded.bind(this);
416
+ *
417
+ * // set all ressources to be loaded
418
+ * me.loader.preload(game.assets);
419
+ *
420
+ * // load everything & display a loading screen
421
+ * me.state.change(me.state.LOADING);
422
+ * };
423
+ *
424
+ * // callback when everything is loaded
425
+ * loaded = function () {
426
+ * // define stuff
427
+ * // ....
428
+ *
429
+ * // change to the menu screen
430
+ * me.state.change(me.state.PLAY);
431
+ * }
432
+ * }; // game
433
+ *
434
+ * // "bootstrap"
435
+ * me.device.onReady(function () {
436
+ * game.onload();
437
+ * });
438
+ */
439
+ function onReady(fn) {
440
+ DOMContentLoaded(fn);
441
+ }
442
+
443
+ /**
444
+ * enable/disable swipe on WebView.
445
+ * @function enableSwipe
446
+ * @memberof device
447
+ * @public
448
+ * @param {boolean} [enable=true] - enable or disable swipe.
449
+ */
450
+ function enableSwipe(enable) {
451
+ let moveEvent = pointerEvent ? "pointermove" : (touchEvent ? "touchmove" : "mousemove");
452
+ if (enable !== false) {
453
+ if (swipeEnabled === false) {
454
+ globalThis.document.removeEventListener(moveEvent, disableSwipeFn);
455
+ swipeEnabled = true;
456
+ }
457
+ } else if (swipeEnabled === true) {
458
+ globalThis.document.addEventListener(moveEvent, disableSwipeFn, { passive: false });
459
+ swipeEnabled = false;
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Returns true if the browser/device is in full screen mode.
465
+ * @function isFullscreen
466
+ * @memberof device
467
+ * @public
468
+ * @returns {boolean}
469
+ */
470
+ function isFullscreen() {
471
+ if (hasFullscreenSupport) {
472
+ return !!(prefixed("fullscreenElement", document) || document.mozFullScreenElement);
473
+ } else {
474
+ return false;
475
+ }
476
+ }
477
+
478
+ /**
479
+ * Triggers a fullscreen request. Requires fullscreen support from the browser/device.
480
+ * @function requestFullscreen
481
+ * @memberof device
482
+ * @public
483
+ * @param {object} [element=default canvas object] - the element to be set in full-screen mode.
484
+ * @example
485
+ * // add a keyboard shortcut to toggle Fullscreen mode on/off
486
+ * me.input.bindKey(me.input.KEY.F, "toggleFullscreen");
487
+ * me.event.on(me.event.KEYDOWN, function (action, keyCode, edge) {
488
+ * // toggle fullscreen on/off
489
+ * if (action === "toggleFullscreen") {
490
+ * me.device.requestFullscreen();
491
+ * } else {
492
+ * me.device.exitFullscreen();
493
+ * }
494
+ * });
495
+ */
496
+ function requestFullscreen(element) {
497
+ if (hasFullscreenSupport && !isFullscreen()) {
498
+ element = element || getParent();
499
+ element.requestFullscreen = prefixed("requestFullscreen", element) || element.mozRequestFullScreen;
500
+ element.requestFullscreen();
501
+ }
502
+ }
503
+
504
+ /**
505
+ * Exit fullscreen mode. Requires fullscreen support from the browser/device.
506
+ * @function exitFullscreen
507
+ * @memberof device
508
+ * @public
509
+ */
510
+ function exitFullscreen() {
511
+ if (hasFullscreenSupport && isFullscreen()) {
512
+ document.exitFullscreen();
513
+ }
514
+ }
515
+
516
+ /**
517
+ * Return a string representing the orientation of the device screen.
518
+ * It can be "any", "natural", "landscape", "portrait", "portrait-primary", "portrait-secondary", "landscape-primary", "landscape-secondary"
519
+ * @function getScreenOrientation
520
+ * @memberof device
521
+ * @public
522
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation
523
+ * @returns {string} the screen orientation
524
+ */
525
+ function getScreenOrientation() {
526
+ const PORTRAIT = "portrait";
527
+ const LANDSCAPE = "landscape";
528
+
529
+ let screen = globalThis.screen;
530
+
531
+ // first try using "standard" values
532
+ if (screenOrientation === true) {
533
+ let orientation = prefixed("orientation", screen);
534
+ if (typeof orientation !== "undefined" && typeof orientation.type === "string") {
535
+ // Screen Orientation API specification
536
+ return orientation.type;
537
+ } else if (typeof orientation === "string") {
538
+ // moz/ms-orientation are strings
539
+ return orientation;
540
+ }
541
+ }
542
+
543
+ // check using the deprecated API
544
+ if (typeof globalThis.orientation === "number") {
545
+ return (Math.abs(globalThis.orientation) === 90) ? LANDSCAPE : PORTRAIT;
546
+ }
547
+
548
+ // fallback to window size check
549
+ return (globalThis.outerWidth > globalThis.outerHeight) ? LANDSCAPE : PORTRAIT;
550
+ }
551
+
552
+ /**
553
+ * locks the device screen into the specified orientation.<br>
554
+ * This method only works for installed Web apps or for Web pages in full-screen mode.
555
+ * @function lockOrientation
556
+ * @memberof device
557
+ * @public
558
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation
559
+ * @param {string|string[]} orientation - The orientation into which to lock the screen.
560
+ * @returns {boolean} true if the orientation was unsuccessfully locked
561
+ */
562
+ function lockOrientation(orientation) {
563
+ let screen = globalThis.screen;
564
+ if (typeof screen !== "undefined") {
565
+ let _lockOrientation = prefixed("lockOrientation", screen);
566
+ if (typeof _lockOrientation !== "undefined") {
567
+ return _lockOrientation(orientation);
568
+ }
569
+ }
570
+ return false;
571
+ }
572
+
573
+ /**
574
+ * unlocks the device screen into the specified orientation.<br>
575
+ * This method only works for installed Web apps or for Web pages in full-screen mode.
576
+ * @function unlockOrientation
577
+ * @memberof device
578
+ * @public
579
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation
580
+ * @returns {boolean} true if the orientation was unsuccessfully unlocked
581
+ */
582
+ function unlockOrientation() {
583
+ let screen = globalThis.screen;
584
+ if (typeof screen !== "undefined") {
585
+ let _unlockOrientation = prefixed("unlockOrientation", screen);
586
+ if (typeof _unlockOrientation !== "undefined") {
587
+ return _unlockOrientation();
588
+ }
589
+ }
590
+ return false;
591
+ }
592
+
593
+ /**
594
+ * return true if the device screen orientation is in Portrait mode
595
+ * @function isPortrait
596
+ * @memberof device
597
+ * @public
598
+ * @returns {boolean}
599
+ */
600
+ function isPortrait() {
601
+ return getScreenOrientation().includes("portrait");
602
+ }
603
+
604
+ /**
605
+ * return true if the device screen orientation is in Portrait mode
606
+ * @function isLandscape
607
+ * @memberof device
608
+ * @public
609
+ * @returns {boolean}
610
+ */
611
+ function isLandscape() {
612
+ return getScreenOrientation().includes("landscape");
613
+ }
614
+
615
+ /**
616
+ * return the device storage
617
+ * @function getStorage
618
+ * @memberof device
619
+ * @public
620
+ * @see save
621
+ * @param {string} [type="local"]
622
+ * @returns {object} a reference to the device storage
623
+ */
624
+ function getStorage(type = "local") {
625
+ switch (type) {
626
+ case "local" :
627
+ return save;
628
+
629
+ default :
630
+ throw new Error("storage type " + type + " not supported");
631
+ }
632
+ }
633
+
634
+ /**
635
+ * return the parent DOM element for the given parent name or HTMLElement object
636
+ * @function getParentElement
637
+ * @memberof device
638
+ * @public
639
+ * @param {string|HTMLElement} element - the parent element name or a HTMLElement object
640
+ * @returns {HTMLElement} the parent Element
641
+ */
642
+ function getParentElement(element) {
643
+ let target = getElement(element);
644
+
645
+ if (target.parentNode !== null) {
646
+ target = target.parentNode;
647
+ }
648
+
649
+ return target;
650
+ }
651
+
652
+ /**
653
+ * return the DOM element for the given element name or HTMLElement object
654
+ * @function getElement
655
+ * @memberof device
656
+ * @public
657
+ * @param {string|HTMLElement} element - the parent element name or a HTMLElement object
658
+ * @returns {HTMLElement} the corresponding DOM Element or null if not existing
659
+ */
660
+ function getElement(element) {
661
+ let target = null;
662
+
663
+ if (element !== "undefined") {
664
+ if (typeof element === "string") {
665
+ target = document.getElementById(element);
666
+ } else if (typeof element === "object" && element.nodeType === Node.ELEMENT_NODE) {
667
+ target = element;
668
+ }
669
+ }
670
+
671
+ // fallback, if invalid target or non HTMLElement object
672
+ if (!target) {
673
+ //default to document.body
674
+ target = document.body;
675
+ }
676
+
677
+ return target;
678
+ }
679
+
680
+ /**
681
+ * returns the size of the given HTMLElement and its position relative to the viewport
682
+ * <br><img src="images/element-box-diagram.png"/>
683
+ * @function getElementBounds
684
+ * @memberof device
685
+ * @public
686
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMRect
687
+ * @param {string|HTMLElement} element - an HTMLElement object
688
+ * @returns {DOMRect} the size and position of the element relatively to the viewport
689
+ */
690
+ function getElementBounds(element) {
691
+ if (typeof element === "object" && element !== document.body && typeof element.getBoundingClientRect !== "undefined") {
692
+ return element.getBoundingClientRect();
693
+ } else {
694
+ domRect.width = domRect.right = globalThis.innerWidth;
695
+ domRect.height = domRect.bottom = globalThis.innerHeight;
696
+ return domRect;
697
+ }
698
+ }
699
+
700
+ /**
701
+ * returns the size of the given HTMLElement Parent and its position relative to the viewport
702
+ * <br><img src="images/element-box-diagram.png"/>
703
+ * @function getParentBounds
704
+ * @memberof device
705
+ * @public
706
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMRect
707
+ * @param {string|HTMLElement} element - an HTMLElement object
708
+ * @returns {DOMRect} the size and position of the given element parent relative to the viewport
709
+ */
710
+ function getParentBounds(element) {
711
+ return getElementBounds(getParentElement(element));
712
+ }
713
+
714
+ /**
715
+ * returns true if the device supports WebGL
716
+ * @function isWebGLSupported
717
+ * @memberof device
718
+ * @public
719
+ * @param {object} [options] - context creation options
720
+ * @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.
721
+ * @returns {boolean} true if WebGL is supported
722
+ */
723
+ function isWebGLSupported(options) {
724
+ let _supported = false;
725
+ try {
726
+ let canvas = document.createElement("canvas");
727
+ let ctxOptions = {
728
+ stencil: true,
729
+ failIfMajorPerformanceCaveat: options.failIfMajorPerformanceCaveat
730
+ };
731
+ _supported = !! (globalThis.WebGLRenderingContext && (canvas.getContext("webgl", ctxOptions) || canvas.getContext("experimental-webgl", ctxOptions)));
732
+ } catch (e) {
733
+ _supported = false;
734
+ }
735
+
736
+ return _supported;
737
+ }
738
+
739
+ /**
740
+ * return the highest precision format supported by this device for GL Shaders
741
+ * @function getMaxShaderPrecision
742
+ * @memberof device
743
+ * @public
744
+ * @param {WebGLRenderingContext} gl
745
+ * @returns {boolean} "lowp", "mediump", or "highp"
746
+ */
747
+ function getMaxShaderPrecision(gl) {
748
+ if (gl.getShaderPrecisionFormat(gl.VERTEX_SHADER, gl.HIGH_FLOAT ).precision > 0 &&
749
+ gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.HIGH_FLOAT ).precision > 0) {
750
+ return "highp";
751
+ }
752
+ if (gl.getShaderPrecisionFormat(gl.VERTEX_SHADER, gl.MEDIUM_FLOAT ).precision > 0 &&
753
+ gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.MEDIUM_FLOAT ).precision > 0) {
754
+ return "mediump";
755
+ }
756
+ return "lowp";
757
+ }
758
+
759
+ /**
760
+ * Makes a request to bring this device window to the front.
761
+ * @function focus
762
+ * @memberof device
763
+ * @public
764
+ * @example
765
+ * if (clicked) {
766
+ * me.device.focus();
767
+ * }
768
+ */
769
+ function focus() {
770
+ if (typeof (globalThis.focus) === "function") {
771
+ globalThis.focus();
772
+ }
773
+ }
774
+
775
+ /**
776
+ * Enable monitor of the device accelerator to detect the amount of physical force of acceleration the device is receiving.
777
+ * (one some device a first user gesture will be required before calling this function)
778
+ * @function watchAccelerometer
779
+ * @memberof device
780
+ * @public
781
+ * @see device.accelerationX
782
+ * @see device.accelerationY
783
+ * @see device.accelerationZ
784
+ * @link {http://www.mobilexweb.com/samples/ball.html}
785
+ * @link {http://www.mobilexweb.com/blog/safari-ios-accelerometer-websockets-html5}
786
+ * @returns {boolean} false if not supported or permission not granted by the user
787
+ * @example
788
+ * // try to enable device accelerometer event on user gesture
789
+ * me.input.registerPointerEvent("pointerleave", me.game.viewport, function() {
790
+ * if (me.device.watchAccelerometer() === true) {
791
+ * // Success
792
+ * me.input.releasePointerEvent("pointerleave", me.game.viewport);
793
+ * } else {
794
+ * // ... fail at enabling the device accelerometer event
795
+ * }
796
+ * });
797
+ */
798
+ function watchAccelerometer() {
799
+ if (hasAccelerometer && !accelInitialized) {
800
+ if (DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission === "function") {
801
+ DeviceOrientationEvent.requestPermission()
802
+ .then(response => {
803
+ if (response === "granted") {
804
+ // add a listener for the devicemotion event
805
+ globalThis.addEventListener("devicemotion", onDeviceMotion, false);
806
+ accelInitialized = true;
807
+ }
808
+ }).catch(console.error);
809
+ } else {
810
+ // add a listener for the devicemotion event
811
+ globalThis.addEventListener("devicemotion", onDeviceMotion, false);
812
+ accelInitialized = true;
813
+ }
814
+ }
815
+ return accelInitialized;
816
+ }
817
+
818
+ /**
819
+ * unwatch Accelerometor event
820
+ * @function unwatchAccelerometer
821
+ * @memberof device
822
+ * @public
823
+ */
824
+ function unwatchAccelerometer() {
825
+ if (accelInitialized) {
826
+ // remove the listener for the devicemotion event
827
+ globalThis.removeEventListener("devicemotion", onDeviceMotion, false);
828
+ accelInitialized = false;
829
+ }
830
+ }
831
+
832
+ /**
833
+ * Enable monitor of the device orientation to detect the current orientation of the device as compared to the Earth coordinate frame.
834
+ * (one some device a first user gesture will be required before calling this function)
835
+ * @function watchDeviceOrientation
836
+ * @memberof device
837
+ * @public
838
+ * @see device.alpha
839
+ * @see device.beta
840
+ * @see device.gamma
841
+ * @returns {boolean} false if not supported or permission not granted by the user
842
+ * @example
843
+ * // try to enable device orientation event on user gesture
844
+ * me.input.registerPointerEvent("pointerleave", me.game.viewport, function() {
845
+ * if (me.device.watchDeviceOrientation() === true) {
846
+ * // Success
847
+ * me.input.releasePointerEvent("pointerleave", me.game.viewport);
848
+ * } else {
849
+ * // ... fail at enabling the device orientation event
850
+ * }
851
+ * });
852
+ */
853
+ function watchDeviceOrientation() {
854
+ if (hasDeviceOrientation && !deviceOrientationInitialized) {
855
+ if (typeof DeviceOrientationEvent.requestPermission === "function") {
856
+ DeviceOrientationEvent.requestPermission()
857
+ .then(response => {
858
+ if (response === "granted") {
859
+ globalThis.addEventListener("deviceorientation", onDeviceRotate, false);
860
+ deviceOrientationInitialized = true;
861
+ }
862
+ }).catch(console.error);
863
+ } else {
864
+ globalThis.addEventListener("deviceorientation", onDeviceRotate, false);
865
+ deviceOrientationInitialized = true;
866
+ }
867
+ }
868
+ return deviceOrientationInitialized;
869
+ }
870
+
871
+ /**
872
+ * unwatch Device orientation event
873
+ * @function unwatchDeviceOrientation
874
+ * @memberof device
875
+ * @public
876
+ */
877
+ function unwatchDeviceOrientation() {
878
+ if (deviceOrientationInitialized) {
879
+ globalThis.removeEventListener("deviceorientation", onDeviceRotate, false);
880
+ deviceOrientationInitialized = false;
881
+ }
882
+ }
883
+
884
+ /**
885
+ * the vibrate method pulses the vibration hardware on the device, <br>
886
+ * If the device doesn't support vibration, this method has no effect. <br>
887
+ * If a vibration pattern is already in progress when this method is called,
888
+ * the previous pattern is halted and the new one begins instead.
889
+ * @function vibrate
890
+ * @memberof device
891
+ * @public
892
+ * @param {number|number[]} pattern - pattern of vibration and pause intervals
893
+ * @example
894
+ * // vibrate for 1000 ms
895
+ * me.device.vibrate(1000);
896
+ * // or alternatively
897
+ * me.device.vibrate([1000]);
898
+ * // vibrate for 50 ms, be still for 100 ms, and then vibrate for 150 ms:
899
+ * me.device.vibrate([50, 100, 150]);
900
+ * // cancel any existing vibrations
901
+ * me.device.vibrate(0);
902
+ */
903
+ function vibrate(pattern) {
904
+ if (typeof globalThis.navigator !== "undefined" && typeof globalThis.navigator.vibrate === "function") {
905
+ globalThis.navigator.vibrate(pattern);
906
+ }
907
+ }
908
+
909
+ export { accelerationX, accelerationY, accelerationZ, alpha, autoFocus, beta, devicePixelRatio, enableSwipe, exitFullscreen, focus, gamma, getElement, getElementBounds, getMaxShaderPrecision, getParentBounds, getParentElement, getScreenOrientation, getStorage, hasAccelerometer, hasDeviceOrientation, hasFullscreenSupport, hasHTML5Audio, hasPointerLockSupport, hasWebAudio, isFullscreen, isLandscape, isMobile, isPortrait, isWebGLSupported, language, localStorage, lockOrientation, maxTouchPoints, nativeBase64, offscreenCanvas, onDeviceRotate, onReady, pauseOnBlur, platform, pointerEvent, requestFullscreen, resumeOnFocus, screenOrientation, sound, stopOnBlur, touch, touchEvent, unlockOrientation, unwatchAccelerometer, unwatchDeviceOrientation, vibrate, watchAccelerometer, watchDeviceOrientation, wheel };