-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLineFigure.js
57 lines (46 loc) · 1.51 KB
/
LineFigure.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
/*
* LineFigure class
*
* Used to draw lines on the canvas
*/
function LineFigure(canvas, startPoint, endPoint) {
if (!canvas || !startPoint || !endPoint) {
throw new Error('ArgumentError');
}
Figure.call(this);
this.canvas = canvas;
this.start = startPoint;
this.end = endPoint;
this.mid = Geometry.getLineMidPoint(startPoint, endPoint);
this.addHandle(new LocatorHandle(this.canvas, this, this.mid));
}
LineFigure.prototype = new Figure();
/**
* For documentation see Figure::doPaint
*/
LineFigure.prototype.doPaint = function() {
this.canvas.setColor(this.getColor());
this.canvas.drawLine(this.start.getX(), this.start.getY(), this.end.getX(), this.end.getY());
};
LineFigure.prototype.touches = function(testPoint) {
return Geometry.linePointAppxIntersect(this.start, this.end, testPoint);
};
/*
* Line location is considered to be the middle of the line
*/
LineFigure.prototype.setLocation = function(point) {
var xdiff = point.getX() - this.mid.getX();
var ydiff = point.getY() - this.mid.getY();
this.start.translate(xdiff, ydiff);
this.end.translate(xdiff, ydiff);
this.mid.translate(xdiff, ydiff);
};
LineFigure.prototype.getLocation = function() {
return this.mid;
};
LineFigure.prototype.getBounds = function() {
return new Rectangle(this.mid, this.end.getX() - this.start.getX(), this.end.getY() - this.start.getY());
};
LineFigure.prototype.toString = function() {
return 'LineFigure:' + this.start + '@' + this.end;
};