From 5dd4cbccb45a8c7c51178fe302afec6d08c8f859 Mon Sep 17 00:00:00 2001 From: Olivier Biot Date: Tue, 3 Jan 2023 13:22:45 +0800 Subject: [PATCH] [#1091] create new global constants for Application and later other classes --- src/application/application.js | 25 ++++++------------------- src/const.js | 23 +++++++++++++++++++++++ src/index.js | 2 ++ src/video/video.js | 25 ++++++++++++++----------- 4 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 src/const.js diff --git a/src/application/application.js b/src/application/application.js index 340fb913d4..989cafd526 100644 --- a/src/application/application.js +++ b/src/application/application.js @@ -9,24 +9,7 @@ import World from "./../physics/world.js"; import { onresize } from "./resize.js"; import { defaultSettings } from "./settings.js"; import { consoleHeader } from "./header.js"; - -/** - * Select the HTML5 Canvas renderer - * @constant - */ -const CANVAS = 0; - -/** - * Select the WebGL renderer - * @constant - */ -const WEBGL = 1; - -/** - * Auto-select the renderer (Attempt WebGL first, with fallback to Canvas) - * @constant - */ -const AUTO = 2; +import { CANVAS, WEBGL, AUTO } from "../const.js"; /** * @classdesc @@ -40,7 +23,7 @@ const AUTO = 2; * @param {number} height - The height of the canvas viewport * @param {object} [options] - The optional video/renderer parameters.
(see Renderer(s) documentation for further specific options) * @param {string|HTMLElement} [options.parent=document.body] - the DOM parent element to hold the canvas in the HTML file - * @param {number|Renderer} [options.renderer=video.AUTO] - renderer to use (me.video.CANVAS, me.video.WEBGL, me.video.AUTO), or a custom renderer class + * @param {number|Renderer} [options.renderer=AUTO] - renderer to use (CANVAS, WEBGL, AUTO), or a custom renderer class * @param {number|string} [options.scale=1.0] - enable scaling of the canvas ('auto' for automatic scaling) * @param {string} [options.scaleMethod="fit"] - screen scaling modes ('fit','fill-min','fill-max','flex','flex-width','flex-height','stretch') * @param {boolean} [options.preferWebGL1=false] - if true the renderer will only use WebGL 1 @@ -49,6 +32,10 @@ const AUTO = 2; * @param {boolean} [options.antiAlias=false] - whether to enable or not video scaling interpolation * @param {boolean} [options.consoleHeader=true] - whether to display melonJS version and basic device information in the console * @throws Will throw an exception if it fails to instantiate a renderer + * @example + * var my game = new Application(640, 480, {renderer: me.video.AUTO}) { + * .... + * } */ constructor(width, height, options) { diff --git a/src/const.js b/src/const.js new file mode 100644 index 0000000000..f065a249db --- /dev/null +++ b/src/const.js @@ -0,0 +1,23 @@ +/** + * constant to select the HTML5 Canvas renderer + * @type {number} + * @static + * @see Application + */ +export const CANVAS = 0; + +/** + * constant to select select the WebGL renderer + * @type {number} + * @static + * @see Application + */ +export const WEBGL = 1; + +/** + * constant to auto-select the renderer (Attempt WebGL first, with fallback to Canvas) + * @static + * @type (number) + * @see Application + */ +export const AUTO = 2; diff --git a/src/index.js b/src/index.js index fca02b5b8c..a5b8f6b564 100644 --- a/src/index.js +++ b/src/index.js @@ -175,6 +175,8 @@ export { // Backward compatibility for deprecated method or properties export * from "./lang/deprecated.js"; +// export all public constants +export * from "./const.js"; /** * a flag indicating that melonJS is fully initialized diff --git a/src/video/video.js b/src/video/video.js index f3a10e7ff6..c10c708bc9 100644 --- a/src/video/video.js +++ b/src/video/video.js @@ -2,37 +2,39 @@ import * as event from "./../system/event.js"; import { initialized, game } from "./../index.js"; import * as device from "./../system/device.js"; import utils from "./../utils/utils.js"; +import * as vc from "../const"; /** * @namespace video */ + /** * Select the HTML5 Canvas renderer * @memberof video - * @constant + * @static */ -export const CANVAS = 0; +export const CANVAS = vc.CANVAS; /** * Select the WebGL renderer * @memberof video - * @constant + * @static */ -export const WEBGL = 1; +export const WEBGL = vc.WEBGL; /** * Auto-select the renderer (Attempt WebGL first, with fallback to Canvas) * @memberof video - * @constant + * @static */ -export const AUTO = 2; +export const AUTO = vc.AUTO; - /** - * A reference to the active Canvas or WebGL active renderer renderer - * @memberof video - * @type {CanvasRenderer|WebGLRenderer} - */ +/** + * A reference to the active Canvas or WebGL active renderer renderer + * @memberof video + * @type {CanvasRenderer|WebGLRenderer} + */ export let renderer = null; /** @@ -172,6 +174,7 @@ export function createCanvas(width, height, returnOffscreenCanvas = false) { /** * return a reference to the parent DOM element holding the main canvas + * @memberof video * @returns {HTMLElement} */ export function getParent() {