class Display {
constructor(w = 640, h = 480) {
[this.w, this.h] = [w, h];
this.canvas = html`<canvas width=${w} height=${h} style="border:1px solid gray" />`;
this.canvas.value = this;
this.ctx = this.canvas.getContext('2d');
}
clear() {
this.ctx.clearRect(0, 0, this.w, this.h);
}
drawPoints(pts, options = {}) {
options = Object.assign(
{ radius: 4, fill: 'black', stroke: null },
options
);
this.ctx.save();
this.ctx.strokeStyle = options.stroke;
this.ctx.fillStyle = options.fill;
for (let p of pts) {
this.ctx.beginPath();
this.ctx.arc(p[0], p[1], options.radius, 0, Math.PI * 2);
if (options.fill) this.ctx.fill();
if (options.stroke) this.ctx.stroke();
}
this.ctx.restore();
}
drawPolygon(pts, options = {}) {
options = Object.assign(
{
radius: 4,
fill: null,
stroke: 'black',
lineWidth: 1,
dash: [],
close: true
},
options
);
this.ctx.save();
this.ctx.strokeStyle = options.stroke;
this.ctx.fillStyle = options.fill;
this.ctx.setLineDash(options.dash);
this.ctx.lineWidth = options.lineWidth;
this.ctx.beginPath();
this.ctx.moveTo(pts[0][0], pts[0][1]);
for (let p of pts) {
this.ctx.lineTo(p[0], p[1]);
}
if (options.close) this.ctx.closePath();
if (options.fill) this.ctx.fill();
if (options.stroke) this.ctx.stroke();
this.ctx.restore();
}
}