melonjs 13.0.0 → 13.2.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.
- package/dist/melonjs.js +642 -583
- package/dist/melonjs.min.js +3 -3
- package/dist/melonjs.module.d.ts +286 -476
- package/dist/melonjs.module.js +629 -592
- package/package.json +7 -7
- package/src/geometries/point.js +80 -0
- package/src/index.js +4 -0
- package/src/input/pointerevent.js +2 -2
- package/src/lang/deprecated.js +27 -1
- package/src/level/tiled/TMXGroup.js +10 -0
- package/src/level/tiled/TMXLayer.js +9 -0
- package/src/level/tiled/TMXObject.js +33 -10
- package/src/level/tiled/TMXTileMap.js +12 -0
- package/src/level/tiled/TMXTileset.js +8 -0
- package/src/math/color.js +63 -43
- package/src/math/observable_vector2.js +26 -2
- package/src/math/observable_vector3.js +32 -4
- package/src/math/vector2.js +23 -0
- package/src/math/vector3.js +26 -0
- package/src/physics/body.js +10 -3
- package/src/physics/bounds.js +10 -9
- package/src/polyfill/index.js +2 -2
- package/src/renderable/nineslicesprite.js +27 -1
- package/src/renderable/renderable.js +49 -135
- package/src/renderable/sprite.js +7 -1
- package/src/text/bitmaptext.js +8 -8
- package/src/text/text.js +1 -1
- package/src/text/textmetrics.js +1 -1
- package/src/video/canvas/canvas_renderer.js +51 -60
- package/src/video/renderer.js +27 -76
- package/src/video/texture/canvas_texture.js +4 -2
- package/src/video/video.js +13 -16
- package/src/video/webgl/glshader.js +0 -28
- package/src/video/webgl/webgl_compositor.js +21 -50
- package/src/video/webgl/webgl_renderer.js +44 -132
|
@@ -5,7 +5,6 @@ import Ellipse from "./../../geometries/ellipse.js";
|
|
|
5
5
|
import RoundRect from "./../../geometries/roundrect.js";
|
|
6
6
|
import Rect from "./../../geometries/rectangle.js";
|
|
7
7
|
import Bounds from "./../../physics/bounds.js";
|
|
8
|
-
import { createCanvas } from "./../video.js";
|
|
9
8
|
import * as event from "./../../system/event.js";
|
|
10
9
|
|
|
11
10
|
/**
|
|
@@ -19,7 +18,6 @@ class CanvasRenderer extends Renderer {
|
|
|
19
18
|
* @param {number} options.width The width of the canvas without scaling
|
|
20
19
|
* @param {number} options.height The height of the canvas without scaling
|
|
21
20
|
* @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
|
|
22
|
-
* @param {boolean} [options.doubleBuffering=false] Whether to enable double buffering
|
|
23
21
|
* @param {boolean} [options.antiAlias=false] Whether to enable anti-aliasing
|
|
24
22
|
* @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas (performance hit when enabled)
|
|
25
23
|
* @param {boolean} [options.subPixel=false] Whether to enable subpixel renderering (performance hit when enabled)
|
|
@@ -32,17 +30,7 @@ class CanvasRenderer extends Renderer {
|
|
|
32
30
|
super(options);
|
|
33
31
|
|
|
34
32
|
// defined the 2d context
|
|
35
|
-
this.context = this.getContext2d(this.
|
|
36
|
-
|
|
37
|
-
// create the back buffer if we use double buffering
|
|
38
|
-
if (this.settings.doubleBuffering) {
|
|
39
|
-
this.backBufferCanvas = createCanvas(this.settings.width, this.settings.height, true);
|
|
40
|
-
this.backBufferContext2D = this.getContext2d(this.backBufferCanvas);
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
this.backBufferCanvas = this.getScreenCanvas();
|
|
44
|
-
this.backBufferContext2D = this.context;
|
|
45
|
-
}
|
|
33
|
+
this.context = this.getContext2d(this.getCanvas(), this.settings.transparent);
|
|
46
34
|
|
|
47
35
|
this.setBlendMode(this.settings.blendMode);
|
|
48
36
|
|
|
@@ -58,13 +46,13 @@ class CanvasRenderer extends Renderer {
|
|
|
58
46
|
}
|
|
59
47
|
|
|
60
48
|
// context lost & restore event for canvas
|
|
61
|
-
this.
|
|
49
|
+
this.getCanvas().addEventListener("contextlost", (e) => {
|
|
62
50
|
e.preventDefault();
|
|
63
51
|
this.isContextValid = false;
|
|
64
52
|
event.emit(event.ONCONTEXT_LOST, this);
|
|
65
53
|
}, false );
|
|
66
54
|
// ctx.restoreContext()
|
|
67
|
-
this.
|
|
55
|
+
this.getCanvas().addEventListener("contextrestored", () => {
|
|
68
56
|
this.isContextValid = true;
|
|
69
57
|
event.emit(event.ONCONTEXT_RESTORED, this);
|
|
70
58
|
}, false );
|
|
@@ -86,7 +74,7 @@ class CanvasRenderer extends Renderer {
|
|
|
86
74
|
* @memberof CanvasRenderer
|
|
87
75
|
*/
|
|
88
76
|
resetTransform() {
|
|
89
|
-
this.
|
|
77
|
+
this.getContext().setTransform(1, 0, 0, 1, 0, 0);
|
|
90
78
|
}
|
|
91
79
|
|
|
92
80
|
/**
|
|
@@ -128,12 +116,6 @@ class CanvasRenderer extends Renderer {
|
|
|
128
116
|
this.currentBlendMode = "normal";
|
|
129
117
|
break;
|
|
130
118
|
}
|
|
131
|
-
|
|
132
|
-
// transparent setting will override the given blendmode for this.context
|
|
133
|
-
if (this.settings.doubleBuffering && this.settings.transparent) {
|
|
134
|
-
// Clears the front buffer for each frame blit
|
|
135
|
-
this.context.globalCompositeOperation = "copy";
|
|
136
|
-
}
|
|
137
119
|
}
|
|
138
120
|
|
|
139
121
|
/**
|
|
@@ -142,19 +124,10 @@ class CanvasRenderer extends Renderer {
|
|
|
142
124
|
* @memberof CanvasRenderer
|
|
143
125
|
*/
|
|
144
126
|
clear() {
|
|
145
|
-
if (this.settings.transparent) {
|
|
146
|
-
this.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* render the main framebuffer on screen
|
|
152
|
-
* @name flush
|
|
153
|
-
* @memberof CanvasRenderer
|
|
154
|
-
*/
|
|
155
|
-
flush() {
|
|
156
|
-
if (this.settings.doubleBuffering) {
|
|
157
|
-
this.context.drawImage(this.backBufferCanvas, 0, 0);
|
|
127
|
+
if (this.settings.transparent === false) {
|
|
128
|
+
var canvas = this.getCanvas();
|
|
129
|
+
var context = this.getContext();
|
|
130
|
+
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
158
131
|
}
|
|
159
132
|
}
|
|
160
133
|
|
|
@@ -165,12 +138,16 @@ class CanvasRenderer extends Renderer {
|
|
|
165
138
|
* @param {Color|string} [color="#000000"] CSS color.
|
|
166
139
|
* @param {boolean} [opaque=false] Allow transparency [default] or clear the surface completely [true]
|
|
167
140
|
*/
|
|
168
|
-
clearColor(color = "#000000", opaque) {
|
|
141
|
+
clearColor(color = "#000000", opaque = false) {
|
|
142
|
+
var canvas = this.getCanvas();
|
|
143
|
+
var context = this.getContext();
|
|
144
|
+
|
|
169
145
|
this.save();
|
|
170
146
|
this.resetTransform();
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
147
|
+
context.globalAlpha = 1;
|
|
148
|
+
context.globalCompositeOperation = opaque === true ? "copy" : "source-over";
|
|
149
|
+
context.fillStyle = (color instanceof Color) ? color.toRGBA() : color;
|
|
150
|
+
this.fillRect(0, 0, canvas.width, canvas.height);
|
|
174
151
|
this.restore();
|
|
175
152
|
}
|
|
176
153
|
|
|
@@ -530,15 +507,28 @@ class CanvasRenderer extends Renderer {
|
|
|
530
507
|
this.strokeRoundRect(x, y, width, height, radius, true);
|
|
531
508
|
}
|
|
532
509
|
|
|
510
|
+
/**
|
|
511
|
+
* Stroke a Point at the specified coordinates
|
|
512
|
+
* @name strokePoint
|
|
513
|
+
* @memberof CanvasRenderer
|
|
514
|
+
* @param {number} x
|
|
515
|
+
* @param {number} y
|
|
516
|
+
*/
|
|
517
|
+
strokePoint(x, y) {
|
|
518
|
+
this.strokeLine(x, y, x + 1, y + 1);
|
|
519
|
+
}
|
|
533
520
|
|
|
534
521
|
/**
|
|
535
|
-
*
|
|
536
|
-
* @name
|
|
522
|
+
* Draw a a point at the specified coordinates
|
|
523
|
+
* @name fillPoint
|
|
537
524
|
* @memberof CanvasRenderer
|
|
538
|
-
* @
|
|
525
|
+
* @param {number} x
|
|
526
|
+
* @param {number} y
|
|
527
|
+
* @param {number} width
|
|
528
|
+
* @param {number} height
|
|
539
529
|
*/
|
|
540
|
-
|
|
541
|
-
|
|
530
|
+
fillPoint(x, y) {
|
|
531
|
+
this.strokePoint(x, y);
|
|
542
532
|
}
|
|
543
533
|
|
|
544
534
|
/**
|
|
@@ -556,7 +546,7 @@ class CanvasRenderer extends Renderer {
|
|
|
556
546
|
* @memberof CanvasRenderer
|
|
557
547
|
*/
|
|
558
548
|
save() {
|
|
559
|
-
this.
|
|
549
|
+
this.getContext().save();
|
|
560
550
|
}
|
|
561
551
|
|
|
562
552
|
/**
|
|
@@ -565,12 +555,12 @@ class CanvasRenderer extends Renderer {
|
|
|
565
555
|
* @memberof CanvasRenderer
|
|
566
556
|
*/
|
|
567
557
|
restore() {
|
|
568
|
-
this.
|
|
558
|
+
this.getContext().restore();
|
|
569
559
|
this.currentColor.glArray[3] = this.getGlobalAlpha();
|
|
570
560
|
this.currentScissor[0] = 0;
|
|
571
561
|
this.currentScissor[1] = 0;
|
|
572
|
-
this.currentScissor[2] = this.
|
|
573
|
-
this.currentScissor[3] = this.
|
|
562
|
+
this.currentScissor[2] = this.getCanvas().width;
|
|
563
|
+
this.currentScissor[3] = this.getCanvas().height;
|
|
574
564
|
}
|
|
575
565
|
|
|
576
566
|
/**
|
|
@@ -580,7 +570,7 @@ class CanvasRenderer extends Renderer {
|
|
|
580
570
|
* @param {number} angle in radians
|
|
581
571
|
*/
|
|
582
572
|
rotate(angle) {
|
|
583
|
-
this.
|
|
573
|
+
this.getContext().rotate(angle);
|
|
584
574
|
}
|
|
585
575
|
|
|
586
576
|
/**
|
|
@@ -591,7 +581,7 @@ class CanvasRenderer extends Renderer {
|
|
|
591
581
|
* @param {number} y
|
|
592
582
|
*/
|
|
593
583
|
scale(x, y) {
|
|
594
|
-
this.
|
|
584
|
+
this.getContext().scale(x, y);
|
|
595
585
|
}
|
|
596
586
|
|
|
597
587
|
/**
|
|
@@ -602,8 +592,9 @@ class CanvasRenderer extends Renderer {
|
|
|
602
592
|
* @param {Color|string} color css color value
|
|
603
593
|
*/
|
|
604
594
|
setColor(color) {
|
|
605
|
-
this.
|
|
606
|
-
|
|
595
|
+
var context = this.getContext();
|
|
596
|
+
context.strokeStyle =
|
|
597
|
+
context.fillStyle = (
|
|
607
598
|
color instanceof Color ?
|
|
608
599
|
color.toRGBA() :
|
|
609
600
|
color
|
|
@@ -617,7 +608,7 @@ class CanvasRenderer extends Renderer {
|
|
|
617
608
|
* @param {number} alpha 0.0 to 1.0 values accepted.
|
|
618
609
|
*/
|
|
619
610
|
setGlobalAlpha(alpha) {
|
|
620
|
-
this.
|
|
611
|
+
this.getContext().globalAlpha = this.currentColor.glArray[3] = alpha;
|
|
621
612
|
}
|
|
622
613
|
|
|
623
614
|
/**
|
|
@@ -627,7 +618,7 @@ class CanvasRenderer extends Renderer {
|
|
|
627
618
|
* @returns {number} global alpha value
|
|
628
619
|
*/
|
|
629
620
|
getGlobalAlpha() {
|
|
630
|
-
return this.
|
|
621
|
+
return this.getContext().globalAlpha;
|
|
631
622
|
}
|
|
632
623
|
|
|
633
624
|
/**
|
|
@@ -637,7 +628,7 @@ class CanvasRenderer extends Renderer {
|
|
|
637
628
|
* @param {number} width Line width
|
|
638
629
|
*/
|
|
639
630
|
setLineWidth(width) {
|
|
640
|
-
this.
|
|
631
|
+
this.getContext().lineWidth = width;
|
|
641
632
|
}
|
|
642
633
|
|
|
643
634
|
/**
|
|
@@ -672,7 +663,7 @@ class CanvasRenderer extends Renderer {
|
|
|
672
663
|
f |= 0;
|
|
673
664
|
}
|
|
674
665
|
|
|
675
|
-
this.
|
|
666
|
+
this.getContext().transform(a, b, c, d, e, f);
|
|
676
667
|
}
|
|
677
668
|
|
|
678
669
|
/**
|
|
@@ -684,9 +675,9 @@ class CanvasRenderer extends Renderer {
|
|
|
684
675
|
*/
|
|
685
676
|
translate(x, y) {
|
|
686
677
|
if (this.settings.subPixel === false) {
|
|
687
|
-
this.
|
|
678
|
+
this.getContext().translate(~~x, ~~y);
|
|
688
679
|
} else {
|
|
689
|
-
this.
|
|
680
|
+
this.getContext().translate(x, y);
|
|
690
681
|
}
|
|
691
682
|
}
|
|
692
683
|
|
|
@@ -704,14 +695,14 @@ class CanvasRenderer extends Renderer {
|
|
|
704
695
|
* @param {number} height
|
|
705
696
|
*/
|
|
706
697
|
clipRect(x, y, width, height) {
|
|
707
|
-
var canvas = this.
|
|
698
|
+
var canvas = this.getCanvas();
|
|
708
699
|
// if requested box is different from the current canvas size;
|
|
709
700
|
if (x !== 0 || y !== 0 || width !== canvas.width || height !== canvas.height) {
|
|
710
701
|
var currentScissor = this.currentScissor;
|
|
711
702
|
// if different from the current scissor box
|
|
712
703
|
if (currentScissor[0] !== x || currentScissor[1] !== y ||
|
|
713
704
|
currentScissor[2] !== width || currentScissor[3] !== height) {
|
|
714
|
-
var context = this.
|
|
705
|
+
var context = this.getContext();
|
|
715
706
|
context.beginPath();
|
|
716
707
|
context.rect(x, y, width, height);
|
|
717
708
|
context.clip();
|
package/src/video/renderer.js
CHANGED
|
@@ -11,6 +11,7 @@ import Polygon from "./../geometries/poly.js";
|
|
|
11
11
|
import Line from "./../geometries/line.js";
|
|
12
12
|
import Bounds from "./../physics/bounds.js";
|
|
13
13
|
import Path2D from "./../geometries/path2d.js";
|
|
14
|
+
import Point from "../geometries/point.js";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* @classdesc
|
|
@@ -22,10 +23,10 @@ class Renderer {
|
|
|
22
23
|
* @param {number} options.width The width of the canvas without scaling
|
|
23
24
|
* @param {number} options.height The height of the canvas without scaling
|
|
24
25
|
* @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
|
|
25
|
-
* @param {boolean} [options.doubleBuffering=false] Whether to enable double buffering (not applicable when using the WebGL Renderer)
|
|
26
26
|
* @param {boolean} [options.antiAlias=false] Whether to enable anti-aliasing, use false (default) for a pixelated effect.
|
|
27
27
|
* @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.
|
|
28
|
-
* @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas
|
|
28
|
+
* @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas
|
|
29
|
+
* @param {boolean} [options.premultipliedAlpha=true] in WebGL, whether the renderer will assume that colors have premultiplied alpha when canvas transparency is enabled
|
|
29
30
|
* @param {boolean} [options.blendMode="normal"] the default blend mode to use ("normal", "multiply")
|
|
30
31
|
* @param {boolean} [options.subPixel=false] Whether to enable subpixel rendering (performance hit when enabled)
|
|
31
32
|
* @param {boolean} [options.verbose=false] Enable the verbose mode that provides additional details as to what the renderer is doing
|
|
@@ -36,26 +37,20 @@ class Renderer {
|
|
|
36
37
|
/**
|
|
37
38
|
* The given constructor options
|
|
38
39
|
* @public
|
|
39
|
-
* @name settings
|
|
40
|
-
* @memberof Renderer#
|
|
41
40
|
* @type {object}
|
|
42
41
|
*/
|
|
43
42
|
this.settings = options;
|
|
44
43
|
|
|
45
44
|
/**
|
|
46
45
|
* true if the current rendering context is valid
|
|
47
|
-
* @name isContextValid
|
|
48
|
-
* @memberof Renderer#
|
|
49
46
|
* @default true
|
|
50
|
-
* type {boolean}
|
|
47
|
+
* @type {boolean}
|
|
51
48
|
*/
|
|
52
49
|
this.isContextValid = true;
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
52
|
* The Path2D instance used by the renderer to draw primitives
|
|
56
|
-
* @name path2D
|
|
57
53
|
* @type {Path2D}
|
|
58
|
-
* @memberof Renderer#
|
|
59
54
|
*/
|
|
60
55
|
this.path2D = new Path2D();
|
|
61
56
|
|
|
@@ -84,13 +79,9 @@ class Renderer {
|
|
|
84
79
|
} else if (typeof this.settings.canvas !== "undefined") {
|
|
85
80
|
this.canvas = this.settings.canvas;
|
|
86
81
|
} else {
|
|
87
|
-
this.canvas = createCanvas(this.settings.
|
|
82
|
+
this.canvas = createCanvas(this.settings.width, this.settings.height);
|
|
88
83
|
}
|
|
89
84
|
|
|
90
|
-
// canvas object and context
|
|
91
|
-
this.backBufferCanvas = this.canvas;
|
|
92
|
-
this.context = null;
|
|
93
|
-
|
|
94
85
|
// global color
|
|
95
86
|
this.currentColor = new Color(0, 0, 0, 1.0);
|
|
96
87
|
|
|
@@ -111,15 +102,16 @@ class Renderer {
|
|
|
111
102
|
|
|
112
103
|
/**
|
|
113
104
|
* prepare the framebuffer for drawing a new frame
|
|
114
|
-
* @name clear
|
|
115
|
-
* @memberof Renderer
|
|
116
105
|
*/
|
|
117
106
|
clear() {}
|
|
118
107
|
|
|
108
|
+
/**
|
|
109
|
+
* render the main framebuffer on screen
|
|
110
|
+
*/
|
|
111
|
+
flush() {}
|
|
112
|
+
|
|
119
113
|
/**
|
|
120
114
|
* Reset context state
|
|
121
|
-
* @name reset
|
|
122
|
-
* @memberof Renderer
|
|
123
115
|
*/
|
|
124
116
|
reset() {
|
|
125
117
|
this.resetTransform();
|
|
@@ -129,46 +121,30 @@ class Renderer {
|
|
|
129
121
|
this.cache.clear();
|
|
130
122
|
this.currentScissor[0] = 0;
|
|
131
123
|
this.currentScissor[1] = 0;
|
|
132
|
-
this.currentScissor[2] = this.
|
|
133
|
-
this.currentScissor[3] = this.
|
|
124
|
+
this.currentScissor[2] = this.getCanvas().width;
|
|
125
|
+
this.currentScissor[3] = this.getCanvas().height;
|
|
134
126
|
this.clearMask();
|
|
135
127
|
}
|
|
136
128
|
|
|
137
129
|
/**
|
|
138
|
-
* return a reference to the
|
|
139
|
-
* @name getCanvas
|
|
140
|
-
* @memberof Renderer
|
|
130
|
+
* return a reference to the canvas which this renderer draws to
|
|
141
131
|
* @returns {HTMLCanvasElement}
|
|
142
132
|
*/
|
|
143
133
|
getCanvas() {
|
|
144
|
-
return this.backBufferCanvas;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* return a reference to the screen canvas
|
|
149
|
-
* @name getScreenCanvas
|
|
150
|
-
* @memberof Renderer
|
|
151
|
-
* @returns {HTMLCanvasElement}
|
|
152
|
-
*/
|
|
153
|
-
getScreenCanvas() {
|
|
154
134
|
return this.canvas;
|
|
155
135
|
}
|
|
156
136
|
|
|
137
|
+
|
|
157
138
|
/**
|
|
158
|
-
* return a reference to
|
|
159
|
-
*
|
|
160
|
-
* @name getScreenContext
|
|
161
|
-
* @memberof Renderer
|
|
162
|
-
* @returns {CanvasRenderingContext2D}
|
|
139
|
+
* return a reference to this renderer canvas corresponding Context
|
|
140
|
+
* @returns {CanvasRenderingContext2D|WebGLRenderingContext}
|
|
163
141
|
*/
|
|
164
|
-
|
|
142
|
+
getContext() {
|
|
165
143
|
return this.context;
|
|
166
144
|
}
|
|
167
145
|
|
|
168
146
|
/**
|
|
169
147
|
* returns the current blend mode for this renderer
|
|
170
|
-
* @name getBlendMode
|
|
171
|
-
* @memberof Renderer
|
|
172
148
|
* @returns {string} blend mode
|
|
173
149
|
*/
|
|
174
150
|
getBlendMode() {
|
|
@@ -178,8 +154,6 @@ class Renderer {
|
|
|
178
154
|
/**
|
|
179
155
|
* Returns the 2D Context object of the given Canvas<br>
|
|
180
156
|
* Also configures anti-aliasing and blend modes based on constructor options.
|
|
181
|
-
* @name getContext2d
|
|
182
|
-
* @memberof Renderer
|
|
183
157
|
* @param {HTMLCanvasElement} canvas
|
|
184
158
|
* @param {boolean} [transparent=true] use false to disable transparency
|
|
185
159
|
* @returns {CanvasRenderingContext2D}
|
|
@@ -215,28 +189,22 @@ class Renderer {
|
|
|
215
189
|
|
|
216
190
|
/**
|
|
217
191
|
* return the width of the system Canvas
|
|
218
|
-
* @name getWidth
|
|
219
|
-
* @memberof Renderer
|
|
220
192
|
* @returns {number}
|
|
221
193
|
*/
|
|
222
194
|
getWidth() {
|
|
223
|
-
return this.
|
|
195
|
+
return this.getCanvas().width;
|
|
224
196
|
}
|
|
225
197
|
|
|
226
198
|
/**
|
|
227
199
|
* return the height of the system Canvas
|
|
228
|
-
* @name getHeight
|
|
229
|
-
* @memberof Renderer
|
|
230
200
|
* @returns {number} height of the system Canvas
|
|
231
201
|
*/
|
|
232
202
|
getHeight() {
|
|
233
|
-
return this.
|
|
203
|
+
return this.getCanvas().height;
|
|
234
204
|
}
|
|
235
205
|
|
|
236
206
|
/**
|
|
237
207
|
* get the current fill & stroke style color.
|
|
238
|
-
* @name getColor
|
|
239
|
-
* @memberof Renderer
|
|
240
208
|
* @returns {Color} current global color
|
|
241
209
|
*/
|
|
242
210
|
getColor() {
|
|
@@ -245,8 +213,6 @@ class Renderer {
|
|
|
245
213
|
|
|
246
214
|
/**
|
|
247
215
|
* return the current global alpha
|
|
248
|
-
* @name globalAlpha
|
|
249
|
-
* @memberof Renderer
|
|
250
216
|
* @returns {number}
|
|
251
217
|
*/
|
|
252
218
|
globalAlpha() {
|
|
@@ -255,8 +221,6 @@ class Renderer {
|
|
|
255
221
|
|
|
256
222
|
/**
|
|
257
223
|
* check if the given rect or bounds overlaps with the renderer screen coordinates
|
|
258
|
-
* @name overlaps
|
|
259
|
-
* @memberof Renderer
|
|
260
224
|
* @param {Rect|Bounds} bounds
|
|
261
225
|
* @returns {boolean} true if overlaps
|
|
262
226
|
*/
|
|
@@ -270,15 +234,14 @@ class Renderer {
|
|
|
270
234
|
|
|
271
235
|
/**
|
|
272
236
|
* resizes the system canvas
|
|
273
|
-
* @name resize
|
|
274
|
-
* @memberof Renderer
|
|
275
237
|
* @param {number} width new width of the canvas
|
|
276
238
|
* @param {number} height new height of the canvas
|
|
277
239
|
*/
|
|
278
240
|
resize(width, height) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
241
|
+
var canvas = this.getCanvas();
|
|
242
|
+
if (width !== canvas.width || height !== canvas.height) {
|
|
243
|
+
canvas.width = width;
|
|
244
|
+
canvas.height = height;
|
|
282
245
|
this.currentScissor[0] = 0;
|
|
283
246
|
this.currentScissor[1] = 0;
|
|
284
247
|
this.currentScissor[2] = width;
|
|
@@ -290,8 +253,6 @@ class Renderer {
|
|
|
290
253
|
|
|
291
254
|
/**
|
|
292
255
|
* enable/disable image smoothing (scaling interpolation) for the given context
|
|
293
|
-
* @name setAntiAlias
|
|
294
|
-
* @memberof Renderer
|
|
295
256
|
* @param {CanvasRenderingContext2D} context
|
|
296
257
|
* @param {boolean} [enable=false]
|
|
297
258
|
*/
|
|
@@ -319,8 +280,6 @@ class Renderer {
|
|
|
319
280
|
|
|
320
281
|
/**
|
|
321
282
|
* set/change the current projection matrix (WebGL only)
|
|
322
|
-
* @name setProjection
|
|
323
|
-
* @memberof Renderer
|
|
324
283
|
* @param {Matrix3d} matrix
|
|
325
284
|
*/
|
|
326
285
|
setProjection(matrix) {
|
|
@@ -329,8 +288,6 @@ class Renderer {
|
|
|
329
288
|
|
|
330
289
|
/**
|
|
331
290
|
* stroke the given shape
|
|
332
|
-
* @name stroke
|
|
333
|
-
* @memberof Renderer
|
|
334
291
|
* @param {Rect|RoundRect|Polygon|Line|Ellipse} shape a shape object to stroke
|
|
335
292
|
* @param {boolean} [fill=false] fill the shape with the current color if true
|
|
336
293
|
*/
|
|
@@ -357,6 +314,10 @@ class Renderer {
|
|
|
357
314
|
);
|
|
358
315
|
return;
|
|
359
316
|
}
|
|
317
|
+
if (shape instanceof Point) {
|
|
318
|
+
this.strokePoint(shape.x, shape.y);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
360
321
|
throw new Error("Invalid geometry for fill/stroke");
|
|
361
322
|
}
|
|
362
323
|
|
|
@@ -372,8 +333,6 @@ class Renderer {
|
|
|
372
333
|
|
|
373
334
|
/**
|
|
374
335
|
* tint the given image or canvas using the given color
|
|
375
|
-
* @name tint
|
|
376
|
-
* @memberof Renderer
|
|
377
336
|
* @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} src the source image to be tinted
|
|
378
337
|
* @param {Color|string} color the color that will be used to tint the image
|
|
379
338
|
* @param {string} [mode="multiply"] the composition mode used to tint the image
|
|
@@ -402,8 +361,6 @@ class Renderer {
|
|
|
402
361
|
* A mask limits rendering elements to the shape and position of the given mask object.
|
|
403
362
|
* So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
|
|
404
363
|
* Mask are not preserved through renderer context save and restore.
|
|
405
|
-
* @name setMask
|
|
406
|
-
* @memberof Renderer
|
|
407
364
|
* @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
|
|
408
365
|
* @param {boolean} [invert=false] either the given shape should define what is visible (default) or the opposite
|
|
409
366
|
*/
|
|
@@ -412,16 +369,12 @@ class Renderer {
|
|
|
412
369
|
|
|
413
370
|
/**
|
|
414
371
|
* disable (remove) the rendering mask set through setMask.
|
|
415
|
-
* @name clearMask
|
|
416
372
|
* @see Renderer#setMask
|
|
417
|
-
* @memberof Renderer
|
|
418
373
|
*/
|
|
419
374
|
clearMask() {}
|
|
420
375
|
|
|
421
376
|
/**
|
|
422
377
|
* set a coloring tint for sprite based renderables
|
|
423
|
-
* @name setTint
|
|
424
|
-
* @memberof Renderer
|
|
425
378
|
* @param {Color} tint the tint color
|
|
426
379
|
* @param {number} [alpha] an alpha value to be applied to the tint
|
|
427
380
|
*/
|
|
@@ -433,9 +386,7 @@ class Renderer {
|
|
|
433
386
|
|
|
434
387
|
/**
|
|
435
388
|
* clear the rendering tint set through setTint.
|
|
436
|
-
* @name clearTint
|
|
437
389
|
* @see Renderer#setTint
|
|
438
|
-
* @memberof Renderer
|
|
439
390
|
*/
|
|
440
391
|
clearTint() {
|
|
441
392
|
// reset to default
|
|
@@ -5,7 +5,8 @@ import { setPrefixed } from "./../../utils/agent.js";
|
|
|
5
5
|
var defaultAttributes = {
|
|
6
6
|
offscreenCanvas : false,
|
|
7
7
|
willReadFrequently : false,
|
|
8
|
-
antiAlias : false
|
|
8
|
+
antiAlias : false,
|
|
9
|
+
context: "2d"
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -15,7 +16,8 @@ class CanvasTexture {
|
|
|
15
16
|
/**
|
|
16
17
|
* @param {number} width the desired width of the canvas
|
|
17
18
|
* @param {number} height the desired height of the canvas
|
|
18
|
-
* @param {object} attributes The attributes to create both the canvas and
|
|
19
|
+
* @param {object} attributes The attributes to create both the canvas and context
|
|
20
|
+
* @param {boolean} [attributes.context="2d"] the context type to be created ("2d", "webgl", "webgl2")
|
|
19
21
|
* @param {boolean} [attributes.offscreenCanvas=false] will create an offscreenCanvas if true instead of a standard canvas
|
|
20
22
|
* @param {boolean} [attributes.willReadFrequently=false] Indicates whether or not a lot of read-back operations are planned
|
|
21
23
|
* @param {boolean} [attributes.antiAlias=false] Whether to enable anti-aliasing, use false (default) for a pixelated effect.
|
package/src/video/video.js
CHANGED
|
@@ -20,11 +20,11 @@ var designHeight = 0;
|
|
|
20
20
|
var settings = {
|
|
21
21
|
parent : undefined,
|
|
22
22
|
renderer : 2, // AUTO
|
|
23
|
-
doubleBuffering : false,
|
|
24
23
|
autoScale : false,
|
|
25
24
|
scale : 1.0,
|
|
26
|
-
scaleMethod : "
|
|
25
|
+
scaleMethod : "manual",
|
|
27
26
|
transparent : false,
|
|
27
|
+
premultipliedAlpha: true,
|
|
28
28
|
blendMode : "normal",
|
|
29
29
|
antiAlias : false,
|
|
30
30
|
failIfMajorPerformanceCaveat : true,
|
|
@@ -65,7 +65,7 @@ function onresize() {
|
|
|
65
65
|
var canvasMaxHeight = Infinity;
|
|
66
66
|
|
|
67
67
|
if (globalThis.getComputedStyle) {
|
|
68
|
-
var style = globalThis.getComputedStyle(renderer.
|
|
68
|
+
var style = globalThis.getComputedStyle(renderer.getCanvas(), null);
|
|
69
69
|
canvasMaxWidth = parseInt(style.maxWidth, 10) || Infinity;
|
|
70
70
|
canvasMaxHeight = parseInt(style.maxHeight, 10) || Infinity;
|
|
71
71
|
}
|
|
@@ -119,6 +119,9 @@ function onresize() {
|
|
|
119
119
|
|
|
120
120
|
// adjust scaling ratio based on the new scaling ratio
|
|
121
121
|
scale(scaleX, scaleY);
|
|
122
|
+
} else {
|
|
123
|
+
// adjust scaling ratio based on the given settings
|
|
124
|
+
scale(settings.scale, settings.scale);
|
|
122
125
|
}
|
|
123
126
|
};
|
|
124
127
|
|
|
@@ -196,7 +199,6 @@ export let renderer = null;
|
|
|
196
199
|
* @param {object} [options] The optional video/renderer parameters.<br> (see Renderer(s) documentation for further specific options)
|
|
197
200
|
* @param {string|HTMLElement} [options.parent=document.body] the DOM parent element to hold the canvas in the HTML file
|
|
198
201
|
* @param {number} [options.renderer=video.AUTO] renderer to use (me.video.CANVAS, me.video.WEBGL, me.video.AUTO)
|
|
199
|
-
* @param {boolean} [options.doubleBuffering=false] enable/disable double buffering
|
|
200
202
|
* @param {number|string} [options.scale=1.0] enable scaling of the canvas ('auto' for automatic scaling)
|
|
201
203
|
* @param {string} [options.scaleMethod="fit"] screen scaling modes ('fit','fill-min','fill-max','flex','flex-width','flex-height','stretch')
|
|
202
204
|
* @param {boolean} [options.preferWebGL1=false] if true the renderer will only use WebGL 1
|
|
@@ -211,8 +213,7 @@ export let renderer = null;
|
|
|
211
213
|
* parent : "screen",
|
|
212
214
|
* renderer : me.video.AUTO,
|
|
213
215
|
* scale : "auto",
|
|
214
|
-
* scaleMethod : "fit"
|
|
215
|
-
* doubleBuffering : true
|
|
216
|
+
* scaleMethod : "fit"
|
|
216
217
|
* });
|
|
217
218
|
*/
|
|
218
219
|
export function init(width, height, options) {
|
|
@@ -228,7 +229,6 @@ export function init(width, height, options) {
|
|
|
228
229
|
// sanitize potential given parameters
|
|
229
230
|
settings.width = width;
|
|
230
231
|
settings.height = height;
|
|
231
|
-
settings.doubleBuffering = !!(settings.doubleBuffering);
|
|
232
232
|
settings.transparent = !!(settings.transparent);
|
|
233
233
|
settings.antiAlias = !!(settings.antiAlias);
|
|
234
234
|
settings.failIfMajorPerformanceCaveat = !!(settings.failIfMajorPerformanceCaveat);
|
|
@@ -248,24 +248,21 @@ export function init(width, height, options) {
|
|
|
248
248
|
console.log("melonJS 2 (v" + version + ") | http://melonjs.org" );
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
-
// override renderer settings if &webgl is defined in the URL
|
|
251
|
+
// override renderer settings if &webgl or &canvas is defined in the URL
|
|
252
252
|
var uriFragment = utils.getUriFragment();
|
|
253
253
|
if (uriFragment.webgl === true || uriFragment.webgl1 === true || uriFragment.webgl2 === true) {
|
|
254
254
|
settings.renderer = WEBGL;
|
|
255
255
|
if (uriFragment.webgl1 === true) {
|
|
256
256
|
settings.preferWebGL1 = true;
|
|
257
257
|
}
|
|
258
|
+
} else if (uriFragment.canvas === true) {
|
|
259
|
+
settings.renderer = CANVAS;
|
|
258
260
|
}
|
|
259
261
|
|
|
260
262
|
// normalize scale
|
|
261
263
|
settings.scale = (settings.autoScale) ? 1.0 : (+settings.scale || 1.0);
|
|
262
264
|
scaleRatio.set(settings.scale, settings.scale);
|
|
263
265
|
|
|
264
|
-
// force double buffering if scaling is required
|
|
265
|
-
if (settings.autoScale || (settings.scale !== 1.0)) {
|
|
266
|
-
settings.doubleBuffering = true;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
266
|
// hold the requested video size ratio
|
|
270
267
|
designRatio = width / height;
|
|
271
268
|
designWidth = width;
|
|
@@ -337,7 +334,7 @@ export function init(width, height, options) {
|
|
|
337
334
|
|
|
338
335
|
// add our canvas (default to document.body if settings.parent is undefined)
|
|
339
336
|
parent = device.getElement(typeof settings.parent !== "undefined" ? settings.parent : document.body);
|
|
340
|
-
parent.appendChild(renderer.
|
|
337
|
+
parent.appendChild(renderer.getCanvas());
|
|
341
338
|
|
|
342
339
|
// Mobile browser hacks
|
|
343
340
|
if (device.platform.isMobile) {
|
|
@@ -434,8 +431,8 @@ export function getParent() {
|
|
|
434
431
|
* @param {number} y y scaling multiplier
|
|
435
432
|
*/
|
|
436
433
|
export function scale(x, y) {
|
|
437
|
-
var canvas = renderer.
|
|
438
|
-
var context = renderer.
|
|
434
|
+
var canvas = renderer.getCanvas();
|
|
435
|
+
var context = renderer.getContext();
|
|
439
436
|
var settings = renderer.settings;
|
|
440
437
|
var pixelRatio = device.devicePixelRatio;
|
|
441
438
|
|