melonjs 15.15.0 → 16.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 (204) 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 +5 -3
  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 +6 -3
  45. package/dist/melonjs.mjs/loader/loader.js +59 -10
  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/parsers/video.js +98 -0
  56. package/dist/melonjs.mjs/loader/settings.js +6 -5
  57. package/dist/melonjs.mjs/math/color.js +2 -2
  58. package/dist/melonjs.mjs/math/math.js +13 -3
  59. package/dist/melonjs.mjs/math/matrix2.js +2 -2
  60. package/dist/melonjs.mjs/math/matrix3.js +2 -2
  61. package/dist/melonjs.mjs/math/observable_vector2.js +2 -2
  62. package/dist/melonjs.mjs/math/observable_vector3.js +2 -2
  63. package/dist/melonjs.mjs/math/vector2.js +7 -9
  64. package/dist/melonjs.mjs/math/vector3.js +6 -10
  65. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/arraymultimap.js +2 -2
  66. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/multimap.js +2 -2
  67. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +2 -2
  68. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +2 -2
  69. package/dist/melonjs.mjs/node_modules/eventemitter3/index2.js +2 -2
  70. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +2 -2
  71. package/dist/melonjs.mjs/particles/emitter.js +2 -2
  72. package/dist/melonjs.mjs/particles/particle.js +2 -2
  73. package/dist/melonjs.mjs/particles/settings.js +3 -5
  74. package/dist/melonjs.mjs/physics/body.js +6 -8
  75. package/dist/melonjs.mjs/physics/bounds.js +2 -2
  76. package/dist/melonjs.mjs/physics/collision.js +3 -5
  77. package/dist/melonjs.mjs/physics/detector.js +2 -2
  78. package/dist/melonjs.mjs/physics/quadtree.js +2 -2
  79. package/dist/melonjs.mjs/physics/response.js +2 -2
  80. package/dist/melonjs.mjs/physics/sat.js +2 -2
  81. package/dist/melonjs.mjs/physics/world.js +2 -2
  82. package/dist/melonjs.mjs/plugin/plugin.js +5 -5
  83. package/dist/melonjs.mjs/renderable/collectable.js +2 -2
  84. package/dist/melonjs.mjs/renderable/colorlayer.js +2 -2
  85. package/dist/melonjs.mjs/renderable/container.js +2 -2
  86. package/dist/melonjs.mjs/renderable/draggable.js +2 -2
  87. package/dist/melonjs.mjs/renderable/dragndrop.js +2 -2
  88. package/dist/melonjs.mjs/renderable/entity/entity.js +2 -2
  89. package/dist/melonjs.mjs/renderable/imagelayer.js +2 -2
  90. package/dist/melonjs.mjs/renderable/light2d.js +2 -2
  91. package/dist/melonjs.mjs/renderable/nineslicesprite.js +2 -6
  92. package/dist/melonjs.mjs/renderable/renderable.js +2 -2
  93. package/dist/melonjs.mjs/renderable/sprite.js +136 -81
  94. package/dist/melonjs.mjs/renderable/text/bitmaptext.js +2 -2
  95. package/dist/melonjs.mjs/renderable/text/bitmaptextdata.js +2 -2
  96. package/dist/melonjs.mjs/renderable/text/glyph.js +2 -2
  97. package/dist/melonjs.mjs/renderable/text/text.js +2 -2
  98. package/dist/melonjs.mjs/renderable/text/textmetrics.js +2 -2
  99. package/dist/melonjs.mjs/renderable/text/textstyle.js +2 -2
  100. package/dist/melonjs.mjs/renderable/trigger.js +2 -2
  101. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +2 -2
  102. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +2 -2
  103. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +2 -2
  104. package/dist/melonjs.mjs/state/stage.js +2 -2
  105. package/dist/melonjs.mjs/state/state.js +3 -4
  106. package/dist/melonjs.mjs/system/device.js +46 -3
  107. package/dist/melonjs.mjs/system/dom.js +2 -2
  108. package/dist/melonjs.mjs/system/event.js +2 -2
  109. package/dist/melonjs.mjs/system/platform.js +2 -2
  110. package/dist/melonjs.mjs/system/pooling.js +3 -20
  111. package/dist/melonjs.mjs/system/save.js +2 -2
  112. package/dist/melonjs.mjs/system/timer.js +3 -19
  113. package/dist/melonjs.mjs/tweens/easing.js +2 -2
  114. package/dist/melonjs.mjs/tweens/interpolation.js +2 -2
  115. package/dist/melonjs.mjs/tweens/tween.js +2 -2
  116. package/dist/melonjs.mjs/utils/agent.js +2 -2
  117. package/dist/melonjs.mjs/utils/array.js +2 -2
  118. package/dist/melonjs.mjs/utils/file.js +2 -2
  119. package/dist/melonjs.mjs/utils/function.js +2 -2
  120. package/dist/melonjs.mjs/utils/string.js +2 -2
  121. package/dist/melonjs.mjs/utils/utils.js +2 -2
  122. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +2 -2
  123. package/dist/melonjs.mjs/video/renderer.js +14 -17
  124. package/dist/melonjs.mjs/video/texture/atlas.js +28 -14
  125. package/dist/melonjs.mjs/video/texture/cache.js +6 -7
  126. package/dist/melonjs.mjs/video/texture/canvas_texture.js +16 -19
  127. package/dist/melonjs.mjs/video/utils/autodetect.js +2 -2
  128. package/dist/melonjs.mjs/video/video.js +2 -2
  129. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +2 -2
  130. package/dist/melonjs.mjs/video/webgl/compositors/compositor.js +2 -2
  131. package/dist/melonjs.mjs/video/webgl/compositors/primitive_compositor.js +2 -2
  132. package/dist/melonjs.mjs/video/webgl/compositors/quad_compositor.js +8 -5
  133. package/dist/melonjs.mjs/video/webgl/glshader.js +2 -2
  134. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +2 -2
  135. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +2 -2
  136. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +2 -2
  137. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +2 -2
  138. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +2 -2
  139. package/dist/melonjs.mjs/video/webgl/utils/precision.js +2 -2
  140. package/dist/melonjs.mjs/video/webgl/utils/program.js +2 -2
  141. package/dist/melonjs.mjs/video/webgl/utils/string.js +2 -2
  142. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +2 -2
  143. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +5 -4
  144. package/dist/melonjs.module.js +1475 -398
  145. package/dist/types/application/application.d.ts +1 -1
  146. package/dist/types/application/settings.d.ts +5 -0
  147. package/dist/types/geometries/path2d.d.ts +24 -0
  148. package/dist/types/geometries/rectangle.d.ts +4 -4
  149. package/dist/types/geometries/roundrect.d.ts +1 -1
  150. package/dist/types/geometries/toarccanvas.d.ts +10 -0
  151. package/dist/types/loader/cache.d.ts +1 -0
  152. package/dist/types/loader/loader.d.ts +37 -8
  153. package/dist/types/loader/parsers/binary.d.ts +0 -8
  154. package/dist/types/loader/parsers/fetchdata.d.ts +15 -0
  155. package/dist/types/loader/parsers/tmx.d.ts +1 -0
  156. package/dist/types/loader/parsers/video.d.ts +9 -0
  157. package/dist/types/loader/settings.d.ts +3 -3
  158. package/dist/types/math/color.d.ts +4 -4
  159. package/dist/types/math/math.d.ts +6 -0
  160. package/dist/types/math/observable_vector2.d.ts +2 -2
  161. package/dist/types/math/observable_vector3.d.ts +3 -3
  162. package/dist/types/physics/bounds.d.ts +4 -4
  163. package/dist/types/renderable/entity/entity.d.ts +1 -1
  164. package/dist/types/renderable/imagelayer.d.ts +1 -1
  165. package/dist/types/renderable/nineslicesprite.d.ts +12 -0
  166. package/dist/types/renderable/renderable.d.ts +3 -3
  167. package/dist/types/renderable/sprite.d.ts +41 -40
  168. package/dist/types/renderable/text/bitmaptext.d.ts +1 -1
  169. package/dist/types/system/device.d.ts +16 -0
  170. package/dist/types/video/renderer.d.ts +11 -23
  171. package/dist/types/video/texture/canvas_texture.d.ts +11 -23
  172. package/dist/types/video/webgl/compositors/quad_compositor.d.ts +2 -1
  173. package/package.json +16 -15
  174. package/src/application/settings.js +3 -1
  175. package/src/audio/audio.js +3 -1
  176. package/src/geometries/ellipse.js +4 -6
  177. package/src/geometries/path2d.js +129 -4
  178. package/src/geometries/point.js +5 -7
  179. package/src/geometries/poly.js +2 -12
  180. package/src/geometries/toarccanvas.js +94 -0
  181. package/src/loader/cache.js +3 -0
  182. package/src/loader/loader.js +57 -7
  183. package/src/loader/parsers/binary.js +13 -56
  184. package/src/loader/parsers/fetchdata.js +53 -0
  185. package/src/loader/parsers/image.js +45 -12
  186. package/src/loader/parsers/json.js +12 -30
  187. package/src/loader/parsers/tmx.js +46 -72
  188. package/src/loader/parsers/video.js +89 -0
  189. package/src/loader/settings.js +4 -3
  190. package/src/math/math.js +10 -0
  191. package/src/math/vector2.js +5 -7
  192. package/src/math/vector3.js +4 -8
  193. package/src/physics/body.js +4 -6
  194. package/src/plugin/plugin.js +1 -1
  195. package/src/polyfill/index.js +3 -0
  196. package/src/renderable/nineslicesprite.js +0 -4
  197. package/src/renderable/sprite.js +134 -79
  198. package/src/system/device.js +43 -0
  199. package/src/video/renderer.js +12 -15
  200. package/src/video/texture/atlas.js +26 -12
  201. package/src/video/texture/cache.js +3 -3
  202. package/src/video/texture/canvas_texture.js +14 -17
  203. package/src/video/webgl/compositors/quad_compositor.js +6 -3
  204. package/src/video/webgl/webgl_renderer.js +3 -2
