artworld.js/artworld/world.js

55 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-07-06 19:17:10 +00:00
export class World {
constructor() {
this.drawables = [];
this.ctx = null;
}
bindCanvas(id) {
this.ctx = document.getElementById(id).getContext("2d");
}
register(drawable) {
this.drawables.push(drawable);
}
draw() {
for (let d of this.drawables) {
d.draw();
}
}
// Canvas 2D /////////////////
setStrokeWeight(scalar) {
this.ctx.lineWidth = scalar;
}
setStrokeColor(color) {
2024-07-06 20:21:35 +00:00
if (color && color.toStr) this.ctx.strokeStyle = color.toStr();
else this.ctx.strokeStyle = color;
2024-07-06 19:17:10 +00:00
}
setFillColor(color) {
2024-07-06 20:21:35 +00:00
if (color && color.toStr) this.ctx.fillStyle = color.toStr();
else this.ctx.fillStyle = color;
2024-07-06 19:17:10 +00:00
}
fillRect(rect) {
this.ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
}
strokeRect(rect) {
this.ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
}
drawLine(from, to) {
this.ctx.beginPath();
this.ctx.moveTo(from.x, from.y);
this.ctx.lineTo(to.x, to.y);
this.ctx.stroke();
}
}
// singleton (for now)
export const artworld = new World();