Skip to content

Commit 699b496

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 699b496

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/components/legend/draw.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,36 +265,40 @@ 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+
console.log('@@', d3.event.deltaY, scrollBarY, scrollBoxY, scrollBarYMax, scrollBoxYMax);
281+
scrollHandler(scrollBarY, scrollBoxY);
282+
d3.event.preventDefault();
276283
});
277284

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

285295
scrollBar.call(drag);
286296
scrollBox.call(drag);
287297

288298
}
289299

290300

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-
301+
function scrollHandler(scrollBarY, scrollBoxY) {
298302
scrollBox.attr('data-scroll', scrollBoxY);
299303
scrollBox.attr('transform', 'translate(0, ' + scrollBoxY + ')');
300304
scrollBar.call(

0 commit comments

Comments
 (0)