Skip to content

Commit 2b5a165

Browse files
authored
Fix handling at canvas edge (#898)
1 parent 2dabfe4 commit 2b5a165

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/handlers.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ function keyDown(chart, event) {
5858
}
5959

6060
function getPointPosition(event, chart) {
61-
const point = getRelativePosition(event, chart);
62-
const canvasArea = chart.canvas.getBoundingClientRect();
63-
if (!_isPointInArea(event, canvasArea)) {
64-
point.x = event.clientX - canvasArea.left;
65-
point.y = event.clientY - canvasArea.top;
61+
if (event.target !== chart.canvas) {
62+
const canvasArea = chart.canvas.getBoundingClientRect();
63+
return {
64+
x: event.clientX - canvasArea.left,
65+
y: event.clientY - canvasArea.top,
66+
};
6667
}
67-
return point;
68+
return getRelativePosition(event, chart);
6869
}
6970

7071
function zoomStart(chart, event, zoomOptions) {

test/specs/zoom.drag.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,63 @@ describe('zoom with drag', function() {
450450
expect(scaleY.options.min).toBeCloseTo(1.2);
451451
expect(scaleY.options.max).toBeCloseTo(1.7);
452452
});
453+
454+
it('handles dragging off the right edge of the chart canvas', function() {
455+
chart = window.acquireChart({
456+
type: 'line',
457+
data,
458+
options: {
459+
scales: {
460+
xScale0: {
461+
id: 'xScale0',
462+
type: 'linear',
463+
min: 0,
464+
max: 4
465+
},
466+
yScale0: {
467+
id: 'yScale0',
468+
type: 'linear',
469+
min: 0,
470+
max: 4,
471+
}
472+
},
473+
plugins: {
474+
zoom: {
475+
zoom: {
476+
drag: {
477+
enabled: true
478+
},
479+
mode: 'xy'
480+
}
481+
}
482+
}
483+
}
484+
}, {
485+
wrapper: {style: 'position: absolute; left: 50px; top: 50px;'}
486+
});
487+
488+
scaleX = chart.scales.xScale0;
489+
scaleY = chart.scales.yScale0;
490+
491+
jasmine.triggerMouseEvent(chart, 'mousedown', {
492+
x: scaleX.getPixelForValue(1.5),
493+
y: scaleY.getPixelForValue(1.2)
494+
});
495+
496+
const rect = chart.canvas.getBoundingClientRect();
497+
document.documentElement.dispatchEvent(new MouseEvent('mouseup', {
498+
clientX: rect.right,
499+
clientY: rect.top + scaleY.getPixelForValue(1.7),
500+
cancelable: true,
501+
bubbles: true,
502+
view: window
503+
}));
504+
505+
expect(scaleX.options.min).toBeCloseTo(1.5);
506+
expect(scaleX.options.max).toBeCloseTo(4);
507+
expect(scaleY.options.min).toBeCloseTo(1.2);
508+
expect(scaleY.options.max).toBeCloseTo(1.7);
509+
});
453510
});
454511

455512
describe('events', function() {

0 commit comments

Comments
 (0)