@@ -4,6 +4,7 @@ import { getImage } from "./../loader/loader.js";
4
4
  import { TextureAtlas } from "./../video/texture/atlas.js";
5
5
  import Renderable from "./renderable.js";
6
6
  import Color from "../math/color.js";
7
+ import * as event from "../system/event.js";
7
8
 
8
9
  /**
9
10
  * @classdesc
@@ -15,7 +16,7 @@ export default class Sprite extends Renderable {
15
16
  * @param {number} x - the x coordinates of the sprite object
16
17
  * @param {number} y - the y coordinates of the sprite object
17
18
  * @param {object} settings - Configuration parameters for the Sprite object
18
- * @param {HTMLImageElement|HTMLCanvasElement|TextureAtlas|string} settings.image - reference to spritesheet image, a texture atlas or to a texture atlas
19
+ * @param {HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|TextureAtlas|string} settings.image - reference to spritesheet image, a texture atlas, a video element, or to a texture atlas
19
20
  * @param {string} [settings.name=""] - name of this object
20
21
  * @param {string} [settings.region] - region name of a specific region to use when using a texture atlas, see {@link TextureAtlas}
21
22
  * @param {number} [settings.framewidth] - Width of a single frame within the spritesheet
@@ -42,6 +43,16 @@ export default class Sprite extends Renderable {
42
43
  * image : mytexture,
43
44
  * region : "npc2.png",
44
45
  * });
46
+ *
47
+ * // create a video sprite
48
+ * let videoSprite = new me.Sprite(0, 0, {
49
+ * image : me.loader.getVideo("bigbunny"),
50
+ * anchorPoint : new me.Vector2d(0.5, 0.5)
51
+ * });
52
+ * // scale the video sprite
53
+ * videoSprite.currentTransform.scale(2);
54
+ * // start playing the video (if video is preloaded with `autoplay` set to false)
55
+ * videoSprite.play();
45
56
  */
