feat(core): add autoPanSpeed prop (#1535)

* feat(core): add `autoPanSpeed` prop

* chore(changeset): add
This commit is contained in:
Braks
2024-07-15 19:14:01 +02:00
parent 6aa82cbc4d
commit d4ccb11a4c
7 changed files with 29 additions and 8 deletions
+13 -6
View File
@@ -5,17 +5,24 @@ import { clamp } from './graph'
// when the mouse is close to the edge of the canvas
function calcAutoPanVelocity(value: number, min: number, max: number) {
if (value < min) {
return clamp(Math.abs(value - min), 1, 50) / 50
} else if (value > max) {
return -clamp(Math.abs(value - max), 1, 50) / 50
return clamp(Math.abs(value - min), 1, min) / min
}
if (value > max) {
return -clamp(Math.abs(value - max), 1, min) / min
}
return 0
}
export function calcAutoPan(pos: XYPosition, bounds: Dimensions) {
const xMovement = calcAutoPanVelocity(pos.x, 35, bounds.width - 35) * 20
const yMovement = calcAutoPanVelocity(pos.y, 35, bounds.height - 35) * 20
export function calcAutoPan(
pos: XYPosition,
bounds: Dimensions,
speed = 15,
distance = 40,
): [xMovement: number, yMovement: number] {
const xMovement = calcAutoPanVelocity(pos.x, distance, bounds.width - distance) * speed
const yMovement = calcAutoPanVelocity(pos.y, distance, bounds.height - distance) * speed
return [xMovement, yMovement]
}