Created
September 21, 2014 20:37
-
-
Save Fil/2768b0b0d23cd0085f86 to your computer and use it in GitHub Desktop.
d3 zoom and pan from the keyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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