Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

brush: fix brush jumping around after mouseup #1836

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/visx-brush/src/BaseBrush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
BrushingType,
BrushPageOffset,
} from './types';
import { getPageCoordinates } from './utils';
import { debounce, getPageCoordinates } from './utils';

type PointerHandlerEvent = React.PointerEvent<SVGRectElement>;

Expand Down Expand Up @@ -177,14 +177,14 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
componentDidMount() {
if (this.props.useWindowMoveEvents) {
window.addEventListener('mouseup', this.handleWindowPointerUp);
window.addEventListener('mousemove', this.handleWindowPointerMove);
window.addEventListener('mousemove', this.debouncedHandleWindowPointerMove);
}
}

componentWillUnmount() {
if (this.props.useWindowMoveEvents) {
window.removeEventListener('mouseup', this.handleWindowPointerUp);
window.removeEventListener('mousemove', this.handleWindowPointerMove);
window.removeEventListener('mousemove', this.debouncedHandleWindowPointerMove);
}
}

Expand Down Expand Up @@ -320,6 +320,8 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
}
};

debouncedHandleWindowPointerMove = debounce(this.handleWindowPointerMove, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could potentially add a comment for why we need this debounce since it was such a cryptic issue before!


getExtent = (start: Partial<Point>, end: Partial<Point>) => {
const { brushDirection, width, height } = this.props;
const x0 = brushDirection === 'vertical' ? 0 : Math.min(start.x || 0, end.x || 0);
Expand Down
11 changes: 11 additions & 0 deletions packages/visx-brush/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ export function getPageCoordinates(event: MouseTouchOrPointerEvent) {
pageY: pointerEvent.pageY,
};
}

// Tweaked from https://dev.to/cantem/how-to-write-a-debounce-function-1bdf
export function debounce<T extends Function>(func: T, delay: number): (...args: any[]) => void {
let timeoutId: ReturnType<typeof setTimeout>;
return function debouncedFn(this: unknown, ...args: unknown[]) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
Loading