46
57
  constructor(x, y, settings) {
47
58
 
@@ -49,39 +60,42 @@ export default class Sprite extends Renderable {
49
60
  super(x, y, 0, 0);
50
61
 
51
62
  /**
52
- * pause and resume animation
53
- * @public
54
63
  * @type {boolean}
55
64
  * @default false
56
- * @name Sprite#animationpause
57
65
  */
58
66
  this.animationpause = false;
59
67
 
60
68
  /**
61
69
  * animation cycling speed (delay between frame in ms)
62
- * @public
63
70
  * @type {number}
64
71
  * @default 100
65
- * @name Sprite#animationspeed
66
72
  */
67
73
  this.animationspeed = 100;
68
74
 
69
75
  /**
70
76
  * global offset for the position to draw from on the source image.
71
- * @public
72
77
  * @type {Vector2d}
73
78
  * @default <0.0,0.0>
74
- * @name offset
75
- * @memberof Sprite#
76
79
  */
77
80
  this.offset = pool.pull("Vector2d", 0, 0);
78
81
 
82
+ /**
83
+ * true if this is a video sprite (e.g. a HTMLVideoElement was passed as as source)
84
+ * @type {boolean}
85
+ * @default false
86
+ */
87
+ this.isVideo = false;
88
+
89
+ /**
90
+ * a callback fired when the end of a video or current animation was reached
91
+ * @type {Function}
92
+ * @default undefined
93
+ */
94
+ this.onended;
95
+
79
96
  /**
80
97
  * The source texture object this sprite object is using
81
- * @public
82
98
  * @type {TextureAtlas}
83
- * @name source
84
- * @memberof Sprite#
85
99
  */
86
100
  this.source = null;
87
101
 
@@ -99,7 +113,7 @@ export default class Sprite extends Renderable {
99
113
  // length of the current animation name
100
114
  length : 0,
101
115
  //current frame texture offset
102
- offset : pool.pull("Vector2d"),
116
+ offset : pool.pull("Vector2d", 0, 0),
103
117
  // current frame size
104
118
  width : 0,
105
119
  height : 0,
@@ -138,17 +152,46 @@ export default class Sprite extends Renderable {
138
152
  }
139
153
  }
140
154
  } else {
141
- // HTMLImageElement/Canvas or {string}
155
+ // HTMLImageElement/HTMLVideoElementCanvas or {string}
142
156
  this.image = (typeof settings.image === "object") ? settings.image : getImage(settings.image);
143
157
  // throw an error if image ends up being null/undefined
144
158
  if (!this.image) {
145
159
  throw new Error("me.Sprite: '" + settings.image + "' image/texture not found!");
146
160
  }
147
- // update the default "current" frame size
148
- this.width = this.current.width = settings.framewidth = settings.framewidth || this.image.width;
149
- this.height = this.current.height = settings.frameheight = settings.frameheight || this.image.height;
150
- this.source = renderer.cache.get(this.image, settings);
151
- this.textureAtlas = this.source.getAtlas();
161
+
162
+ this.isVideo = HTMLVideoElement && this.image instanceof HTMLVideoElement;
163
+
164
+ if (this.isVideo) {
165
+ this.width = this.current.width = settings.framewidth = settings.framewidth || this.image.videoWidth;
166
+ this.height = this.current.height = settings.frameheight = settings.frameheight || this.image.videoHeight;
167
+ // video specific parameter
168
+ this.animationpause = this.image.autoplay !== true;
169
+ if (this.animationpause) {
170
+ this.image.pause();
171
+ }
172
+
173
+ // pause the video when losing focus
174
+ this._onBlurFn = () => { this.image.pause(); };
175
+ event.on(event.STATE_PAUSE, this._onBlurFn);
176
+
177
+ // call the onended when the video has ended
178
+ this.image.onended = () => {
179
+ if (typeof this.onended === "function") {
180
+ // prevent the video from restarting if video.loop is false
181
+ if (!this.image.loop) {
182
+ this.animationpause = true;
183
+ }
184
+ this.onended();
185
+ }
186
+ };
187
+
188
+ } else {
189
+ // update the default "current" frame size
190
+ this.width = this.current.width = settings.framewidth = settings.framewidth || this.image.width;
191
+ this.height = this.current.height = settings.frameheight = settings.frameheight || this.image.height;
192
+ this.source = renderer.cache.get(this.image, settings);
193
+ this.textureAtlas = this.source.getAtlas();
194
+ }
152
195
  }
153
196
 
154
197
  // store/reset the current atlas information if specified
@@ -196,7 +239,7 @@ export default class Sprite extends Renderable {
196
239
  }
197
240
 
198
241
  // addAnimation will return 0 if no texture atlas is defined
199
- if (this.addAnimation("default", null) !== 0) {
242
+ if (!this.isVideo && this.addAnimation("default", null) !== 0) {
200
243
  // set as default
201
244
  this.setCurrentAnimation("default");
202
245
  }
@@ -204,18 +247,28 @@ export default class Sprite extends Renderable {
204
247
 
205
248
  /**
206
249
  * return the flickering state of the object
207
- * @name isFlickering
208
- * @memberof Sprite
209
250
  * @returns {boolean}
210
251
  */
211
252
  isFlickering() {
212
253
  return this._flicker.isFlickering;
213
254
  }
214
255
 
256
+ /**
257
+ * play or resume the current animation or video
258
+ */
259
+ play() {
260
+ this.animationpause = false;
261
+ }
262
+
263
+ /**
264
+ * play or resume the current animation or video
265
+ */
266
+ pause() {
267
+ this.animationpause = true;
268
+ }
269
+
215
270
  /**
216
271
  * make the object flicker
217
- * @name flicker
218
- * @memberof Sprite
219
272
  * @param {number} duration - expressed in milliseconds
220
273
  * @param {Function} callback - Function to call when flickering ends
221
274
  * @returns {Sprite} Reference to this object for method chaining
@@ -244,11 +297,8 @@ export default class Sprite extends Renderable {
244
297
  * For fixed-sized cell sprite sheet, the index list must follow the
245
298
  * logic as per the following example :<br>
246
299
  * <img src="images/spritesheet_grid.png"/>
247
- * @name addAnimation
248
- * @memberof Sprite
249
300
  * @param {string} name - animation id
250
- * @param {number[]|string[]|object[]} index - list of sprite index or name
251
- * defining the animation. Can also use objects to specify delay for each frame, see below
301
+ * @param {number[]|string[]|object[]} index - list of sprite index or name defining the animation. Can also use objects to specify delay for each frame, see below
252
302
  * @param {number} [animationspeed] - cycling speed for animation in ms
253
303
  * @returns {number} frame amount of frame added to the animation (delay between each frame).
254
304
  * @see Sprite#animationspeed
@@ -343,8 +393,6 @@ export default class Sprite extends Renderable {
343
393
  /**
344
394
  * set the current animation
345
395
  * this will always change the animation & set the frame to zero
346
- * @name setCurrentAnimation
347
- * @memberof Sprite
348
396
  * @param {string} name - animation id
349
397
  * @param {string|Function} [resetAnim] - animation id to switch to when complete, or callback
350
398
  * @param {boolean} [preserve_dt=false] - if false will reset the elapsed time counter since last frame
@@ -405,8 +453,6 @@ export default class Sprite extends Renderable {
405
453
 
406
454
  /**
407
455
  * reverse the given or current animation if none is specified
408
- * @name reverseAnimation
409
- * @memberof Sprite
410
456
  * @param {string} [name] - animation id
411
457
  * @returns {Sprite} Reference to this object for method chaining
412
458
  * @see Sprite#animationspeed
@@ -423,8 +469,6 @@ export default class Sprite extends Renderable {
423
469
 
424
470
  /**
425
471
  * return true if the specified animation is the current one.
426
- * @name isCurrentAnimation
427
- * @memberof Sprite
428
472
  * @param {string} name - animation id
429
473
  * @returns {boolean}
430
474
  * @example
@@ -439,8 +483,6 @@ export default class Sprite extends Renderable {
439
483
  /**
440
484
  * change the current texture atlas region for this sprite
441
485
  * @see Texture.getRegion
442
- * @name setRegion
443
- * @memberof Sprite
444
486
  * @param {object} region - typically returned through me.Texture.getRegion()
445
487
  * @returns {Sprite} Reference to this object for method chaining
446
488
  * @example
@@ -472,8 +514,6 @@ export default class Sprite extends Renderable {
472
514
 
473
515
  /**
474
516
  * force the current animation frame index.
475
- * @name setAnimationFrame
476
- * @memberof Sprite
477
517
  * @param {number} [index=0] - animation frame index
478
518
  * @returns {Sprite} Reference to this object for method chaining
479
519
  * @example
@@ -487,8 +527,6 @@ export default class Sprite extends Renderable {
487
527
 
488
528
  /**
489
529
  * return the current animation frame index.
490
- * @name getCurrentAnimationFrame
491
- * @memberof Sprite
492
530
  * @returns {number} current animation frame index
493
531
  */
494
532
  getCurrentAnimationFrame() {
@@ -497,8 +535,6 @@ export default class Sprite extends Renderable {
497
535
 
498
536
  /**
499
537
  * Returns the frame object by the index.
500
- * @name getAnimationFrameObjectByIndex
501
- * @memberof Sprite
502
538
  * @ignore
503
539
  * @param {number} id - the frame id
504
540
  * @returns {number} if using number indices. Returns {object} containing frame data if using texture atlas
@@ -510,38 +546,51 @@ export default class Sprite extends Renderable {
510
546
  /**
511
547
  * update function. <br>
512
548
  * automatically called by the game manager {@link game}
513
- * @name update
514
- * @memberof Sprite
515
549
  * @protected
516
550
  * @param {number} dt - time since the last update in milliseconds.
517
551
  * @returns {boolean} true if the Sprite is dirty
518
552
  */
519
553
  update(dt) {
520
- // Update animation if necessary
521
- if (!this.animationpause && this.current.length > 1) {
522
- let duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay;
523
- this.dt += dt;
524
- while (this.dt >= duration) {
525
- this.isDirty = true;
526
- this.dt -= duration;
527
-
528
- let nextFrame = (this.current.length > 1 ? this.current.idx + 1 : this.current.idx);
529
- this.setAnimationFrame(nextFrame);
530
-
531
- // Switch animation if we reach the end of the strip and a callback is defined
532
- if (this.current.idx === 0 && typeof this.resetAnim === "function") {
533
- // Otherwise is must be callable
534
- if (this.resetAnim() === false) {
535
- // Reset to last frame
536
- this.setAnimationFrame(this.current.length - 1);
537
-
538
- // Bail early without skipping any more frames.
539
- this.dt %= duration;
540
- break;
554
+ // play/pause video if necessary
555
+ if (this.isVideo) {
556
+ if (this.animationpause) {
557
+ this.image.pause();
558
+ } else if (this.image.paused) {
559
+ this.image.play();
560
+ }
561
+ this.isDirty = !this.image.paused;
562
+ } else {
563
+ // Update animation if necessary
564
+ if (!this.animationpause && this.current.length > 1) {
565
+ let duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay;
566
+ this.dt += dt;
567
+ while (this.dt >= duration) {
568
+ this.isDirty = true;
569
+ this.dt -= duration;
570
+
571
+ let nextFrame = (this.current.length > 1 ? this.current.idx + 1 : this.current.idx);
572
+ this.setAnimationFrame(nextFrame);
573
+
574
+ // Switch animation if we reach the end of the strip and a callback is defined
575
+ if (this.current.idx === 0) {
576
+ if (typeof this.onended === "function") {
577
+ this.onended();
578
+ }
579
+ if (typeof this.resetAnim === "function") {
580
+ // Otherwise is must be callable
581
+ if (this.resetAnim() === false) {
582
+ // Reset to last frame
583
+ this.setAnimationFrame(this.current.length - 1);
584
+
585
+ // Bail early without skipping any more frames.
586
+ this.dt %= duration;
587
+ break;
588
+ }
589
+ }
541
590
  }
591
+ // Get next frame duration
592
+ duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay;
542
593
  }
543
- // Get next frame duration
544
- duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay;
545
594
  }
546
595
  }
547
596
 
@@ -560,20 +609,8 @@ export default class Sprite extends Renderable {
560
609
  return super.update(dt);
561
610
  }
562
611
 
563
- /**
564
- * Destroy function<br>
565
- * @ignore
566
- */
567
- destroy() {
568
- pool.push(this.offset);
569
- this.offset = undefined;
570
- super.destroy();
571
- }
572
-
573
612
  /**
574
613
  * draw this srite (automatically called by melonJS)
575
- * @name draw
576
- * @memberof Sprite
577
614
  * @protected
578
615
  * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer instance
579
616
  * @param {Camera2d} [viewport] - the viewport to (re)draw
@@ -620,4 +657,22 @@ export default class Sprite extends Renderable {
620
657
  w, h // dw,dh
621
658
  );
622
659
  }
660
+
661
+ /**
662
+ * Destroy function<br>
663
+ * @ignore
664
+ */
665
+ destroy() {
666
+ pool.push(this.offset);
667
+ this.offset = undefined;
668
+ if (this.isVideo) {
669
+ event.off(event.STATE_PAUSE, this._onBlurFn);
670
+ this._onBlurFn = undefined;
671
+ this.image.onended = undefined;
672
+ this.image.pause();
673
+ this.image.currentTime = 0;
674
+ }
675
+ this.image = undefined;
676
+ super.destroy();
677
+ }
623
678
  }
@@ -17,6 +17,9 @@ let swipeEnabled = true;
17
17
  // a cache DOMRect object
18
18
  let domRect = {left: 0, top: 0, x: 0, y: 0, width: 0, height: 0, right: 0, bottom: 0};
19
19
 
20
+ // a list of supported videoCodecs;
21
+ let videoCodecs;
22
+
20
23
  function disableSwipeFn(e) {
21
24
  e.preventDefault();
22
25
  if (typeof globalThis.scroll === "function") {
@@ -215,6 +218,18 @@ export const hasHTML5Audio = (typeof globalThis.Audio !== "undefined");
215
218
  */
216
219
  export const sound = hasWebAudio || hasHTML5Audio;
217
220
 
221
+
222
+ /**
223
+ * Device Video Support
224
+ * @name hasVideo
225
+ * @memberof device
226
+ * @type {boolean}
227
+ * @readonly
228
+ * @public
229
+ */
230
+ export const hasVideo = typeof globalThis.document !== "undefined" && !!globalThis.document.createElement("video").canPlayType;
231
+
232
+
218
233
  /**
219
234
  * Browser Local Storage capabilities <br>
220
235
  * (this flag will be set to false if cookies are blocked)
@@ -919,3 +934,31 @@ export function vibrate(pattern) {
919
934
  globalThis.navigator.vibrate(pattern);
920
935
  }
921
936
  }
937
+
938
+ /**
939
+ * detect if the given video format is supported
940
+ * @function hasVideoFormat
941
+ * @param {"h264"|"h265"|"ogg"|"mp4"|"m4v"|"webm"|"vp9"|"hls"} codec - the video format to check for support
942
+ * @returns {boolean} return true if the given video format is supported
943
+ */
944
+ export function hasVideoFormat(codec) {
945
+ let result = false;
946
+ if (hasVideo === true) {
947
+ if (typeof videoCodecs === "undefined") {
948
+ // check for support
949
+ const videoElement = document.createElement("video");
950
+ videoCodecs = {
951
+ h264:videoElement.canPlayType('video/mp4; codecs="avc1.42E01E"').replace(/^no$/, ""),
952
+ h265:videoElement.canPlayType('video/mp4; codecs="hev1"').replace(/^no$/, ""),
953
+ ogg:videoElement.canPlayType('video/ogg; codecs="theora"').replace(/^no$/, ""),
954
+ mp4:videoElement.canPlayType('video/mp4; codecs="avc1.42E01E"').replace(/^no$/, ""),
955
+ m4v:videoElement.canPlayType("video/x-m4v").replace(/^no$/, ""),
956
+ webm:videoElement.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/^no$/, ""),
957
+ vp9:videoElement.canPlayType('video/webm; codecs="vp9"').replace(/^no$/, ""),
958
+ hls:videoElement.canPlayType('application/x-mpegURL; codecs="avc1.42E01E"').replace(/^no$/, "")
959
+ };
960
+ }
961
+ result = !!videoCodecs[codec];
962
+ }
963
+ return result;
964
+ }
@@ -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
  }
@@ -88,7 +88,7 @@ class TextureCache {
88
88
 
89
89
  if (typeof entry === "undefined") {
90
90
  if (!atlas) {
91
- atlas = createAtlas(image.width, image.height, image.src ? fileUtil.getBasename(image.src) : undefined);
91
+ atlas = createAtlas(image.width || image.videoWidth, image.videoHeight, image.src ? fileUtil.getBasename(image.src) : undefined);
92
92
  }
93
93
  entry = new TextureAtlas(atlas, image, false);
94
94
  this.set(image, entry);
@@ -132,8 +132,8 @@ class TextureCache {
132
132
  * @ignore
133
133
  */
134
134
  set(image, texture) {
135
- let width = image.width;
136
- let height = image.height;
135
+ let width = image.width || image.videoWidth;
136
+ let height = image.height || image.videoHeight;
137
137
 
138
138
  // warn if a non POT texture is added to the cache when using WebGL1
139
139
  if (renderer.WebGLVersion === 1 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))) {