Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akcyp committed Mar 5, 2021
1 parent dd99d6f commit efecc43
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
18 changes: 13 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ export const findPointByPosition = (
return { point: { ...points[index] }, index };
};

export const getDistance = (p1: Point, p2: Point) => {
return Math.hypot(p2.x - p1.x, p2.y - p1.y);
};

export const getAngle = (p1: Point, p2: Point) => {
return Math.atan2(p2.y - p1.y, p2.x - p1.x);
};

export const approximateToAnAngleMultiplicity = (
startPoint: Point,
endPoint: Point,
minAngle: number
): Point => {
const r = Math.hypot(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
const angle = Math.atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x);
const r = getDistance(startPoint, endPoint);
const angle = getAngle(startPoint, endPoint);
const newAngle = Math.round(angle / minAngle) * minAngle;
return {
x: startPoint.x + r * Math.cos(newAngle),
Expand All @@ -68,8 +76,8 @@ export const approximateToAngles = (
endPoint: Point,
angles: number[]
): Point => {
const r = Math.hypot(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
const angle = Math.atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x);
const r = getDistance(startPoint, endPoint);
const angle = getAngle(startPoint, endPoint);
const nearestAngle = angles.reduce(
(prev, now) => (Math.abs(now - angle) < Math.abs(prev - angle) ? now : prev),
Infinity
Expand All @@ -88,7 +96,7 @@ export const calculateAnglesBeetwenPoints = (points: Point[]) => {
const alpha2 = alpha + Math.PI;
angles.push(alpha, alpha2 > Math.PI ? alpha2 - 2 * Math.PI : alpha2);
}
return angles.filter((val, idx, ths) => ths.indexOf(val) === idx);
return angles.filter((val, idx, arr) => arr.indexOf(val) === idx);
};

export function getClippedImageCanvas(
Expand Down
76 changes: 40 additions & 36 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,12 @@ export class ReactLasso extends React.Component<ReactLassoProps, ReactLassoState
}
};
onPointClick = (idx: number) => {
if (!this.isLoaded() || this.props.disabled || this.path.closed) return;
this.dispatchPathAction({
type: pathActions.ADD,
payload: this.path.points[idx]
});
if (this.isLoaded() && !this.props.disabled && !this.path.closed) {
this.dispatchPathAction({
type: pathActions.ADD,
payload: this.path.points[idx]
});
}
};
onDragEnd = () => {
this.checkIfPathUpdated(false);
Expand All @@ -350,25 +351,25 @@ export class ReactLasso extends React.Component<ReactLassoProps, ReactLassoState
this.props.onImageError(e);
};
onClickTouchEvent = (e: touchOrMouseEvent<SVGSVGElement>) => {
if (!this.isLoaded()) return;
if (this.props.disabled) return;
if (this.path.closed) {
if (e.target === this.svgRef.current) {
if (this.isLoaded() && !this.props.disabled) {
if (this.path.closed) {
if (e.target === this.svgRef.current) {
this.dispatchPathAction({
type: pathActions.RESET
});
}
return;
}
const [pointer] = this.getMousePosition(e);
if (!this.svg.isAboveTheBorder(pointer)) {
this.dispatchPathAction({
type: pathActions.RESET
type: pathActions.ADD,
payload: roundPointCoordinates(pointer, 1e3),
pointer
});
} else {
this.hidePointer();
}
return;
}
const [pointer] = this.getMousePosition(e);
if (!this.svg.isAboveTheBorder(pointer)) {
this.dispatchPathAction({
type: pathActions.ADD,
payload: roundPointCoordinates(pointer, 1e3),
pointer
});
} else {
this.hidePointer();
}
};
onClick = (e: React.MouseEvent<SVGSVGElement, MouseEvent>) => {
Expand All @@ -381,23 +382,26 @@ export class ReactLasso extends React.Component<ReactLassoProps, ReactLassoState
}
};
onMouseMove = (e: touchOrMouseEvent<SVGSVGElement>) => {
if (!this.isLoaded()) return;
const [pointer] = this.getMousePosition(e);
this.setPointer(pointer);
if (this.isLoaded()) {
const [pointer] = this.getMousePosition(e);
this.setPointer(pointer);
}
};
onContextMenu = (e: React.MouseEvent<SVGSVGElement, MouseEvent>) => {
if (!this.isLoaded()) return;
e.preventDefault();
if (this.props.disabled || this.path.closed) return;
const [pointer, { index }] = this.getMousePosition(e);
if (index > -1) {
this.dispatchPathAction({
type: pathActions.DELETE,
payload: index,
pointer
});
} else {
this.setPointer(pointer);
if (this.isLoaded()) {
e.preventDefault();
if (!this.props.disabled && !this.path.closed) {
const [pointer, { index }] = this.getMousePosition(e);
if (index > -1) {
this.dispatchPathAction({
type: pathActions.DELETE,
payload: index,
pointer
});
} else {
this.setPointer(pointer);
}
}
}
};
static propTypes = {
Expand Down

0 comments on commit efecc43

Please sign in to comment.