melonjs 14.0.2 → 14.1.1
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/README.md +2 -0
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
- package/dist/melonjs.mjs/_virtual/howler.js +10 -0
- package/dist/melonjs.mjs/_virtual/index.js +10 -0
- package/dist/melonjs.mjs/_virtual/index2.js +10 -0
- package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
- package/dist/melonjs.mjs/application/application.js +238 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +731 -0
- package/dist/melonjs.mjs/entity/entity.js +247 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +274 -0
- package/dist/melonjs.mjs/geometries/line.js +115 -0
- package/dist/melonjs.mjs/geometries/path2d.js +318 -0
- package/dist/melonjs.mjs/geometries/point.js +88 -0
- package/dist/melonjs.mjs/geometries/poly.js +498 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +374 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +167 -0
- package/dist/melonjs.mjs/index.js +248 -0
- package/dist/melonjs.mjs/input/gamepad.js +501 -0
- package/dist/melonjs.mjs/input/input.js +26 -0
- package/dist/melonjs.mjs/input/keyboard.js +470 -0
- package/dist/melonjs.mjs/input/pointer.js +393 -0
- package/dist/melonjs.mjs/input/pointerevent.js +818 -0
- package/dist/melonjs.mjs/lang/deprecated.js +157 -0
- package/dist/melonjs.mjs/level/level.js +297 -0
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +446 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +193 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +636 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +309 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +81 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +504 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +218 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +155 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +124 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +107 -0
- package/dist/melonjs.mjs/loader/loader.js +801 -0
- package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
- package/dist/melonjs.mjs/math/color.js +616 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +501 -0
- package/dist/melonjs.mjs/math/matrix3.js +679 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +469 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +559 -0
- package/dist/melonjs.mjs/math/vector2.js +526 -0
- package/dist/melonjs.mjs/math/vector3.js +567 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
- package/dist/melonjs.mjs/particles/emitter.js +265 -0
- package/dist/melonjs.mjs/particles/particle.js +186 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +702 -0
- package/dist/melonjs.mjs/physics/bounds.js +459 -0
- package/dist/melonjs.mjs/physics/collision.js +132 -0
- package/dist/melonjs.mjs/physics/detector.js +194 -0
- package/dist/melonjs.mjs/physics/quadtree.js +394 -0
- package/dist/melonjs.mjs/physics/response.js +57 -0
- package/dist/melonjs.mjs/physics/sat.js +483 -0
- package/dist/melonjs.mjs/physics/world.js +219 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +60 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +78 -0
- package/dist/melonjs.mjs/renderable/container.js +1016 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +305 -0
- package/dist/melonjs.mjs/renderable/light2d.js +155 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +246 -0
- package/dist/melonjs.mjs/renderable/renderable.js +781 -0
- package/dist/melonjs.mjs/renderable/sprite.js +653 -0
- package/dist/melonjs.mjs/renderable/trigger.js +156 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +212 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +225 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +127 -0
- package/dist/melonjs.mjs/state/stage.js +236 -0
- package/dist/melonjs.mjs/state/state.js +596 -0
- package/dist/melonjs.mjs/system/device.js +909 -0
- package/dist/melonjs.mjs/system/dom.js +78 -0
- package/dist/melonjs.mjs/system/event.js +537 -0
- package/dist/melonjs.mjs/system/platform.js +41 -0
- package/dist/melonjs.mjs/system/pooling.js +209 -0
- package/dist/melonjs.mjs/system/save.js +157 -0
- package/dist/melonjs.mjs/system/timer.js +286 -0
- package/dist/melonjs.mjs/text/bitmaptext.js +363 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +198 -0
- package/dist/melonjs.mjs/text/glyph.js +65 -0
- package/dist/melonjs.mjs/text/text.js +452 -0
- package/dist/melonjs.mjs/text/textmetrics.js +175 -0
- package/dist/melonjs.mjs/text/textstyle.js +23 -0
- package/dist/melonjs.mjs/tweens/easing.js +336 -0
- package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
- package/dist/melonjs.mjs/tweens/tween.js +479 -0
- package/dist/melonjs.mjs/utils/agent.js +76 -0
- package/dist/melonjs.mjs/utils/array.js +63 -0
- package/dist/melonjs.mjs/utils/file.js +42 -0
- package/dist/melonjs.mjs/utils/function.js +70 -0
- package/dist/melonjs.mjs/utils/string.js +82 -0
- package/dist/melonjs.mjs/utils/utils.js +173 -0
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +806 -0
- package/dist/melonjs.mjs/video/renderer.js +410 -0
- package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
- package/dist/melonjs.mjs/video/texture/cache.js +143 -0
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
- package/dist/melonjs.mjs/video/video.js +462 -0
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +142 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +167 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
- package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
- package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +494 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1035 -0
- package/dist/melonjs.module.d.ts +1298 -1359
- package/dist/melonjs.module.js +2072 -3520
- package/package.json +21 -16
- package/src/application/application.js +4 -5
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +32 -33
- package/src/entity/entity.js +18 -19
- package/src/geometries/ellipse.js +17 -18
- package/src/geometries/line.js +6 -7
- package/src/geometries/path2d.js +33 -34
- package/src/geometries/point.js +1 -2
- package/src/geometries/poly.js +16 -18
- package/src/geometries/rectangle.js +19 -20
- package/src/geometries/roundrect.js +9 -10
- package/src/input/gamepad.js +15 -15
- package/src/input/keyboard.js +12 -12
- package/src/input/pointer.js +6 -6
- package/src/input/pointerevent.js +12 -12
- package/src/lang/deprecated.js +12 -12
- package/src/level/level.js +25 -25
- package/src/level/tiled/TMXLayer.js +23 -24
- package/src/level/tiled/TMXTile.js +6 -7
- package/src/level/tiled/TMXTileMap.js +8 -10
- package/src/level/tiled/TMXTileset.js +3 -4
- package/src/level/tiled/TMXTilesetGroup.js +1 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
- package/src/level/tiled/renderer/TMXRenderer.js +20 -21
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -2
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +21 -22
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +17 -18
- package/src/math/matrix3.js +26 -27
- package/src/math/observable_vector2.js +15 -16
- package/src/math/observable_vector3.js +17 -18
- package/src/math/vector2.js +10 -11
- package/src/math/vector3.js +11 -12
- package/src/particles/emitter.js +7 -8
- package/src/particles/particle.js +3 -4
- package/src/physics/body.js +29 -30
- package/src/physics/bounds.js +10 -10
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +18 -23
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +6 -7
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +4 -6
- package/src/renderable/colorlayer.js +6 -8
- package/src/renderable/container.js +25 -27
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +14 -15
- package/src/renderable/light2d.js +4 -5
- package/src/renderable/nineslicesprite.js +17 -18
- package/src/renderable/renderable.js +26 -28
- package/src/renderable/sprite.js +29 -30
- package/src/renderable/trigger.js +16 -17
- package/src/renderable/ui/uibaseelement.js +8 -9
- package/src/renderable/ui/uispriteelement.js +8 -8
- package/src/renderable/ui/uitextbutton.js +15 -15
- package/src/state/stage.js +8 -9
- package/src/state/state.js +17 -17
- package/src/system/device.js +11 -11
- package/src/system/event.js +10 -10
- package/src/system/pooling.js +9 -9
- package/src/system/save.js +2 -2
- package/src/system/timer.js +10 -10
- package/src/text/bitmaptext.js +19 -20
- package/src/text/bitmaptextdata.js +3 -4
- package/src/text/glyph.js +1 -2
- package/src/text/text.js +24 -25
- package/src/text/textmetrics.js +9 -10
- package/src/tweens/tween.js +20 -21
- package/src/utils/agent.js +5 -5
- package/src/utils/array.js +4 -4
- package/src/utils/file.js +2 -2
- package/src/utils/function.js +6 -6
- package/src/utils/string.js +5 -5
- package/src/utils/utils.js +4 -4
- package/src/video/canvas/canvas_renderer.js +72 -73
- package/src/video/renderer.js +27 -28
- package/src/video/texture/atlas.js +22 -22
- package/src/video/texture/canvas_texture.js +9 -9
- package/src/video/video.js +17 -17
- package/src/video/webgl/buffer/vertex.js +1 -2
- package/src/video/webgl/glshader.js +12 -12
- package/src/video/webgl/webgl_compositor.js +42 -43
- package/src/video/webgl/webgl_renderer.js +76 -77
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* melonJS Game Engine - v14.1.1
|
|
3
|
+
* http://www.melonjs.org
|
|
4
|
+
* melonjs is licensed under the MIT License.
|
|
5
|
+
* http://www.opensource.org/licenses/mit-license
|
|
6
|
+
* @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
|
|
7
|
+
*/
|
|
8
|
+
import Color from '../math/color.js';
|
|
9
|
+
import Matrix3d from '../math/matrix3.js';
|
|
10
|
+
import { createCanvas, renderer } from './video.js';
|
|
11
|
+
import { on, emit, GAME_RESET, CANVAS_ONRESIZE } from '../system/event.js';
|
|
12
|
+
import { platform } from '../system/device.js';
|
|
13
|
+
import { setPrefixed } from '../utils/agent.js';
|
|
14
|
+
import Rect from '../geometries/rectangle.js';
|
|
15
|
+
import RoundRect from '../geometries/roundrect.js';
|
|
16
|
+
import Ellipse from '../geometries/ellipse.js';
|
|
17
|
+
import Polygon from '../geometries/poly.js';
|
|
18
|
+
import Line from '../geometries/line.js';
|
|
19
|
+
import Bounds from '../physics/bounds.js';
|
|
20
|
+
import Path2D from '../geometries/path2d.js';
|
|
21
|
+
import Point from '../geometries/point.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @classdesc
|
|
25
|
+
* a base renderer object
|
|
26
|
+
*/
|
|
27
|
+
class Renderer {
|
|
28
|
+
/**
|
|
29
|
+
* @param {object} options - The renderer parameters
|
|
30
|
+
* @param {number} options.width - The width of the canvas without scaling
|
|
31
|
+
* @param {number} options.height - The height of the canvas without scaling
|
|
32
|
+
* @param {HTMLCanvasElement} [options.canvas] - The html canvas to draw to on screen
|
|
33
|
+
* @param {boolean} [options.antiAlias=false] - Whether to enable anti-aliasing, use false (default) for a pixelated effect.
|
|
34
|
+
* @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.
|
|
35
|
+
* @param {boolean} [options.transparent=false] - Whether to enable transparency on the canvas
|
|
36
|
+
* @param {boolean} [options.premultipliedAlpha=true] - in WebGL, whether the renderer will assume that colors have premultiplied alpha when canvas transparency is enabled
|
|
37
|
+
* @param {boolean} [options.blendMode="normal"] - the default blend mode to use ("normal", "multiply")
|
|
38
|
+
* @param {boolean} [options.subPixel=false] - Whether to enable subpixel rendering (performance hit when enabled)
|
|
39
|
+
* @param {boolean} [options.verbose=false] - Enable the verbose mode that provides additional details as to what the renderer is doing
|
|
40
|
+
* @param {number} [options.zoomX=width] - The actual width of the canvas with scaling applied
|
|
41
|
+
* @param {number} [options.zoomY=height] - The actual height of the canvas with scaling applied
|
|
42
|
+
*/
|
|
43
|
+
constructor(options) {
|
|
44
|
+
/**
|
|
45
|
+
* The given constructor options
|
|
46
|
+
* @public
|
|
47
|
+
* @type {object}
|
|
48
|
+
*/
|
|
49
|
+
this.settings = options;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* true if the current rendering context is valid
|
|
53
|
+
* @default true
|
|
54
|
+
* @type {boolean}
|
|
55
|
+
*/
|
|
56
|
+
this.isContextValid = true;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The Path2D instance used by the renderer to draw primitives
|
|
60
|
+
* @type {Path2D}
|
|
61
|
+
*/
|
|
62
|
+
this.path2D = new Path2D();
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @ignore
|
|
66
|
+
*/
|
|
67
|
+
this.currentScissor = new Int32Array([ 0, 0, this.settings.width, this.settings.height ]);
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @ignore
|
|
71
|
+
*/
|
|
72
|
+
this.maskLevel = 0;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @ignore
|
|
76
|
+
*/
|
|
77
|
+
this.currentBlendMode = "none";
|
|
78
|
+
|
|
79
|
+
// create the main screen canvas
|
|
80
|
+
if (platform.ejecta === true) {
|
|
81
|
+
// a main canvas is already automatically created by Ejecta
|
|
82
|
+
this.canvas = document.getElementById("canvas");
|
|
83
|
+
} else if (typeof globalThis.canvas !== "undefined") {
|
|
84
|
+
// a global canvas is available, e.g. webapp adapter for wechat
|
|
85
|
+
this.canvas = globalThis.canvas;
|
|
86
|
+
} else if (typeof this.settings.canvas !== "undefined") {
|
|
87
|
+
this.canvas = this.settings.canvas;
|
|
88
|
+
} else {
|
|
89
|
+
this.canvas = createCanvas(this.settings.width, this.settings.height);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// global color
|
|
93
|
+
this.currentColor = new Color(0, 0, 0, 1.0);
|
|
94
|
+
|
|
95
|
+
// global tint color
|
|
96
|
+
this.currentTint = new Color(255, 255, 255, 1.0);
|
|
97
|
+
|
|
98
|
+
// the projectionMatrix (set through setProjection)
|
|
99
|
+
this.projectionMatrix = new Matrix3d();
|
|
100
|
+
|
|
101
|
+
// default uvOffset
|
|
102
|
+
this.uvOffset = 0;
|
|
103
|
+
|
|
104
|
+
// reset the instantiated renderer on game reset
|
|
105
|
+
on(GAME_RESET, () => {
|
|
106
|
+
renderer.reset();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* prepare the framebuffer for drawing a new frame
|
|
112
|
+
*/
|
|
113
|
+
clear() {}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* render the main framebuffer on screen
|
|
117
|
+
*/
|
|
118
|
+
flush() {}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Reset context state
|
|
122
|
+
*/
|
|
123
|
+
reset() {
|
|
124
|
+
this.resetTransform();
|
|
125
|
+
this.setBlendMode(this.settings.blendMode);
|
|
126
|
+
this.setColor("#000000");
|
|
127
|
+
this.clearTint();
|
|
128
|
+
this.cache.clear();
|
|
129
|
+
this.currentScissor[0] = 0;
|
|
130
|
+
this.currentScissor[1] = 0;
|
|
131
|
+
this.currentScissor[2] = this.getCanvas().width;
|
|
132
|
+
this.currentScissor[3] = this.getCanvas().height;
|
|
133
|
+
this.clearMask();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* return a reference to the canvas which this renderer draws to
|
|
138
|
+
* @returns {HTMLCanvasElement}
|
|
139
|
+
*/
|
|
140
|
+
getCanvas() {
|
|
141
|
+
return this.canvas;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* return a reference to this renderer canvas corresponding Context
|
|
147
|
+
* @returns {CanvasRenderingContext2D|WebGLRenderingContext}
|
|
148
|
+
*/
|
|
149
|
+
getContext() {
|
|
150
|
+
return this.context;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* returns the current blend mode for this renderer
|
|
155
|
+
* @returns {string} blend mode
|
|
156
|
+
*/
|
|
157
|
+
getBlendMode() {
|
|
158
|
+
return this.currentBlendMode;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Returns the 2D Context object of the given Canvas<br>
|
|
163
|
+
* Also configures anti-aliasing and blend modes based on constructor options.
|
|
164
|
+
* @param {HTMLCanvasElement} canvas
|
|
165
|
+
* @param {boolean} [transparent=true] - use false to disable transparency
|
|
166
|
+
* @returns {CanvasRenderingContext2D}
|
|
167
|
+
*/
|
|
168
|
+
getContext2d(canvas, transparent) {
|
|
169
|
+
if (typeof canvas === "undefined" || canvas === null) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
"You must pass a canvas element in order to create " +
|
|
172
|
+
"a 2d context"
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (typeof canvas.getContext === "undefined") {
|
|
177
|
+
throw new Error(
|
|
178
|
+
"Your browser does not support HTML5 canvas."
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (typeof transparent !== "boolean") {
|
|
183
|
+
transparent = true;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
var _context = canvas.getContext("2d", {
|
|
187
|
+
"alpha" : transparent
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
if (!_context.canvas) {
|
|
191
|
+
_context.canvas = canvas;
|
|
192
|
+
}
|
|
193
|
+
this.setAntiAlias(_context, this.settings.antiAlias);
|
|
194
|
+
return _context;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* return the width of the system Canvas
|
|
199
|
+
* @returns {number}
|
|
200
|
+
*/
|
|
201
|
+
getWidth() {
|
|
202
|
+
return this.getCanvas().width;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* return the height of the system Canvas
|
|
207
|
+
* @returns {number} height of the system Canvas
|
|
208
|
+
*/
|
|
209
|
+
getHeight() {
|
|
210
|
+
return this.getCanvas().height;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* get the current fill & stroke style color.
|
|
215
|
+
* @returns {Color} current global color
|
|
216
|
+
*/
|
|
217
|
+
getColor() {
|
|
218
|
+
return this.currentColor;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* return the current global alpha
|
|
223
|
+
* @returns {number}
|
|
224
|
+
*/
|
|
225
|
+
globalAlpha() {
|
|
226
|
+
return this.currentColor.glArray[3];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* check if the given rect or bounds overlaps with the renderer screen coordinates
|
|
231
|
+
* @param {Rect|Bounds} bounds
|
|
232
|
+
* @returns {boolean} true if overlaps
|
|
233
|
+
*/
|
|
234
|
+
overlaps(bounds) {
|
|
235
|
+
return (
|
|
236
|
+
bounds.left <= this.getWidth() && bounds.right >= 0 &&
|
|
237
|
+
bounds.top <= this.getHeight() && bounds.bottom >= 0
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* resizes the system canvas
|
|
244
|
+
* @param {number} width - new width of the canvas
|
|
245
|
+
* @param {number} height - new height of the canvas
|
|
246
|
+
*/
|
|
247
|
+
resize(width, height) {
|
|
248
|
+
var canvas = this.getCanvas();
|
|
249
|
+
if (width !== canvas.width || height !== canvas.height) {
|
|
250
|
+
canvas.width = width;
|
|
251
|
+
canvas.height = height;
|
|
252
|
+
this.currentScissor[0] = 0;
|
|
253
|
+
this.currentScissor[1] = 0;
|
|
254
|
+
this.currentScissor[2] = width;
|
|
255
|
+
this.currentScissor[3] = height;
|
|
256
|
+
// publish the corresponding event
|
|
257
|
+
emit(CANVAS_ONRESIZE, width, height);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* enable/disable image smoothing (scaling interpolation) for the given context
|
|
263
|
+
* @param {CanvasRenderingContext2D} context
|
|
264
|
+
* @param {boolean} [enable=false]
|
|
265
|
+
*/
|
|
266
|
+
setAntiAlias(context, enable) {
|
|
267
|
+
var canvas = context.canvas;
|
|
268
|
+
|
|
269
|
+
// enable/disable antialis on the given Context2d object
|
|
270
|
+
setPrefixed("imageSmoothingEnabled", enable === true, context);
|
|
271
|
+
|
|
272
|
+
// set antialias CSS property on the main canvas
|
|
273
|
+
if (enable !== true) {
|
|
274
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering
|
|
275
|
+
canvas.style["image-rendering"] = "optimizeSpeed"; // legal fallback
|
|
276
|
+
canvas.style["image-rendering"] = "-moz-crisp-edges"; // Firefox
|
|
277
|
+
canvas.style["image-rendering"] = "-o-crisp-edges"; // Opera
|
|
278
|
+
canvas.style["image-rendering"] = "-webkit-optimize-contrast"; // Safari
|
|
279
|
+
canvas.style["image-rendering"] = "optimize-contrast"; // CSS 3
|
|
280
|
+
canvas.style["image-rendering"] = "crisp-edges"; // CSS 4
|
|
281
|
+
canvas.style["image-rendering"] = "pixelated"; // CSS 4
|
|
282
|
+
canvas.style.msInterpolationMode = "nearest-neighbor"; // IE8+
|
|
283
|
+
} else {
|
|
284
|
+
canvas.style["image-rendering"] = "auto";
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* set/change the current projection matrix (WebGL only)
|
|
290
|
+
* @param {Matrix3d} matrix
|
|
291
|
+
*/
|
|
292
|
+
setProjection(matrix) {
|
|
293
|
+
this.projectionMatrix.copy(matrix);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* stroke the given shape
|
|
298
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} shape - a shape object to stroke
|
|
299
|
+
* @param {boolean} [fill=false] - fill the shape with the current color if true
|
|
300
|
+
*/
|
|
301
|
+
stroke(shape, fill) {
|
|
302
|
+
if (shape instanceof RoundRect) {
|
|
303
|
+
this.strokeRoundRect(shape.left, shape.top, shape.width, shape.height, shape.radius, fill);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (shape instanceof Rect || shape instanceof Bounds) {
|
|
307
|
+
this.strokeRect(shape.left, shape.top, shape.width, shape.height, fill);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (shape instanceof Line || shape instanceof Polygon) {
|
|
311
|
+
this.strokePolygon(shape, fill);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (shape instanceof Ellipse) {
|
|
315
|
+
this.strokeEllipse(
|
|
316
|
+
shape.pos.x,
|
|
317
|
+
shape.pos.y,
|
|
318
|
+
shape.radiusV.x,
|
|
319
|
+
shape.radiusV.y,
|
|
320
|
+
fill
|
|
321
|
+
);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (shape instanceof Point) {
|
|
325
|
+
this.strokePoint(shape.x, shape.y);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
throw new Error("Invalid geometry for fill/stroke");
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* fill the given shape
|
|
333
|
+
* @name fill
|
|
334
|
+
* @memberof Renderer
|
|
335
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} shape - a shape object to fill
|
|
336
|
+
*/
|
|
337
|
+
fill(shape) {
|
|
338
|
+
this.stroke(shape, true);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* tint the given image or canvas using the given color
|
|
343
|
+
* @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} src - the source image to be tinted
|
|
344
|
+
* @param {Color|string} color - the color that will be used to tint the image
|
|
345
|
+
* @param {string} [mode="multiply"] - the composition mode used to tint the image
|
|
346
|
+
* @returns {HTMLCanvasElement|OffscreenCanvas} a new canvas element representing the tinted image
|
|
347
|
+
*/
|
|
348
|
+
tint(src, color, mode) {
|
|
349
|
+
var canvas = createCanvas(src.width, src.height, true);
|
|
350
|
+
var context = this.getContext2d(canvas);
|
|
351
|
+
|
|
352
|
+
context.save();
|
|
353
|
+
|
|
354
|
+
context.fillStyle = color instanceof Color ? color.toRGB() : color;
|
|
355
|
+
context.fillRect(0, 0, src.width, src.height);
|
|
356
|
+
|
|
357
|
+
context.globalCompositeOperation = mode || "multiply";
|
|
358
|
+
context.drawImage(src, 0, 0);
|
|
359
|
+
context.globalCompositeOperation = "destination-atop";
|
|
360
|
+
context.drawImage(src, 0, 0);
|
|
361
|
+
|
|
362
|
+
context.restore();
|
|
363
|
+
|
|
364
|
+
return canvas;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* A mask limits rendering elements to the shape and position of the given mask object.
|
|
369
|
+
* So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
|
|
370
|
+
* Mask are not preserved through renderer context save and restore.
|
|
371
|
+
* @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] - the shape defining the mask to be applied
|
|
372
|
+
* @param {boolean} [invert=false] - either the given shape should define what is visible (default) or the opposite
|
|
373
|
+
*/
|
|
374
|
+
// eslint-disable-next-line no-unused-vars
|
|
375
|
+
setMask(mask) {}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* disable (remove) the rendering mask set through setMask.
|
|
379
|
+
* @see Renderer#setMask
|
|
380
|
+
*/
|
|
381
|
+
clearMask() {}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* set a coloring tint for sprite based renderables
|
|
385
|
+
* @param {Color} tint - the tint color
|
|
386
|
+
* @param {number} [alpha] - an alpha value to be applied to the tint
|
|
387
|
+
*/
|
|
388
|
+
setTint(tint, alpha = tint.alpha) {
|
|
389
|
+
// global tint color
|
|
390
|
+
this.currentTint.copy(tint);
|
|
391
|
+
this.currentTint.alpha *= alpha;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* clear the rendering tint set through setTint.
|
|
396
|
+
* @see Renderer#setTint
|
|
397
|
+
*/
|
|
398
|
+
clearTint() {
|
|
399
|
+
// reset to default
|
|
400
|
+
this.currentTint.setColor(255, 255, 255, 1.0);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* @ignore
|
|
405
|
+
*/
|
|
406
|
+
drawFont(/*bounds*/) {}
|
|
407
|
+
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export { Renderer as default };
|