Using orbitcontrols.js (with THREE.js), I want to achieve the same effect in code as rotating the mouse wheel. For example, I want to call something like camera.zoomIn() and have it move a set distance toward the target. Anyone know how to do this?
Using orbitcontrols.js (with THREE.js), I want to achieve the same effect in code as rotating the mouse wheel. For example, I want to call something like camera.zoomIn() and have it move a set distance toward the target. Anyone know how to do this?
Share Improve this question asked Dec 17, 2016 at 12:34 Len WhiteLen White 99216 silver badges27 bronze badges3 Answers
Reset to default 7At least for simple cases, you can change the position of the camera (e.g. multiply x
, y
and z
by a factor), which automagically updates the OrbitControls
.
Example with a <input type="range">
slider:
zoomer.addEventListener('input', function(e) {
var zoomDistance = Number(zoomer.value),
currDistance = camera.position.length(),
factor = zoomDistance/currDistance;
camera.position.x *= factor;
camera.position.y *= factor;
camera.position.z *= factor;
});
https://codepen.io/Sphinxxxx/pen/yPZQMV
From looking at the source code you can call .dollyIn(dollyScale)
and .dollyOut(dollyScale)
on the OrbitalControls
object.
Edit: these aren't public methods; one can access them by editing OrbitalControls.js
though.
I added this.dIn = dollyIn;
to the THREE.OrbitControls
function, I can now zoom in by calling controls.dIn(1.05); controls.update();
from outside.
You can set values on OrbitControls.maxDistance and OrbitControls.minDistance to zoom in and out programmatically.
For example, if you wanted to animate a zoom out with gsap you could do something like this:
gsap.to(myOrbitControls, {
minDistance: 20, // target min distance
duration: 1,
overwrite: 'auto',
ease: 'power1.inOut',
onComplete: () => {
myOrbitControls.minDistance = 0 // reset to initial min distance
},
})