Perf: Use a computed cache for masked shape page bounds (#3460)
This PR adds a computed cache for masked shape page bounds, which speeds up visibility checks (a lot!). ### Change Type - [x] `sdk` — Changes the tldraw SDK - [x] `improvement` — Improving existing features
This commit is contained in:
parent
143755fda0
commit
87f70b7de5
1 changed files with 18 additions and 15 deletions
|
@ -4057,22 +4057,25 @@ export class Editor extends EventEmitter<TLEventMap> {
|
|||
*/
|
||||
getShapeMaskedPageBounds(shape: TLShapeId | TLShape): Box | undefined {
|
||||
if (typeof shape !== 'string') shape = shape.id
|
||||
const pageBounds = this._getShapePageBoundsCache().get(shape)
|
||||
return this._getShapeMaskedPageBoundsCache().get(shape)
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@computed private _getShapeMaskedPageBoundsCache(): ComputedCache<Box, TLShape> {
|
||||
return this.store.createComputedCache('shapeMaskedPageBoundsCache', (shape) => {
|
||||
const pageBounds = this._getShapePageBoundsCache().get(shape.id)
|
||||
if (!pageBounds) return
|
||||
const pageMask = this._getShapeMaskCache().get(shape)
|
||||
const pageMask = this._getShapeMaskCache().get(shape.id)
|
||||
if (pageMask) {
|
||||
if (pageMask.length === 0) return undefined
|
||||
|
||||
const { corners } = pageBounds
|
||||
if (corners.every((p, i) => p && Vec.Equals(p, pageMask[i]))) return pageBounds.clone()
|
||||
|
||||
// todo: find out why intersect polygon polygon for identical polygons produces zero w/h intersections
|
||||
const intersection = intersectPolygonPolygon(pageMask, corners)
|
||||
if (!intersection) return
|
||||
return Box.FromPoints(intersection)
|
||||
}
|
||||
|
||||
return pageBounds
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue