- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
import * as event from "./../system/event.js";
import { initialized, game } from "./../index.js";
import * as device from "./../system/device.js";
import { throttle } from "../utils/function.js";
/**
* @namespace video
*/
/**
* Select the HTML5 Canvas renderer
* @memberof video
* @name CANVAS
* @static
*/
export { CANVAS } from "../const";
/**
* Select the WebGL renderer
* @memberof video
* @name WEBGL
* @static
*/
export { WEBGL } from "../const";
/**
* Auto-select the renderer (Attempt WebGL first, with fallback to Canvas)
* @memberof video
* @name AUTO
* @static
*/
export { AUTO } from "../const";
/**
* A reference to the active Canvas or WebGL active renderer renderer
* @memberof video
* @type {CanvasRenderer|WebGLRenderer}
*/
export let renderer = null;
/**
* Initialize the "video" system (create a canvas based on the given arguments, and the related renderer). <br>
* @memberof video
* @param {number} width - The width of the canvas viewport
* @param {number} height - The height of the canvas viewport
* @param {Application.Settings} [options] - optional parameters for the renderer
* @returns {boolean} false if initialization failed (canvas not supported)
* @example
* // init the video with a 640x480 canvas
* me.video.init(640, 480, {
* parent : "screen",
* renderer : me.video.AUTO,
* scale : "auto",
* scaleMethod : "fit"
* });
*/
export function init(width, height, options) {
// ensure melonjs has been properly initialized
if (!initialized) {
throw new Error("me.video.init() called before engine initialization.");
}
try {
// initialize the default game Application with the given options
game.init(width, height, options);
} catch (e) {
console.log(e.message);
// me.video.init() historically returns false if failing at creating/using a HTML5 canvas
return false;
}
// assign the default renderer
renderer = game.renderer;
//add a channel for the onresize/onorientationchange event
globalThis.addEventListener(
"resize",
throttle(
(e) => {
event.emit(event.WINDOW_ONRESIZE, e);
}, 100
), false
);
// Screen Orientation API
globalThis.addEventListener(
"orientationchange",
(e) => {
event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
},
false
);
// pre-fixed implementation on mozzila
globalThis.addEventListener(
"onmozorientationchange",
(e) => {
event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
},
false
);
if (device.screenOrientation === true) {
globalThis.screen.orientation.onchange = function (e) {
event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
};
}
// Automatically update relative canvas position on scroll
globalThis.addEventListener("scroll", throttle((e) => {
event.emit(event.WINDOW_ONSCROLL, e);
}, 100), false);
// notify the video has been initialized
event.emit(event.VIDEO_INIT, game.renderer);
return true;
}
/**
* Create and return a new Canvas element
* @memberof video
* @param {number} width - width
* @param {number} height - height
* @param {boolean} [returnOffscreenCanvas=false] - will return an OffscreenCanvas if supported
* @returns {HTMLCanvasElement|OffscreenCanvas} a new Canvas element of the given size
*/
export function createCanvas(width, height, returnOffscreenCanvas = false) {
let _canvas;
if (width === 0 || height === 0) {
throw new Error("width or height was zero, Canvas could not be initialized !");
}
if (device.offscreenCanvas === true && returnOffscreenCanvas === true) {
_canvas = new globalThis.OffscreenCanvas(0, 0);
// stubbing style for compatibility,
// as OffscreenCanvas is detached from the DOM
if (typeof _canvas.style === "undefined") {
_canvas.style = {};
}
} else {
// "else" create a "standard" canvas
_canvas = globalThis.document.createElement("canvas");
}
_canvas.width = width;
_canvas.height = height;
return _canvas;
}
/**
* return a reference to the parent DOM element holding the main canvas
* @memberof video
* @returns {HTMLElement} the HTML parent element
*/
export function getParent() {
return game.getParentElement();
}