Skip to content

Commit c96581c

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 c96581c

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/components/legend/draw.js

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

274-
scrollHandler(0,legendHeight);
274+
var scrollBarYMax = legendHeight -
275+
constants.scrollBarHeight -
276+
2 * constants.scrollBarMargin,
277+
scrollBoxYMax = opts.height - legendHeight,
278+
scrollBarY = constants.scrollBarMargin,
279+
scrollBoxY = 0;
275280

276-
legend.on('wheel',null);
281+
scrollHandler(scrollBarY, scrollBoxY);
277282

283+
legend.on('wheel',null);
278284
legend.on('wheel', function() {
279-
var e = d3.event;
280-
e.preventDefault();
281-
scrollHandler(e.deltaY / 20, legendHeight);
285+
scrollBoxY = Lib.constrain(
286+
scrollBox.attr('data-scroll') -
287+
d3.event.deltaY / scrollBarYMax * scrollBoxYMax,
288+
-scrollBoxYMax, 0);
289+
scrollBarY = constants.scrollBarMargin -
290+
scrollBoxY / scrollBoxYMax * scrollBarYMax;
291+
scrollHandler(scrollBarY, scrollBoxY);
292+
d3.event.preventDefault();
282293
});
283294

284295
scrollBar.on('.drag',null);
285296
scrollBox.on('.drag',null);
286-
var drag = d3.behavior.drag()
287-
.on('drag', function() {
288-
scrollHandler(d3.event.dy, legendHeight);
289-
});
297+
var drag = d3.behavior.drag().on('drag', function() {
298+
scrollBarY = Lib.constrain(
299+
d3.event.y - constants.scrollBarHeight / 2,
300+
constants.scrollBarMargin,
301+
constants.scrollBarMargin + scrollBarYMax);
302+
scrollBoxY = - (scrollBarY - constants.scrollBarMargin) /
303+
scrollBarYMax * scrollBoxYMax;
304+
scrollHandler(scrollBarY, scrollBoxY);
305+
});
290306

291307
scrollBar.call(drag);
292308
scrollBox.call(drag);
293309

294310
}
295311

296312

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-
313+
function scrollHandler(scrollBarY, scrollBoxY) {
304314
scrollBox.attr('data-scroll', scrollBoxY);
305315
scrollBox.attr('transform', 'translate(0, ' + scrollBoxY + ')');
306316
scrollBar.call(

0 commit comments

Comments
 (0)