fix: emitted visible scene bounds not accounting for offsets (#7450)

This commit is contained in:
David Luzar 2023-12-16 17:32:54 +01:00 committed by GitHub
parent 561e919a2e
commit 6dfa89e846
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 82 additions and 54 deletions

View file

@ -13,6 +13,8 @@ Please add the latest change on the top under the correct section.
## Unreleased
- Expose `getVisibleSceneBounds` helper to get scene bounds of visible canvas area. [#7450](https://github.com/excalidraw/excalidraw/pull/7450)
### Breaking Changes
- `appState.openDialog` type was changed from `null | string` to `null | { name: string }`. [#7336](https://github.com/excalidraw/excalidraw/pull/7336)

View file

@ -20,7 +20,7 @@ import {
isHandToolActive,
} from "../appState";
import { DEFAULT_CANVAS_BACKGROUND_PICKS } from "../colors";
import { Bounds } from "../element/bounds";
import { SceneBounds } from "../element/bounds";
import { setCursor } from "../cursor";
export const actionChangeViewBackgroundColor = register({
@ -211,7 +211,7 @@ export const actionResetZoom = register({
});
const zoomValueToFitBoundsOnViewport = (
bounds: Bounds,
bounds: SceneBounds,
viewportDimensions: { width: number; height: number },
) => {
const [x1, y1, x2, y2] = bounds;
@ -235,7 +235,7 @@ export const zoomToFitBounds = ({
fitToViewport = false,
viewportZoomFactor = 0.7,
}: {
bounds: readonly [number, number, number, number];
bounds: SceneBounds;
appState: Readonly<AppState>;
/** whether to fit content to viewport (beyond >100%) */
fitToViewport: boolean;

View file

@ -9,7 +9,7 @@ import {
import { distance2d, rotate, rotatePoint } from "../math";
import rough from "roughjs/bin/rough";
import { Drawable, Op } from "roughjs/bin/core";
import { Point } from "../types";
import { AppState, Point } from "../types";
import { generateRoughOptions } from "../scene/Shape";
import {
isArrowElement,
@ -35,7 +35,9 @@ export type RectangleBox = {
type MaybeQuadraticSolution = [number | null, number | null] | false;
// x and y position of top left corner, x and y position of bottom right corner
/**
* x and y position of top left corner, x and y position of bottom right corner
*/
export type Bounds = readonly [
minX: number,
minY: number,
@ -43,6 +45,13 @@ export type Bounds = readonly [
maxY: number,
];
export type SceneBounds = readonly [
sceneX: number,
sceneY: number,
sceneX2: number,
sceneY2: number,
];
export class ElementBounds {
private static boundsCache = new WeakMap<
ExcalidrawElement,
@ -879,3 +888,21 @@ export const getCommonBoundingBox = (
midY: (minY + maxY) / 2,
};
};
/**
* returns scene coords of user's editor viewport (visible canvas area) bounds
*/
export const getVisibleSceneBounds = ({
scrollX,
scrollY,
width,
height,
zoom,
}: AppState): SceneBounds => {
return [
-scrollX,
-scrollY,
-scrollX + width / zoom.value,
-scrollY + height / zoom.value,
];
};

View file

@ -249,7 +249,7 @@ export { TTDDialogTrigger } from "./components/TTDDialog/TTDDialogTrigger";
export { normalizeLink } from "./data/url";
export { zoomToFitBounds } from "./actions/actionCanvas";
export { convertToExcalidrawElements } from "./data/transform";
export { getCommonBounds } from "./element/bounds";
export { getCommonBounds, getVisibleSceneBounds } from "./element/bounds";
export {
elementsOverlappingBBox,