Refactored to use canvasUtility
This commit is contained in:
parent
eb3236f3cd
commit
9058ee8e24
|
@ -9,7 +9,7 @@
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
#canvas > canvas {
|
#canvas {
|
||||||
width: 500px;
|
width: 500px;
|
||||||
height: 500px;
|
height: 500px;
|
||||||
border: 1px solid black;
|
border: 1px solid black;
|
||||||
|
|
61
ts/app.ts
61
ts/app.ts
|
@ -1,9 +1,11 @@
|
||||||
|
import CanvasUtil from "./canvas-util/canvasUtil.js";
|
||||||
|
import Coordinates from "./canvas-util/coordinates.js";
|
||||||
|
|
||||||
class CanvasReceiverController {
|
class CanvasReceiverController {
|
||||||
private canvasDiv: HTMLDivElement;
|
|
||||||
private canvas!: HTMLCanvasElement;
|
private canvas!: HTMLCanvasElement;
|
||||||
private ctx!: CanvasRenderingContext2D | null;
|
private canvasDiv: HTMLDivElement;
|
||||||
|
private canvasUtil: CanvasUtil;
|
||||||
private ws: WebSocket;
|
private ws: WebSocket;
|
||||||
private coordinates?: Coordinates;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents whether the app is waiting for the first coordinates of a draw path. If lineTo
|
* Represents whether the app is waiting for the first coordinates of a draw path. If lineTo
|
||||||
|
@ -14,8 +16,12 @@ class CanvasReceiverController {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.canvasDiv = document.querySelector('#canvas') as HTMLDivElement;
|
this.canvasDiv = document.querySelector('#canvas') as HTMLDivElement;
|
||||||
|
this.canvasUtil = new CanvasUtil(this.canvasDiv.offsetWidth, this.canvasDiv.offsetHeight);
|
||||||
|
|
||||||
this.createCanvas();
|
this.createCanvas();
|
||||||
|
if (!this.canvasUtil.isCanvasApiSupported) {
|
||||||
|
alert('Canvas API unavailable, drawing will not work!');
|
||||||
|
}
|
||||||
|
|
||||||
this.ws = new WebSocket('ws://localhost:8081', 'json');
|
this.ws = new WebSocket('ws://localhost:8081', 'json');
|
||||||
|
|
||||||
|
@ -23,16 +29,9 @@ class CanvasReceiverController {
|
||||||
this.ws.onopen = () => this.ws.send('RECEIVER');
|
this.ws.onopen = () => this.ws.send('RECEIVER');
|
||||||
}
|
}
|
||||||
|
|
||||||
private createCanvas() {
|
createCanvas() {
|
||||||
this.canvas = document.createElement('canvas');
|
const canvas = this.canvasUtil.getCanvas();
|
||||||
this.canvasDiv.appendChild(this.canvas);
|
this.canvasDiv.appendChild(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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
processMessage(message: MessageEvent<any>) {
|
processMessage(message: MessageEvent<any>) {
|
||||||
|
@ -54,56 +53,30 @@ class CanvasReceiverController {
|
||||||
|
|
||||||
startDrawing() {
|
startDrawing() {
|
||||||
console.log('Received START command');
|
console.log('Received START command');
|
||||||
if (!this.ctx) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.firstCoordinates = true;
|
this.firstCoordinates = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
stopDrawing() {
|
stopDrawing() {
|
||||||
if (!this.ctx) {
|
this.canvasUtil.stopPath();
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private clear() {
|
clear() {
|
||||||
this.canvasDiv.removeChild(this.canvas);
|
this.canvasDiv.removeChild(this.canvas);
|
||||||
|
this.canvasUtil.recreateCanvas();
|
||||||
this.createCanvas();
|
this.createCanvas();
|
||||||
}
|
}
|
||||||
|
|
||||||
draw(coordinates: Coordinates) {
|
draw(coordinates: Coordinates) {
|
||||||
if (!this.ctx) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.ctx.beginPath();
|
|
||||||
|
|
||||||
if (this.coordinates) {
|
|
||||||
this.ctx.moveTo(this.coordinates.x, this.coordinates.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.firstCoordinates) {
|
if (this.firstCoordinates) {
|
||||||
this.firstCoordinates = false;
|
this.firstCoordinates = false;
|
||||||
} else {
|
this.canvasUtil.startPath(coordinates);
|
||||||
this.ctx.lineTo(coordinates.x, coordinates.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ctx.stroke();
|
this.canvasUtil.draw(coordinates);
|
||||||
this.coordinates = coordinates;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Coordinates {
|
|
||||||
public x: number;
|
|
||||||
public y: number;
|
|
||||||
|
|
||||||
constructor(x: number, y: number) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const app = new CanvasReceiverController();
|
const app = new CanvasReceiverController();
|
||||||
|
|
||||||
export { app };
|
export { app };
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
import Coordinates from "./coordinates.js";
|
||||||
|
|
||||||
|
export default class CanvasUtil {
|
||||||
|
private canvas?: HTMLCanvasElement;
|
||||||
|
private ctx?: CanvasRenderingContext2D | null;
|
||||||
|
|
||||||
|
private canvasWidth: number;
|
||||||
|
private canvasHeight: number;
|
||||||
|
|
||||||
|
private currentCoordinates: Coordinates;
|
||||||
|
|
||||||
|
public paths: Coordinates[][] = [];
|
||||||
|
public currentPath: Coordinates[] = [];
|
||||||
|
|
||||||
|
constructor(canvasWidth: number, canvasHeight: number) {
|
||||||
|
this.currentCoordinates = new Coordinates(0, 0);
|
||||||
|
this.canvasWidth = canvasWidth;
|
||||||
|
this.canvasHeight = canvasHeight;
|
||||||
|
this.createCanvas();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getCanvas(): HTMLCanvasElement {
|
||||||
|
if (!this.canvas) {
|
||||||
|
this.createCanvas();
|
||||||
|
}
|
||||||
|
return this.canvas!;
|
||||||
|
}
|
||||||
|
|
||||||
|
public recreateCanvas() {
|
||||||
|
this.createCanvas();
|
||||||
|
return this.canvas;
|
||||||
|
}
|
||||||
|
|
||||||
|
public startPath(coordinates: Coordinates) {
|
||||||
|
this.currentPath.push(coordinates);
|
||||||
|
this.currentCoordinates = coordinates;
|
||||||
|
}
|
||||||
|
|
||||||
|
public stopPath() {
|
||||||
|
if (this.currentPath.length) {
|
||||||
|
this.paths.push(this.currentPath);
|
||||||
|
}
|
||||||
|
this.currentPath = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public draw(coordinates: Coordinates) {
|
||||||
|
if (!this.ctx) {
|
||||||
|
throw new Error('No canvas context available!');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ctx.beginPath();
|
||||||
|
this.ctx.moveTo(this.currentCoordinates.x, this.currentCoordinates.y);
|
||||||
|
this.ctx.lineTo(coordinates.x, coordinates.y);
|
||||||
|
this.ctx.lineWidth = 1;
|
||||||
|
this.ctx.stroke();
|
||||||
|
|
||||||
|
this.currentPath.push(coordinates);
|
||||||
|
this.currentCoordinates = coordinates;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* States whether the canvas API is supported by the browser and whether
|
||||||
|
* drawing will work
|
||||||
|
*/
|
||||||
|
public get isCanvasApiSupported(): boolean {
|
||||||
|
return !!this.ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
private createCanvas() {
|
||||||
|
this.canvas = document.createElement('canvas');
|
||||||
|
this.canvas.width = this.canvasWidth;
|
||||||
|
this.canvas.height = this.canvasHeight;
|
||||||
|
if (this.canvas.getContext) {
|
||||||
|
this.ctx = this.canvas.getContext('2d');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
export default class Coordinates {
|
||||||
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(x: number, y: number) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue