Skip to content

Commit

Permalink
feat: auto pan when out of view
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc authored and tiensonqin committed Feb 19, 2023
1 parent a6c3cca commit 0f48180
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tldraw/packages/core/src/lib/TLViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class TLViewport {

static readonly minZoom = 0.1
static readonly maxZoom = 4
static readonly panMultiplier = 0.05

/* ------------------- Properties ------------------- */

Expand Down Expand Up @@ -48,6 +49,17 @@ export class TLViewport {
})
}

panToPointWhenOutOfBounds = (point: number[]) => {
const deltaMax = Vec.sub([this.currentView.maxX, this.currentView.maxY], point)
const deltaMin = Vec.sub([this.currentView.minX, this.currentView.minY], point)

const deltaX = deltaMax[0] < 0 ? deltaMax[0] : deltaMin[0] > 0 ? deltaMin[0] : 0
const deltaY = deltaMax[1] < 0 ? deltaMax[1] : deltaMin[1] > 0 ? deltaMin[1] : 0

this.panCamera(Vec.mul([deltaX, deltaY], -TLViewport.panMultiplier))
}


@action update = ({ point, zoom }: Partial<{ point: number[]; zoom: number }>): this => {
if (point !== undefined && !isNaN(point[0]) && !isNaN(point[1])) this.camera.point = point
if (zoom !== undefined && !isNaN(zoom)) this.camera.zoom = Math.min(4, Math.max(0.1, zoom))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class BrushingState<
// Select hit shapes
this.app.setSelectedShapes(hits)
}
this.app.viewport.panToPointWhenOutOfBounds(currentPoint)
}

onPointerUp: TLEvents<S>['pointer'] = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class ResizingState<
})
})
this.updateCursor(scaleX, scaleY)
this.app.viewport.panToPointWhenOutOfBounds(currentPoint)
}

onPointerUp: TLEvents<S>['pointer'] = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ export class TranslatingState<
}

onPointerMove: TLEvents<S>['pointer'] = () => {
const {
inputs: { currentPoint },
} = this.app

this.moveSelectedShapesToPointer()
this.app.viewport.panToPointWhenOutOfBounds(currentPoint)
}

onPointerDown: TLEvents<S>['pointer'] = () => {
Expand Down

0 comments on commit 0f48180

Please sign in to comment.