melonjs 10.6.1 → 10.7.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.
@@ -54,6 +54,19 @@ class CanvasRenderer extends Renderer {
54
54
  // enable the tile texture seam fix with the canvas renderer
55
55
  this.uvOffset = 1;
56
56
  }
57
+
58
+ // context lost & restore event for canvas
59
+ this.getScreenCanvas().addEventListener("contextlost", (e) => {
60
+ e.preventDefault();
61
+ this.isContextValid = false;
62
+ event.emit(event.ONCONTEXT_LOST, this);
63
+ }, false );
64
+ // ctx.restoreContext()
65
+ this.getScreenCanvas().addEventListener("contextrestored", () => {
66
+ this.isContextValid = true;
67
+ event.emit(event.ONCONTEXT_RESTORED, this);
68
+ me.game.repaint();
69
+ }, false );
57
70
  }
58
71
 
59
72
  /**
@@ -84,7 +97,7 @@ class CanvasRenderer extends Renderer {
84
97
  * <img src="images/normal-blendmode.png" width="510"/> <br>
85
98
  * - "multiply" : the pixels of the top layer are multiplied with the corresponding pixel of the bottom layer. A darker picture is the result. <br>
86
99
  * <img src="images/multiply-blendmode.png" width="510"/> <br>
87
- * - "lighter" : where both content overlap the color is determined by adding color values. <br>
100
+ * - "additive or lighter" : where both content overlap the color is determined by adding color values. <br>
88
101
  * <img src="images/lighter-blendmode.png" width="510"/> <br>
89
102
  * - "screen" : The pixels are inverted, multiplied, and inverted again. A lighter picture is the result (opposite of multiply) <br>
90
103
  * <img src="images/screen-blendmode.png" width="510"/> <br>
@@ -92,7 +105,7 @@ class CanvasRenderer extends Renderer {
92
105
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
93
106
  * @memberof CanvasRenderer.prototype
94
107
  * @function
95
- * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter, "screen"
108
+ * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter, "additive", "screen"
96
109
  * @param {CanvasRenderingContext2D} [context]
97
110
  */
