ujs/src/js/app.js

50 lines
1.1 KiB
JavaScript

import { Scene } from './scene.js';
import { Settings } from './settings.js';
import { Pointer } from './pointer.js';
export class App {
constructor(
canvas,
w,
h,
options = {
backgroundColor: '#ffcc68',
}
) {
this.view = document.createElement('canvas');
this.canvas = this.view;
this.context = this.canvas.getContext('2d');
this.options = options;
this.scene = new Scene(this.canvas, this.context, w, h);
this.prevTime = Date.now();
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);
};
}