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

@ -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,
];
};