Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from Cohereum/fix-offset-coordinates
Browse files Browse the repository at this point in the history
Fix for inconsistent values from nativeEvent.offsetX and Y
  • Loading branch information
Secretmapper authored Nov 2, 2019
2 parents 2838a54 + d2c42bf commit 8dc568b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
7 changes: 2 additions & 5 deletions src/hocs/OvalSelector.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const square = n => Math.pow(n, 2)
import { getCoordPercentage } from '../utils/offsetCoordinates';

const getCoordPercentage = (e) => ({
x: e.nativeEvent.offsetX / e.currentTarget.offsetWidth * 100,
y: e.nativeEvent.offsetY / e.currentTarget.offsetHeight * 100
})
const square = n => Math.pow(n, 2)

export const TYPE = 'OVAL'

Expand Down
6 changes: 1 addition & 5 deletions src/hocs/PointSelector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { getCoordPercentage } from '../utils/offsetCoordinates';
const MARGIN = 6

const getCoordPercentage = (e) => ({
x: e.nativeEvent.offsetX / e.currentTarget.offsetWidth * 100,
y: e.nativeEvent.offsetY / e.currentTarget.offsetHeight * 100
})

const marginToPercentage = (container) => ({
marginX: MARGIN / container.width * 100,
marginY: MARGIN / container.height * 100
Expand Down
5 changes: 1 addition & 4 deletions src/hocs/RectangleSelector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const getCoordPercentage = (e) => ({
x: e.nativeEvent.offsetX / e.currentTarget.offsetWidth * 100,
y: e.nativeEvent.offsetY / e.currentTarget.offsetHeight * 100
})
import { getCoordPercentage } from '../utils/offsetCoordinates';

export const TYPE = 'RECTANGLE'

Expand Down
23 changes: 23 additions & 0 deletions src/utils/offsetCoordinates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function getOffsetCoordPercentage(e, container) {
// nativeEvent.offsetX gives inconsistent results when dragging
// up and to the left rather than the more natural down and to the
// right. The reason could be browser implementation (it is still experimental)
// or it could be that nativeEvent offsets are based on target rather than
// currentTarget.
// To keep consistent behavior of the selector use the bounding client rect.
const rect = container.getBoundingClientRect();
const offsetX = e.clientX - rect.x;
const offsetY = e.clientY - rect.y;

return {
x: offsetX / rect.width * 100,
y: offsetY / rect.height * 100
};
}

function getCoordPercentage(e) {
return getOffsetCoordPercentage(e, e.currentTarget);
}


export { getOffsetCoordPercentage, getCoordPercentage };
7 changes: 3 additions & 4 deletions src/utils/withRelativeMousePos.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PureComponent as Component } from 'react'
import { getOffsetCoordPercentage } from './offsetCoordinates';

const withRelativeMousePos = (key = 'relativeMousePos') => DecoratedComponent => {
class WithRelativeMousePos extends Component {
Expand All @@ -9,10 +10,8 @@ const withRelativeMousePos = (key = 'relativeMousePos') => DecoratedComponent =>
}

onMouseMove = (e) => {
this.setState({
x: (e.nativeEvent.offsetX / this.container.width) * 100,
y: (e.nativeEvent.offsetY / this.container.height) * 100,
})
const xystate = getOffsetCoordPercentage(e, this.container);
this.setState(xystate);
}

onMouseLeave = (e) => {
Expand Down

0 comments on commit 8dc568b

Please sign in to comment.