ujs/src/js/scene.js

101 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-02-16 00:04:40 +03:00
import { Pointer } from './pointer.js';
2023-04-06 17:15:27 +03:00
import { uLayer, uRect, uStrokeRect } from './objects.js';
2023-02-16 00:04:40 +03:00
export class Scene {
2023-04-06 15:03:09 +03:00
#canvas;
#context;
2023-04-06 16:52:57 +03:00
#layers;
2023-04-06 15:03:09 +03:00
constructor(canvas, context, width, height) {
this.#canvas = canvas;
this.#context = context;
2023-04-06 16:52:57 +03:00
this.#layers = Array();
2023-04-06 17:15:27 +03:00
let layer = new uLayer('Entry', [
new uRect(50, 50, 100, 100, 'red'),
new uStrokeRect(150, 150, 40, 40, 'green', 'blue', 5),
]);
this.addLayer(layer);
2023-04-06 16:52:57 +03:00
2023-04-06 14:44:41 +03:00
this.init(width, height);
2023-02-16 00:04:40 +03:00
}
2023-04-06 16:52:57 +03:00
init(width, height) {
2023-04-06 14:44:41 +03:00
this.setScreenSize(width, height);
2023-02-16 00:04:40 +03:00
}
2023-04-06 14:54:58 +03:00
#clearCanvas() {
2023-04-06 15:03:09 +03:00
this.#context.clearRect(0, 0, this.#canvas.width, this.#canvas.height);
2023-04-06 14:54:58 +03:00
}
2023-02-16 00:04:40 +03:00
run() {
2023-04-06 14:44:41 +03:00
// clear canvas
2023-04-06 14:54:58 +03:00
this.#clearCanvas();
2023-04-06 15:03:09 +03:00
this.#scene();
}
2023-04-06 14:52:58 +03:00
2023-04-06 15:03:09 +03:00
#scene() {
2023-04-06 14:52:58 +03:00
// fill canvas
2023-04-06 15:03:09 +03:00
this.#context.fillStyle = '#523c4e';
this.#context.fillRect(0, 0, this.#canvas.width, this.#canvas.height);
2023-04-06 14:52:58 +03:00
// draw border
2023-04-06 15:03:09 +03:00
this.#context.strokeStyle = '#8bd0ba';
this.#context.lineWidth = 1;
this.#context.strokeRect(5, 5, this.#canvas.width - 10, this.#canvas.height - 10);
2023-04-06 14:52:58 +03:00
2023-04-06 17:15:27 +03:00
// read layers & draw items
this.#layers.forEach((layer) => {
layer.objects().forEach((item) => {
switch (item.constructor.name) {
case 'uRect':
this.#drawRect(item.x, item.y, item.width, item.height, item.fillColor);
break;
case 'uStrokeRect':
2023-04-06 18:03:40 +03:00
this.#drawStrokeRect(
item.x,
item.y,
item.width,
item.height,
item.fillColor,
item.strokeColor,
item.strokeWidth
);
2023-04-06 17:15:27 +03:00
break;
default:
console.log(`⛔ Error display '${item.constructor.name}' object.`);
}
});
2023-04-06 16:52:57 +03:00
});
}
#drawRect(x, y, w, h, fillColor) {
this.#context.fillStyle = fillColor;
this.#context.fillRect(x, y, w, h);
}
#drawStrokeRect(x, y, w, h, fillColor, strokeWidth, strokeColor) {
2023-04-06 17:15:27 +03:00
this.#drawRect(x, y, w, h, fillColor);
2023-04-06 16:52:57 +03:00
2023-04-06 17:15:27 +03:00
this.#context.strokeWidth = strokeWidth;
this.#context.strokeStyle = strokeColor;
this.#context.strokeRect(x, y, w, h);
2023-04-06 16:52:57 +03:00
}
2023-04-06 17:15:27 +03:00
addLayer(layer) {
this.#layers.push(layer);
}
removeLayers() {
this.#layers = Array();
2023-02-16 00:04:40 +03:00
}
2023-04-06 14:44:41 +03:00
setScreenSize(w, h) {
2023-04-06 15:03:09 +03:00
this.#canvas.width = w;
this.#canvas.height = h;
2023-02-16 00:04:40 +03:00
}
}