ujs/src/js/main.js

71 lines
1.7 KiB
JavaScript

import { Settings } from './settings.js';
import { Pointer } from './pointer.js';
import { Scene, SceneLayer } from './scene.js';
import { Rect, StrokeRect, Sprite, TiledSprite } from './objects.js';
export class App {
#version;
constructor(
w,
h,
options = {
backgroundColor: '#ffcc68',
welcome: true,
}
) {
this.#version = 'beeeeeeta';
this.view = document.createElement('canvas');
this.canvas = this.view;
this.context = this.canvas.getContext('2d');
this.options = options;
// check options || FIXIT:
if (typeof this.options.backgroundColor === 'undefined') {
this.options.backgroundColor = '#ffcc68';
}
if (typeof this.options.welcome === 'undefined') {
this.options.welcome = true;
}
this.scene = new Scene(this.canvas, this.context, w, h);
this.prevTime = Date.now();
if (this.options.welcome) {
console.log('ujs engine');
console.log('version:', this.#version);
console.log('feedback:', 'iiiypuk {dog} fastmail.fm');
}
Pointer.init();
this.run();
}
run = () => {
let newTime = Date.now();
Settings.delta = (newTime - this.prevTime) / 1000;
this.prevTime = newTime;
// clear canvas
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
// fill canvas
this.context.fillStyle = this.options.backgroundColor;
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
// draw border
this.context.strokeStyle = '#101024';
this.context.lineWidth = 1;
this.context.strokeRect(5, 5, this.canvas.width - 10, this.canvas.height - 10);
this.scene.run();
requestAnimationFrame(this.run);
};
}
export { Scene, SceneLayer, Rect, StrokeRect, Sprite, TiledSprite };