question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TrackballCamera Zoom in/out with mousewheel

See original GitHub issue

Some 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:closed
  • Created 12 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Mugen87commented, Mar 19, 2018

@klonsemann Please don’t request help at closed issues and github in general. Use the forum instead. Thanks.

0reactions
poudrocommented, Nov 15, 2011

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found