-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
366 lines (298 loc) · 8.39 KB
/
script.ts
File metadata and controls
366 lines (298 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
class Point2D {
readonly x: number;
readonly y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
interface Shape {
readonly id: number;
draw(ctx: CanvasRenderingContext2D): void;
}
abstract class AbstractShape implements Shape {
static counter: number;
readonly id: number;
constructor() {
this.id = AbstractShape.counter++;
}
draw(ctx: CanvasRenderingContext2D) {}
}
class Line extends AbstractShape implements Shape {
constructor(readonly from: Point2D, readonly to: Point2D) {
super();
}
draw(ctx: CanvasRenderingContext2D) {
ctx.moveTo(this.from.x, this.from.y);
ctx.lineTo(this.to.x, this.to.y);
ctx.stroke();
}
}
class Circle extends AbstractShape implements Shape {
constructor(readonly center: Point2D, readonly radius: number) {
super();
}
draw(ctx: CanvasRenderingContext2D) {
ctx.beginPath();
ctx.arc(this.center.x, this.center.y, this.radius, 0, 2 * Math.PI);
ctx.stroke();
}
}
class Rectangle extends AbstractShape implements Shape {
readonly width: number;
readonly height: number;
constructor(readonly from: Point2D, readonly to: Point2D) {
super();
this.width = to.x - from.x;
this.height = to.y - from.y;
}
draw(ctx: CanvasRenderingContext2D) {
ctx.beginPath();
ctx.strokeRect(this.from.x, this.from.y, this.width, this.height);
}
}
class Triangle extends AbstractShape implements Shape {
constructor(
readonly p1: Point2D,
readonly p2: Point2D,
readonly p3: Point2D
) {
super();
}
draw(ctx: CanvasRenderingContext2D) {
ctx.beginPath();
ctx.moveTo(this.p1.x, this.p1.y);
ctx.lineTo(this.p2.x, this.p2.y);
ctx.lineTo(this.p3.x, this.p3.y);
ctx.lineTo(this.p1.x, this.p1.y);
ctx.stroke();
}
}
class TextShape extends AbstractShape implements Shape {
constructor(readonly from: Point2D, readonly text: string) {
super();
}
draw(ctx: CanvasRenderingContext2D) {
ctx.fillText(this.text, this.from.x, this.from.y);
}
}
interface ShapeManager {
addShape(shape: Shape, redraw?: boolean): this;
removeShape(shape: Shape, redraw?: boolean): this;
removeShapeById(id: number, redraw?: boolean): this;
}
class Canvas implements ShapeManager {
ctx: CanvasRenderingContext2D;
shapes: Shape[];
constructor(readonly canvas: HTMLCanvasElement) {
this.ctx = canvas.getContext('2d');
this.shapes = [];
}
addShape(shape: Shape, redraw?: boolean): this {
this.shapes.push(shape);
if (redraw) {
this.draw();
} else {
return this;
}
}
removeShape(shape: Shape, redraw?: boolean): this {
this.shapes.splice(this.shapes.indexOf(shape), 1);
if (redraw) {
this.draw();
} else {
return this;
}
}
removeShapeById(id: number, redraw?: boolean): this {
let index: number;
for (let i = 0; i < this.shapes.length; i++) {
if (this.shapes[i].id === id) {
index = i;
break;
}
}
this.shapes.splice(index, 1);
if (redraw) {
this.draw();
} else {
return this;
}
}
draw(): this {
// Reset canvas
this.ctx.beginPath();
this.ctx.fillStyle = 'white';
this.ctx.fillRect(0, 0, canvasWidth, canvasHeight);
this.ctx.stroke();
this.ctx.font = '42px Consolas';
// Draw shapes
this.ctx.fillStyle = 'black';
for (let id in this.shapes) {
this.shapes[id].draw(this.ctx);
}
return this;
}
}
interface ShapeFactory {
label: string;
handleMouseDown(x: number, y: number): void;
handleMouseUp(x: number, y: number): void;
handleMouseMove(x: number, y: number): void;
}
abstract class AbstractFactory<T extends Shape> {
private from: Point2D;
private tmpTo: Point2D;
private tmpShape: T;
constructor(readonly shapeManager: ShapeManager) {}
abstract createShape(from: Point2D, to: Point2D): T;
handleMouseDown(x: number, y: number) {
this.from = new Point2D(x, y);
}
handleMouseUp(x: number, y: number) {
// Remove temporary line
if (this.tmpShape) {
this.shapeManager.removeShapeById(this.tmpShape.id, false);
}
this.shapeManager.addShape(
this.createShape(this.from, new Point2D(x, y)),
true
);
this.from = undefined;
}
handleMouseMove(x: number, y: number) {
if (!this.from) {
return;
}
if (!this.tmpTo || this.tmpTo.x !== x || this.tmpTo.y !== y) {
this.tmpTo = new Point2D(x, y);
if (this.tmpShape) {
this.shapeManager.removeShapeById(this.tmpShape.id, false);
}
// Add new temporary shape
this.tmpShape = this.createShape(this.from, new Point2D(x, y));
this.shapeManager.addShape(this.tmpShape);
}
}
}
class LineFactory extends AbstractFactory<Line> implements ShapeFactory {
public label: string = 'Linie';
constructor(shapeManager: ShapeManager) {
super(shapeManager);
}
createShape(from: Point2D, to: Point2D): Line {
return new Line(from, to);
}
}
class CircleFactory extends AbstractFactory<Circle> implements ShapeFactory {
public label: string = 'Kreis';
constructor(shapeManager: ShapeManager) {
super(shapeManager);
}
createShape(from: Point2D, to: Point2D): Circle {
return new Circle(
from,
Math.sqrt(Math.pow(to.x - from.x, 2) + Math.pow(to.y - from.y, 2))
);
}
}
class RectangleFactory extends AbstractFactory<Rectangle>
implements ShapeFactory {
public label: string = 'Rechteck';
constructor(shapeManager: ShapeManager) {
super(shapeManager);
}
createShape(from: Point2D, to: Point2D): Rectangle {
return new Rectangle(from, to);
}
}
class TriangleFactory implements ShapeFactory {
public label: string = 'Dreieck';
private p1: Point2D = undefined;
private p2: Point2D = undefined;
constructor(readonly shapeManager: ShapeManager) {}
handleMouseDown(x: number, y: number): void {}
handleMouseUp(x: number, y: number): void {
if (!this.p1) {
this.p1 = new Point2D(x, y);
} else if (!this.p2) {
this.p2 = new Point2D(x, y);
} else {
this.shapeManager.addShape(
new Triangle(this.p1, this.p2, new Point2D(x, y)),
true
);
this.p1 = undefined;
this.p2 = undefined;
}
}
handleMouseMove(x: number, y: number): void {}
}
class TextFactory implements ShapeFactory {
public label: string = 'Text';
constructor(readonly shapeManager: ShapeManager) {}
handleMouseDown(x: number, y: number): void {}
handleMouseUp(x: number, y: number): void {
this.shapeManager.addShape(
new TextShape(new Point2D(x, y), (textInput as HTMLInputElement).value),
true
);
}
handleMouseMove(x: number, y: number): void {}
}
class ToolArea {
private selectedTool: ShapeFactory = undefined;
constructor(toolSelector: ShapeFactory[], menu: Element) {
const domElems = [];
toolSelector.forEach((tool) => {
const domSelectionElem = document.createElement('li');
domSelectionElem.innerText = tool.label;
menu.appendChild(domSelectionElem);
domElems.push(domSelectionElem);
domSelectionElem.addEventListener('click', () => {
selectFactory.call(this, tool, domSelectionElem);
});
});
function selectFactory(tool: ShapeFactory, domElem: HTMLElement) {
this.selectedTool = tool;
domElems.forEach((element) => {
if (element.classList.contains('selected')) {
element.classList.remove('selected');
}
});
domElem.classList.add('selected');
}
}
getSelectedTool(): ShapeFactory {
return this.selectedTool;
}
}
const canvas: HTMLCanvasElement = <HTMLCanvasElement>(
document.getElementById('canvas')
);
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const textInput = document.getElementById('textInput');
const app = new Canvas(canvas);
const toolArea = new ToolArea(
[
new LineFactory(app),
new CircleFactory(app),
new RectangleFactory(app),
new TriangleFactory(app),
new TextFactory(app),
],
document.getElementById('toolArea')
);
canvas.addEventListener('mousedown', handleMouseDown);
canvas.addEventListener('mouseup', handleMouseUp);
function handleMouseDown(ev: MouseEvent): void {
toolArea
.getSelectedTool()
.handleMouseDown(ev.pageX - canvas.offsetLeft, ev.pageY - canvas.offsetTop);
}
function handleMouseUp(ev: MouseEvent): void {
toolArea
.getSelectedTool()
.handleMouseUp(ev.pageX - canvas.offsetLeft, ev.pageY - canvas.offsetTop);
}