37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import type { HousePlannerCanvas } from './canvas';
|
|
|
|
export class HousePlannerCanvasGrid {
|
|
constructor(
|
|
public manager: HousePlannerCanvas,
|
|
public gridSnap = 8,
|
|
public edgeOffset = 8 * 4
|
|
) {}
|
|
|
|
setGridSize(size: number) {
|
|
this.gridSnap = size;
|
|
}
|
|
|
|
draw() {
|
|
const cellsPerWidth = Math.round(
|
|
(this.manager.width - this.edgeOffset) / this.gridSnap
|
|
);
|
|
const cellsPerHeight = Math.round(
|
|
(this.manager.height - this.edgeOffset) / this.gridSnap
|
|
);
|
|
const path = new Path2D();
|
|
for (let x = 0; x < cellsPerWidth - 3; x++) {
|
|
const absX = this.edgeOffset + x * this.gridSnap;
|
|
path.moveTo(absX, this.edgeOffset);
|
|
path.lineTo(absX, cellsPerHeight * this.gridSnap);
|
|
}
|
|
for (let y = 0; y < cellsPerHeight - 3; y++) {
|
|
const absY = this.edgeOffset + y * this.gridSnap;
|
|
path.moveTo(this.edgeOffset, absY);
|
|
path.lineTo(cellsPerWidth * this.gridSnap, absY);
|
|
}
|
|
this.manager.ctx.strokeStyle = '#ddd';
|
|
this.manager.ctx.lineWidth = 1;
|
|
this.manager.ctx.stroke(path);
|
|
}
|
|
}
|