2021-08-26 17:38:49 +00:00
|
|
|
import CanvasUtil from "./canvas-util/canvasUtil.js";
|
2021-08-26 18:05:40 +00:00
|
|
|
import Coordinates from "./canvas-util/coordinates.js";
|
2021-08-26 17:38:49 +00:00
|
|
|
|
2021-08-26 18:05:40 +00:00
|
|
|
class CanvasSenderController {
|
2021-08-26 17:38:49 +00:00
|
|
|
private canvasUtil: CanvasUtil;
|
2021-08-24 17:21:14 +00:00
|
|
|
private canvasDiv: HTMLDivElement;
|
2021-08-24 20:26:23 +00:00
|
|
|
private ws: WebSocket;
|
2021-08-24 17:21:14 +00:00
|
|
|
|
|
|
|
private isMouseDown: boolean = false;
|
2021-08-24 17:57:25 +00:00
|
|
|
|
2021-08-24 17:21:14 +00:00
|
|
|
constructor() {
|
2021-08-24 20:26:23 +00:00
|
|
|
this.canvasDiv = document.querySelector('#canvas') as HTMLDivElement;
|
2021-08-26 17:38:49 +00:00
|
|
|
this.canvasUtil = new CanvasUtil(this.canvasDiv.offsetWidth, this.canvasDiv.offsetHeight);
|
2021-08-24 17:21:14 +00:00
|
|
|
|
2021-08-26 17:38:49 +00:00
|
|
|
const canvas = this.canvasUtil.getCanvas();
|
|
|
|
this.canvasDiv.appendChild(canvas);
|
2021-08-25 20:57:01 +00:00
|
|
|
|
2021-08-26 17:38:49 +00:00
|
|
|
if (!this.canvasUtil.isCanvasApiSupported) {
|
2021-08-25 20:57:01 +00:00
|
|
|
alert('Canvas API unavailable, drawing will not work!');
|
|
|
|
} else {
|
2021-08-26 17:38:49 +00:00
|
|
|
// Events
|
|
|
|
canvas.addEventListener('mousedown', e => this.onMouseDown(e));
|
|
|
|
canvas.addEventListener('mouseup', () => this.onMouseUp());
|
|
|
|
canvas.addEventListener('mousemove', e => this.onMouseMove(e));
|
|
|
|
|
|
|
|
canvas.addEventListener('touchstart', e => this.onMouseDown(e.touches[0]));
|
|
|
|
canvas.addEventListener('touchend', () => this.onMouseUp());
|
|
|
|
canvas.addEventListener('touchmove', e => this.onMouseMove(e.touches[0]));
|
2021-08-25 20:57:01 +00:00
|
|
|
}
|
2021-08-24 20:26:23 +00:00
|
|
|
|
|
|
|
this.ws = new WebSocket('ws://localhost:8081', 'json');
|
2021-08-25 20:57:01 +00:00
|
|
|
this.ws.onopen = () => this.ws.send('SENDER');
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 20:57:01 +00:00
|
|
|
onMouseDown(event: MouseEvent | Touch) {
|
2021-08-24 17:21:14 +00:00
|
|
|
this.isMouseDown = true;
|
2021-08-26 17:38:49 +00:00
|
|
|
|
|
|
|
const coordinates = new Coordinates(event.clientX, event.clientY);
|
|
|
|
this.canvasUtil.startPath(coordinates);
|
|
|
|
|
|
|
|
this.ws.send('START');
|
|
|
|
this.ws.send(JSON.stringify(coordinates));
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onMouseUp() {
|
|
|
|
this.isMouseDown = false;
|
2021-08-26 17:38:49 +00:00
|
|
|
this.canvasUtil.stopPath();
|
|
|
|
this.ws.send('STOP');
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 20:57:01 +00:00
|
|
|
onMouseMove(event: MouseEvent | Touch) {
|
|
|
|
if (!this.isMouseDown) {
|
2021-08-24 17:21:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-25 20:57:01 +00:00
|
|
|
const coordinates = new Coordinates(event.clientX, event.clientY);
|
2021-08-26 17:38:49 +00:00
|
|
|
this.canvasUtil.draw(coordinates);
|
2021-08-25 20:57:01 +00:00
|
|
|
this.ws.send(JSON.stringify(coordinates));
|
|
|
|
}
|
2021-08-24 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 17:57:25 +00:00
|
|
|
|
2021-08-26 18:05:40 +00:00
|
|
|
const app = new CanvasSenderController();
|
2021-08-24 17:21:14 +00:00
|
|
|
|
|
|
|
export { app };
|