Skip to content

Commit

Permalink
feat(OrbitControls): expose zoom methods for programmatical controls (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun authored Oct 30, 2024
1 parent 363c0f9 commit 98aaa0b
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions src/controls/OrbitControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class OrbitControls extends EventDispatcher {
setPolarAngle: (x: number) => void
setAzimuthalAngle: (x: number) => void
getDistance: () => number
// Not used in most scenarios, however they can be useful for specific use cases
getZoomScale: () => number

listenToKeyEvents: (domElement: HTMLElement) => void
stopListenToKeyEvents: () => void
Expand All @@ -108,6 +110,16 @@ class OrbitControls extends EventDispatcher {
connect: (domElement: HTMLElement) => void
dispose: () => void

// Dolly in programmatically
dollyIn: (dollyScale?: number) => void
// Dolly out programmatically
dollyOut: (dollyScale?: number) => void
// Get the current scale
getScale: () => number
// Set the current scale (these are not used in most scenarios, however they can be useful for specific use cases)
setScale: (newScale: number) => void


constructor(object: PerspectiveCamera | OrthographicCamera, domElement?: HTMLElement) {
super()

Expand Down Expand Up @@ -564,28 +576,24 @@ class OrbitControls extends EventDispatcher {
}
})()

function dollyOut(dollyScale: number) {
function setScale(newScale: number) {
if (
(scope.object instanceof PerspectiveCamera && scope.object.isPerspectiveCamera) ||
(scope.object instanceof OrthographicCamera && scope.object.isOrthographicCamera)
) {
scale /= dollyScale
scale = newScale
} else {
console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.')
scope.enableZoom = false
}
}

function dollyOut(dollyScale: number) {
setScale(scale / dollyScale)
}

function dollyIn(dollyScale: number) {
if (
(scope.object instanceof PerspectiveCamera && scope.object.isPerspectiveCamera) ||
(scope.object instanceof OrthographicCamera && scope.object.isOrthographicCamera)
) {
scale *= dollyScale
} else {
console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.')
scope.enableZoom = false
}
setScale(scale * dollyScale)
}

function updateMouseParameters(event: MouseEvent): void {
Expand Down Expand Up @@ -1079,6 +1087,31 @@ class OrbitControls extends EventDispatcher {
return pointerPositions[pointer.pointerId]
}

// Add dolly in/out methods for public API

this.dollyIn = (dollyScale = getZoomScale()) => {
dollyIn(dollyScale)
scope.update()
}

this.dollyOut = (dollyScale = getZoomScale()) => {
dollyOut(dollyScale)
scope.update()
}

this.getScale = () => {
return scale;
}

this.setScale = (newScale) => {
setScale(newScale)
scope.update()
}

this.getZoomScale = () => {
return getZoomScale();
}

// connect events
if (domElement !== undefined) this.connect(domElement)
// force an update at start
Expand Down

0 comments on commit 98aaa0b

Please sign in to comment.