TrackballCamera Zoom in/out with mousewheel
See original GitHub issueSome people might find this useful : catching the mousewheel event to zoom in and out of pane. I do anyway, thought it could be interesting feature to add to the TrackballCamera.
(I’m a total noob at github, sorry if this is out of context…)
Here’s a basic implementation I’ve used :
// need both for FF and Webkit - others I haven't tested
window.addEventListener('DOMMouseScroll', mousewheel, false);
window.addEventListener('mousewheel', mousewheel, false);
function mousewheel( event )
{
var amount = 100; // parameter
// get wheel direction
var d = ((typeof e.wheelDelta != "undefined")?(-e.wheelDelta):e.detail);
d = 100 * ((d>0)?1:-1);
// do calculations, I'm not using any three.js internal methods here, maybe there is a better way of doing this
// applies movement in the direction of (0,0,0), assuming this is where the camera is pointing
var cPos = camera.position;
var r = cPos.x*cPos.x + cPos.y*cPos.y;
var sqr = Math.sqrt(r);
var sqrZ = Math.sqrt(cPos.z*cPos.z + r);
var nx = cPos.x + ((r==0)?0:(d * cPos.x/sqr));
var ny = cPos.y + ((r==0)?0:(d * cPos.y/sqr));
var nz = cPos.z + ((sqrZ==0)?0:(d * cPos.z/sqrZ));
// verify we're applying valid numbers
if (isNaN(nx) || isNaN(ny) || isNaN(nz))
return;
cPos.x = nx;
cPos.y = ny;
cPos.z = nz;
}
Issue Analytics
- State:
- Created 12 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
zooming based on the position of cursor in mousewheel
While using trackball control zooming it is zooming relative to the center of the model.But i want to zoom according to the position...
Read more >zoom via mouse wheel on terrain style · Issue #1022 - GitHub
Description Hello, I find that the terrain style of camera control is much more intuitive for the types of data I am plotting....
Read more >Changing hotkeys for zoom in/zoom out. : r/SecretWorldLegends
But my trackball can emulate the mouse wheel. Today so many things need mouse wheel. It's not just zoom you can also select...
Read more >How To Zoom The Camera On Mouse Wheel Scroll - YouTube
Hey guys, in today's video I'm going to be showing you how to zoom the camera, or set its position, when the player...
Read more >Zoom to cursor with Trackball Controls - three.js forum
I'm an absolute dunce in 3D who's trying to figure out how to zoom towards the cursor position using the Trackball controls.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@klonsemann Please don’t request help at closed issues and github in general. Use the forum instead. Thanks.
Thanks for pointing that out, I just editted the comment.
An yes, it is in fact just moving the camera in/out (I’d always assumed that was a zoom 😃. For a proper zoom, you might look into mrdoob’s comment using the projectionMatrix.