Skip to content

Commit ab15b52

Browse files
committed
Improve drag and wheel of legend scrollbars
* Now drag events make the scrollbar follow the mouse position. * Now wheel events move the scrollbar position as fast as drag events.
1 parent b747224 commit ab15b52

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/components/legend/draw.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,36 +265,39 @@ module.exports = function draw(gd) {
265265
scrollBox.attr('data-scroll',0);
266266
}
267267

268-
scrollHandler(0,legendHeight);
268+
var scrollBarYMax = legendHeight - constants.scrollBarHeight,
269+
scrollBoxYMax = opts.height - legendHeight;
269270

270-
legend.on('wheel',null);
271+
scrollHandler(0, 0);
271272

273+
legend.on('wheel',null);
272274
legend.on('wheel', function() {
273-
var e = d3.event;
274-
e.preventDefault();
275-
scrollHandler(e.deltaY / 20, legendHeight);
275+
var scrollBoxY = Lib.constrain(
276+
scrollBox.attr('data-scroll') -
277+
d3.event.deltaY / scrollBarYMax * scrollBoxYMax,
278+
-scrollBoxYMax, 0),
279+
scrollBarY = - scrollBoxY / scrollBoxYMax * scrollBarYMax;
280+
scrollHandler(scrollBarY, scrollBoxY);
281+
d3.event.preventDefault();
276282
});
277283

278284
scrollBar.on('.drag',null);
279285
scrollBox.on('.drag',null);
280-
var drag = d3.behavior.drag()
281-
.on('drag', function() {
282-
scrollHandler(d3.event.dy, legendHeight);
283-
});
286+
var drag = d3.behavior.drag().on('drag', function() {
287+
var scrollBarY = Lib.constrain(
288+
d3.event.y - constants.scrollBarHeight / 2,
289+
0, scrollBarYMax),
290+
scrollBoxY = - scrollBarY / scrollBarYMax * scrollBoxYMax;
291+
scrollHandler(scrollBarY, scrollBoxY);
292+
});
284293

285294
scrollBar.call(drag);
286295
scrollBox.call(drag);
287296

288297
}
289298

290299

291-
function scrollHandler(delta, legendHeight) {
292-
293-
var scrollBarTrack = legendHeight - constants.scrollBarHeight - 2 * constants.scrollBarMargin,
294-
translateY = scrollBox.attr('data-scroll'),
295-
scrollBoxY = Lib.constrain(translateY - delta, legendHeight-opts.height, 0),
296-
scrollBarY = -scrollBoxY / (opts.height - legendHeight) * scrollBarTrack + constants.scrollBarMargin;
297-
300+
function scrollHandler(scrollBarY, scrollBoxY) {
298301
scrollBox.attr('data-scroll', scrollBoxY);
299302
scrollBox.attr('transform', 'translate(0, ' + scrollBoxY + ')');
300303
scrollBar.call(

0 commit comments

Comments
 (0)