websocket-canvas/ts/app.ts

110 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-08-24 17:21:14 +00:00
class CanvasController {
private canvas: HTMLCanvasElement;
private canvasDiv: HTMLDivElement;
2021-08-25 20:57:01 +00:00
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-25 20:57:01 +00:00
if (!this.canvas.getContext) {
alert('Canvas API unavailable, drawing will not work!');
} else {
this.ctx = this.canvas.getContext('2d');
}
2021-08-24 17:21:14 +00:00
// Events
2021-08-25 20:57:01 +00:00
if (this.ctx) {
this.canvas.addEventListener('mousedown', e => this.onMouseDown(e));
this.canvas.addEventListener('mouseup', () => this.onMouseUp());
this.canvas.addEventListener('mousemove', e => this.onMouseMove(e));
this.canvas.addEventListener('touchstart', e => this.onMouseDown(e.touches[0]));
this.canvas.addEventListener('touchend', () => this.onMouseUp());
this.canvas.addEventListener('touchmove', e => this.onMouseMove(e.touches[0]));
}
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;
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-25 20:57:01 +00:00
this.startDrawing();
2021-08-24 17:21:14 +00:00
}
onMouseUp() {
this.isMouseDown = false;
2021-08-24 17:57:25 +00:00
this.paths.push(this.currentPath);
2021-08-25 20:57:01 +00:00
// this.sendPath();
this.stopDrawing()
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
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:21:14 +00:00
2021-08-25 20:57:01 +00:00
const coordinates = new Coordinates(event.clientX, event.clientY);
this.currentPath.push(coordinates);
this.sendCoordindates(coordinates);
2021-08-24 17:57:25 +00:00
2021-08-24 17:21:14 +00:00
this.x = event.clientX;
this.y = event.clientY;
}
2021-08-24 20:26:23 +00:00
2021-08-25 20:57:01 +00:00
startDrawing() {
this.ws.send('START');
this.ws.send(JSON.stringify(new Coordinates(this.x, this.y)));
}
sendCoordindates(coordinates: Coordinates) {
this.ws.send(JSON.stringify(coordinates));
}
stopDrawing() {
this.ws.send('STOP');
}
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 };