-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkData.js
112 lines (108 loc) · 3.21 KB
/
MarkData.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
function Positon(x, y) {
this.copy = function(pos) {
this.x = pos.x;
this.y = pos.y;
this.updateControl();
}
this.update = function(x, y) {
this.x = x;
this.y = y;
this.updateControl();
}
this.translate = function(dx, dy) {
this.x += dx;
this.y += dy;
this.updateControl();
}
this.setElement = function(elem) {
this.control = elem;
}
this.updateControl = function () {
if (this.control != null) {
this.control.style.transform = "translate("+this.x+"px,"+this.y+"px)";
// this.control.style.left = this.x;
// this.control.style.top = this.y;
};
}
this.update(x, y);
return this;
}
function Point(x, y) {
this.x = x;
this.y = y;
this.distanceTo = function(p) {
return Math.sqrt((p.x - this.x) * (p.x - this.x) + (p.y - this.y) * (p.y - this.y));
}
this.draw = function(ctx, vertical, dash) {
var mark_x = this.x,
mark_y = this.y;
var dst_x = this.x,
dst_y = this.y;
var offset_x = mark_handle_half_size,
offset_y = mark_handle_half_size;
if (dash) {
offset_x = mark_layer.width;
offset_y = mark_layer.height;
};
if (vertical) {
mark_x = this.x - offset_x;
dst_x = this.x + offset_x;
} else {
mark_y = this.y - offset_y;
dst_y = this.y + offset_y;
}
if (dash) {
ctx.save();
ctx.setLineDash([4, 4]);
}
ctx.beginPath();
ctx.moveTo(mark_x, mark_y);
ctx.lineTo(dst_x, dst_y);
ctx.stroke();
if (dash) {
ctx.restore();
}
}
}
function LineMark(start, end) {
this.start = start;
this.end = end;
this._cache_length = null;
this.length = function() {
if (this == current_mark) {
this._cache_length = this.start.distanceTo(this.end) + 1;
}
return this._cache_length;
}
this.isVertical = function() {
return this.end.x == this.start.x;
}
this.draw = function(ctx) {
ctx.beginPath();
ctx.moveTo(this.start.x, this.start.y);
ctx.lineTo(this.end.x, this.end.y);
ctx.stroke();
this.start.draw(ctx, this.isVertical(), this == current_mark);
this.end.draw(ctx, this.isVertical(), this == current_mark);
var text = "" + Math.floor(this.length());
var center_x = (this.start.x + this.end.x) / 2;
var center_y = (this.start.y + this.end.y) / 2;
paint_text(text, center_x, center_y, ctx);
}
}
function ColorMark(position, color, lable) {
this.color = color;
this.position = position;
this.lable = lable;
this.draw = function(ctx) {
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, 5, 0, Math.PI * 2);
ctx.moveTo(this.position.x, this.position.y);
ctx.lineTo(this.lable.x, this.lable.y);
ctx.stroke();
ctx.save();
ctx.textBaseline = (this.lable.y > this.position.y)?"top":"bottom";
paint_text(this.color, this.lable.x, this.lable.y, ctx);
ctx.restore();
}
}