ujs/src/js/app.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-02-16 00:04:40 +03:00
import { Scene } from './scene.js';
import { Settings } from './settings.js';
import { Pointer } from './pointer.js';
2023-04-06 14:44:41 +03:00
export class App {
2023-04-06 18:46:01 +03:00
constructor(
canvas,
w,
h,
options = {
backgroundColor: '#ffcc68',
}
) {
this.view = document.createElement('canvas');
this.canvas = this.view;
2023-04-06 15:03:09 +03:00
this.context = this.canvas.getContext('2d');
2023-04-06 18:46:01 +03:00
this.options = options;
2023-04-06 15:03:09 +03:00
this.scene = new Scene(this.canvas, this.context, w, h);
2023-02-16 00:04:40 +03:00
this.prevTime = Date.now();
Pointer.init();
this.run();
}
run = () => {
let newTime = Date.now();
Settings.delta = (newTime - this.prevTime) / 1000;
this.prevTime = newTime;
2023-04-06 18:46:01 +03:00
// 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);
2023-02-16 00:04:40 +03:00
this.scene.run();
requestAnimationFrame(this.run);
2023-04-06 18:03:40 +03:00
};
2023-02-16 00:04:40 +03:00
}