melonjs 13.1.1 → 13.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,8 @@ class WebGLRenderer extends Renderer {
22
22
  * @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
23
23
  * @param {boolean} [options.antiAlias=false] Whether to enable anti-aliasing
24
24
  * @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.
25
- * @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas (performance hit when enabled)
25
+ * @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas
26
+ * @param {boolean} [options.premultipliedAlpha=true] in WebGL, whether the renderer will assume that colors have premultiplied alpha when canvas transparency is enabled
26
27
  * @param {boolean} [options.subPixel=false] Whether to enable subpixel renderering (performance hit when enabled)
27
28
  * @param {boolean} [options.preferWebGL1=false] if true the renderer will only use WebGL 1
28
29
  * @param {string} [options.powerPreference="default"] a hint to the user agent indicating what configuration of GPU is suitable for the WebGL context ("default", "high-performance", "low-power"). To be noted that Safari and Chrome (since version 80) both default to "low-power" to save battery life and improve the user experience on these dual-GPU machines.
@@ -37,8 +38,6 @@ class WebGLRenderer extends Renderer {
37
38
 
38
39
  /**
39
40
  * The WebGL version used by this renderer (1 or 2)
40
- * @name WebGLVersion
41
- * @memberof WebGLRenderer#
42
41
  * @type {number}
43
42
  * @default 1
44
43
  * @readonly
@@ -47,8 +46,6 @@ class WebGLRenderer extends Renderer {
47
46
 
48
47
  /**
49
48
  * The vendor string of the underlying graphics driver.
50
- * @name GPUVendor
51
- * @memberof WebGLRenderer#
52
49
  * @type {string}
53
50
  * @default null
54
51
  * @readonly
@@ -57,8 +54,6 @@ class WebGLRenderer extends Renderer {
57
54
 
58
55
  /**
59
56
  * The renderer string of the underlying graphics driver.
60
- * @name GPURenderer
61
- * @memberof WebGLRenderer#
62
57
  * @type {string}
63
58
  * @default null
64
59
  * @readonly
@@ -68,15 +63,12 @@ class WebGLRenderer extends Renderer {
68
63
  /**
69
64
  * The WebGL context
70
65
  * @name gl
71
- * @memberof WebGLRenderer
72
66
  * @type {WebGLRenderingContext}
73
67
  */
74
68
  this.context = this.gl = this.getContextGL(this.getCanvas(), options.transparent);
75
69
 
76
70
  /**
77
71
  * Maximum number of texture unit supported under the current context
78
- * @name maxTextures
79
- * @memberof WebGLRenderer#
80
72
  * @type {number}
81
73
  * @readonly
82
74
  */
@@ -104,25 +96,19 @@ class WebGLRenderer extends Renderer {
104
96
 
105
97
  /**
106
98
  * The current transformation matrix used for transformations on the overall scene
107
- * @name currentTransform
108
99
  * @type {Matrix2d}
109
- * @memberof WebGLRenderer#
110
100
  */
111
101
  this.currentTransform = new Matrix2d();
112
102
 
113
103
  /**
114
104
  * The current compositor used by the renderer
115
- * @name currentCompositor
116
105
  * @type {WebGLCompositor}
117
- * @memberof WebGLRenderer#
118
106
  */
119
107
  this.currentCompositor = null;
120
108
 
121
109
  /**
122
110
  * The list of active compositors
123
- * @name compositors
124
111
  * @type {Map<WebGLCompositor>}
125
- * @memberof WebGLRenderer#
126
112
  */
127
113
  this.compositors = new Map();
128
114
 
@@ -168,8 +154,6 @@ class WebGLRenderer extends Renderer {
168
154
 
169
155
  /**
170
156
  * Reset context state
171
- * @name reset
172
- * @memberof WebGLRenderer
173
157
  */
174
158
  reset() {
175
159
  super.reset();
@@ -192,9 +176,7 @@ class WebGLRenderer extends Renderer {
192
176
 
193
177
  /**
194
178
  * set the active compositor for this renderer
195
- * @name setCompositor
196
179
  * @param {WebGLCompositor|string} compositor a compositor name or instance
197
- * @memberof WebGLRenderer
198
180
  */
199
181
  setCompositor(compositor = "default") {
200
182
 
@@ -218,8 +200,6 @@ class WebGLRenderer extends Renderer {
218
200
 
219
201
  /**
220
202
  * Reset the gl transform to identity
221
- * @name resetTransform
222
- * @memberof WebGLRenderer
223
203
  */
224
204
  resetTransform() {
225
205
  this.currentTransform.identity();
@@ -264,8 +244,6 @@ class WebGLRenderer extends Renderer {
264
244
 
265
245
  /**
266
246
  * Create a pattern with the specified repetition
267
- * @name createPattern
268
- * @memberof WebGLRenderer
269
247
  * @param {Image} image Source image
270
248
  * @param {string} repeat Define how the pattern should be repeated
271
249
  * @returns {TextureAtlas}
@@ -296,8 +274,6 @@ class WebGLRenderer extends Renderer {
296
274
 
297
275
  /**
298
276
  * Flush the compositor to the frame buffer
299
- * @name flush
300
- * @memberof WebGLRenderer
301
277
  */
302
278
  flush() {
303
279
  this.currentCompositor.flush();
@@ -305,8 +281,6 @@ class WebGLRenderer extends Renderer {
305
281
 
306
282
  /**
307
283
  * set/change the current projection matrix (WebGL only)
308
- * @name setProjection
309
- * @memberof WebGLRenderer
310
284
  * @param {Matrix3d} matrix
311
285
  */
312
286
  setProjection(matrix) {
@@ -314,10 +288,15 @@ class WebGLRenderer extends Renderer {
314
288
  this.currentCompositor.setProjection(matrix);
315
289
  }
316
290
 
291
+ /**
292
+ * prepare the framebuffer for drawing a new frame
293
+ */
294
+ clear() {
295
+ this.currentCompositor.clear(this.settings.transparent ? 0.0 : 1.0);
296
+ }
297
+
317
298
  /**
318
299
  * Clears the gl context with the given color.
319
- * @name clearColor
320
- * @memberof WebGLRenderer
321
300
  * @param {Color|string} [color="#000000"] CSS color.
322
301
  * @param {boolean} [opaque=false] Allow transparency [default] or clear the surface completely [true]
323
302
  */
@@ -334,17 +313,10 @@ class WebGLRenderer extends Renderer {
334
313
  }
335
314
  // clear gl context with the specified color
336
315
  this.currentCompositor.clearColor(glArray[0], glArray[1], glArray[2], (opaque === true) ? 1.0 : glArray[3]);
337
- this.currentCompositor.clear();
338
-
339
- // restore default clear Color black
340
- this.currentCompositor.clearColor(0.0, 0.0, 0.0, 0.0);
341
-
342
316
  }
343
317
 
344
318
  /**
345
319
  * Erase the pixels in the given rectangular area by setting them to transparent black (rgba(0,0,0,0)).
346
- * @name clearRect
347
- * @memberof WebGLRenderer
348
320
  * @param {number} x x axis of the coordinate for the rectangle starting point.
349
321
  * @param {number} y y axis of the coordinate for the rectangle starting point.
350
322
  * @param {number} width The rectangle's width.
@@ -378,7 +350,7 @@ class WebGLRenderer extends Renderer {
378
350
  uvs[1],
379
351
  uvs[2],
380
352
  uvs[3],
381
- this.currentTint.toUint32()
353
+ this.currentTint.toUint32(this.getGlobalAlpha())
382
354
  );
383
355
 
384
356
  // Clear font context2D
@@ -392,8 +364,6 @@ class WebGLRenderer extends Renderer {
392
364
 
393
365
  /**
394
366
  * Draw an image to the gl context
395
- * @name drawImage
396
- * @memberof WebGLRenderer
397
367
  * @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.
398
368
  * @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.
399
369
  * @param {number} sy The Y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
@@ -439,13 +409,11 @@ class WebGLRenderer extends Renderer {
439
409
 
440
410
  var texture = this.cache.get(image);
441
411
  var uvs = texture.getUVs(sx + "," + sy + "," + sw + "," + sh);
442
- this.currentCompositor.addQuad(texture, dx, dy, dw, dh, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32());
412
+ this.currentCompositor.addQuad(texture, dx, dy, dw, dh, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32(this.getGlobalAlpha()));
443
413
  }
444
414
 
445
415
  /**
446
416
  * Draw a pattern within the given rectangle.
447
- * @name drawPattern
448
- * @memberof WebGLRenderer
449
417
  * @param {TextureAtlas} pattern Pattern object
450
418
  * @param {number} x
451
419
  * @param {number} y
@@ -455,18 +423,16 @@ class WebGLRenderer extends Renderer {
455
423
  */
456
424
  drawPattern(pattern, x, y, width, height) {
457
425
  var uvs = pattern.getUVs("0,0," + width + "," + height);
458
- this.currentCompositor.addQuad(pattern, x, y, width, height, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32());
426
+ this.currentCompositor.addQuad(pattern, x, y, width, height, uvs[0], uvs[1], uvs[2], uvs[3], this.currentTint.toUint32(this.getGlobalAlpha()));
459
427
  }
460
428
 
461
429
  /**
462
- * Returns the WebGL Context object of the given Canvas
463
- * @name getContextGL
464
- * @memberof WebGLRenderer
430
+ * Returns the WebGL Context object of the given canvas element
465
431
  * @param {HTMLCanvasElement} canvas
466
- * @param {boolean} [transparent=true] use false to disable transparency
432
+ * @param {boolean} [transparent=false] use true to enable transparency
467
433
  * @returns {WebGLRenderingContext}
468
434
  */
469
- getContextGL(canvas, transparent) {
435
+ getContextGL(canvas, transparent = false) {
470
436
  if (typeof canvas === "undefined" || canvas === null) {
471
437
  throw new Error(
472
438
  "You must pass a canvas element in order to create " +
@@ -474,17 +440,13 @@ class WebGLRenderer extends Renderer {
474
440
  );
475
441
  }
476
442
 
477
- if (typeof transparent !== "boolean") {
478
- transparent = true;
479
- }
480
-
481
443
  var attr = {
482
444
  alpha : transparent,
483
445
  antialias : this.settings.antiAlias,
484
446
  depth : false,
485
447
  stencil: true,
486
448
  preserveDrawingBuffer : false,
487
- premultipliedAlpha: transparent,
449
+ premultipliedAlpha: transparent ? this.settings.premultipliedAlpha : false,
488
450
  powerPreference: this.settings.powerPreference,
489
451
  failIfMajorPerformanceCaveat : this.settings.failIfMajorPerformanceCaveat
490
452
  };
@@ -517,8 +479,6 @@ class WebGLRenderer extends Renderer {
517
479
  /**
518
480
  * Returns the WebGLContext instance for the renderer
519
481
  * return a reference to the system 2d Context
520
- * @name getContext
521
- * @memberof WebGLRenderer
522
482
  * @returns {WebGLRenderingContext}
523
483
  */
524
484
  getContext() {
@@ -536,9 +496,7 @@ class WebGLRenderer extends Renderer {
536
496
  * <img src="images/lighter-blendmode.png" width="510"/> <br>
537
497
  * - "screen" : The pixels are inverted, multiplied, and inverted again. A lighter picture is the result (opposite of multiply) <br>
538
498
  * <img src="images/screen-blendmode.png" width="510"/> <br>
539
- * @name setBlendMode
540
499
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
541
- * @memberof WebGLRenderer
542
500
  * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter", "additive", "screen"
543
501
  * @param {WebGLRenderingContext} [gl]
544
502
  */
@@ -587,8 +545,6 @@ class WebGLRenderer extends Renderer {
587
545
 
588
546
  /**
589
547
  * restores the canvas context
590
- * @name restore
591
- * @memberof WebGLRenderer
592
548
  */
593
549
  restore() {
594
550
  // do nothing if there is no saved states
@@ -622,8 +578,6 @@ class WebGLRenderer extends Renderer {
622
578
 
623
579
  /**
624
580
  * saves the canvas context
625
- * @name save
626
- * @memberof WebGLRenderer
627
581
  */
628
582
  save() {
629
583
  this._colorStack.push(this.currentColor.clone());
@@ -639,8 +593,6 @@ class WebGLRenderer extends Renderer {
639
593
 
640
594
  /**
641
595
  * rotates the uniform matrix
642
- * @name rotate
643
- * @memberof WebGLRenderer
644
596
  * @param {number} angle in radians
645
597
  */
646
598
  rotate(angle) {
@@ -649,8 +601,6 @@ class WebGLRenderer extends Renderer {
649
601
 
650
602
  /**
651
603
  * scales the uniform matrix
652
- * @name scale
653
- * @memberof WebGLRenderer
654
604
  * @param {number} x
655
605
  * @param {number} y
656
606
  */
@@ -669,8 +619,6 @@ class WebGLRenderer extends Renderer {
669
619
 
670
620
  /**
671
621
  * Set the global alpha
672
- * @name setGlobalAlpha
673
- * @memberof WebGLRenderer
674
622
  * @param {number} alpha 0.0 to 1.0 values accepted.
675
623
  */
676
624
  setGlobalAlpha(alpha) {
@@ -679,8 +627,6 @@ class WebGLRenderer extends Renderer {
679
627
 
680
628
  /**
681
629
  * Return the global alpha
682
- * @name getGlobalAlpha
683
- * @memberof WebGLRenderer
684
630
  * @returns {number} global alpha value
685
631
  */
686
632
  getGlobalAlpha() {
@@ -690,8 +636,6 @@ class WebGLRenderer extends Renderer {
690
636
  /**
691
637
  * Set the current fill & stroke style color.
692
638
  * By default, or upon reset, the value is set to #000000.
693
- * @name setColor
694
- * @memberof WebGLRenderer
695
639
  * @param {Color|string} color css color string.
696
640
  */
697
641
  setColor(color) {
@@ -702,8 +646,6 @@ class WebGLRenderer extends Renderer {
702
646
 
703
647
  /**
704
648
  * Set the line width
705
- * @name setLineWidth
706
- * @memberof WebGLRenderer
707
649
  * @param {number} width Line width
708
650
  */
709
651
  setLineWidth(width) {
@@ -712,8 +654,6 @@ class WebGLRenderer extends Renderer {
712
654
 
713
655
  /**
714
656
  * Stroke an arc at the specified coordinates with given radius, start and end points
715
- * @name strokeArc
716
- * @memberof WebGLRenderer
717
657
  * @param {number} x arc center point x-axis
718
658
  * @param {number} y arc center point y-axis
719
659
  * @param {number} radius
@@ -739,8 +679,6 @@ class WebGLRenderer extends Renderer {
739
679
 
740
680
  /**
741
681
  * Fill an arc at the specified coordinates with given radius, start and end points
742
- * @name fillArc
743
- * @memberof WebGLRenderer
744
682
  * @param {number} x arc center point x-axis
745
683
  * @param {number} y arc center point y-axis
746
684
  * @param {number} radius
@@ -754,8 +692,6 @@ class WebGLRenderer extends Renderer {
754
692
 
755
693
  /**
756
694
  * Stroke an ellipse at the specified coordinates with given radius
757
- * @name strokeEllipse
758
- * @memberof WebGLRenderer
759
695
  * @param {number} x ellipse center point x-axis
760
696
  * @param {number} y ellipse center point y-axis
761
697
  * @param {number} w horizontal radius of the ellipse
@@ -779,8 +715,6 @@ class WebGLRenderer extends Renderer {
779
715
 
780
716
  /**
781
717
  * Fill an ellipse at the specified coordinates with given radius
782
- * @name fillEllipse
783
- * @memberof WebGLRenderer
784
718
  * @param {number} x ellipse center point x-axis
785
719
  * @param {number} y ellipse center point y-axis
786
720
  * @param {number} w horizontal radius of the ellipse
@@ -792,8 +726,6 @@ class WebGLRenderer extends Renderer {
792
726
 
793
727
  /**
794
728
  * Stroke a line of the given two points
795
- * @name strokeLine
796
- * @memberof WebGLRenderer
797
729
  * @param {number} startX the start x coordinate
798
730
  * @param {number} startY the start y coordinate
799
731
  * @param {number} endX the end x coordinate
@@ -813,8 +745,6 @@ class WebGLRenderer extends Renderer {
813
745
 
814
746
  /**
815
747
  * Fill a line of the given two points
816
- * @name fillLine
817
- * @memberof WebGLRenderer
818
748
  * @param {number} startX the start x coordinate
819
749
  * @param {number} startY the start y coordinate
820
750
  * @param {number} endX the end x coordinate
@@ -826,8 +756,6 @@ class WebGLRenderer extends Renderer {
826
756
 
827
757
  /**
828
758
  * Stroke a me.Polygon on the screen with a specified color
829
- * @name strokePolygon
830
- * @memberof WebGLRenderer
831
759
  * @param {Polygon} poly the shape to draw
832
760
  * @param {boolean} [fill=false] also fill the shape with the current color if true
833
761
  */
@@ -857,8 +785,6 @@ class WebGLRenderer extends Renderer {
857
785
 
858
786
  /**
859
787
  * Fill a me.Polygon on the screen
860
- * @name fillPolygon
861
- * @memberof WebGLRenderer
862
788
  * @param {Polygon} poly the shape to draw
863
789
  */
864
790
  fillPolygon(poly) {
@@ -867,8 +793,6 @@ class WebGLRenderer extends Renderer {
867
793
 
868
794
  /**
869
795
  * Draw a stroke rectangle at the specified coordinates
870
- * @name strokeRect
871
- * @memberof WebGLRenderer
872
796
  * @param {number} x
873
797
  * @param {number} y
874
798
  * @param {number} width
@@ -891,8 +815,6 @@ class WebGLRenderer extends Renderer {
891
815
 
892
816
  /**
893
817
  * Draw a filled rectangle at the specified coordinates
894
- * @name fillRect
895
- * @memberof WebGLRenderer
896
818
  * @param {number} x
897
819
  * @param {number} y
898
820
  * @param {number} width
@@ -904,8 +826,6 @@ class WebGLRenderer extends Renderer {
904
826
 
905
827
  /**
906
828
  * Stroke a rounded rectangle at the specified coordinates
907
- * @name strokeRoundRect
908
- * @memberof WebGLRenderer
909
829
  * @param {number} x
910
830
  * @param {number} y
911
831
  * @param {number} width
@@ -930,8 +850,6 @@ class WebGLRenderer extends Renderer {
930
850
 
931
851
  /**
932
852
  * Draw a rounded filled rectangle at the specified coordinates
933
- * @name fillRoundRect
934
- * @memberof WebGLRenderer
935
853
  * @param {number} x
936
854
  * @param {number} y
937
855
  * @param {number} width
@@ -942,11 +860,29 @@ class WebGLRenderer extends Renderer {
942
860
  this.strokeRoundRect(x, y, width, height, radius, true);
943
861
  }
944
862
 
863
+ /**
864
+ * Stroke a Point at the specified coordinates
865
+ * @param {number} x
866
+ * @param {number} y
867
+ */
868
+ strokePoint(x, y) {
869
+ this.strokeLine(x, y, x + 1, y + 1);
870
+ }
871
+
872
+ /**
873
+ * Draw a a point at the specified coordinates
874
+ * @param {number} x
875
+ * @param {number} y
876
+ * @param {number} width
877
+ * @param {number} height
878
+ */
879
+ fillPoint(x, y) {
880
+ this.strokePoint(x, y);
881
+ }
882
+
945
883
  /**
946
884
  * Reset (overrides) the renderer transformation matrix to the
947
885
  * identity one, and then apply the given transformation matrix.
948
- * @name setTransform
949
- * @memberof WebGLRenderer
950
886
  * @param {Matrix2d} mat2d Matrix to transform by
951
887
  */
952
888
  setTransform(mat2d) {
@@ -956,8 +892,6 @@ class WebGLRenderer extends Renderer {
956
892
 
957
893
  /**
958
894
  * Multiply given matrix into the renderer tranformation matrix
959
- * @name transform
960
- * @memberof WebGLRenderer
961
895
  * @param {Matrix2d} mat2d Matrix to transform by
962
896
  */
963
897
  transform(mat2d) {
@@ -973,8 +907,6 @@ class WebGLRenderer extends Renderer {
973
907
 
974
908
  /**
975
909
  * Translates the uniform matrix by the given coordinates
976
- * @name translate
977
- * @memberof WebGLRenderer
978
910
  * @param {number} x
979
911
  * @param {number} y
980
912
  */
@@ -995,8 +927,6 @@ class WebGLRenderer extends Renderer {
995
927
  * You can however save the current region using the save(),
996
928
  * and restore it (with the restore() method) any time in the future.
997
929
  * (<u>this is an experimental feature !</u>)
998
- * @name clipRect
999
- * @memberof WebGLRenderer
1000
930
  * @param {number} x
1001
931
  * @param {number} y
1002
932
  * @param {number} width
@@ -1042,8 +972,6 @@ class WebGLRenderer extends Renderer {
1042
972
  * A mask limits rendering elements to the shape and position of the given mask object.
1043
973
  * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
1044
974
  * Mask are not preserved through renderer context save and restore.
1045
- * @name setMask
1046
- * @memberof WebGLRenderer
1047
975
  * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] a shape defining the mask to be applied
1048
976
  * @param {boolean} [invert=false] either the given shape should define what is visible (default) or the opposite
1049
977
  */
@@ -1057,8 +985,6 @@ class WebGLRenderer extends Renderer {
1057
985
  // Enable and setup GL state to write to stencil buffer
1058
986
  gl.enable(gl.STENCIL_TEST);
1059
987
  gl.clear(gl.STENCIL_BUFFER_BIT);
1060
-
1061
-
1062
988
  }
1063
989
 
1064
990
  this.maskLevel++;
@@ -1087,9 +1013,7 @@ class WebGLRenderer extends Renderer {
1087
1013
 
1088
1014
  /**
1089
1015
  * disable (remove) the rendering mask set through setMask.
1090
- * @name clearMask
1091
1016
  * @see WebGLRenderer#setMask
1092
- * @memberof WebGLRenderer
1093
1017
  */
1094
1018
  clearMask() {
1095
1019
  if (this.maskLevel > 0) {