mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Cache event offset coordinates (#7795)
This commit is contained in:
parent
2563ff174e
commit
2f888172d4
@ -993,7 +993,9 @@ class Chart {
|
||||
}
|
||||
};
|
||||
|
||||
let listener = function(e) {
|
||||
let listener = function(e, x, y) {
|
||||
e.offsetX = x;
|
||||
e.offsetY = y;
|
||||
me._eventHandler(e);
|
||||
};
|
||||
|
||||
|
||||
@ -93,7 +93,7 @@ export function getRelativePosition(evt, chart) {
|
||||
const e = evt.originalEvent || evt;
|
||||
const touches = e.touches;
|
||||
const source = touches && touches.length ? touches[0] : e;
|
||||
const {offsetX, offsetY, layerX, layerY, target} = source;
|
||||
const {offsetX, offsetY} = source;
|
||||
|
||||
if (offsetX > 0 || offsetY > 0) {
|
||||
return {
|
||||
@ -102,13 +102,6 @@ export function getRelativePosition(evt, chart) {
|
||||
};
|
||||
}
|
||||
|
||||
if (layerX > 0 || layerY > 0) {
|
||||
return {
|
||||
x: layerX - target.offsetLeft,
|
||||
y: layerY - target.offsetTop
|
||||
};
|
||||
}
|
||||
|
||||
return calculateRelativePositionFromClientXY(source, chart);
|
||||
}
|
||||
|
||||
|
||||
@ -19,13 +19,15 @@ export const requestAnimFrame = (function() {
|
||||
* Latest argments are used on the actual call
|
||||
* @param {function} fn
|
||||
* @param {*} thisArg
|
||||
* @param {function} [updateFn]
|
||||
*/
|
||||
export function throttled(fn, thisArg) {
|
||||
export function throttled(fn, thisArg, updateFn) {
|
||||
const updateArgs = updateFn || ((args) => Array.prototype.slice.call(args));
|
||||
let ticking = false;
|
||||
let args = [];
|
||||
|
||||
return function(...rest) {
|
||||
args = Array.prototype.slice.call(rest);
|
||||
args = updateArgs(rest);
|
||||
|
||||
if (!ticking) {
|
||||
ticking = true;
|
||||
|
||||
@ -245,7 +245,10 @@ function createProxyAndListen(chart, type, listener) {
|
||||
if (chart.ctx !== null) {
|
||||
listener(fromNativeEvent(event, chart));
|
||||
}
|
||||
}, chart);
|
||||
}, chart, (args) => {
|
||||
const event = args[0];
|
||||
return [event, event.offsetX, event.offsetY];
|
||||
});
|
||||
|
||||
addListener(canvas, type, proxy);
|
||||
|
||||
|
||||
@ -263,33 +263,7 @@ describe('DOM helpers tests', function() {
|
||||
expect(helpers.getRelativePosition(event, chart)).toEqual({x: 0, y: 10});
|
||||
});
|
||||
|
||||
it('should use layerX/Y - target offsets when available', function() {
|
||||
const chart = undefined;
|
||||
|
||||
const event1 = {
|
||||
layerX: 0,
|
||||
layerY: 10,
|
||||
target: {
|
||||
offsetLeft: 0,
|
||||
offsetTop: 5
|
||||
}
|
||||
};
|
||||
expect(helpers.getRelativePosition(event1, chart)).toEqual({x: 0, y: 5});
|
||||
|
||||
const event2 = {
|
||||
offsetX: 0,
|
||||
offsetY: 0,
|
||||
layerX: 10,
|
||||
layerY: 10,
|
||||
target: {
|
||||
offsetLeft: 0,
|
||||
offsetTop: 5
|
||||
}
|
||||
};
|
||||
expect(helpers.getRelativePosition(event2, chart)).toEqual({x: 10, y: 5});
|
||||
});
|
||||
|
||||
it('should calculate from clientX/Y if offset/layer coords are not available', function() {
|
||||
it('should calculate from clientX/Y as fallback', function() {
|
||||
const chart = window.acquireChart({}, {
|
||||
canvas: {
|
||||
height: 200,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user