excalidraw/src/scene/scrollbars.ts

102 lines
2.5 KiB
TypeScript
Raw Normal View History

import { ExcalidrawElement } from "../element/types";
import { getCommonBounds } from "../element";
const SCROLLBAR_MIN_SIZE = 15;
const SCROLLBAR_MARGIN = 4;
export const SCROLLBAR_WIDTH = 6;
export const SCROLLBAR_COLOR = "rgba(0,0,0,0.3)";
export function getScrollBars(
elements: readonly ExcalidrawElement[],
canvasWidth: number,
canvasHeight: number,
scrollX: number,
2020-01-24 12:04:54 +02:00
scrollY: number,
) {
let [minX, minY, maxX, maxY] = getCommonBounds(elements);
minX += scrollX;
maxX += scrollX;
minY += scrollY;
maxY += scrollY;
const leftOverflow = Math.max(-minX, 0);
const rightOverflow = Math.max(-(canvasWidth - maxX), 0);
const topOverflow = Math.max(-minY, 0);
const bottomOverflow = Math.max(-(canvasHeight - maxY), 0);
// horizontal scrollbar
let horizontalScrollBar = null;
if (leftOverflow || rightOverflow) {
horizontalScrollBar = {
x: Math.min(
leftOverflow + SCROLLBAR_MARGIN,
2020-01-24 12:04:54 +02:00
canvasWidth - SCROLLBAR_MIN_SIZE - SCROLLBAR_MARGIN,
),
y: canvasHeight - SCROLLBAR_WIDTH - SCROLLBAR_MARGIN,
width: Math.max(
canvasWidth - rightOverflow - leftOverflow - SCROLLBAR_MARGIN * 2,
2020-01-24 12:04:54 +02:00
SCROLLBAR_MIN_SIZE,
),
2020-01-24 12:04:54 +02:00
height: SCROLLBAR_WIDTH,
};
}
// vertical scrollbar
let verticalScrollBar = null;
if (topOverflow || bottomOverflow) {
verticalScrollBar = {
x: canvasWidth - SCROLLBAR_WIDTH - SCROLLBAR_MARGIN,
y: Math.min(
topOverflow + SCROLLBAR_MARGIN,
2020-01-24 12:04:54 +02:00
canvasHeight - SCROLLBAR_MIN_SIZE - SCROLLBAR_MARGIN,
),
width: SCROLLBAR_WIDTH,
height: Math.max(
canvasHeight - bottomOverflow - topOverflow - SCROLLBAR_WIDTH * 2,
2020-01-24 12:04:54 +02:00
SCROLLBAR_MIN_SIZE,
),
};
}
return {
horizontal: horizontalScrollBar,
2020-01-24 12:04:54 +02:00
vertical: verticalScrollBar,
};
}
export function isOverScrollBars(
elements: readonly ExcalidrawElement[],
x: number,
y: number,
canvasWidth: number,
canvasHeight: number,
scrollX: number,
2020-01-24 12:04:54 +02:00
scrollY: number,
) {
const scrollBars = getScrollBars(
elements,
canvasWidth,
canvasHeight,
scrollX,
2020-01-24 12:04:54 +02:00
scrollY,
);
const [isOverHorizontalScrollBar, isOverVerticalScrollBar] = [
scrollBars.horizontal,
2020-01-24 12:04:54 +02:00
scrollBars.vertical,
].map(
scrollBar =>
scrollBar &&
scrollBar.x <= x &&
x <= scrollBar.x + scrollBar.width &&
scrollBar.y <= y &&
2020-01-24 12:04:54 +02:00
y <= scrollBar.y + scrollBar.height,
);
return {
isOverHorizontalScrollBar,
2020-01-24 12:04:54 +02:00
isOverVerticalScrollBar,
};
}