From 160100e6df746b2b349d70adf09008a0f0a4c0c6 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Tue, 7 Jul 2015 10:19:38 +0200 Subject: [PATCH] forcegraph: keyboard zoom using + - --- lib/forcegraph.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/forcegraph.js b/lib/forcegraph.js index 7f80827..1c3b9da 100644 --- a/lib/forcegraph.js +++ b/lib/forcegraph.js @@ -469,6 +469,21 @@ define(["d3"], function (d3) { } } + function keyboardZoom(z) { + return function () { + var e = d3.event + + if (e.altKey || e.ctrlKey || e.metaKey) + return + + if (e.keyCode === 43) + animatePanzoom(z.translate(), z.scale() * 1.41) + + if (e.keyCode === 45) + animatePanzoom(z.translate(), z.scale() / 1.41) + } + } + el = document.createElement("div") el.classList.add("graph") self.div = el @@ -479,10 +494,11 @@ define(["d3"], function (d3) { .translate([sidebar.getWidth(), 0]) canvas = d3.select(el) - .call(zoomBehavior) .append("canvas") - .attr("pointer-events", "all") + .attr("tabindex", 1) .on("click", onClick) + .on("keypress", keyboardZoom(zoomBehavior)) + .call(zoomBehavior) .call(draggableNode) .node()