Skip to content

Commit 151b29d

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 8baac47 commit 151b29d

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/components/legend/draw.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -271,36 +271,39 @@ module.exports = function draw(gd) {
271271
scrollBox.attr('data-scroll',0);
272272
}
273273

274-
scrollHandler(0,legendHeight);
274+
var scrollBarYMax = legendHeight - constants.scrollBarHeight,
275+
scrollBoxYMax = opts.height - legendHeight;
275276

276-
legend.on('wheel',null);
277+
scrollHandler(0, 0);
277278

279+
legend.on('wheel',null);
278280
legend.on('wheel', function() {
279-
var e = d3.event;
280-
e.preventDefault();
281-
scrollHandler(e.deltaY / 20, legendHeight);
281+
var scrollBoxY = Lib.constrain(
282+
scrollBox.attr('data-scroll') -
283+
d3.event.deltaY / scrollBarYMax * scrollBoxYMax,
284+
-scrollBoxYMax, 0),
285+
scrollBarY = - scrollBoxY / scrollBoxYMax * scrollBarYMax;
286+
scrollHandler(scrollBarY, scrollBoxY);
287+
d3.event.preventDefault();
282288
});
283289

284290
scrollBar.on('.drag',null);
285291
scrollBox.on('.drag',null);
286-
var drag = d3.behavior.drag()
287-
.on('drag', function() {
288-
scrollHandler(d3.event.dy, legendHeight);
289-
});
292+
var drag = d3.behavior.drag().on('drag', function() {
293+
var scrollBarY = Lib.constrain(
294+
d3.event.y - constants.scrollBarHeight / 2,
295+
0, scrollBarYMax),
296+
scrollBoxY = - scrollBarY / scrollBarYMax * scrollBoxYMax;
297+
scrollHandler(scrollBarY, scrollBoxY);
298+
});
290299

291300
scrollBar.call(drag);
292301
scrollBox.call(drag);
293302

294303
}
295304

296305

297-
function scrollHandler(delta, legendHeight) {
298-
299-
var scrollBarTrack = legendHeight - constants.scrollBarHeight - 2 * constants.scrollBarMargin,
300-
translateY = scrollBox.attr('data-scroll'),
301-
scrollBoxY = Lib.constrain(translateY - delta, legendHeight-opts.height, 0),
302-
scrollBarY = -scrollBoxY / (opts.height - legendHeight) * scrollBarTrack + constants.scrollBarMargin;
303-
306+
function scrollHandler(scrollBarY, scrollBoxY) {
304307
scrollBox.attr('data-scroll', scrollBoxY);
305308
scrollBox.attr('transform', 'translate(0, ' + scrollBoxY + ')');
306309
scrollBar.call(

0 commit comments

Comments
 (0)