-
Notifications
You must be signed in to change notification settings - Fork 1
/
rectangle_interactor.js
214 lines (190 loc) · 6.12 KB
/
rectangle_interactor.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
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
"use strict";
function rectangleInteractor(state, x, y) {
// x, y are d3.scale objects (linear, log, etc) from parent
// dispatch is the d3 event dispatcher: should have event "update" register
//var state = options;
var name = state.name;
var radius = ( state.radius == null ) ? 5 : state.radius;
var event_name = "rectangle." + state.name;
var dispatch = d3.dispatch("update");
var x = x || d3.scale.linear();
var y = y || d3.scale.linear();
var show_points = (state.show_points == null) ? true : state.show_points;
var show_lines = (state.show_lines == null) ? true : state.show_lines;
var show_center = (state.show_center == null) ? true : state.show_center;
var fixed = (state.fixed == null) ? false : state.fixed;
var cursor = (fixed) ? "auto" : "move";
var line = d3.svg.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); });
var state_to_pairs = function(state) {
// convert from xmin, xmax... to pairs of points for rectangle
if (show_lines) {
return [
[[state.xmin, state.ymin], [state.xmax, state.ymin]],
[[state.xmax, state.ymin], [state.xmax, state.ymax]],
[[state.xmax, state.ymax], [state.xmin, state.ymax]],
[[state.xmin, state.ymax], [state.xmin, state.ymin]]
]
}
else {
return [];
}
}
var state_to_points = function(state) {
if (show_points) {
return [
[state.xmin, state.ymin],
[state.xmax, state.ymin],
[state.xmax, state.ymax],
[state.xmin, state.ymax],
]
}
else {
return [];
}
}
var state_to_center = function(state) {
if (show_center) {
return [
[x.invert((x(state.xmax) + x(state.xmin)) / 2.0),
y.invert((y(state.ymax) + y(state.ymin)) / 2.0)]
]
}
else {
return [];
}
}
var drag_corner = d3.behavior.drag()
.on("drag", dragmove_corner)
.on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); });
var drag_center = d3.behavior.drag()
.on("drag", dragmove_center)
.on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); });
var drag_edge = d3.behavior.drag()
.on("drag", dragmove_edge)
.on("dragstart", function() { d3.event.sourceEvent.stopPropagation(); });
function interactor(selection) {
var group = selection.append("g")
.classed("interactors interactor-" + name, true)
.style("cursor", cursor)
var edges = group.append("g")
.attr("class", "edges")
.style("stroke", state.color1)
.style("stroke-linecap", "round")
.selectAll(".edge")
.data(state_to_pairs(state))
.enter().append("path")
.classed("edge", true)
.attr("side", function(d,i) { return i.toFixed()})
.attr("fill", "none")
.attr("stroke-width", "4px")
.attr("d", line)
if (!fixed) edges.call(drag_edge);
var corners = group.append("g")
.classed("corners", true)
.attr("fill", state.color1)
.selectAll("corner")
.data(state_to_points(state))
.enter().append("circle")
.classed("corner", true)
.attr("vertex", function(d,i) { return i.toFixed()})
.attr("r", radius)
.attr("cx", function(d) {return x(d[0])})
.attr("cy", function(d) {return y(d[1])})
if (!fixed) corners.call(drag_corner);
var center_group = group.append("g")
.classed("center_group", true)
.attr("fill", state.color1)
.selectAll("center")
.data(state_to_center(state))
.enter().append("circle")
.classed("center", true)
.attr("r", radius)
.attr("cx", function(d) {return x(d[0])})
.attr("cy", function(d) {return y(d[1])})
if (!fixed) center_group.call(drag_center);
interactor.update = function() {
group.selectAll('.center').data(state_to_center(state))
.attr("cx", function(d) { return x(d[0]); })
.attr("cy", function(d) { return y(d[1]); });
group.selectAll('.corner').data(state_to_points(state))
.attr("cx", function(d) { return x(d[0]); })
.attr("cy", function(d) { return y(d[1]); });
group.selectAll('.edge').data(state_to_pairs(state))
.attr("d", line);
// fire!
dispatch.update();
}
}
function dragmove_center() {
state.xmin = x.invert(x(state.xmin) + d3.event.dx);
state.xmax = x.invert(x(state.xmax) + d3.event.dx);
state.ymin = y.invert(y(state.ymin) + d3.event.dy);
state.ymax = y.invert(y(state.ymax) + d3.event.dy);
interactor.update();
}
function dragmove_corner(d) {
var new_x = x.invert(d3.event.x),
new_y = y.invert(d3.event.y);
var vertex = parseInt(d3.select(this).attr("vertex"));
// enforce relationship between corners:
switch (vertex) {
case 0:
state.xmin = new_x;
state.ymin = new_y;
break
case 1:
state.xmax = new_x;
state.ymin = new_y;
break
case 2:
state.xmax = new_x;
state.ymax = new_y;
break
case 3:
state.xmin = new_x;
state.ymax = new_y;
break
default:
console.log("default", d3.event, d3.select(this));
}
interactor.update();
}
function dragmove_edge() {
var new_x = x.invert(d3.event.x),
new_y = y.invert(d3.event.y);
var side = parseInt(d3.select(this).attr("side"));
// enforce relationship between edges and corners:
switch (side) {
case 0:
state.ymin = new_y;
break
case 1:
state.xmax = new_x;
break
case 2:
state.ymax = new_y;
break
case 3:
state.xmin = new_x;
break
default:
console.log("default", d3.event, d3.select(this));
}
interactor.update();
}
interactor.x = function(_) {
if (!arguments.length) return x;
x = _;
return interactor;
};
interactor.y = function(_) {
if (!arguments.length) return y;
y = _;
return interactor;
};
interactor.state = state;
interactor.dispatch = dispatch;
return interactor
}