2021-08-24 17:21:14 +00:00
|
|
|
class CanvasController {
|
|
|
|
private canvas: HTMLCanvasElement;
|
|
|
|
private canvasDiv: HTMLDivElement;
|
|
|
|
private ctx: CanvasRenderingContext2D | null;
|
2021-08-24 20:26:23 +00:00
|
|
|
private ws: WebSocket;
|
2021-08-24 17:21:14 +00:00
|
|
|
|
|
|
|
private isMouseDown: boolean = false;
|
|
|
|
private x: number = 0;
|
|
|
|
private y: number = 0;
|
|
|
|
|
2021-08-24 17:57:25 +00:00
|
|
|
private paths: Coordinates[][] = [];
|
|
|
|
private currentPath: Coordinates[] = [];
|
|
|
|
|
2021-08-24 17:21:14 +00:00
|
|
|
constructor() {
|
2021-08-24 20:26:23 +00:00
|
|
|
this.canvas = document.createElement('canvas');
|
|
|
|
this.canvasDiv = document.querySelector('#canvas') as HTMLDivElement;
|
2021-08-24 17:21:14 +00:00
|
|
|
|
|
|
|
this.canvasDiv.appendChild(this.canvas);
|
|
|
|
this.canvas.width = this.canvas.offsetWidth;
|
|
|
|
this.canvas.height = this.canvas.offsetHeight;
|
2021-08-24 20:26:23 +00:00
|
|
|
this.ctx = this.canvas.getContext('2d');
|
2021-08-24 17:21:14 +00:00
|
|
|
|
|
|
|
// Events
|
2021-08-24 20:26:23 +00:00
|
|
|
this.canvasDiv.addEventListener('mousedown', e => this.onMouseDown(e));
|
|
|
|
this.canvasDiv.addEventListener('mouseup', () => this.onMouseUp());
|
|
|
|
this.canvasDiv.addEventListener('mousemove', e => this.onMouseMove(e));
|
|
|
|
|
|
|
|
this.ws = new WebSocket('ws://localhost:8081', 'json');
|
|
|
|
this.ws.onopen = () => this.ws.send("SENDER");
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onMouseDown(event: MouseEvent) {
|
|
|
|
this.isMouseDown = true;
|
|
|
|
this.x = event.clientX;
|
|
|
|
this.y = event.clientY;
|
2021-08-24 17:57:25 +00:00
|
|
|
this.currentPath = [new Coordinates(this.x, this.y)];
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onMouseUp() {
|
|
|
|
this.isMouseDown = false;
|
2021-08-24 17:57:25 +00:00
|
|
|
this.ctx?.closePath();
|
|
|
|
this.paths.push(this.currentPath);
|
2021-08-24 20:26:23 +00:00
|
|
|
this.sendPath();
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onMouseMove(event: MouseEvent) {
|
|
|
|
if (!this.isMouseDown || !this.ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ctx.beginPath();
|
|
|
|
this.ctx.moveTo(this.x, this.y);
|
|
|
|
this.ctx.lineTo(event.clientX, event.clientY);
|
|
|
|
this.ctx.lineWidth = 1;
|
|
|
|
this.ctx.stroke();
|
|
|
|
|
2021-08-24 17:57:25 +00:00
|
|
|
this.currentPath.push(new Coordinates(event.clientX, event.clientY));
|
|
|
|
|
2021-08-24 17:21:14 +00:00
|
|
|
this.x = event.clientX;
|
|
|
|
this.y = event.clientY;
|
|
|
|
}
|
2021-08-24 20:26:23 +00:00
|
|
|
|
|
|
|
sendPath() {
|
|
|
|
this.ws.send(JSON.stringify(this.currentPath));
|
|
|
|
}
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 17:57:25 +00:00
|
|
|
|
|
|
|
class Coordinates {
|
|
|
|
public x: number;
|
|
|
|
public y: number;
|
|
|
|
|
|
|
|
constructor(x: number, y: number) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:21:14 +00:00
|
|
|
const app = new CanvasController();
|
|
|
|
|
|
|
|
export { app };
|