melonjs 13.1.1 → 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.
@@ -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
@@ -24,7 +25,8 @@ class Renderer {
24
25
  * @param {HTMLCanvasElement} [options.canvas] The html canvas to draw to on screen
25
26
  * @param {boolean} [options.antiAlias=false] Whether to enable anti-aliasing, use false (default) for a pixelated effect.
26
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.
27
- * @param {boolean} [options.transparent=false] Whether to enable transparency on the canvas (performance hit when enabled)
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
28
30
  * @param {boolean} [options.blendMode="normal"] the default blend mode to use ("normal", "multiply")
29
31
  * @param {boolean} [options.subPixel=false] Whether to enable subpixel rendering (performance hit when enabled)
30
32
  * @param {boolean} [options.verbose=false] Enable the verbose mode that provides additional details as to what the renderer is doing
@@ -35,26 +37,20 @@ class Renderer {
35
37
  /**
36
38
  * The given constructor options
37
39
  * @public
38
- * @name settings
39
- * @memberof Renderer#
40
40
  * @type {object}
41
41
  */
42
42
  this.settings = options;
43
43
 
44
44
  /**
45
45
  * true if the current rendering context is valid
46
- * @name isContextValid
47
- * @memberof Renderer#
48
46
  * @default true
49
- * type {boolean}
47
+ * @type {boolean}
50
48
  */
51
49
  this.isContextValid = true;
52
50
 
53
51
  /**
54
52
  * The Path2D instance used by the renderer to draw primitives
55
- * @name path2D
56
53
  * @type {Path2D}
57
- * @memberof Renderer#
58
54
  */
59
55
  this.path2D = new Path2D();
60
56
 
@@ -106,22 +102,16 @@ class Renderer {
106
102
 
107
103
  /**
108
104
  * prepare the framebuffer for drawing a new frame
109
- * @name clear
110
- * @memberof Renderer
111
105
  */
112
106
  clear() {}
113
107
 
114
108
  /**
115
109
  * render the main framebuffer on screen
116
- * @name flush
117
- * @memberof Renderer
118
110
  */
119
111
  flush() {}
120
112
 
121
113
  /**
122
114
  * Reset context state
123
- * @name reset
124
- * @memberof Renderer
125
115
  */
126
116
  reset() {
127
117
  this.resetTransform();
@@ -138,8 +128,6 @@ class Renderer {
138
128
 
139
129
  /**
140
130
  * return a reference to the canvas which this renderer draws to
141
- * @name getCanvas
142
- * @memberof Renderer
143
131
  * @returns {HTMLCanvasElement}
144
132
  */
145
133
  getCanvas() {
@@ -149,8 +137,6 @@ class Renderer {
149
137
 
150
138
  /**
151
139
  * return a reference to this renderer canvas corresponding Context
152
- * @name getContext
153
- * @memberof Renderer
154
140
  * @returns {CanvasRenderingContext2D|WebGLRenderingContext}
155
141
  */
156
142
  getContext() {
@@ -159,8 +145,6 @@ class Renderer {
159
145
 
160
146
  /**
161
147
  * returns the current blend mode for this renderer
162
- * @name getBlendMode
163
- * @memberof Renderer
164
148
  * @returns {string} blend mode
165
149
  */
166
150
  getBlendMode() {
@@ -170,8 +154,6 @@ class Renderer {
170
154
  /**
171
155
  * Returns the 2D Context object of the given Canvas<br>
172
156
  * Also configures anti-aliasing and blend modes based on constructor options.
173
- * @name getContext2d
174
- * @memberof Renderer
175
157
  * @param {HTMLCanvasElement} canvas
176
158
  * @param {boolean} [transparent=true] use false to disable transparency
177
159
  * @returns {CanvasRenderingContext2D}
@@ -207,8 +189,6 @@ class Renderer {
207
189
 
208
190
  /**
209
191
  * return the width of the system Canvas
210
- * @name getWidth
211
- * @memberof Renderer
212
192
  * @returns {number}
213
193
  */
214
194
  getWidth() {
@@ -217,8 +197,6 @@ class Renderer {
217
197
 
218
198
  /**
219
199
  * return the height of the system Canvas
220
- * @name getHeight
221
- * @memberof Renderer
222
200
  * @returns {number} height of the system Canvas
223
201
  */
224
202
  getHeight() {
@@ -227,8 +205,6 @@ class Renderer {
227
205
 
228
206
  /**
229
207
  * get the current fill & stroke style color.
230
- * @name getColor
231
- * @memberof Renderer
232
208
  * @returns {Color} current global color
233
209
  */
234
210
  getColor() {
@@ -237,8 +213,6 @@ class Renderer {
237
213
 
238
214
  /**
239
215
  * return the current global alpha
240
- * @name globalAlpha
241
- * @memberof Renderer
242
216
  * @returns {number}
243
217
  */
244
218
  globalAlpha() {
@@ -247,8 +221,6 @@ class Renderer {
247
221
 
248
222
  /**
249
223
  * check if the given rect or bounds overlaps with the renderer screen coordinates
250
- * @name overlaps
251
- * @memberof Renderer
252
224
  * @param {Rect|Bounds} bounds
253
225
  * @returns {boolean} true if overlaps
254
226
  */
@@ -262,8 +234,6 @@ class Renderer {
262
234
 
263
235
  /**
264
236
  * resizes the system canvas
265
- * @name resize
266
- * @memberof Renderer
267
237
  * @param {number} width new width of the canvas
268
238
  * @param {number} height new height of the canvas
269
239
  */
@@ -283,8 +253,6 @@ class Renderer {
283
253
 
284
254
  /**
285
255
  * enable/disable image smoothing (scaling interpolation) for the given context
286
- * @name setAntiAlias
287
- * @memberof Renderer
288
256
  * @param {CanvasRenderingContext2D} context
289
257
  * @param {boolean} [enable=false]
290
258
  */
@@ -312,8 +280,6 @@ class Renderer {
312
280
 
313
281
  /**
314
282
  * set/change the current projection matrix (WebGL only)
315
- * @name setProjection
316
- * @memberof Renderer
317
283
  * @param {Matrix3d} matrix
318
284
  */
319
285
  setProjection(matrix) {
@@ -322,8 +288,6 @@ class Renderer {
322
288
 
323
289
  /**
324
290
  * stroke the given shape
325
- * @name stroke
326
- * @memberof Renderer
327
291
  * @param {Rect|RoundRect|Polygon|Line|Ellipse} shape a shape object to stroke
328
292
  * @param {boolean} [fill=false] fill the shape with the current color if true
329
293
  */
@@ -350,6 +314,10 @@ class Renderer {
350
314
  );
351
315
  return;
352
316
  }
317
+ if (shape instanceof Point) {
318
+ this.strokePoint(shape.x, shape.y);
319
+ return;
320
+ }
353
321
  throw new Error("Invalid geometry for fill/stroke");
354
322
  }
355
323
 
@@ -365,8 +333,6 @@ class Renderer {
365
333
 
366
334
  /**
367
335
  * tint the given image or canvas using the given color
368
- * @name tint
369
- * @memberof Renderer
370
336
  * @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas} src the source image to be tinted
371
337
  * @param {Color|string} color the color that will be used to tint the image
372
338
  * @param {string} [mode="multiply"] the composition mode used to tint the image
@@ -395,8 +361,6 @@ class Renderer {
395
361
  * A mask limits rendering elements to the shape and position of the given mask object.
396
362
  * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
397
363
  * Mask are not preserved through renderer context save and restore.
398
- * @name setMask
399
- * @memberof Renderer
400
364
  * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
401
365
  * @param {boolean} [invert=false] either the given shape should define what is visible (default) or the opposite
402
366
  */
@@ -405,16 +369,12 @@ class Renderer {
405
369
 
406
370
  /**
407
371
  * disable (remove) the rendering mask set through setMask.
408
- * @name clearMask
409
372
  * @see Renderer#setMask
410
- * @memberof Renderer
411
373
  */
412
374
  clearMask() {}
413
375
 
414
376
  /**
415
377
  * set a coloring tint for sprite based renderables
416
- * @name setTint
417
- * @memberof Renderer
418
378
  * @param {Color} tint the tint color
419
379
  * @param {number} [alpha] an alpha value to be applied to the tint
420
380
  */
@@ -426,9 +386,7 @@ class Renderer {
426
386
 
427
387
  /**
428
388
  * clear the rendering tint set through setTint.
429
- * @name clearTint
430
389
  * @see Renderer#setTint
431
- * @memberof Renderer
432
390
  */
433
391
  clearTint() {
434
392
  // reset to default
@@ -24,6 +24,7 @@ var settings = {
24
24
  scale : 1.0,
25
25
  scaleMethod : "manual",
26
26
  transparent : false,
27
+ premultipliedAlpha: true,
27
28
  blendMode : "normal",
28
29
  antiAlias : false,
29
30
  failIfMajorPerformanceCaveat : true,
@@ -42,56 +42,38 @@ class GLShader {
42
42
 
43
43
  /**
44
44
  * the active gl rendering context
45
- * @public
46
45
  * @type {WebGLRenderingContext}
47
- * @name gl
48
- * @memberof GLShader
49
46
  */
50
47
  this.gl = gl;
51
48
 
52
49
  /**
53
50
  * the vertex shader source code
54
- * @public
55
51
  * @type {string}
56
- * @name vertex
57
- * @memberof GLShader
58
52
  */
59
53
  this.vertex = setPrecision(minify(vertex), precision || device.getMaxShaderPrecision(this.gl));
60
54
 
61
55
  /**
62
56
  * the fragment shader source code
63
- * @public
64
57
  * @type {string}
65
- * @name vertex
66
- * @memberof GLShader
67
58
  */
68
59
  this.fragment = setPrecision(minify(fragment), precision || device.getMaxShaderPrecision(this.gl));
69
60
 
70
61
  /**
71
62
  * the location attributes of the shader
72
- * @public
73
63
  * @type {GLint[]}
74
- * @name attributes
75
- * @memberof GLShader
76
64
  */
77
65
  this.attributes = extractAttributes(this.gl, this);
78
66
 
79
67
 
80
68
  /**
81
69
  * a reference to the shader program (once compiled)
82
- * @public
83
70
  * @type {WebGLProgram}
84
- * @name program
85
- * @memberof GLShader
86
71
  */
87
72
  this.program = compileProgram(this.gl, this.vertex, this.fragment, this.attributes);
88
73
 
89
74
  /**
90
75
  * the uniforms of the shader
91
- * @public
92
76
  * @type {object}
93
- * @name uniforms
94
- * @memberof GLShader
95
77
  */
96
78
  this.uniforms = extractUniforms(this.gl, this);
97
79
 
@@ -101,8 +83,6 @@ class GLShader {
101
83
 
102
84
  /**
103
85
  * Installs this shader program as part of current rendering state
104
- * @name bind
105
- * @memberof GLShader
106
86
  */
107
87
  bind() {
108
88
  this.gl.useProgram(this.program);
@@ -110,8 +90,6 @@ class GLShader {
110
90
 
111
91
  /**
112
92
  * returns the location of an attribute variable in this shader program
113
- * @name getAttribLocation
114
- * @memberof GLShader
115
93
  * @param {string} name the name of the attribute variable whose location to get.
116
94
  * @returns {GLint} number indicating the location of the variable name if found. Returns -1 otherwise
117
95
  */
@@ -126,8 +104,6 @@ class GLShader {
126
104
 
127
105
  /**
128
106
  * Set the uniform to the given value
129
- * @name setUniform
130
- * @memberof GLShader
131
107
  * @param {string} name the uniform name
132
108
  * @param {object|Float32Array} value the value to assign to that uniform
133
109
  * @example
@@ -148,8 +124,6 @@ class GLShader {
148
124
 
149
125
  /**
150
126
  * activate the given vertex attribute for this shader
151
- * @name setVertexAttributes
152
- * @memberof GLShader
153
127
  * @param {WebGLRenderingContext} gl the current WebGL rendering context
154
128
  * @param {object[]} attributes an array of vertex attributes
155
129
  * @param {number} vertexByteSize the size of a single vertex in bytes
@@ -171,8 +145,6 @@ class GLShader {
171
145
 
172
146
  /**
173
147
  * destroy this shader objects resources (program, attributes, uniforms)
174
- * @name destroy
175
- * @memberof GLShader
176
148
  */
177
149
  destroy() {
178
150
  this.uniforms = null;
@@ -56,44 +56,37 @@ class WebGLCompositor {
56
56
 
57
57
  /**
58
58
  * a reference to the active WebGL shader
59
- * @name activeShader
60
- * @memberof WebGLCompositor
61
59
  * @type {GLShader}
62
60
  */
63
61
  this.activeShader = null;
64
62
 
65
63
  /**
66
64
  * primitive type to render (gl.POINTS, gl.LINE_STRIP, gl.LINE_LOOP, gl.LINES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.TRIANGLES)
67
- * @name mode
68
- * @see WebGLCompositor
69
- * @memberof WebGLCompositor
65
+ * @type {number}
70
66
  * @default gl.TRIANGLES
71
67
  */
72
68
  this.mode = gl.TRIANGLES;
73
69
 
74
70
  /**
75
71
  * an array of vertex attribute properties
76
- * @name attributes
77
72
  * @see WebGLCompositor.addAttribute
78
- * @memberof WebGLCompositor
73
+ * @type {Array}
79
74
  */
80
75
  this.attributes = [];
81
76
 
82
77
  /**
83
78
  * the size of a single vertex in bytes
84
79
  * (will automatically be calculated as attributes definitions are added)
85
- * @name vertexByteSize
86
80
  * @see WebGLCompositor.addAttribute
87
- * @memberof WebGLCompositor
81
+ * @type {number}
88
82
  */
89
83
  this.vertexByteSize = 0;
90
84
 
91
85
  /**
92
86
  * the size of a single vertex in floats
93
87
  * (will automatically be calculated as attributes definitions are added)
94
- * @name vertexSize
95
88
  * @see WebGLCompositor.addAttribute
96
- * @memberof WebGLCompositor
89
+ * @type {number}
97
90
  */
98
91
  this.vertexSize = 0;
99
92
 
@@ -154,8 +147,6 @@ class WebGLCompositor {
154
147
 
155
148
  /**
156
149
  * add vertex attribute property definition to the compositor
157
- * @name addAttribute
158
- * @memberof WebGLCompositor
159
150
  * @param {string} name name of the attribute in the vertex shader
160
151
  * @param {number} size number of components per vertex attribute. Must be 1, 2, 3, or 4.
161
152
  * @param {GLenum} type data type of each component in the array
@@ -201,8 +192,6 @@ class WebGLCompositor {
201
192
 
202
193
  /**
203
194
  * Sets the viewport
204
- * @name setViewport
205
- * @memberof WebGLCompositor
206
195
  * @param {number} x x position of viewport
207
196
  * @param {number} y y position of viewport
208
197
  * @param {number} w width of viewport
@@ -214,8 +203,6 @@ class WebGLCompositor {
214
203
 
215
204
  /**
216
205
  * Create a WebGL texture from an image
217
- * @name createTexture2D
218
- * @memberof WebGLCompositor
219
206
  * @param {number} unit Destination texture unit
220
207
  * @param {Image|HTMLCanvasElement|ImageData|Uint8Array[]|Float32Array[]} image Source image
221
208
  * @param {number} filter gl.LINEAR or gl.NEAREST
@@ -258,8 +245,6 @@ class WebGLCompositor {
258
245
 
259
246
  /**
260
247
  * delete the given WebGL texture
261
- * @name bindTexture2D
262
- * @memberof WebGLCompositor
263
248
  * @param {WebGLTexture} [texture] a WebGL texture to delete
264
249
  * @param {number} [unit] Texture unit to delete
265
250
  */
@@ -270,8 +255,6 @@ class WebGLCompositor {
270
255
 
271
256
  /**
272
257
  * returns the WebGL texture associated to the given texture unit
273
- * @name bindTexture2D
274
- * @memberof WebGLCompositor
275
258
  * @param {number} unit Texture unit to which a texture is bound
276
259
  * @returns {WebGLTexture} texture a WebGL texture
277
260
  */
@@ -281,8 +264,6 @@ class WebGLCompositor {
281
264
 
282
265
  /**
283
266
  * assign the given WebGL texture to the current batch
284
- * @name bindTexture2D
285
- * @memberof WebGLCompositor
286
267
  * @param {WebGLTexture} texture a WebGL texture
287
268
  * @param {number} unit Texture unit to which the given texture is bound
288
269
  */
@@ -308,8 +289,6 @@ class WebGLCompositor {
308
289
 
309
290
  /**
310
291
  * unbind the given WebGL texture, forcing it to be reuploaded
311
- * @name unbindTexture2D
312
- * @memberof WebGLCompositor
313
292
  * @param {WebGLTexture} [texture] a WebGL texture
314
293
  * @param {number} [unit] a WebGL texture
315
294
  * @returns {number} unit the unit number that was associated with the given texture
@@ -354,8 +333,6 @@ class WebGLCompositor {
354
333
 
355
334
  /**
356
335
  * set/change the current projection matrix
357
- * @name setProjection
358
- * @memberof WebGLCompositor
359
336
  * @param {Matrix3d} matrix
360
337
  */
361
338
  setProjection(matrix) {
@@ -364,9 +341,7 @@ class WebGLCompositor {
364
341
 
365
342
  /**
366
343
  * Select the shader to use for compositing
367
- * @name useShader
368
344
  * @see GLShader
369
- * @memberof WebGLCompositor
370
345
  * @param {GLShader} shader a reference to a GLShader instance
371
346
  */
372
347
  useShader(shader) {
@@ -381,8 +356,6 @@ class WebGLCompositor {
381
356
 
382
357
  /**
383
358
  * Add a textured quad
384
- * @name addQuad
385
- * @memberof WebGLCompositor
386
359
  * @param {TextureAtlas} texture Source texture atlas
387
360
  * @param {number} x Destination x-coordinate
388
361
  * @param {number} y Destination y-coordinate
@@ -438,7 +411,6 @@ class WebGLCompositor {
438
411
  /**
439
412
  * Flush batched texture operations to the GPU
440
413
  * @param {number} [mode=gl.TRIANGLES] the GL drawing mode
441
- * @memberof WebGLCompositor
442
414
  */
443
415
  flush(mode = this.mode) {
444
416
  var vertex = this.vertexBuffer;
@@ -464,8 +436,6 @@ class WebGLCompositor {
464
436
 
465
437
  /**
466
438
  * Draw an array of vertices
467
- * @name drawVertices
468
- * @memberof WebGLCompositor
469
439
  * @param {GLenum} mode primitive type to render (gl.POINTS, gl.LINE_STRIP, gl.LINE_LOOP, gl.LINES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.TRIANGLES)
470
440
  * @param {Vector2d[]} verts vertices
471
441
  * @param {number} [vertexCount=verts.length] amount of points defined in the points array
@@ -492,25 +462,26 @@ class WebGLCompositor {
492
462
  }
493
463
 
494
464
  /**
495
- * Specify the color values used when clearing color buffers. The values are clamped between 0 and 1.
496
- * @name clearColor
497
- * @memberof WebGLCompositor
498
- * @param {number} [r=0] - the red color value used when the color buffers are cleared
499
- * @param {number} [g=0] - the green color value used when the color buffers are cleared
500
- * @param {number} [b=0] - the blue color value used when the color buffers are cleared
501
- * @param {number} [a=0] - the alpha color value used when the color buffers are cleared
465
+ * Clear the frame buffer
466
+ * @param {number} [alpha = 0.0] - the alpha value used when clearing the framebuffer
502
467
  */
503
- clearColor(r, g, b, a) {
504
- this.gl.clearColor(r, g, b, a);
468
+ clear(alpha = 0) {
469
+ var gl = this.gl;
470
+ gl.clearColor(0, 0, 0, alpha);
471
+ gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
505
472
  }
506
473
 
507
474
  /**
508
- * Clear the frame buffer
509
- * @name clear
510
- * @memberof WebGLCompositor
475
+ * Specify the color values used when clearing color buffers. The values are clamped between 0 and 1.
476
+ * @param {number} [r = 0] - the red color value used when the color buffers are cleared
477
+ * @param {number} [g = 0] - the green color value used when the color buffers are cleared
478
+ * @param {number} [b = 0] - the blue color value used when the color buffers are cleared
479
+ * @param {number} [a = 0] - the alpha color value used when the color buffers are cleared
511
480
  */
512
- clear() {
513
- this.gl.clear(this.gl.COLOR_BUFFER_BIT);
481
+ clearColor(r = 0, g = 0, b = 0, a = 0) {
482
+ var gl = this.gl;
483
+ gl.clearColor(r, g, b, a);
484
+ gl.clear(gl.COLOR_BUFFER_BIT);
514
485
  }
515
486
  };
516
487