homemanager-fe/src/modules/house-planner/index.ts

38 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-01-16 19:37:39 +00:00
import { HousePlannerCanvas } from './canvas';
import { Layer } from './interfaces';
export class HousePlanner {
public canvas!: HTMLCanvasElement;
public manager?: HousePlannerCanvas;
initialize(canvas: HTMLCanvasElement, initialData: Layer[]) {
this.canvas = canvas;
this.resizeCanvas();
this.addResizeEvents();
this.manager = new HousePlannerCanvas(canvas);
this.manager.layers = initialData;
this.manager.tools.selectLayer(
initialData[initialData.findIndex((layer) => layer.active)]
);
this.manager.draw();
return () => this.cleanUp();
}
cleanUp() {
window.removeEventListener('resize', this.boundResizeEvent);
this.manager?.cleanUp();
}
private addResizeEvents() {
window.addEventListener('resize', this.boundResizeEvent);
}
resizeCanvas() {
this.canvas.width = this.canvas.parentElement!.clientWidth;
this.canvas.height = this.canvas.parentElement!.clientHeight;
this.manager?.draw();
}
private boundResizeEvent = this.resizeCanvas.bind(this);
}