Skip to content

Use getBoundingRect in raycasting examples #23120

@Botchal

Description

@Botchal

In the raycaster examples (e.g. https://github.com/mrdoob/three.js/blob/dev/examples/webgl_instancing_raycast.html#L112), the mouse coordinates are not calculated correctly. Сode in the example works when the width of the canvas is equal to the width
window. And its position is 0 0.

Code

This is correct example for raycaster with https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect :

const rect = this.renderer.domElement.getBoundingClientRect();
mouse.x = ( ( event.clientX - rect.left ) / ( rect.width - rect.left ) ) * 2 - 1;
mouse.y = - ( ( event.clientY - rect.top ) / ( rect.bottom - rect.top) ) * 2 + 1;

Screenshots

example.

Platform:

  • Device: [Desktop, Mobile]
  • OS: [Windows, MacOS, Linux, Android, iOS]
  • Browser: [Chrome, Firefox, Safari, Edge]
  • Three.js version: [r136]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions