Raycaster fails hit test



Any idea why raycaster fails the hit test for the example at https://jsfiddle.net/zdy1hc9z/ ? It has a cube and a thin cylinder inside it. If you click on the cylinder in the example, then raycaster fails the hit test. It works on the cube. If I increase radius of the cylinder, for example to 0.2, then the hit test works.


camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerWidth, 0.1, 1000 );

is window.innerWidth / window.innerWidth intentional?


That was not intentional. Fixed it in the code. But that does not change the result.


window.addEventListener('mousedown', onMouseDown, false );
window.addEventListener('mousemove', onMouseDown, false );
run the fiddle and slowly move your mouse across the cylinder’s part, which is out of the cube. Notice the amount of hits, when the mouse slightly to the left from the cylinder.

Then, add this style for the canvas and see, where your renderer.domElement locates on the window:

  border: 1px solid blue;

To solve this, add one more style for body:

  overflow: hidden;
  margin: 0;

and remove previous style for canvas. That’s all :slight_smile:


It’s better to raycast using the canvas dimensions, rather than the window - I’ve updated your method here:


This way you can have a canvas of any size, anywhere in the screen (or fullscreen), and raycasting will still work.


Thanks @looeee, that solved the issue.


It makes things simplier, if to add the event listener to the canvas (renderer.domElement) and use offsetX and offsetY :beers: :

[SOLVED] Raycaster on mouse click laggs an action behind

@prisoner849 even better! :tada: