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>
<style>
body {
margin: 0 0 0 0;
}
#canvas {
height: 100vh;
width: 100vw;
}
#canvas > canvas {
width: 100%;
height: 100%;
width: 500px;
height: 500px;
border: 1px solid black;
}
</style>
</html>

View File

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