Skip to content

Commit

Permalink
Use keypress events for arrow zooming.
Browse files Browse the repository at this point in the history
The plus and minus key codes are not consistent across browsers. The char codes
appear to be more consistent, so we use those; note that this also has the
side-effect of enabling repeated zooming if the plus or minus key is held down.
This makes zooming more consistent with the current (custom repeating) pan
behavior.
  • Loading branch information
Mike Bostock committed Aug 31, 2010
1 parent 3f591e7 commit a9137c1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 104 deletions.
40 changes: 14 additions & 26 deletions polymaps.js
Expand Up @@ -1351,7 +1351,7 @@ po.wheel = function() {
var bug40441 = /WebKit\/533/.test(navigator.userAgent) ? -1 : 0;
po.arrow = function() {
var arrow = {},
key = {left: 0, right: 0, up: 0, down: 0, plus: 0, minus: 0},
key = {left: 0, right: 0, up: 0, down: 0},
last = 0,
repeatTimer,
repeatDelay = 250,
Expand All @@ -1362,7 +1362,7 @@ po.arrow = function() {

function keydown(e) {
if (e.ctrlKey || e.altKey || e.metaKey) return;
var now = Date.now(), dx = 0, dy = 0, dz = 0;
var now = Date.now(), dx = 0, dy = 0;
switch (e.keyCode) {
case 37: {
if (!key.left) {
Expand Down Expand Up @@ -1396,30 +1396,9 @@ po.arrow = function() {
}
break;
}
case 109: case 189: {
if (!key.plus) {
last = now;
key.plus = 1;
if (!key.minus) dz = -1;
}
break;
}
case 61: case 187: {
if (!key.minus) {
last = now;
key.minus = 1;
if (!key.plus) dz = 1;
}
break;
}
default: return;
}
if (dz) {
var z = map.zoom();
map.zoom(dz < 0 ? Math.ceil(z) - 1 : Math.floor(z) + 1);
} else if (dx || dy) {
map.panBy({x: dx, y: dy});
}
if (dx || dy) map.panBy({x: dx, y: dy});
if (!repeatTimer && (key.left | key.right | key.up | key.down)) {
repeatTimer = setInterval(repeat, repeatInterval);
}
Expand All @@ -1433,8 +1412,6 @@ po.arrow = function() {
case 39: key.right = 0; break;
case 38: key.up = 0; break;
case 40: key.down = 0; break;
case 109: case 189: key.plus = 0; break;
case 61: case 187: key.minus = 0; break;
default: return;
}
if (repeatTimer && !(key.left | key.right | key.up | key.down)) {
Expand All @@ -1443,6 +1420,15 @@ po.arrow = function() {
e.preventDefault();
}

function keypress(e) {
switch (e.charCode) {
case 45: case 95: map.zoom(Math.ceil(map.zoom()) - 1); break;
case 43: case 61: map.zoom(Math.floor(map.zoom()) + 1); break;
default: return;
}
e.preventDefault();
}

function repeat() {
if (!map) return;
if (Date.now() < last + repeatDelay) return;
Expand All @@ -1454,12 +1440,14 @@ po.arrow = function() {
arrow.map = function(x) {
if (!arguments.length) return map;
if (map) {
parent.removeEventListener("keypress", keypress, false);
parent.removeEventListener("keydown", keydown, false);
parent.removeEventListener("keyup", keyup, false);
parent = null;
}
if (map = x) {
parent = map.focusableParent();
parent.addEventListener("keypress", keypress, false);
parent.addEventListener("keydown", keydown, false);
parent.addEventListener("keyup", keyup, false);
}
Expand Down

0 comments on commit a9137c1

Please sign in to comment.