Skip to content

Commit f0e8e98

Browse files
committed
move mouse position getter to lib and work-around Firefox bug (#5525)
1 parent 7c23483 commit f0e8e98

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/lib/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,18 @@ lib.isIOS = function() {
715715
return IS_IOS_REGEX.test(window.navigator.userAgent);
716716
};
717717

718+
var FIREFOX_VERSION_REGEX = /Firefox\/(\d+)\.\d+/;
719+
lib.getFirefoxVersion = function() {
720+
var match = FIREFOX_VERSION_REGEX.exec(window.navigator.userAgent);
721+
if(match && match.length === 2) {
722+
var versionInt = parseInt(match[1]);
723+
if(!Number.isNaN(versionInt)) {
724+
return versionInt;
725+
}
726+
}
727+
return null;
728+
};
729+
718730
lib.isD3Selection = function(obj) {
719731
return obj instanceof d3.selection;
720732
};
@@ -1263,3 +1275,28 @@ lib.join2 = function(arr, mainSeparator, lastSeparator) {
12631275
}
12641276
return arr.join(mainSeparator);
12651277
};
1278+
1279+
/**
1280+
* Return the mouse position from the last event registered by D3.
1281+
* @returns An array with two numbers, representing the x and y coordinates of the mouse pointer
1282+
* at the event relative to the targeted node.
1283+
*/
1284+
lib.getPositionFromD3Event = function() {
1285+
var firefoxVersion = lib.getFirefoxVersion();
1286+
1287+
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1684973
1288+
var isProblematicFirefox = firefoxVersion && firefoxVersion < 86;
1289+
1290+
if(isProblematicFirefox) {
1291+
// layerX and layerY are non-standard, so we only fallback to them when we have to:
1292+
return [
1293+
d3.event.layerX,
1294+
d3.event.layerY
1295+
];
1296+
} else {
1297+
return [
1298+
d3.event.offsetX,
1299+
d3.event.offsetY
1300+
];
1301+
}
1302+
};

src/plots/geo/geo.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,7 @@ proto.updateFx = function(fullLayout, geoLayout) {
506506
}
507507

508508
bgRect.on('mousemove', function() {
509-
var d3EventPosition = [
510-
d3.event.offsetX,
511-
d3.event.offsetY,
512-
];
513-
var lonlat = _this.projection.invert(d3EventPosition);
509+
var lonlat = _this.projection.invert(Lib.getPositionFromD3Event());
514510

515511
if(!lonlat || isNaN(lonlat[0]) || isNaN(lonlat[1])) {
516512
return dragElement.unhover(gd, d3.event);

0 commit comments

Comments
 (0)