]>
git.r.bdr.sh - rbdr/sumo/blob - lib/sumo.js
da72991982c6ea6831699ac23d3eb005c686d8f4
1 import Serpentity
from '@serpentity/serpentity';
3 /* global window document */
6 kNoElementError: 'No element found. Cannot render.',
8 // Handler for the window load event. Initializes and runs the app.
12 const sumo
= new internals
.Sumo({
13 element: document
.getElementById('sumo-app-entry-point')
18 internals
.exports
.sumo
= sumo
;
23 * Sumo - main entry point. Attached to window->load
27 * @param {object} config the configuration to extend the object
29 * @property {HTMLElement} [element=null] the element in which to render.
30 * Required, will throw if not provided
31 * @property {Number} [fps=60] the fps target to maintain
32 * @property {Number} [verticalResolution=224] how many pixels to render in the vertical
33 * axis (gets scaled if the canvas is larger)
34 * @property {Array<Number>} [aspectRatio=[2.76, 1]] the aspect ratio experssed as
35 * an array of two numbers, where aspect ratio x:y is [x, y] (eg. [16, 9])
38 internals
.Sumo
= class Sumo
{
43 this.aspectRatio
= [2.76, 1];
44 this.verticalResolution
= 224;
46 Object
.assign(this, config
);
49 throw new Error(internals
.kNoElementError
);
52 this._engine
= new Serpentity();
54 this._previousTime
= 0;
55 this._looping
= false;
57 // Initialization functions
58 this._initializeCanvas();
59 this._initializeSystems();
60 this._initializeEntities();
64 * Starts the main loop. Resets the FPS (if you change it it won't go
65 * live until after you stop and start the loop)
74 this._frameDuration
= 1000 / this.fps
;
75 window
.requestAnimationFrame(this._loop
.bind(this));
87 this._looping
= false;
90 // The main loop used above. Runs the serpentity update process and
91 // attempts to maintain FPS. The rest is handled by the engine.
99 window
.requestAnimationFrame(this._loop
.bind(this));
101 const currentFrameDuration
= currentTime
- this._previousTime
;
103 if (currentFrameDuration
> this._frameDuration
) {
105 // We're sending the currentTime since it gives better results for
106 // this type of renderer, though usually we expect the delta
107 this._engine
.update(currentTime
);
108 this._previousTime
= currentTime
;
112 // Creates a canvas for rendering
114 _initializeCanvas() {
116 this._canvas
= document
.createElement('canvas');
117 this.element
.appendChild(this._canvas
);
118 this._resizeCanvas();
119 window
.addEventListener('resize', this._resizeCanvas
.bind(this));
122 // Resizes the canvas to a square the size of the smallest magnitude
127 let width
= window
.innerWidth
;
128 let height
= Math
.round(width
* this.aspectRatio
[1] / this.aspectRatio
[0]);
130 if (window
.innerHeight
< height
) {
131 height
= window
.innerHeight
;
132 width
= Math
.round(height
* this.aspectRatio
[0] / this.aspectRatio
[1]);
135 this._canvas
.style
.width
= `${width}px`;
136 this._canvas
.style
.height
= `${height}px`;
138 this._canvas
.width
= Math
.round(this.verticalResolution
* this.aspectRatio
[0] / this.aspectRatio
[1]);
139 this._canvas
.height
= this.verticalResolution
;
142 // Initializes the serpentity systems
144 _initializeSystems() {
148 // Initializes the serpentity entities
150 _initializeEntities() {
155 export default internals
.exports
= {};
158 window
.addEventListener('load', internals
.onLoad
);