melonjs 11.0.0 → 13.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.
- package/LICENSE.md +1 -1
- package/README.md +6 -6
- package/dist/melonjs.js +22854 -22604
- package/dist/melonjs.min.js +5 -6
- package/dist/melonjs.module.d.ts +289 -284
- package/dist/melonjs.module.js +22427 -22175
- package/package.json +16 -16
- package/src/application/application.js +231 -0
- package/src/audio/audio.js +13 -7
- package/src/camera/camera2d.js +6 -6
- package/src/game.js +9 -232
- package/src/index.js +3 -3
- package/src/input/keyboard.js +2 -2
- package/src/input/pointer.js +4 -5
- package/src/input/pointerevent.js +10 -10
- package/src/lang/deprecated.js +27 -30
- package/src/level/level.js +2 -2
- package/src/level/tiled/TMXGroup.js +10 -0
- package/src/level/tiled/TMXLayer.js +11 -2
- package/src/level/tiled/TMXObject.js +13 -2
- package/src/level/tiled/TMXTileMap.js +15 -3
- package/src/level/tiled/TMXTileset.js +8 -0
- package/src/loader/loader.js +64 -28
- package/src/loader/loadingscreen.js +28 -115
- package/src/loader/melonjs_logo.png +0 -0
- package/src/math/color.js +62 -42
- 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 +27 -51
- package/src/physics/detector.js +3 -3
- package/src/physics/quadtree.js +58 -29
- package/src/physics/world.js +32 -3
- package/src/polyfill/index.js +4 -0
- package/src/renderable/container.js +2 -2
- package/src/renderable/imagelayer.js +8 -8
- package/src/renderable/nineslicesprite.js +27 -1
- package/src/renderable/trigger.js +4 -4
- package/src/state/stage.js +1 -1
- package/src/state/state.js +50 -3
- package/src/system/device.js +814 -981
- package/src/system/event.js +2 -1
- package/src/system/platform.js +32 -0
- package/src/system/save.js +23 -14
- package/src/system/timer.js +12 -35
- package/src/text/bitmaptext.js +1 -2
- package/src/text/text.js +10 -14
- package/src/text/textmetrics.js +1 -2
- package/src/tweens/tween.js +6 -6
- package/src/utils/string.js +13 -24
- package/src/video/canvas/canvas_renderer.js +30 -65
- package/src/video/renderer.js +23 -30
- package/src/video/texture/canvas_texture.js +39 -3
- package/src/video/video.js +27 -25
- package/src/video/webgl/glshader.js +1 -1
- package/src/video/webgl/webgl_compositor.js +2 -2
- package/src/video/webgl/webgl_renderer.js +8 -20
|
@@ -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
|
/**
|
|
@@ -147,17 +129,6 @@ class CanvasRenderer extends Renderer {
|
|
|
147
129
|
}
|
|
148
130
|
}
|
|
149
131
|
|
|
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);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
132
|
/**
|
|
162
133
|
* Clears the main framebuffer with the given color
|
|
163
134
|
* @name clearColor
|
|
@@ -166,11 +137,14 @@ class CanvasRenderer extends Renderer {
|
|
|
166
137
|
* @param {boolean} [opaque=false] Allow transparency [default] or clear the surface completely [true]
|
|
167
138
|
*/
|
|
168
139
|
clearColor(color = "#000000", opaque) {
|
|
140
|
+
var canvas = this.getCanvas();
|
|
141
|
+
var context = this.getContext();
|
|
142
|
+
|
|
169
143
|
this.save();
|
|
170
144
|
this.resetTransform();
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
this.fillRect(0, 0,
|
|
145
|
+
context.globalCompositeOperation = opaque ? "copy" : "source-over";
|
|
146
|
+
context.fillStyle = (color instanceof Color) ? color.toRGBA() : color;
|
|
147
|
+
this.fillRect(0, 0, canvas.width, canvas.height);
|
|
174
148
|
this.restore();
|
|
175
149
|
}
|
|
176
150
|
|
|
@@ -530,17 +504,6 @@ class CanvasRenderer extends Renderer {
|
|
|
530
504
|
this.strokeRoundRect(x, y, width, height, radius, true);
|
|
531
505
|
}
|
|
532
506
|
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* return a reference to the system 2d Context
|
|
536
|
-
* @name getContext
|
|
537
|
-
* @memberof CanvasRenderer
|
|
538
|
-
* @returns {CanvasRenderingContext2D}
|
|
539
|
-
*/
|
|
540
|
-
getContext() {
|
|
541
|
-
return this.backBufferContext2D;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
507
|
/**
|
|
545
508
|
* return a reference to the font 2d Context
|
|
546
509
|
* @ignore
|
|
@@ -556,7 +519,7 @@ class CanvasRenderer extends Renderer {
|
|
|
556
519
|
* @memberof CanvasRenderer
|
|
557
520
|
*/
|
|
558
521
|
save() {
|
|
559
|
-
this.
|
|
522
|
+
this.getContext().save();
|
|
560
523
|
}
|
|
561
524
|
|
|
562
525
|
/**
|
|
@@ -565,12 +528,12 @@ class CanvasRenderer extends Renderer {
|
|
|
565
528
|
* @memberof CanvasRenderer
|
|
566
529
|
*/
|
|
567
530
|
restore() {
|
|
568
|
-
this.
|
|
531
|
+
this.getContext().restore();
|
|
569
532
|
this.currentColor.glArray[3] = this.getGlobalAlpha();
|
|
570
533
|
this.currentScissor[0] = 0;
|
|
571
534
|
this.currentScissor[1] = 0;
|
|
572
|
-
this.currentScissor[2] = this.
|
|
573
|
-
this.currentScissor[3] = this.
|
|
535
|
+
this.currentScissor[2] = this.getCanvas().width;
|
|
536
|
+
this.currentScissor[3] = this.getCanvas().height;
|
|
574
537
|
}
|
|
575
538
|
|
|
576
539
|
/**
|
|
@@ -580,7 +543,7 @@ class CanvasRenderer extends Renderer {
|
|
|
580
543
|
* @param {number} angle in radians
|
|
581
544
|
*/
|
|
582
545
|
rotate(angle) {
|
|
583
|
-
this.
|
|
546
|
+
this.getContext().rotate(angle);
|
|
584
547
|
}
|
|
585
548
|
|
|
586
549
|
/**
|
|
@@ -591,7 +554,7 @@ class CanvasRenderer extends Renderer {
|
|
|
591
554
|
* @param {number} y
|
|
592
555
|
*/
|
|
593
556
|
scale(x, y) {
|
|
594
|
-
this.
|
|
557
|
+
this.getContext().scale(x, y);
|
|
595
558
|
}
|
|
596
559
|
|
|
597
560
|
/**
|
|
@@ -602,8 +565,9 @@ class CanvasRenderer extends Renderer {
|
|
|
602
565
|
* @param {Color|string} color css color value
|
|
603
566
|
*/
|
|
604
567
|
setColor(color) {
|
|
605
|
-
this.
|
|
606
|
-
|
|
568
|
+
var context = this.getContext();
|
|
569
|
+
context.strokeStyle =
|
|
570
|
+
context.fillStyle = (
|
|
607
571
|
color instanceof Color ?
|
|
608
572
|
color.toRGBA() :
|
|
609
573
|
color
|
|
@@ -617,7 +581,7 @@ class CanvasRenderer extends Renderer {
|
|
|
617
581
|
* @param {number} alpha 0.0 to 1.0 values accepted.
|
|
618
582
|
*/
|
|
619
583
|
setGlobalAlpha(alpha) {
|
|
620
|
-
this.
|
|
584
|
+
this.getContext().globalAlpha = this.currentColor.glArray[3] = alpha;
|
|
621
585
|
}
|
|
622
586
|
|
|
623
587
|
/**
|
|
@@ -627,7 +591,7 @@ class CanvasRenderer extends Renderer {
|
|
|
627
591
|
* @returns {number} global alpha value
|
|
628
592
|
*/
|
|
629
593
|
getGlobalAlpha() {
|
|
630
|
-
return this.
|
|
594
|
+
return this.getContext().globalAlpha;
|
|
631
595
|
}
|
|
632
596
|
|
|
633
597
|
/**
|
|
@@ -637,7 +601,7 @@ class CanvasRenderer extends Renderer {
|
|
|
637
601
|
* @param {number} width Line width
|
|
638
602
|
*/
|
|
639
603
|
setLineWidth(width) {
|
|
640
|
-
this.
|
|
604
|
+
this.getContext().lineWidth = width;
|
|
641
605
|
}
|
|
642
606
|
|
|
643
607
|
/**
|
|
@@ -672,7 +636,7 @@ class CanvasRenderer extends Renderer {
|
|
|
672
636
|
f |= 0;
|
|
673
637
|
}
|
|
674
638
|
|
|
675
|
-
this.
|
|
639
|
+
this.getContext().transform(a, b, c, d, e, f);
|
|
676
640
|
}
|
|
677
641
|
|
|
678
642
|
/**
|
|
@@ -684,9 +648,9 @@ class CanvasRenderer extends Renderer {
|
|
|
684
648
|
*/
|
|
685
649
|
translate(x, y) {
|
|
686
650
|
if (this.settings.subPixel === false) {
|
|
687
|
-
this.
|
|
651
|
+
this.getContext().translate(~~x, ~~y);
|
|
688
652
|
} else {
|
|
689
|
-
this.
|
|
653
|
+
this.getContext().translate(x, y);
|
|
690
654
|
}
|
|
691
655
|
}
|
|
692
656
|
|
|
@@ -704,14 +668,14 @@ class CanvasRenderer extends Renderer {
|
|
|
704
668
|
* @param {number} height
|
|
705
669
|
*/
|
|
706
670
|
clipRect(x, y, width, height) {
|
|
707
|
-
var canvas = this.
|
|
671
|
+
var canvas = this.getCanvas();
|
|
708
672
|
// if requested box is different from the current canvas size;
|
|
709
673
|
if (x !== 0 || y !== 0 || width !== canvas.width || height !== canvas.height) {
|
|
710
674
|
var currentScissor = this.currentScissor;
|
|
711
675
|
// if different from the current scissor box
|
|
712
676
|
if (currentScissor[0] !== x || currentScissor[1] !== y ||
|
|
713
677
|
currentScissor[2] !== width || currentScissor[3] !== height) {
|
|
714
|
-
var context = this.
|
|
678
|
+
var context = this.getContext();
|
|
715
679
|
context.beginPath();
|
|
716
680
|
context.rect(x, y, width, height);
|
|
717
681
|
context.clip();
|
|
@@ -743,11 +707,11 @@ class CanvasRenderer extends Renderer {
|
|
|
743
707
|
}
|
|
744
708
|
|
|
745
709
|
// https://github.com/melonjs/melonJS/issues/648
|
|
746
|
-
|
|
710
|
+
if (mask instanceof RoundRect) {
|
|
747
711
|
context.roundRect(mask.top, mask.left, mask.width, mask.height, mask.radius);
|
|
748
712
|
} else if (mask instanceof Rect || mask instanceof Bounds) {
|
|
749
713
|
context.rect(mask.top, mask.left, mask.width, mask.height);
|
|
750
|
-
}
|
|
714
|
+
} else if (mask instanceof Ellipse) {
|
|
751
715
|
const _x = mask.pos.x, _y = mask.pos.y,
|
|
752
716
|
hw = mask.radiusV.x,
|
|
753
717
|
hh = mask.radiusV.y,
|
|
@@ -769,6 +733,7 @@ class CanvasRenderer extends Renderer {
|
|
|
769
733
|
context.bezierCurveTo(xmin, by, lx, ymax, lx, _y);
|
|
770
734
|
context.bezierCurveTo(lx, ymin, xmin, ty, _x, ty);
|
|
771
735
|
} else {
|
|
736
|
+
// polygon
|
|
772
737
|
const _x = mask.pos.x, _y = mask.pos.y;
|
|
773
738
|
var point;
|
|
774
739
|
|
package/src/video/renderer.js
CHANGED
|
@@ -2,7 +2,7 @@ import Color from "./../math/color.js";
|
|
|
2
2
|
import Matrix3d from "./../math/matrix3.js";
|
|
3
3
|
import { createCanvas, renderer } from "./video.js";
|
|
4
4
|
import * as event from "./../system/event.js";
|
|
5
|
-
import device from "./../system/device.js";
|
|
5
|
+
import * as device from "./../system/device.js";
|
|
6
6
|
import { setPrefixed } from "./../utils/agent.js";
|
|
7
7
|
import Rect from "./../geometries/rectangle.js";
|
|
8
8
|
import RoundRect from "./../geometries/roundrect.js";
|
|
@@ -22,7 +22,6 @@ class Renderer {
|
|
|
22
22
|
* @param {number} options.width The width of the canvas without scaling
|
|
23
23
|
* @param {number} options.height The height of the canvas without scaling
|
|
24
24
|
* @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
|
|
25
|
-
* @param {boolean} [options.doubleBuffering=false] Whether to enable double buffering
|
|
26
25
|
* @param {boolean} [options.antiAlias=false] Whether to enable anti-aliasing, use false (default) for a pixelated effect.
|
|
27
26
|
* @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
27
|
* @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas (performance hit when enabled)
|
|
@@ -84,13 +83,9 @@ class Renderer {
|
|
|
84
83
|
} else if (typeof this.settings.canvas !== "undefined") {
|
|
85
84
|
this.canvas = this.settings.canvas;
|
|
86
85
|
} else {
|
|
87
|
-
this.canvas = createCanvas(this.settings.
|
|
86
|
+
this.canvas = createCanvas(this.settings.width, this.settings.height);
|
|
88
87
|
}
|
|
89
88
|
|
|
90
|
-
// canvas object and context
|
|
91
|
-
this.backBufferCanvas = this.canvas;
|
|
92
|
-
this.context = null;
|
|
93
|
-
|
|
94
89
|
// global color
|
|
95
90
|
this.currentColor = new Color(0, 0, 0, 1.0);
|
|
96
91
|
|
|
@@ -116,6 +111,13 @@ class Renderer {
|
|
|
116
111
|
*/
|
|
117
112
|
clear() {}
|
|
118
113
|
|
|
114
|
+
/**
|
|
115
|
+
* render the main framebuffer on screen
|
|
116
|
+
* @name flush
|
|
117
|
+
* @memberof Renderer
|
|
118
|
+
*/
|
|
119
|
+
flush() {}
|
|
120
|
+
|
|
119
121
|
/**
|
|
120
122
|
* Reset context state
|
|
121
123
|
* @name reset
|
|
@@ -129,39 +131,29 @@ class Renderer {
|
|
|
129
131
|
this.cache.clear();
|
|
130
132
|
this.currentScissor[0] = 0;
|
|
131
133
|
this.currentScissor[1] = 0;
|
|
132
|
-
this.currentScissor[2] = this.
|
|
133
|
-
this.currentScissor[3] = this.
|
|
134
|
+
this.currentScissor[2] = this.getCanvas().width;
|
|
135
|
+
this.currentScissor[3] = this.getCanvas().height;
|
|
134
136
|
this.clearMask();
|
|
135
137
|
}
|
|
136
138
|
|
|
137
139
|
/**
|
|
138
|
-
* return a reference to the
|
|
140
|
+
* return a reference to the canvas which this renderer draws to
|
|
139
141
|
* @name getCanvas
|
|
140
142
|
* @memberof Renderer
|
|
141
143
|
* @returns {HTMLCanvasElement}
|
|
142
144
|
*/
|
|
143
145
|
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
146
|
return this.canvas;
|
|
155
147
|
}
|
|
156
148
|
|
|
149
|
+
|
|
157
150
|
/**
|
|
158
|
-
* return a reference to
|
|
159
|
-
*
|
|
160
|
-
* @name getScreenContext
|
|
151
|
+
* return a reference to this renderer canvas corresponding Context
|
|
152
|
+
* @name getContext
|
|
161
153
|
* @memberof Renderer
|
|
162
|
-
* @returns {CanvasRenderingContext2D}
|
|
154
|
+
* @returns {CanvasRenderingContext2D|WebGLRenderingContext}
|
|
163
155
|
*/
|
|
164
|
-
|
|
156
|
+
getContext() {
|
|
165
157
|
return this.context;
|
|
166
158
|
}
|
|
167
159
|
|
|
@@ -220,7 +212,7 @@ class Renderer {
|
|
|
220
212
|
* @returns {number}
|
|
221
213
|
*/
|
|
222
214
|
getWidth() {
|
|
223
|
-
return this.
|
|
215
|
+
return this.getCanvas().width;
|
|
224
216
|
}
|
|
225
217
|
|
|
226
218
|
/**
|
|
@@ -230,7 +222,7 @@ class Renderer {
|
|
|
230
222
|
* @returns {number} height of the system Canvas
|
|
231
223
|
*/
|
|
232
224
|
getHeight() {
|
|
233
|
-
return this.
|
|
225
|
+
return this.getCanvas().height;
|
|
234
226
|
}
|
|
235
227
|
|
|
236
228
|
/**
|
|
@@ -276,9 +268,10 @@ class Renderer {
|
|
|
276
268
|
* @param {number} height new height of the canvas
|
|
277
269
|
*/
|
|
278
270
|
resize(width, height) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
271
|
+
var canvas = this.getCanvas();
|
|
272
|
+
if (width !== canvas.width || height !== canvas.height) {
|
|
273
|
+
canvas.width = width;
|
|
274
|
+
canvas.height = height;
|
|
282
275
|
this.currentScissor[0] = 0;
|
|
283
276
|
this.currentScissor[1] = 0;
|
|
284
277
|
this.currentScissor[2] = width;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { createCanvas } from "./../video.js";
|
|
2
|
+
import { setPrefixed } from "./../../utils/agent.js";
|
|
2
3
|
|
|
3
|
-
// default
|
|
4
|
+
// default canvas settings
|
|
4
5
|
var defaultAttributes = {
|
|
5
6
|
offscreenCanvas : false,
|
|
6
|
-
willReadFrequently : false
|
|
7
|
+
willReadFrequently : false,
|
|
8
|
+
antiAlias : false,
|
|
9
|
+
context: "2d"
|
|
7
10
|
};
|
|
8
11
|
|
|
9
12
|
/**
|
|
@@ -13,9 +16,11 @@ class CanvasTexture {
|
|
|
13
16
|
/**
|
|
14
17
|
* @param {number} width the desired width of the canvas
|
|
15
18
|
* @param {number} height the desired height of the canvas
|
|
16
|
-
* @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")
|
|
17
21
|
* @param {boolean} [attributes.offscreenCanvas=false] will create an offscreenCanvas if true instead of a standard canvas
|
|
18
22
|
* @param {boolean} [attributes.willReadFrequently=false] Indicates whether or not a lot of read-back operations are planned
|
|
23
|
+
* @param {boolean} [attributes.antiAlias=false] Whether to enable anti-aliasing, use false (default) for a pixelated effect.
|
|
19
24
|
*/
|
|
20
25
|
constructor(width, height, attributes = defaultAttributes) {
|
|
21
26
|
|
|
@@ -33,6 +38,9 @@ class CanvasTexture {
|
|
|
33
38
|
* @type {CanvasRenderingContext2D}
|
|
34
39
|
*/
|
|
35
40
|
this.context = this.canvas.getContext("2d", { willReadFrequently: attributes.willReadFrequently });
|
|
41
|
+
|
|
42
|
+
// enable or disable antiAlias if specified
|
|
43
|
+
this.setAntiAlias(attributes.antiAlias);
|
|
36
44
|
}
|
|
37
45
|
|
|
38
46
|
/**
|
|
@@ -51,6 +59,34 @@ class CanvasTexture {
|
|
|
51
59
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
52
60
|
}
|
|
53
61
|
|
|
62
|
+
/**
|
|
63
|
+
* enable/disable image smoothing (scaling interpolation)
|
|
64
|
+
* @param {boolean} [enable=false]
|
|
65
|
+
*/
|
|
66
|
+
setAntiAlias(enable = false) {
|
|
67
|
+
var canvas = this.canvas;
|
|
68
|
+
|
|
69
|
+
// enable/disable antialias on the given Context2d object
|
|
70
|
+
setPrefixed("imageSmoothingEnabled", enable, this.context);
|
|
71
|
+
|
|
72
|
+
// set antialias CSS property on the main canvas
|
|
73
|
+
if (typeof canvas.style !== "undefined") {
|
|
74
|
+
if (enable !== true) {
|
|
75
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering
|
|
76
|
+
canvas.style["image-rendering"] = "optimizeSpeed"; // legal fallback
|
|
77
|
+
canvas.style["image-rendering"] = "-moz-crisp-edges"; // Firefox
|
|
78
|
+
canvas.style["image-rendering"] = "-o-crisp-edges"; // Opera
|
|
79
|
+
canvas.style["image-rendering"] = "-webkit-optimize-contrast"; // Safari
|
|
80
|
+
canvas.style["image-rendering"] = "optimize-contrast"; // CSS 3
|
|
81
|
+
canvas.style["image-rendering"] = "crisp-edges"; // CSS 4
|
|
82
|
+
canvas.style["image-rendering"] = "pixelated"; // CSS 4
|
|
83
|
+
canvas.style.msInterpolationMode = "nearest-neighbor"; // IE8+
|
|
84
|
+
} else {
|
|
85
|
+
canvas.style["image-rendering"] = "auto";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
54
90
|
/**
|
|
55
91
|
* Resizes the canvas texture to the given width and height.
|
|
56
92
|
* @param {number} width the desired width
|
package/src/video/video.js
CHANGED
|
@@ -3,8 +3,8 @@ import WebGLRenderer from "./webgl/webgl_renderer.js";
|
|
|
3
3
|
import CanvasRenderer from "./canvas/canvas_renderer.js";
|
|
4
4
|
import utils from "./../utils/utils.js";
|
|
5
5
|
import * as event from "./../system/event.js";
|
|
6
|
-
import
|
|
7
|
-
import device from "./../system/device.js";
|
|
6
|
+
import game from "./../game.js";
|
|
7
|
+
import * as device from "./../system/device.js";
|
|
8
8
|
import { initialized, version } from "./../index.js";
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -20,10 +20,9 @@ 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,
|
|
28
27
|
blendMode : "normal",
|
|
29
28
|
antiAlias : false,
|
|
@@ -65,7 +64,7 @@ function onresize() {
|
|
|
65
64
|
var canvasMaxHeight = Infinity;
|
|
66
65
|
|
|
67
66
|
if (globalThis.getComputedStyle) {
|
|
68
|
-
var style = globalThis.getComputedStyle(renderer.
|
|
67
|
+
var style = globalThis.getComputedStyle(renderer.getCanvas(), null);
|
|
69
68
|
canvasMaxWidth = parseInt(style.maxWidth, 10) || Infinity;
|
|
70
69
|
canvasMaxHeight = parseInt(style.maxHeight, 10) || Infinity;
|
|
71
70
|
}
|
|
@@ -119,6 +118,9 @@ function onresize() {
|
|
|
119
118
|
|
|
120
119
|
// adjust scaling ratio based on the new scaling ratio
|
|
121
120
|
scale(scaleX, scaleY);
|
|
121
|
+
} else {
|
|
122
|
+
// adjust scaling ratio based on the given settings
|
|
123
|
+
scale(settings.scale, settings.scale);
|
|
122
124
|
}
|
|
123
125
|
};
|
|
124
126
|
|
|
@@ -196,7 +198,6 @@ export let renderer = null;
|
|
|
196
198
|
* @param {object} [options] The optional video/renderer parameters.<br> (see Renderer(s) documentation for further specific options)
|
|
197
199
|
* @param {string|HTMLElement} [options.parent=document.body] the DOM parent element to hold the canvas in the HTML file
|
|
198
200
|
* @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
201
|
* @param {number|string} [options.scale=1.0] enable scaling of the canvas ('auto' for automatic scaling)
|
|
201
202
|
* @param {string} [options.scaleMethod="fit"] screen scaling modes ('fit','fill-min','fill-max','flex','flex-width','flex-height','stretch')
|
|
202
203
|
* @param {boolean} [options.preferWebGL1=false] if true the renderer will only use WebGL 1
|
|
@@ -211,8 +212,7 @@ export let renderer = null;
|
|
|
211
212
|
* parent : "screen",
|
|
212
213
|
* renderer : me.video.AUTO,
|
|
213
214
|
* scale : "auto",
|
|
214
|
-
* scaleMethod : "fit"
|
|
215
|
-
* doubleBuffering : true
|
|
215
|
+
* scaleMethod : "fit"
|
|
216
216
|
* });
|
|
217
217
|
*/
|
|
218
218
|
export function init(width, height, options) {
|
|
@@ -228,7 +228,6 @@ export function init(width, height, options) {
|
|
|
228
228
|
// sanitize potential given parameters
|
|
229
229
|
settings.width = width;
|
|
230
230
|
settings.height = height;
|
|
231
|
-
settings.doubleBuffering = !!(settings.doubleBuffering);
|
|
232
231
|
settings.transparent = !!(settings.transparent);
|
|
233
232
|
settings.antiAlias = !!(settings.antiAlias);
|
|
234
233
|
settings.failIfMajorPerformanceCaveat = !!(settings.failIfMajorPerformanceCaveat);
|
|
@@ -248,24 +247,21 @@ export function init(width, height, options) {
|
|
|
248
247
|
console.log("melonJS 2 (v" + version + ") | http://melonjs.org" );
|
|
249
248
|
}
|
|
250
249
|
|
|
251
|
-
// override renderer settings if &webgl is defined in the URL
|
|
250
|
+
// override renderer settings if &webgl or &canvas is defined in the URL
|
|
252
251
|
var uriFragment = utils.getUriFragment();
|
|
253
252
|
if (uriFragment.webgl === true || uriFragment.webgl1 === true || uriFragment.webgl2 === true) {
|
|
254
253
|
settings.renderer = WEBGL;
|
|
255
254
|
if (uriFragment.webgl1 === true) {
|
|
256
255
|
settings.preferWebGL1 = true;
|
|
257
256
|
}
|
|
257
|
+
} else if (uriFragment.canvas === true) {
|
|
258
|
+
settings.renderer = CANVAS;
|
|
258
259
|
}
|
|
259
260
|
|
|
260
261
|
// normalize scale
|
|
261
262
|
settings.scale = (settings.autoScale) ? 1.0 : (+settings.scale || 1.0);
|
|
262
263
|
scaleRatio.set(settings.scale, settings.scale);
|
|
263
264
|
|
|
264
|
-
// force double buffering if scaling is required
|
|
265
|
-
if (settings.autoScale || (settings.scale !== 1.0)) {
|
|
266
|
-
settings.doubleBuffering = true;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
265
|
// hold the requested video size ratio
|
|
270
266
|
designRatio = width / height;
|
|
271
267
|
designWidth = width;
|
|
@@ -302,7 +298,7 @@ export function init(width, height, options) {
|
|
|
302
298
|
false
|
|
303
299
|
);
|
|
304
300
|
|
|
305
|
-
if (device.
|
|
301
|
+
if (device.screenOrientation === true) {
|
|
306
302
|
globalThis.screen.orientation.onchange = function (e) {
|
|
307
303
|
event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
|
|
308
304
|
};
|
|
@@ -337,7 +333,13 @@ export function init(width, height, options) {
|
|
|
337
333
|
|
|
338
334
|
// add our canvas (default to document.body if settings.parent is undefined)
|
|
339
335
|
parent = device.getElement(typeof settings.parent !== "undefined" ? settings.parent : document.body);
|
|
340
|
-
parent.appendChild(renderer.
|
|
336
|
+
parent.appendChild(renderer.getCanvas());
|
|
337
|
+
|
|
338
|
+
// Mobile browser hacks
|
|
339
|
+
if (device.platform.isMobile) {
|
|
340
|
+
// Prevent the webview from moving on a swipe
|
|
341
|
+
device.enableSwipe(false);
|
|
342
|
+
}
|
|
341
343
|
|
|
342
344
|
// trigger an initial resize();
|
|
343
345
|
onresize();
|
|
@@ -362,7 +364,7 @@ export function init(width, height, options) {
|
|
|
362
364
|
renderType + " renderer" + gpu_renderer + " | " +
|
|
363
365
|
audioType + " | " +
|
|
364
366
|
"pixel ratio " + device.devicePixelRatio + " | " +
|
|
365
|
-
(device.nodeJS ? "node.js" : device.isMobile ? "mobile" : "desktop") + " | " +
|
|
367
|
+
(device.platform.nodeJS ? "node.js" : device.platform.isMobile ? "mobile" : "desktop") + " | " +
|
|
366
368
|
device.getScreenOrientation() + " | " +
|
|
367
369
|
device.language
|
|
368
370
|
);
|
|
@@ -382,18 +384,18 @@ export function init(width, height, options) {
|
|
|
382
384
|
* @function video.createCanvas
|
|
383
385
|
* @param {number} width width
|
|
384
386
|
* @param {number} height height
|
|
385
|
-
* @param {boolean} [
|
|
387
|
+
* @param {boolean} [returnOffscreenCanvas=false] will return an OffscreenCanvas if supported
|
|
386
388
|
* @returns {HTMLCanvasElement|OffscreenCanvas}
|
|
387
389
|
*/
|
|
388
|
-
export function createCanvas(width, height,
|
|
390
|
+
export function createCanvas(width, height, returnOffscreenCanvas = false) {
|
|
389
391
|
var _canvas;
|
|
390
392
|
|
|
391
393
|
if (width === 0 || height === 0) {
|
|
392
394
|
throw new Error("width or height was zero, Canvas could not be initialized !");
|
|
393
395
|
}
|
|
394
396
|
|
|
395
|
-
if (device.
|
|
396
|
-
_canvas = new OffscreenCanvas(0, 0);
|
|
397
|
+
if (device.offscreenCanvas === true && returnOffscreenCanvas === true) {
|
|
398
|
+
_canvas = new globalThis.OffscreenCanvas(0, 0);
|
|
397
399
|
// stubbing style for compatibility,
|
|
398
400
|
// as OffscreenCanvas is detached from the DOM
|
|
399
401
|
if (typeof _canvas.style === "undefined") {
|
|
@@ -428,8 +430,8 @@ export function getParent() {
|
|
|
428
430
|
* @param {number} y y scaling multiplier
|
|
429
431
|
*/
|
|
430
432
|
export function scale(x, y) {
|
|
431
|
-
var canvas = renderer.
|
|
432
|
-
var context = renderer.
|
|
433
|
+
var canvas = renderer.getCanvas();
|
|
434
|
+
var context = renderer.getContext();
|
|
433
435
|
var settings = renderer.settings;
|
|
434
436
|
var pixelRatio = device.devicePixelRatio;
|
|
435
437
|
|
|
@@ -448,5 +450,5 @@ export function scale(x, y) {
|
|
|
448
450
|
renderer.setBlendMode(settings.blendMode, context);
|
|
449
451
|
|
|
450
452
|
// force repaint
|
|
451
|
-
repaint();
|
|
453
|
+
game.repaint();
|
|
452
454
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as event from "./../../system/event.js";
|
|
2
|
-
import device from "./../../system/device.js";
|
|
2
|
+
import * as device from "./../../system/device.js";
|
|
3
3
|
import { extractUniforms } from "./utils/uniforms.js";
|
|
4
4
|
import { extractAttributes } from "./utils/attributes.js";
|
|
5
5
|
import { compileProgram } from "./utils/program.js";
|
|
@@ -132,8 +132,8 @@ class WebGLCompositor {
|
|
|
132
132
|
// initial viewport size
|
|
133
133
|
this.setViewport(
|
|
134
134
|
0, 0,
|
|
135
|
-
this.renderer.
|
|
136
|
-
this.renderer.
|
|
135
|
+
this.renderer.getCanvas().width,
|
|
136
|
+
this.renderer.getCanvas().height
|
|
137
137
|
);
|
|
138
138
|
|
|
139
139
|
// Initialize clear color
|