Skip to content

Commit

Permalink
Merge branch 'fix-zoom-touch' into 3.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 1, 2013
2 parents af85c4e + 43f439e commit b563495
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 53 deletions.
58 changes: 36 additions & 22 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,9 +1152,9 @@ d3 = function() {
return d3.rebind(drag, event, "on");
};
d3.behavior.zoom = function() {
var translate = [ 0, 0 ], translate0, scale = 1, scaleExtent = d3_behavior_zoomInfinity, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", event = d3_eventDispatch(zoom, "zoom"), x0, x1, y0, y1, touchtime;
var translate = [ 0, 0 ], translate0, scale = 1, scaleExtent = d3_behavior_zoomInfinity, mousedown = "mousedown.zoom", mousemove = "mousemove.zoom", mouseup = "mouseup.zoom", touchstart = "touchstart.zoom", touchmove = "touchmove.zoom", touchend = "touchend.zoom", touchtime, event = d3_eventDispatch(zoom, "zoom"), x0, x1, y0, y1;
function zoom() {
this.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on(mousemove, mousewheelreset).on("dblclick.zoom", dblclicked).on("touchstart.zoom", touchstarted);
this.on(mousedown, mousedowned).on(d3_behavior_zoomWheel + ".zoom", mousewheeled).on(mousemove, mousewheelreset).on("dblclick.zoom", dblclicked).on(touchstart, touchstarted);
}
zoom.translate = function(x) {
if (!arguments.length) return translate;
Expand Down Expand Up @@ -1232,27 +1232,37 @@ d3 = function() {
}
}
function touchstarted() {
var target = this, event_ = event.of(target, arguments), touches = d3.touches(target), locations = {}, distance0 = 0, scale0 = scale, now = Date.now(), name = "zoom-" + d3.event.changedTouches[0].identifier, touchmove = "touchmove." + name, touchend = "touchend." + name, w = d3.select(d3_window).on(touchmove, moved).on(touchend, ended), t = d3.select(target).on(mousedown, null), dragRestore = d3_event_dragSuppress();
touches.forEach(function(t) {
locations[t.identifier] = location(t);
});
if (touches.length === 1) {
if (now - touchtime < 500) {
var p = touches[0], l = location(touches[0]);
scaleTo(scale * 2);
translateTo(p, l);
d3_eventPreventDefault();
dispatch(event_);
var target = this, event_ = event.of(target, arguments), locations0, distance0 = 0, scale0, w = d3.select(d3_window).on(touchmove, moved).on(touchend, ended), t = d3.select(target).on(mousedown, null).on(touchstart, started), dragRestore = d3_event_dragSuppress();
started();
function relocate() {
var touches = d3.touches(target);
scale0 = scale;
locations0 = {};
touches.forEach(function(t) {
locations0[t.identifier] = location(t);
});
return touches;
}
function started() {
var now = Date.now(), touches = relocate();
if (touches.length === 1) {
if (now - touchtime < 500) {
var p = touches[0], l = locations0[p.identifier];
scaleTo(scale * 2);
translateTo(p, l);
d3_eventPreventDefault();
dispatch(event_);
}
touchtime = now;
} else if (touches.length > 1) {
var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1];
distance0 = dx * dx + dy * dy;
}
touchtime = now;
} else if (touches.length > 1) {
var p = touches[0], q = touches[1], dx = p[0] - q[0], dy = p[1] - q[1];
distance0 = dx * dx + dy * dy;
}
function moved() {
var touches = d3.touches(target), p0 = touches[0], l0 = locations[p0.identifier];
var touches = d3.touches(target), p0 = touches[0], l0 = locations0[p0.identifier];
if (p1 = touches[1]) {
var p1, l1 = locations[p1.identifier], scale1 = d3.event.scale;
var p1, l1 = locations0[p1.identifier], scale1 = d3.event.scale;
if (scale1 == null) {
var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1;
scale1 = distance0 && Math.sqrt(distance1 / distance0);
Expand All @@ -1266,9 +1276,13 @@ d3 = function() {
dispatch(event_);
}
function ended() {
w.on(touchmove, null).on(touchend, null);
t.on(mousedown, mousedowned);
dragRestore();
if (d3.event.touches.length) {
relocate();
} else {
w.on(touchmove, null).on(touchend, null);
t.on(mousedown, mousedowned).on(touchstart, touchstarted);
dragRestore();
}
}
}
function mousewheeled() {
Expand Down
Loading

0 comments on commit b563495

Please sign in to comment.