Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Fil
Created September 21, 2014 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fil/2768b0b0d23cd0085f86 to your computer and use it in GitHub Desktop.
Save Fil/2768b0b0d23cd0085f86 to your computer and use it in GitHub Desktop.
d3 zoom and pan from the keyboard
d3.select('body').on('keydown', function () {
var step = 200;
var key = d3.event.key || d3.event.keyCode; // safari doesn't know .key
switch (key) {
case 'Esc':
case 27:
zoom.translate([0, 0]).scale(1).event(mapsvg.transition());
break;
case '+':
case '=':
case 187:
zoomByFactor(2);
break;
case '-':
case 189:
zoomByFactor(1 / 2);
break;
case 'Left':
case 37:
zoom.translate([zoom.translate()[0] + step, zoom.translate()[1]]).event(mapsvg.transition());
break;
case 'Right':
case 39:
zoom.translate([zoom.translate()[0] - step, zoom.translate()[1]]).event(mapsvg.transition());
break;
case 'Up':
case 38:
zoom.translate([zoom.translate()[0], zoom.translate()[1] + step]).event(mapsvg.transition());
break;
case 'Down':
case 40:
zoom.translate([zoom.translate()[0], zoom.translate()[1] - step]).event(mapsvg.transition());
break;
default:
debug('key pressed: \'' + key + '\'');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment