Live preview - send every coordinate

This commit is contained in:
Robin Steinberg 2021-08-25 22:57:01 +02:00
parent c3d2f4b031
commit d15ba4cc79
2 changed files with 48 additions and 28 deletions

View File

@ -9,18 +9,10 @@
</body> </body>
<style> <style>
body {
margin: 0 0 0 0;
}
#canvas {
height: 100vh;
width: 100vw;
}
#canvas > canvas { #canvas > canvas {
width: 100%; width: 500px;
height: 100%; height: 500px;
border: 1px solid black;
} }
</style> </style>
</html> </html>

View File

@ -1,7 +1,7 @@
class CanvasController { class CanvasController {
private canvas: HTMLCanvasElement; private canvas: HTMLCanvasElement;
private canvasDiv: HTMLDivElement; private canvasDiv: HTMLDivElement;
private ctx: CanvasRenderingContext2D | null; private ctx?: CanvasRenderingContext2D | null;
private ws: WebSocket; private ws: WebSocket;
private isMouseDown: boolean = false; private isMouseDown: boolean = false;
@ -18,48 +18,76 @@ class CanvasController {
this.canvasDiv.appendChild(this.canvas); this.canvasDiv.appendChild(this.canvas);
this.canvas.width = this.canvas.offsetWidth; this.canvas.width = this.canvas.offsetWidth;
this.canvas.height = this.canvas.offsetHeight; this.canvas.height = this.canvas.offsetHeight;
this.ctx = this.canvas.getContext('2d');
if (!this.canvas.getContext) {
alert('Canvas API unavailable, drawing will not work!');
} else {
this.ctx = this.canvas.getContext('2d');
}
// Events // Events
this.canvasDiv.addEventListener('mousedown', e => this.onMouseDown(e)); if (this.ctx) {
this.canvasDiv.addEventListener('mouseup', () => this.onMouseUp()); this.canvas.addEventListener('mousedown', e => this.onMouseDown(e));
this.canvasDiv.addEventListener('mousemove', e => this.onMouseMove(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]));
}
this.ws = new WebSocket('ws://localhost:8081', 'json'); this.ws = new WebSocket('ws://localhost:8081', 'json');
this.ws.onopen = () => this.ws.send("SENDER"); this.ws.onopen = () => this.ws.send('SENDER');
} }
onMouseDown(event: MouseEvent) { onMouseDown(event: MouseEvent | Touch) {
this.isMouseDown = true; this.isMouseDown = true;
this.x = event.clientX; this.x = event.clientX;
this.y = event.clientY; this.y = event.clientY;
this.currentPath = [new Coordinates(this.x, this.y)]; this.currentPath = [new Coordinates(this.x, this.y)];
this.startDrawing();
} }
onMouseUp() { onMouseUp() {
this.isMouseDown = false; this.isMouseDown = false;
this.ctx?.closePath();
this.paths.push(this.currentPath); this.paths.push(this.currentPath);
this.sendPath(); // this.sendPath();
this.stopDrawing()
} }
onMouseMove(event: MouseEvent) { onMouseMove(event: MouseEvent | Touch) {
if (!this.isMouseDown || !this.ctx) { if (!this.isMouseDown) {
return; return;
} }
this.ctx.beginPath(); this.ctx!.beginPath();
this.ctx.moveTo(this.x, this.y); this.ctx!.moveTo(this.x, this.y);
this.ctx.lineTo(event.clientX, event.clientY); this.ctx!.lineTo(event.clientX, event.clientY);
this.ctx.lineWidth = 1; this.ctx!.lineWidth = 1;
this.ctx.stroke(); this.ctx!.stroke();
this.currentPath.push(new Coordinates(event.clientX, event.clientY)); const coordinates = new Coordinates(event.clientX, event.clientY);
this.currentPath.push(coordinates);
this.sendCoordindates(coordinates);
this.x = event.clientX; this.x = event.clientX;
this.y = event.clientY; this.y = event.clientY;
} }
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');
}
sendPath() { sendPath() {
this.ws.send(JSON.stringify(this.currentPath)); this.ws.send(JSON.stringify(this.currentPath));
} }