-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_draw.js
63 lines (62 loc) · 2.25 KB
/
line_draw.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
function line_draw() {
var self = this;
self.collection = [];
self.type = 'line';
self.clickHandler = function(d) {
var clickPoint = d3.mouse(this);
if (startPoint) {
self.collection.push([startPoint, getSnapPoint(clickPoint)]);
startPoint = undefined;
self.redraw();
} else {
startPoint = clickPoint;
if (beginSnap && self.collection.length > 0) {
startPoint = getSnapPoint(clickPoint);
}
}
snapTo = undefined;
};
self.mousemoveHandler = function(d) {
if(startPoint){
var mousePosition = getSnapPoint(d3.mouse(this));
vis.select(self.type + '.crnt').remove();
vis.selectAll(self.type + '.crnt')
.data([startPoint, mousePosition]).enter()
.append(self.type)
.attrs({'class': 'crnt', stroke: "white", "stroke-width": 2})
.attr("x1", function(d){return startPoint[0];})
.attr("y1", function(d){return startPoint[1];})
.attr("x2", function(d){return d[0];})
.attr("y2", function(d){return d[1];});
}
};
self.redraw = function() {
vis.selectAll(self.type).remove();
vis.selectAll(self.type)
.data(self.collection).enter()
.append(self.type)
.attrs({stroke: "white", "stroke-width": 2})
.attr("x1", function(d){return d[0][0];})
.attr("y1", function(d){return d[0][1];})
.attr("x2", function(d){return d[1][0];})
.attr("y2", function(d){return d[1][1];});
};
function getSnapPoint(mousePosition) {
if (!d3.event.ctrlKey) {
// don't snap to point if the ctrl key isn't being pressed
return mousePosition;
}
var retVal, line, i;
for( i = 0; i < self.collection.length; i++ ) {
line = self.collection[i];
if (Math.abs(mousePosition[0] - line[0][0]) <= 10 && Math.abs(mousePosition[1] - line[0][1]) <= 10) {
retVal = line[0];
} else if (Math.abs(mousePosition[0] - line[1][0]) <= 10 && Math.abs(mousePosition[1] - line[1][1]) <= 10) {
retVal = line[1];
}
}
return retVal || mousePosition;
}
}
var lineBtn = addButton('Line', function() { setShape('line'); });
shapes.line = new line_draw();