From d0fefdf04ed1334b1ca268584b9ed03f2cf08606 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Wed, 1 Feb 2023 20:55:34 +0100 Subject: [PATCH] feat(core): add auto pan utils Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com> --- packages/core/src/utils/autopan.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 packages/core/src/utils/autopan.ts diff --git a/packages/core/src/utils/autopan.ts b/packages/core/src/utils/autopan.ts new file mode 100644 index 00000000..feb60c35 --- /dev/null +++ b/packages/core/src/utils/autopan.ts @@ -0,0 +1,20 @@ +import type { Dimensions, XYPosition } from '~/types' + +// 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 +const 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 0 +} + +export const 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 + + return [xMovement, yMovement] +}