This repository has been archived on 2021-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
canvas-receiver/ts/app.ts

110 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-08-24 20:27:20 +00:00
class CanvasReceiverController {
private canvasDiv: HTMLDivElement;
private canvas!: HTMLCanvasElement;
private ctx!: CanvasRenderingContext2D | null;
private ws: WebSocket;
2021-08-25 21:12:19 +00:00
private coordinates?: Coordinates;
2021-08-24 20:27:20 +00:00
2021-08-25 20:57:21 +00:00
/**
2021-08-25 21:12:19 +00:00
* Represents whether the app is waiting for the first coordinates of a draw path. If lineTo
* is called with the first pair of coordinates, every path will be connected. Therefore this
* variable is needed to prevent drawing a line to the first pair of coordinates.
2021-08-25 20:57:21 +00:00
*/
2021-08-25 21:12:19 +00:00
private firstCoordinates: boolean = false;
2021-08-25 20:57:21 +00:00
2021-08-24 20:27:20 +00:00
constructor() {
this.canvasDiv = document.querySelector('#canvas') as HTMLDivElement;
this.createCanvas();
this.ws = new WebSocket('ws://localhost:8081', 'json');
this.ws.onmessage = message => this.processMessage(message);
2021-08-25 20:57:21 +00:00
this.ws.onopen = () => this.ws.send('RECEIVER');
2021-08-24 20:27:20 +00:00
}
private createCanvas() {
this.canvas = document.createElement('canvas');
this.canvasDiv.appendChild(this.canvas);
this.canvas.width = this.canvas.offsetWidth;
this.canvas.height = this.canvas.offsetHeight;
2021-08-25 20:57:21 +00:00
if (!this.canvas.getContext) {
alert('Canvas API unavailable, drawing will not work!');
} else {
this.ctx = this.canvas.getContext('2d');
}
2021-08-24 20:27:20 +00:00
}
processMessage(message: MessageEvent<any>) {
2021-08-25 20:57:21 +00:00
switch (message.data) {
case 'CLEAR':
this.clear();
break;
case 'START':
this.startDrawing();
break;
case 'STOP':
this.stopDrawing();
break;
default:
this.draw(JSON.parse(message.data));
break;
2021-08-24 20:27:20 +00:00
}
}
2021-08-25 20:57:21 +00:00
startDrawing() {
console.log('Received START command');
2021-08-24 20:27:20 +00:00
if (!this.ctx) {
return;
}
2021-08-25 21:12:19 +00:00
this.firstCoordinates = true;
2021-08-25 20:57:21 +00:00
}
2021-08-24 20:27:20 +00:00
2021-08-25 20:57:21 +00:00
stopDrawing() {
if (!this.ctx) {
2021-08-24 20:27:20 +00:00
return;
}
2021-08-25 20:57:21 +00:00
}
2021-08-24 20:27:20 +00:00
2021-08-25 20:57:21 +00:00
private clear() {
this.canvasDiv.removeChild(this.canvas);
this.createCanvas();
}
draw(coordinates: Coordinates) {
if (!this.ctx) {
return;
}
2021-08-24 20:27:20 +00:00
2021-08-25 21:12:19 +00:00
this.ctx.beginPath();
if (this.coordinates) {
this.ctx.moveTo(this.coordinates.x, this.coordinates.y);
}
if (this.firstCoordinates) {
this.firstCoordinates = false;
2021-08-25 20:57:21 +00:00
} else {
this.ctx.lineTo(coordinates.x, coordinates.y);
2021-08-24 20:27:20 +00:00
}
this.ctx.stroke();
2021-08-25 21:12:19 +00:00
this.coordinates = coordinates;
2021-08-24 20:27:20 +00:00
}
}
class Coordinates {
public x: number;
public y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const app = new CanvasReceiverController();
export { app };