98
111
  setBlendMode(mode = "normal", context) {
@@ -104,6 +117,7 @@ class CanvasRenderer extends Renderer {
104
117
  break;
105
118
 
106
119
  case "lighter" :
120
+ case "additive" :
107
121
  context.globalCompositeOperation = "lighter";
108
122
  break;
109
123
 
@@ -63,9 +63,9 @@ class Renderer {
63
63
  if (device.ejecta === true) {
64
64
  // a main canvas is already automatically created by Ejecta
65
65
  this.canvas = document.getElementById("canvas");
66
- } else if (typeof window.canvas !== "undefined") {
66
+ } else if (typeof globalThis.canvas !== "undefined") {
67
67
  // a global canvas is available, e.g. webapp adapter for wechat
68
- this.canvas = window.canvas;
68
+ this.canvas = globalThis.canvas;
69
69
  } else if (typeof this.settings.canvas !== "undefined") {
70
70
  this.canvas = this.settings.canvas;
71
71
  } else {
@@ -18,7 +18,7 @@ var designHeight = 0;
18
18
 
19
19
  // default video settings
20
20
  var settings = {
21
- parent : document.body,
21
+ parent : undefined,
22
22
  renderer : 2, // AUTO
23
23
  doubleBuffering : false,
24
24
  autoScale : false,
@@ -64,8 +64,8 @@ function onresize() {
64
64
  var canvasMaxWidth = Infinity;
65
65
  var canvasMaxHeight = Infinity;
66
66
 
67
- if (window.getComputedStyle) {
68
- var style = window.getComputedStyle(renderer.getScreenCanvas(), null);
67
+ if (globalThis.getComputedStyle) {
68
+ var style = globalThis.getComputedStyle(renderer.getScreenCanvas(), null);
69
69
  canvasMaxWidth = parseInt(style.maxWidth, 10) || Infinity;
70
70
  canvasMaxHeight = parseInt(style.maxHeight, 10) || Infinity;
71
71
  }
@@ -276,7 +276,7 @@ export function init(width, height, options) {
276
276
  settings.zoomY = height * scaleRatio.y;
277
277
 
278
278
  //add a channel for the onresize/onorientationchange event
279
- window.addEventListener(
279
+ globalThis.addEventListener(
280
280
  "resize",
281
281
  utils.function.throttle(
282
282
  function (e) {
@@ -286,7 +286,7 @@ export function init(width, height, options) {
286
286
  );
287
287
 
288
288
  // Screen Orientation API
289
- window.addEventListener(
289
+ globalThis.addEventListener(
290
290
  "orientationchange",
291
291
  function (e) {
292
292
  event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
@@ -294,7 +294,7 @@ export function init(width, height, options) {
294
294
  false
295
295
  );
296
296
  // pre-fixed implementation on mozzila
297
- window.addEventListener(
297
+ globalThis.addEventListener(
298
298
  "onmozorientationchange",
299
299
  function (e) {
300
300
  event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
@@ -303,13 +303,13 @@ export function init(width, height, options) {
303
303
  );
304
304
 
305
305
  if (device.ScreenOrientation === true) {
306
- window.screen.orientation.onchange = function (e) {
306
+ globalThis.screen.orientation.onchange = function (e) {
307
307
  event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
308
308
  };
309
309
  }
310
310
 
311
311
  // Automatically update relative canvas position on scroll
312
- window.addEventListener("scroll", utils.function.throttle(
312
+ globalThis.addEventListener("scroll", utils.function.throttle(
313
313
  function (e) {
314
314
  event.emit(event.WINDOW_ONSCROLL, e);
315
315
  }, 100
@@ -336,14 +336,14 @@ export function init(width, height, options) {
336
336
  }
337
337
 
338
338
  // add our canvas (default to document.body if settings.parent is undefined)
339
- parent = device.getElement(settings.parent);
339
+ parent = device.getElement(typeof settings.parent !== "undefined" ? settings.parent : document.body);
340
340
  parent.appendChild(renderer.getScreenCanvas());
341
341
 
342
342
  // trigger an initial resize();
343
343
  onresize();
344
344
 
345
345
  // add an observer to detect when the dom tree is modified
346
- if ("MutationObserver" in window) {
346
+ if ("MutationObserver" in globalThis) {
347
347
  // Create an observer instance linked to the callback function
348
348
  var observer = new MutationObserver(onresize.bind(this));
349
349
 
@@ -362,7 +362,7 @@ export function init(width, height, options) {
362
362
  renderType + " renderer" + gpu_renderer + " | " +
363
363
  audioType + " | " +
364
364
  "pixel ratio " + device.devicePixelRatio + " | " +
365
- (device.isMobile ? "mobile" : "desktop") + " | " +
365
+ (device.nodeJS ? "node.js" : device.isMobile ? "mobile" : "desktop") + " | " +
366
366
  device.getScreenOrientation() + " | " +
367
367
  device.language
368
368
  );
@@ -284,7 +284,7 @@ class GLShader {
284
284
  this.uniforms = extractUniforms(this.gl, this);
285
285
 
286
286
  // destroy the shader on context lost (will be recreated on context restore)
287
- event.on(event.WEBGL_ONCONTEXT_LOST, this.destroy, this);
287
+ event.on(event.ONCONTEXT_LOST, this.destroy, this);
288
288
  }
289
289
 
290
290
  /**
@@ -163,19 +163,19 @@ class WebGLRenderer extends Renderer {
163
163
  // Create a texture cache
164
164
  this.cache = new TextureCache(this.maxTextures);
165
165
 
166
- // to simulate context lost and restore :
166
+ // to simulate context lost and restore in WebGL:
167
167
  // var ctx = me.video.renderer.context.getExtension('WEBGL_lose_context');
168
168
  // ctx.loseContext()
169
169
  this.getScreenCanvas().addEventListener("webglcontextlost", (e) => {
170
170
  e.preventDefault();
171
171
  this.isContextValid = false;
172
- event.emit(event.WEBGL_ONCONTEXT_LOST, this);
172
+ event.emit(event.ONCONTEXT_LOST, this);
173
173
  }, false );
174
174
  // ctx.restoreContext()
175
175
  this.getScreenCanvas().addEventListener("webglcontextrestored", () => {
176
176
  this.reset();
177
177
  this.isContextValid = true;
178
- event.emit(event.WEBGL_ONCONTEXT_RESTORED, this);
178
+ event.emit(event.ONCONTEXT_RESTORED, this);
179
179
  }, false );
180
180
  }
181
181
 
@@ -570,7 +570,7 @@ class WebGLRenderer extends Renderer {
570
570
  * <img src="images/normal-blendmode.png" width="510"/> <br>
571
571
  * - "multiply" : the pixels of the top layer are multiplied with the corresponding pixel of the bottom layer. A darker picture is the result. <br>
572
572
  * <img src="images/multiply-blendmode.png" width="510"/> <br>
573
- * - "lighter" : where both content overlap the color is determined by adding color values. <br>
573
+ * - "additive or lighter" : where both content overlap the color is determined by adding color values. <br>
574
574
  * <img src="images/lighter-blendmode.png" width="510"/> <br>
575
575
  * - "screen" : The pixels are inverted, multiplied, and inverted again. A lighter picture is the result (opposite of multiply) <br>
576
576
  * <img src="images/screen-blendmode.png" width="510"/> <br>
@@ -578,7 +578,7 @@ class WebGLRenderer extends Renderer {
578
578
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
579
579
  * @memberof WebGLRenderer.prototype
580
580
  * @function
581
- * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter", "screen"
581
+ * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter", "additive", "screen"
582
582
  * @param {WebGLRenderingContext} [gl]
583
583
  */
584
584
  setBlendMode(mode = "normal", gl = this.gl) {
@@ -594,6 +594,7 @@ class WebGLRenderer extends Renderer {
594
594
  break;
595
595
 
596
596
  case "lighter" :
597
+ case "additive" :
597
598
  gl.blendFunc(gl.ONE, gl.ONE);
598
599
  break;
599
600
 
@@ -1,95 +0,0 @@
1
- import Container from "./../renderable/container.js";
2
- import { viewport } from "./../game.js";
3
-
4
- /**
5
- * @classdesc
6
- * Particle Container Object.
7
- * @class ParticleContainer
8
- * @augments Container
9
- * @param {ParticleEmitter} emitter the emitter which owns this container
10
- */
11
-
12
- class ParticleContainer extends Container {
13
-
14
- /**
15
- * @ignore
16
- */
17
- constructor(emitter) {
18
- // call the super constructor
19
- super(
20
- viewport.pos.x,
21
- viewport.pos.y,
22
- viewport.width,
23
- viewport.height
24
- );
25
-
26
- // don't sort the particles by z-index
27
- this.autoSort = false;
28
-
29
- // count the updates
30
- this._updateCount = 0;
31
-
32
- // internally store how much time was skipped when frames are skipped
33
- this._dt = 0;
34
-
35
- // cache the emitter for later use
36
- this._emitter = emitter;
37
-
38
- this.autoTransform = false;
39
-
40
- this.anchorPoint.set(0, 0);
41
-
42
- this.isKinematic = true;
43
- }
44
-
45
- /**
46
- * @ignore
47
- */
48
- update(dt) {
49
- // skip frames if necessary
50
- if (++this._updateCount > this._emitter.framesToSkip) {
51
- this._updateCount = 0;
52
- }
53
- if (this._updateCount > 0) {
54
- this._dt += dt;
55
- return false;
56
- }
57
-
58
- // apply skipped delta time
59
- dt += this._dt;
60
- this._dt = 0;
61
-
62
- // Update particles and remove them if they are dead
63
- for (var i = this.children.length - 1; i >= 0; --i) {
64
- var particle = this.children[i];
65
- particle.inViewport = viewport.isVisible(particle, this.floating);
66
- if (!particle.update(dt)) {
67
- this.removeChildNow(particle);
68
- }
69
- }
70
- return true;
71
- }
72
-
73
- /**
74
- * @ignore
75
- */
76
- draw(renderer, rect) {
77
- if (this.children.length > 0) {
78
- var context = renderer.getContext(),
79
- gco;
80
- // Check for additive draw
81
- if (this._emitter.textureAdditive) {
82
- gco = context.globalCompositeOperation;
83
- context.globalCompositeOperation = "lighter";
84
- }
85
-
86
- super.draw(renderer, rect);
87
-
88
- // Restore globalCompositeOperation
89
- if (this._emitter.textureAdditive) {
90
- context.globalCompositeOperation = gco;
91
- }
92
- }
93
- }
94
- };
95
- export default ParticleContainer;