-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCanvas.js
145 lines (122 loc) · 3.81 KB
/
Canvas.js
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
/*
* Canvas class
*
* Among it's (many) responsibilities is:
* - to hide and manage HTMLCanvasElement
* - add helper methods for drwaing basic figures that wraps specific calls
* the context
* - keep track of all the figures and help to identify them on the canvas
* - manage the way of using event handlers
*/
function Canvas(document) {
if (!document) {
throw new Error('ArgumentError');
}
if (!(this.htmlCanvas = document.getElementById("canvas"))) {
this.htmlCanvas = this.createCanvas();
}
this.ctx = this.getContext();
this.figures = [];
this.tool = null;
this.activateEventHandlers();
}
/*
* This method is simply to be able to mock context
* For example see testRectangle::testPaint()
*/
Canvas.prototype.getContext = function() {
return this.htmlCanvas.getContext("2d");
};
Canvas.prototype.createCanvas = function() {
var htmlCanvas = document.createElement("canvas");
htmlCanvas.id = 'canvas';
htmlCanvas.width = '550';
htmlCanvas.height= '400';
htmlCanvas.style.border ='2px solid red';
document.body.appendChild(htmlCanvas);
return htmlCanvas;
};
Canvas.prototype.addFigure = function(figure) {
this.figures.add(figure);
};
Canvas.prototype.removeFigure = function(figure) {
this.figures.remove(figure);
}
// WARNING: returns refecence to the array and not clonned array
Canvas.prototype.getFigures = function() {
return this.figures;
};
Canvas.prototype.getSelectedFigures = function() {
var selectedFigures = [];
for (var i = 0; this.figures[i]; i++) {
if (this.figures[i].isSelected()) {
selectedFigures.add(this.figures[i]);
}
}
return selectedFigures;
};
Canvas.prototype.figureAtPoint = function(point) {
for (var i = 0; this.figures[i]; i++) {
if (this.figures[i].touches(point)) {
return this.figures[i];
}
}
};
Canvas.prototype.setColor = function(color) {
this.ctx.fillStyle = color;
this.ctx.strokeStyle = color;
};
Canvas.prototype.strokeRect = function(x,y,width,height) {
this.ctx.strokeRect(x,y,width,height);
};
Canvas.prototype.fillRect = function(x,y,width,height) {
this.ctx.fillRect(x,y,width,height);
};
Canvas.prototype.drawLine = function(startX, startY, endX, endY) {
this.ctx.beginPath();
this.ctx.moveTo(startX, startY);
this.ctx.lineTo(endX, endY);
this.ctx.stroke();
};
Canvas.prototype.repaint = function() {
this.clearAll();
var figures = this.getFigures();
for(var i = 0; figures[i]; i++) {
figures[i].paint();
}
};
Canvas.prototype.clear = function(startPoint, endPoint) {
this.ctx.clearRect(startPoint.getX(),startPoint.getY(),endPoint.getX(),endPoint.getY());
};
Canvas.prototype.clearAll = function() {
this.clear(new Point(0,0),new Point(this.htmlCanvas.width,this.htmlCanvas.height));
};
Canvas.prototype.setTool = function(tool) {
this.tool = tool;
};
Canvas.prototype.getTool = function() {
return this.tool;
};
/*
* Some event handlers are registered here but others may be registered within
* specifig tool when particular event takes place. A good example is mousemove
* event handler which for performance reasons is registered in SelectionTool
* in mousedown event and unregistered (removed) in mouseup event.
*/
Canvas.prototype.activateEventHandlers = function()
{
this.htmlCanvas.addEventListener('mousedown', this.eventHandler, false);
this.htmlCanvas.addEventListener('mouseup', this.eventHandler, false);
};
/*
* This method is called from outside of the context of the canvas object
* so we have to use service locator to locate canvas.
*/
Canvas.prototype.eventHandler = function(oEvent)
{
var func = oEvent.type;
var tool = sl.canvas.getTool();
if(tool && tool[func]) {
tool[func](oEvent);
}
};