-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
317 lines (317 loc) · 10.3 KB
/
script.js
File metadata and controls
317 lines (317 loc) · 10.3 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
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Point2D = /** @class */ (function () {
function Point2D(x, y) {
this.x = x;
this.y = y;
}
return Point2D;
}());
var AbstractShape = /** @class */ (function () {
function AbstractShape() {
this.id = AbstractShape.counter++;
}
AbstractShape.prototype.draw = function (ctx) { };
return AbstractShape;
}());
var Line = /** @class */ (function (_super) {
__extends(Line, _super);
function Line(from, to) {
var _this = _super.call(this) || this;
_this.from = from;
_this.to = to;
return _this;
}
Line.prototype.draw = function (ctx) {
ctx.moveTo(this.from.x, this.from.y);
ctx.lineTo(this.to.x, this.to.y);
ctx.stroke();
};
return Line;
}(AbstractShape));
var Circle = /** @class */ (function (_super) {
__extends(Circle, _super);
function Circle(center, radius) {
var _this = _super.call(this) || this;
_this.center = center;
_this.radius = radius;
return _this;
}
Circle.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.arc(this.center.x, this.center.y, this.radius, 0, 2 * Math.PI);
ctx.stroke();
};
return Circle;
}(AbstractShape));
var Rectangle = /** @class */ (function (_super) {
__extends(Rectangle, _super);
function Rectangle(from, to) {
var _this = _super.call(this) || this;
_this.from = from;
_this.to = to;
_this.width = to.x - from.x;
_this.height = to.y - from.y;
return _this;
}
Rectangle.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.strokeRect(this.from.x, this.from.y, this.width, this.height);
};
return Rectangle;
}(AbstractShape));
var Triangle = /** @class */ (function (_super) {
__extends(Triangle, _super);
function Triangle(p1, p2, p3) {
var _this = _super.call(this) || this;
_this.p1 = p1;
_this.p2 = p2;
_this.p3 = p3;
return _this;
}
Triangle.prototype.draw = function (ctx) {
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();
};
return Triangle;
}(AbstractShape));
var TextShape = /** @class */ (function (_super) {
__extends(TextShape, _super);
function TextShape(from, text) {
var _this = _super.call(this) || this;
_this.from = from;
_this.text = text;
return _this;
}
TextShape.prototype.draw = function (ctx) {
ctx.fillText(this.text, this.from.x, this.from.y);
};
return TextShape;
}(AbstractShape));
var Canvas = /** @class */ (function () {
function Canvas(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.shapes = [];
}
Canvas.prototype.addShape = function (shape, redraw) {
this.shapes.push(shape);
if (redraw) {
this.draw();
}
else {
return this;
}
};
Canvas.prototype.removeShape = function (shape, redraw) {
this.shapes.splice(this.shapes.indexOf(shape), 1);
if (redraw) {
this.draw();
}
else {
return this;
}
};
Canvas.prototype.removeShapeById = function (id, redraw) {
var index;
for (var 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;
}
};
Canvas.prototype.draw = function () {
// 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 (var id in this.shapes) {
this.shapes[id].draw(this.ctx);
}
return this;
};
return Canvas;
}());
var AbstractFactory = /** @class */ (function () {
function AbstractFactory(shapeManager) {
this.shapeManager = shapeManager;
}
AbstractFactory.prototype.handleMouseDown = function (x, y) {
this.from = new Point2D(x, y);
};
AbstractFactory.prototype.handleMouseUp = function (x, y) {
// 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;
};
AbstractFactory.prototype.handleMouseMove = function (x, y) {
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);
}
};
return AbstractFactory;
}());
var LineFactory = /** @class */ (function (_super) {
__extends(LineFactory, _super);
function LineFactory(shapeManager) {
var _this = _super.call(this, shapeManager) || this;
_this.label = 'Linie';
return _this;
}
LineFactory.prototype.createShape = function (from, to) {
return new Line(from, to);
};
return LineFactory;
}(AbstractFactory));
var CircleFactory = /** @class */ (function (_super) {
__extends(CircleFactory, _super);
function CircleFactory(shapeManager) {
var _this = _super.call(this, shapeManager) || this;
_this.label = 'Kreis';
return _this;
}
CircleFactory.prototype.createShape = function (from, to) {
return new Circle(from, Math.sqrt(Math.pow(to.x - from.x, 2) + Math.pow(to.y - from.y, 2)));
};
return CircleFactory;
}(AbstractFactory));
var RectangleFactory = /** @class */ (function (_super) {
__extends(RectangleFactory, _super);
function RectangleFactory(shapeManager) {
var _this = _super.call(this, shapeManager) || this;
_this.label = 'Rechteck';
return _this;
}
RectangleFactory.prototype.createShape = function (from, to) {
return new Rectangle(from, to);
};
return RectangleFactory;
}(AbstractFactory));
var TriangleFactory = /** @class */ (function () {
function TriangleFactory(shapeManager) {
this.shapeManager = shapeManager;
this.label = 'Dreieck';
this.p1 = undefined;
this.p2 = undefined;
}
TriangleFactory.prototype.handleMouseDown = function (x, y) { };
TriangleFactory.prototype.handleMouseUp = function (x, y) {
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;
}
};
TriangleFactory.prototype.handleMouseMove = function (x, y) { };
return TriangleFactory;
}());
var TextFactory = /** @class */ (function () {
function TextFactory(shapeManager) {
this.shapeManager = shapeManager;
this.label = 'Text';
}
TextFactory.prototype.handleMouseDown = function (x, y) { };
TextFactory.prototype.handleMouseUp = function (x, y) {
this.shapeManager.addShape(new TextShape(new Point2D(x, y), textInput.value), true);
};
TextFactory.prototype.handleMouseMove = function (x, y) { };
return TextFactory;
}());
var ToolArea = /** @class */ (function () {
function ToolArea(toolSelector, menu) {
var _this = this;
this.selectedTool = undefined;
var domElems = [];
toolSelector.forEach(function (tool) {
var domSelectionElem = document.createElement('li');
domSelectionElem.innerText = tool.label;
menu.appendChild(domSelectionElem);
domElems.push(domSelectionElem);
domSelectionElem.addEventListener('click', function () {
selectFactory.call(_this, tool, domSelectionElem);
});
});
function selectFactory(tool, domElem) {
this.selectedTool = tool;
domElems.forEach(function (element) {
if (element.classList.contains('selected')) {
element.classList.remove('selected');
}
});
domElem.classList.add('selected');
}
}
ToolArea.prototype.getSelectedTool = function () {
return this.selectedTool;
};
return ToolArea;
}());
var canvas = (document.getElementById('canvas'));
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var textInput = document.getElementById('textInput');
var app = new Canvas(canvas);
var 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) {
toolArea
.getSelectedTool()
.handleMouseDown(ev.pageX - canvas.offsetLeft, ev.pageY - canvas.offsetTop);
}
function handleMouseUp(ev) {
toolArea
.getSelectedTool()
.handleMouseUp(ev.pageX - canvas.offsetLeft, ev.pageY - canvas.offsetTop);
}