Skip to content

Commit 98aaa0b

Browse files
authored
feat(OrbitControls): expose zoom methods for programmatical controls (#383)
1 parent 363c0f9 commit 98aaa0b

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

src/controls/OrbitControls.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class OrbitControls extends EventDispatcher {
9999
setPolarAngle: (x: number) => void
100100
setAzimuthalAngle: (x: number) => void
101101
getDistance: () => number
102+
// Not used in most scenarios, however they can be useful for specific use cases
103+
getZoomScale: () => number
102104

103105
listenToKeyEvents: (domElement: HTMLElement) => void
104106
stopListenToKeyEvents: () => void
@@ -108,6 +110,16 @@ class OrbitControls extends EventDispatcher {
108110
connect: (domElement: HTMLElement) => void
109111
dispose: () => void
110112

113+
// Dolly in programmatically
114+
dollyIn: (dollyScale?: number) => void
115+
// Dolly out programmatically
116+
dollyOut: (dollyScale?: number) => void
117+
// Get the current scale
118+
getScale: () => number
119+
// Set the current scale (these are not used in most scenarios, however they can be useful for specific use cases)
120+
setScale: (newScale: number) => void
121+
122+
111123
constructor(object: PerspectiveCamera | OrthographicCamera, domElement?: HTMLElement) {
112124
super()
113125

@@ -564,28 +576,24 @@ class OrbitControls extends EventDispatcher {
564576
}
565577
})()
566578

567-
function dollyOut(dollyScale: number) {
579+
function setScale(newScale: number) {
568580
if (
569581
(scope.object instanceof PerspectiveCamera && scope.object.isPerspectiveCamera) ||
570582
(scope.object instanceof OrthographicCamera && scope.object.isOrthographicCamera)
571583
) {
572-
scale /= dollyScale
584+
scale = newScale
573585
} else {
574586
console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.')
575587
scope.enableZoom = false
576588
}
577589
}
578590

591+
function dollyOut(dollyScale: number) {
592+
setScale(scale / dollyScale)
593+
}
594+
579595
function dollyIn(dollyScale: number) {
580-
if (
581-
(scope.object instanceof PerspectiveCamera && scope.object.isPerspectiveCamera) ||
582-
(scope.object instanceof OrthographicCamera && scope.object.isOrthographicCamera)
583-
) {
584-
scale *= dollyScale
585-
} else {
586-
console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.')
587-
scope.enableZoom = false
588-
}
596+
setScale(scale * dollyScale)
589597
}
590598

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

1090+
// Add dolly in/out methods for public API
1091+
1092+
this.dollyIn = (dollyScale = getZoomScale()) => {
1093+
dollyIn(dollyScale)
1094+
scope.update()
1095+
}
1096+
1097+
this.dollyOut = (dollyScale = getZoomScale()) => {
1098+
dollyOut(dollyScale)
1099+
scope.update()
1100+
}
1101+
1102+
this.getScale = () => {
1103+
return scale;
1104+
}
1105+
1106+
this.setScale = (newScale) => {
1107+
setScale(newScale)
1108+
scope.update()
1109+
}
1110+
1111+
this.getZoomScale = () => {
1112+
return getZoomScale();
1113+
}
1114+
10821115
// connect events
10831116
if (domElement !== undefined) this.connect(domElement)
10841117
// force an update at start

0 commit comments

Comments
 (0)