|
1078 | 1078 | y -= rect_height(); |
1079 | 1079 | return y; |
1080 | 1080 | } |
| 1081 | + |
| 1082 | + var acc_x = 0, acc_y = 0, pad_w = 1, pad_h = 1; |
1081 | 1083 |
|
1082 | 1084 | var drag_move = d3.behavior.drag().origin(Object) |
1083 | 1085 | .on("dragstart", function() { |
1084 | 1086 | d3.event.sourceEvent.preventDefault(); |
1085 | 1087 |
|
| 1088 | + acc_x = 0; acc_y = 0; |
| 1089 | + pad_w = Number(pthis.svg_pad(true).attr("width")) - rect_width(); |
| 1090 | + pad_h = Number(pthis.svg_pad(true).attr("height")) - rect_height(); |
| 1091 | + |
1086 | 1092 | pthis[drag_rect_name] = |
1087 | 1093 | pthis.svg_pad(true) |
1088 | 1094 | .append("rect") |
|
1095 | 1101 | .style("cursor", "move"); |
1096 | 1102 | }).on("drag", function() { |
1097 | 1103 | d3.event.sourceEvent.preventDefault(); |
1098 | | - pthis[drag_rect_name].attr("x", Number(pthis[drag_rect_name].attr("x")) + d3.event.dx); |
1099 | | - pthis[drag_rect_name].attr("y", Number(pthis[drag_rect_name].attr("y")) + d3.event.dy); |
| 1104 | + |
| 1105 | + var x = Number(pthis[drag_rect_name].attr("x")); |
| 1106 | + var y = Number(pthis[drag_rect_name].attr("y")); |
| 1107 | + var dx = d3.event.dx, dy = d3.event.dy; |
| 1108 | + |
| 1109 | + if (((acc_x<0) && (dx>0)) || ((acc_x>0) && (dx<0))) { acc_x += dx; dx = 0; } |
| 1110 | + if (((acc_y<0) && (dy>0)) || ((acc_y>0) && (dy<0))) { acc_y += dy; dy = 0; } |
| 1111 | + |
| 1112 | + if ((x + dx < 0) || (x +dx > pad_w)) acc_x += dx; else x+=dx; |
| 1113 | + if ((y+dy < 0) || (y+dy > pad_h)) acc_y += dy; else y += dy; |
| 1114 | + |
| 1115 | + pthis[drag_rect_name].attr("x", x); |
| 1116 | + pthis[drag_rect_name].attr("y", y); |
1100 | 1117 | d3.event.sourceEvent.stopPropagation(); |
1101 | 1118 | }).on("dragend", function() { |
1102 | 1119 | d3.event.sourceEvent.preventDefault(); |
1103 | | - |
| 1120 | + |
1104 | 1121 | pthis[drag_rect_name].style("cursor", "auto"); |
1105 | 1122 |
|
1106 | 1123 | var x = Number(pthis[drag_rect_name].attr("x")); |
|
1133 | 1150 | var drag_resize = d3.behavior.drag().origin(Object) |
1134 | 1151 | .on( "dragstart", function() { |
1135 | 1152 | d3.event.sourceEvent.preventDefault(); |
| 1153 | + acc_x = 0; acc_y = 0; |
| 1154 | + pad_w = Number(pthis.svg_pad(true).attr("width")) - rect_x(); |
| 1155 | + pad_h = Number(pthis.svg_pad(true).attr("height")) - rect_y(); |
1136 | 1156 | pthis[drag_rect_name] = |
1137 | 1157 | pthis.svg_pad(true) |
1138 | 1158 | .append("rect") |
|
1146 | 1166 | // main_rect.style("cursor", "move"); |
1147 | 1167 | }).on("drag", function() { |
1148 | 1168 | d3.event.sourceEvent.preventDefault(); |
1149 | | - pthis[drag_rect_name].attr("width", Number(pthis[drag_rect_name].attr("width")) + d3.event.dx); |
1150 | | - pthis[drag_rect_name].attr("height", Number(pthis[drag_rect_name].attr("height")) + d3.event.dy); |
| 1169 | + |
| 1170 | + var w = Number(pthis[drag_rect_name].attr("width")); |
| 1171 | + var h = Number(pthis[drag_rect_name].attr("height")); |
| 1172 | + var dx = d3.event.dx, dy = d3.event.dy; |
| 1173 | + if ((acc_x>0) && (dx<0)) { acc_x += dx; dx = 0; } |
| 1174 | + if ((acc_y>0) && (dy<0)) { acc_y += dy; dy = 0; } |
| 1175 | + if (w+dx > pad_w) acc_x += dx; else w+=dx; |
| 1176 | + if (h+dy > pad_h) acc_y += dy; else h+=dy; |
| 1177 | + pthis[drag_rect_name].attr("width", w); |
| 1178 | + pthis[drag_rect_name].attr("height", h); |
1151 | 1179 | d3.event.sourceEvent.stopPropagation(); |
1152 | 1180 | }).on( "dragend", function() { |
1153 | 1181 | d3.event.sourceEvent.preventDefault(); |
|
0 commit comments