refactor(autopan): cleanup

This commit is contained in:
moklick
2023-01-23 15:05:18 +01:00
parent 00726085be
commit 24e8b8adc0
3 changed files with 21 additions and 12 deletions
+10 -3
View File
@@ -16,16 +16,23 @@ export const clampPosition = (position: XYPosition = { x: 0, y: 0 }, extent: Coo
// returns a number between 0 and 1 that represents the velocity of the movement
// when the mouse is close to the edge of the canvas
export const calcAutoPanVelocity = (value: number, min: number, max: number): number => {
const calcAutoPanVelocity = (value: number, min: number, max: number): number => {
if (value < min) {
return clamp(Math.abs(value - min), 1, 100) / 100;
return clamp(Math.abs(value - min), 1, 50) / 50;
} else if (value > max) {
return -clamp(Math.abs(value - max), 1, 100) / 100;
return -clamp(Math.abs(value - max), 1, 50) / 50;
}
return 0;
};
export const calcAutoPan = (pos: XYPosition, bounds: Dimensions): number[] => {
const xMovement = calcAutoPanVelocity(pos.x, 35, bounds.width - 35) * 20;
const yMovement = calcAutoPanVelocity(pos.y, 35, bounds.height - 35) * 20;
return [xMovement, yMovement];
};
export const getHostForElement = (element: HTMLElement): Document | ShadowRoot =>
(element.getRootNode?.() as Document | ShadowRoot) || window?.document;