melonjs 10.9.0 → 10.10.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.
@@ -2,8 +2,8 @@ import Color from "./../../math/color.js";
2
2
  import Matrix2d from "./../../math/matrix2.js";
3
3
  import WebGLCompositor from "./webgl_compositor.js";
4
4
  import Renderer from "./../renderer.js";
5
- import TextureCache from "./../texture_cache.js";
6
- import { TextureAtlas, createAtlas } from "./../texture.js";
5
+ import TextureCache from "./../texture/cache.js";
6
+ import { TextureAtlas, createAtlas } from "./../texture/atlas.js";
7
7
  import { createCanvas, renderer } from "./../video.js";
8
8
  import * as event from "./../../system/event.js";
9
9
  import pool from "./../../system/pooling.js";
@@ -70,8 +70,8 @@ class WebGLRenderer extends Renderer {
70
70
  /**
71
71
  * The WebGL context
72
72
  * @name gl
73
- * @memberof WebGLRenderer#
74
- * type {WebGLRenderingContext}
73
+ * @memberof WebGLRenderer
74
+ * @type {WebGLRenderingContext}
75
75
  */
76
76
  this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);
77
77
 
@@ -171,7 +171,7 @@ class WebGLRenderer extends Renderer {
171
171
  /**
172
172
  * Reset context state
173
173
  * @name reset
174
- * @memberof WebGLRenderer.prototype
174
+ * @memberof WebGLRenderer
175
175
  * @function
176
176
  */
177
177
  reset() {
@@ -198,7 +198,7 @@ class WebGLRenderer extends Renderer {
198
198
  * @name setCompositor
199
199
  * @function
200
200
  * @param {WebGLCompositor|string} compositor a compositor name or instance
201
- * @memberof WebGLRenderer.prototype
201
+ * @memberof WebGLRenderer
202
202
  * @function
203
203
  */
204
204
  setCompositor(compositor = "default") {
@@ -224,7 +224,7 @@ class WebGLRenderer extends Renderer {
224
224
  /**
225
225
  * Reset the gl transform to identity
226
226
  * @name resetTransform
227
- * @memberof WebGLRenderer.prototype
227
+ * @memberof WebGLRenderer
228
228
  * @function
229
229
  */
230
230
  resetTransform() {
@@ -271,7 +271,7 @@ class WebGLRenderer extends Renderer {
271
271
  /**
272
272
  * Create a pattern with the specified repetition
273
273
  * @name createPattern
274
- * @memberof WebGLRenderer.prototype
274
+ * @memberof WebGLRenderer
275
275
  * @function
276
276
  * @param {Image} image Source image
277
277
  * @param {string} repeat Define how the pattern should be repeated
@@ -304,7 +304,7 @@ class WebGLRenderer extends Renderer {
304
304
  /**
305
305
  * Flush the compositor to the frame buffer
306
306
  * @name flush
307
- * @memberof WebGLRenderer.prototype
307
+ * @memberof WebGLRenderer
308
308
  * @function
309
309
  */
310
310
  flush() {
@@ -314,7 +314,7 @@ class WebGLRenderer extends Renderer {
314
314
  /**
315
315
  * set/change the current projection matrix (WebGL only)
316
316
  * @name setProjection
317
- * @memberof WebGLRenderer.prototype
317
+ * @memberof WebGLRenderer
318
318
  * @function
319
319
  * @param {Matrix3d} matrix
320
320
  */
@@ -326,7 +326,7 @@ class WebGLRenderer extends Renderer {
326
326
  /**
327
327
  * Clears the gl context with the given color.
328
328
  * @name clearColor
329
- * @memberof WebGLRenderer.prototype
329
+ * @memberof WebGLRenderer
330
330
  * @function
331
331
  * @param {Color|string} [color="#000000"] CSS color.
332
332
  * @param {boolean} [opaque=false] Allow transparency [default] or clear the surface completely [true]
@@ -354,7 +354,7 @@ class WebGLRenderer extends Renderer {
354
354
  /**
355
355
  * Erase the pixels in the given rectangular area by setting them to transparent black (rgba(0,0,0,0)).
356
356
  * @name clearRect
357
- * @memberof WebGLRenderer.prototype
357
+ * @memberof WebGLRenderer
358
358
  * @function
359
359
  * @param {number} x x axis of the coordinate for the rectangle starting point.
360
360
  * @param {number} y y axis of the coordinate for the rectangle starting point.
@@ -404,7 +404,7 @@ class WebGLRenderer extends Renderer {
404
404
  /**
405
405
  * Draw an image to the gl context
406
406
  * @name drawImage
407
- * @memberof WebGLRenderer.prototype
407
+ * @memberof WebGLRenderer
408
408
  * @function
409
409
  * @param {Image} image An element to draw into the context. The specification permits any canvas image source (CanvasImageSource), specifically, a CSSImageValue, an HTMLImageElement, an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, or an OffscreenCanvas.
410
410
  * @param {number} sx The X coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
@@ -457,7 +457,7 @@ class WebGLRenderer extends Renderer {
457
457
  /**
458
458
  * Draw a pattern within the given rectangle.
459
459
  * @name drawPattern
460
- * @memberof WebGLRenderer.prototype
460
+ * @memberof WebGLRenderer
461
461
  * @function
462
462
  * @param {TextureAtlas} pattern Pattern object
463
463
  * @param {number} x
@@ -475,7 +475,7 @@ class WebGLRenderer extends Renderer {
475
475
  /**
476
476
  * return a reference to the screen canvas corresponding WebGL Context
477
477
  * @name getScreenContext
478
- * @memberof WebGLRenderer.prototype
478
+ * @memberof WebGLRenderer
479
479
  * @function
480
480
  * @returns {WebGLRenderingContext}
481
481
  */
@@ -486,7 +486,7 @@ class WebGLRenderer extends Renderer {
486
486
  /**
487
487
  * Returns the WebGL Context object of the given Canvas
488
488
  * @name getContextGL
489
- * @memberof WebGLRenderer.prototype
489
+ * @memberof WebGLRenderer
490
490
  * @function
491
491
  * @param {HTMLCanvasElement} canvas
492
492
  * @param {boolean} [transparent=true] use false to disable transparency
@@ -544,7 +544,7 @@ class WebGLRenderer extends Renderer {
544
544
  * Returns the WebGLContext instance for the renderer
545
545
  * return a reference to the system 2d Context
546
546
  * @name getContext
547
- * @memberof WebGLRenderer.prototype
547
+ * @memberof WebGLRenderer
548
548
  * @function
549
549
  * @returns {WebGLRenderingContext}
550
550
  */
@@ -565,7 +565,7 @@ class WebGLRenderer extends Renderer {
565
565
  * <img src="images/screen-blendmode.png" width="510"/> <br>
566
566
  * @name setBlendMode
567
567
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
568
- * @memberof WebGLRenderer.prototype
568
+ * @memberof WebGLRenderer
569
569
  * @function
570
570
  * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter", "additive", "screen"
571
571
  * @param {WebGLRenderingContext} [gl]
@@ -616,7 +616,7 @@ class WebGLRenderer extends Renderer {
616
616
  /**
617
617
  * restores the canvas context
618
618
  * @name restore
619
- * @memberof WebGLRenderer.prototype
619
+ * @memberof WebGLRenderer
620
620
  * @function
621
621
  */
622
622
  restore() {
@@ -652,7 +652,7 @@ class WebGLRenderer extends Renderer {
652
652
  /**
653
653
  * saves the canvas context
654
654
  * @name save
655
- * @memberof WebGLRenderer.prototype
655
+ * @memberof WebGLRenderer
656
656
  * @function
657
657
  */
658
658
  save() {
@@ -670,7 +670,7 @@ class WebGLRenderer extends Renderer {
670
670
  /**
671
671
  * rotates the uniform matrix
672
672
  * @name rotate
673
- * @memberof WebGLRenderer.prototype
673
+ * @memberof WebGLRenderer
674
674
  * @function
675
675
  * @param {number} angle in radians
676
676
  */
@@ -681,7 +681,7 @@ class WebGLRenderer extends Renderer {
681
681
  /**
682
682
  * scales the uniform matrix
683
683
  * @name scale
684
- * @memberof WebGLRenderer.prototype
684
+ * @memberof WebGLRenderer
685
685
  * @function
686
686
  * @param {number} x
687
687
  * @param {number} y
@@ -702,7 +702,7 @@ class WebGLRenderer extends Renderer {
702
702
  /**
703
703
  * Set the global alpha
704
704
  * @name setGlobalAlpha
705
- * @memberof WebGLRenderer.prototype
705
+ * @memberof WebGLRenderer
706
706
  * @function
707
707
  * @param {number} alpha 0.0 to 1.0 values accepted.
708
708
  */
@@ -713,7 +713,7 @@ class WebGLRenderer extends Renderer {
713
713
  /**
714
714
  * Return the global alpha
715
715
  * @name getGlobalAlpha
716
- * @memberof WebGLRenderer.prototype
716
+ * @memberof WebGLRenderer
717
717
  * @function
718
718
  * @returns {number} global alpha value
719
719
  */
@@ -725,7 +725,7 @@ class WebGLRenderer extends Renderer {
725
725
  * Set the current fill & stroke style color.
726
726
  * By default, or upon reset, the value is set to #000000.
727
727
  * @name setColor
728
- * @memberof WebGLRenderer.prototype
728
+ * @memberof WebGLRenderer
729
729
  * @function
730
730
  * @param {Color|string} color css color string.
731
731
  */
@@ -738,7 +738,7 @@ class WebGLRenderer extends Renderer {
738
738
  /**
739
739
  * Set the line width
740
740
  * @name setLineWidth
741
- * @memberof WebGLRenderer.prototype
741
+ * @memberof WebGLRenderer
742
742
  * @function
743
743
  * @param {number} width Line width
744
744
  */
@@ -749,7 +749,7 @@ class WebGLRenderer extends Renderer {
749
749
  /**
750
750
  * Stroke an arc at the specified coordinates with given radius, start and end points
751
751
  * @name strokeArc
752
- * @memberof WebGLRenderer.prototype
752
+ * @memberof WebGLRenderer
753
753
  * @function
754
754
  * @param {number} x arc center point x-axis
755
755
  * @param {number} y arc center point y-axis
@@ -777,7 +777,7 @@ class WebGLRenderer extends Renderer {
777
777
  /**
778
778
  * Fill an arc at the specified coordinates with given radius, start and end points
779
779
  * @name fillArc
780
- * @memberof WebGLRenderer.prototype
780
+ * @memberof WebGLRenderer
781
781
  * @function
782
782
  * @param {number} x arc center point x-axis
783
783
  * @param {number} y arc center point y-axis
@@ -793,7 +793,7 @@ class WebGLRenderer extends Renderer {
793
793
  /**
794
794
  * Stroke an ellipse at the specified coordinates with given radius
795
795
  * @name strokeEllipse
796
- * @memberof WebGLRenderer.prototype
796
+ * @memberof WebGLRenderer
797
797
  * @function
798
798
  * @param {number} x ellipse center point x-axis
799
799
  * @param {number} y ellipse center point y-axis
@@ -819,7 +819,7 @@ class WebGLRenderer extends Renderer {
819
819
  /**
820
820
  * Fill an ellipse at the specified coordinates with given radius
821
821
  * @name fillEllipse
822
- * @memberof WebGLRenderer.prototype
822
+ * @memberof WebGLRenderer
823
823
  * @function
824
824
  * @param {number} x ellipse center point x-axis
825
825
  * @param {number} y ellipse center point y-axis
@@ -833,7 +833,7 @@ class WebGLRenderer extends Renderer {
833
833
  /**
834
834
  * Stroke a line of the given two points
835
835
  * @name strokeLine
836
- * @memberof WebGLRenderer.prototype
836
+ * @memberof WebGLRenderer
837
837
  * @function
838
838
  * @param {number} startX the start x coordinate
839
839
  * @param {number} startY the start y coordinate
@@ -855,7 +855,7 @@ class WebGLRenderer extends Renderer {
855
855
  /**
856
856
  * Fill a line of the given two points
857
857
  * @name fillLine
858
- * @memberof WebGLRenderer.prototype
858
+ * @memberof WebGLRenderer
859
859
  * @function
860
860
  * @param {number} startX the start x coordinate
861
861
  * @param {number} startY the start y coordinate
@@ -869,7 +869,7 @@ class WebGLRenderer extends Renderer {
869
869
  /**
870
870
  * Stroke a me.Polygon on the screen with a specified color
871
871
  * @name strokePolygon
872
- * @memberof WebGLRenderer.prototype
872
+ * @memberof WebGLRenderer
873
873
  * @function
874
874
  * @param {Polygon} poly the shape to draw
875
875
  * @param {boolean} [fill=false] also fill the shape with the current color if true
@@ -901,7 +901,7 @@ class WebGLRenderer extends Renderer {
901
901
  /**
902
902
  * Fill a me.Polygon on the screen
903
903
  * @name fillPolygon
904
- * @memberof WebGLRenderer.prototype
904
+ * @memberof WebGLRenderer
905
905
  * @function
906
906
  * @param {Polygon} poly the shape to draw
907
907
  */
@@ -912,7 +912,7 @@ class WebGLRenderer extends Renderer {
912
912
  /**
913
913
  * Draw a stroke rectangle at the specified coordinates
914
914
  * @name strokeRect
915
- * @memberof WebGLRenderer.prototype
915
+ * @memberof WebGLRenderer
916
916
  * @function
917
917
  * @param {number} x
918
918
  * @param {number} y
@@ -937,7 +937,7 @@ class WebGLRenderer extends Renderer {
937
937
  /**
938
938
  * Draw a filled rectangle at the specified coordinates
939
939
  * @name fillRect
940
- * @memberof WebGLRenderer.prototype
940
+ * @memberof WebGLRenderer
941
941
  * @function
942
942
  * @param {number} x
943
943
  * @param {number} y
@@ -951,7 +951,7 @@ class WebGLRenderer extends Renderer {
951
951
  /**
952
952
  * Stroke a rounded rectangle at the specified coordinates
953
953
  * @name strokeRoundRect
954
- * @memberof WebGLRenderer.prototype
954
+ * @memberof WebGLRenderer
955
955
  * @function
956
956
  * @param {number} x
957
957
  * @param {number} y
@@ -978,7 +978,7 @@ class WebGLRenderer extends Renderer {
978
978
  /**
979
979
  * Draw a rounded filled rectangle at the specified coordinates
980
980
  * @name fillRoundRect
981
- * @memberof WebGLRenderer.prototype
981
+ * @memberof WebGLRenderer
982
982
  * @function
983
983
  * @param {number} x
984
984
  * @param {number} y
@@ -994,7 +994,7 @@ class WebGLRenderer extends Renderer {
994
994
  * Reset (overrides) the renderer transformation matrix to the
995
995
  * identity one, and then apply the given transformation matrix.
996
996
  * @name setTransform
997
- * @memberof WebGLRenderer.prototype
997
+ * @memberof WebGLRenderer
998
998
  * @function
999
999
  * @param {Matrix2d} mat2d Matrix to transform by
1000
1000
  */
@@ -1006,7 +1006,7 @@ class WebGLRenderer extends Renderer {
1006
1006
  /**
1007
1007
  * Multiply given matrix into the renderer tranformation matrix
1008
1008
  * @name transform
1009
- * @memberof WebGLRenderer.prototype
1009
+ * @memberof WebGLRenderer
1010
1010
  * @function
1011
1011
  * @param {Matrix2d} mat2d Matrix to transform by
1012
1012
  */
@@ -1024,7 +1024,7 @@ class WebGLRenderer extends Renderer {
1024
1024
  /**
1025
1025
  * Translates the uniform matrix by the given coordinates
1026
1026
  * @name translate
1027
- * @memberof WebGLRenderer.prototype
1027
+ * @memberof WebGLRenderer
1028
1028
  * @function
1029
1029
  * @param {number} x
1030
1030
  * @param {number} y
@@ -1047,7 +1047,7 @@ class WebGLRenderer extends Renderer {
1047
1047
  * and restore it (with the restore() method) any time in the future.
1048
1048
  * (<u>this is an experimental feature !</u>)
1049
1049
  * @name clipRect
1050
- * @memberof WebGLRenderer.prototype
1050
+ * @memberof WebGLRenderer
1051
1051
  * @function
1052
1052
  * @param {number} x
1053
1053
  * @param {number} y
@@ -1095,31 +1095,46 @@ class WebGLRenderer extends Renderer {
1095
1095
  * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
1096
1096
  * Mask are not preserved through renderer context save and restore.
1097
1097
  * @name setMask
1098
- * @memberof WebGLRenderer.prototype
1098
+ * @memberof WebGLRenderer
1099
1099
  * @function
1100
- * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
1100
+ * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] a shape defining the mask to be applied
1101
+ * @param {boolean} [invert=false] either the given shape should define what is visible (default) or the opposite
1101
1102
  */
1102
- setMask(mask) {
1103
+ setMask(mask, invert = false) {
1103
1104
  var gl = this.gl;
1104
1105
 
1105
1106
  // flush the compositor
1106
1107
  this.flush();
1107
1108
 
1108
- // Enable and setup GL state to write to stencil buffer
1109
- gl.enable(gl.STENCIL_TEST);
1110
- gl.clear(gl.STENCIL_BUFFER_BIT);
1109
+ if (this.maskLevel === 0) {
1110
+ // Enable and setup GL state to write to stencil buffer
1111
+ gl.enable(gl.STENCIL_TEST);
1112
+ gl.clear(gl.STENCIL_BUFFER_BIT);
1113
+
1114
+
1115
+ }
1116
+
1117
+ this.maskLevel++;
1118
+
1111
1119
  gl.colorMask(false, false, false, false);
1112
- gl.stencilFunc(gl.NOTEQUAL, 1, 1);
1120
+ gl.stencilFunc(gl.EQUAL, this.maskLevel, 1);
1113
1121
  gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
1114
1122
 
1123
+
1124
+ // fill the given mask shape
1115
1125
  this.fill(mask);
1116
1126
 
1117
1127
  // flush the compositor
1118
1128
  this.flush();
1119
1129
 
1120
- // Use stencil buffer to affect next rendering object
1121
1130
  gl.colorMask(true, true, true, true);
1122
- gl.stencilFunc(gl.EQUAL, 1, 1);
1131
+
1132
+ // Use stencil buffer to affect next rendering object
1133
+ if (invert === true) {
1134
+ gl.stencilFunc(gl.EQUAL, this.maskLevel + 1, 1);
1135
+ } else {
1136
+ gl.stencilFunc(gl.NOTEQUAL, this.maskLevel + 1, 1);
1137
+ }
1123
1138
  gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
1124
1139
  }
1125
1140
 
@@ -1127,13 +1142,16 @@ class WebGLRenderer extends Renderer {
1127
1142
  * disable (remove) the rendering mask set through setMask.
1128
1143
  * @name clearMask
1129
1144
  * @see WebGLRenderer#setMask
1130
- * @memberof WebGLRenderer.prototype
1145
+ * @memberof WebGLRenderer
1131
1146
  * @function
1132
1147
  */
1133
1148
  clearMask() {
1134
- // flush the compositor
1135
- this.flush();
1136
- this.gl.disable(this.gl.STENCIL_TEST);
1149
+ if (this.maskLevel > 0) {
1150
+ // flush the compositor
1151
+ this.flush();
1152
+ this.maskLevel = 0;
1153
+ this.gl.disable(this.gl.STENCIL_TEST);
1154
+ }
1137
1155
  }
1138
1156
  };
1139
1157