Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(OrbitControls): expose zoom methods for programmatical controls #383

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading