Tips for enabling mouse functionality while utilizing PointerLockControls

I'm currently working on a 3D art gallery using Three.js and PointerLockControls for navigation.

My goal is to have the artwork on the gallery walls clickable, triggering a pop-up window with additional information. However, it seems that PointerLockControls restrict clicking and mouse interaction beyond looking around.

Is there a way to enable clicking and display the mouse cursor while still utilizing PointerLockControls?

I've tried adding event listeners for clicks but without success. The console.log function doesn't even output any messages when attached to the artwork element.

The artwork renders correctly, yet the desired pop-up remains elusive. Below is the code snippet related to this issue:

Snippet of the code :

let art2Texture = new THREE.TextureLoader().load('img/art/2.png');

const art2Geometry = new THREE.PlaneBufferGeometry(8, 8);
const art2Material = new THREE.MeshBasicMaterial({
    map: art2Texture
});

const art2 = new THREE.Mesh(art2Geometry, art2Material);

art2.position.z = -49.9;
art2.position.y = 3.5;

art2.userData = {
name: 'Artwork 2',
description: 'This is the description of Artwork 2',
link: 'https://here-goes-the-link',
};

art2.addEventListener('click', () => {
console.log('Artwork 2 clicked!');
const popup = document.createElement('div');
popup.className = 'popup';

const title = document.createElement('h2');
title.textContent = art2.userData.name;

const description = document.createElement('p');
description.textContent = art2.userData.description;

const link = document.createElement('a');
link.href = art2.userData.link;
link.target = '_blank';
link.textContent = 'View on OpenSea';

popup.appendChild(title);
popup.appendChild(description);
popup.appendChild(link);

document.body.appendChild(popup);
});

// CSS for the pop-up
const popupCSS = `
.popup {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
border: 2px solid black;
padding: 16px;
width: 300px;
text-align: center;
z-index: 1;
}

.popup h2 {
margin-top: 0;
}

.popup a {
display: block;
margin-top: 16px;
}
`;

const popupStyle = document.createElement('style');
popupStyle.textContent = popupCSS;

document.head.appendChild(popupStyle);
scene.add(art2);

Answer №1

To interact with a Mesh in Three.js, you cannot directly use the .addEventListener("click") method since it is meant for HTMLElements. Instead, you can attach a click event listener to the canvas and then utilize a Raycaster to determine which element is being clicked on.

For a practical example, you can refer to this demo where you can see how to identify the object under the cursor by clicking on the "< >" icon at the bottom to view its source code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The jQuery template fails to render any content on the page

Recently, I've been exploring jQuery Javascript Templates and trying to follow a tutorial that involves fetching tweets from Twitter. The tutorial can be found here: . After carefully implementing the code as instructed, you can view my progress on t ...

All pixels have a value of 1 for gl_FragCoord.x, y, and z, while the depth value is represented by

I'm currently utilizing THREE.js in combination with a shader. My goal is to access the zbuffer information. For the vertex shader: // enabling high precision floats #ifdef GL_ES precision highp float; #endif void main() { gl_Position = project ...

Display/conceal within a jQuery fixed navigation bar (center-aligned)

I am facing challenges with creating a sticky menu that shows/hides with a click button. Considering abandoning the show/hide feature altogether and rebuilding it from scratch in the future. Two major problems I have identified: How can I make the sho ...

Creating a file logging system with log4js to capture Console logs

Is there a way to automatically log all console logs, including failed expectations and exceptions, to a file without using try and catch in JavaScript? In Java's LOG4j, the rootlogger feature does this by default. Is there a similar functionality ava ...

Implementing nested popup windows using Bootstrap

I am facing an issue with my two-page sign-in and sign-up setup in the header of my angular2 project. On the sign-up page, I have a link that should redirect to the sign-in page when clicked, but I am struggling to make it work. Can someone provide guidanc ...

The functionality for tabbed content seems to be malfunctioning on Chrome and Firefox, however it works perfectly

In my index.js file, I have various functions set up like so: // a and b is placed at index.jsp $("#a").click(function (){ //this works on index.jsp and display.jsp(where the servlets forwards it). $("#b").load('servletA.html?action=dis ...

A guide to displaying numerous notifications using notistack in a React environment

I'm currently experimenting with React's 'notistack' module to showcase multiple notifications as a stack. However, it seems like I might be making an error since every time I encounter a warning: react_devtools_backend.js:3973 Warning ...

What is the best way to retrieve the data from this date object?

How can I access the date and time in the code below? I've attempted using functions within the Text block without success. It's unclear to me what mistake I'm making or how to correctly access this object, or transform it into an object th ...

When the document is shifted by an angular function call, the mouseup event fails to trigger

My problem involves an angular function that alters the ng-if state to display the sidebar when the image is clicked. However, upon calling the function and shifting the entire webpage, the mouseup event for the image no longer triggers when I release th ...

Utilize the id in AngularJS to bring attention to a specific row within a table

I am brand new to using angularjs. I currently have a table structured like this: HTML <table class="table table-striped" id="manageResumeTable"> <thead class="text-center text-info text-capitalize"> <th class="text-center col- ...

Tips for retrieving the values of both a checkbox and radio button in AngularJS

Hello, I have created MY PLUNKER I have a condition in my JSON where if the minimum value of the ingredients is equal to one, it will change to a radio button. If it's greater than one, it will change to a checkbox. To display as a radio button: ng ...

Troubleshooting: Angular.js error when Typescript class constructor returns undefined

I'm currently trying to create a simple TypeScript class, however I keep encountering an error stating this is undefined in the constructor. Class: class MyNewClass { items: string; constructor(){ this.items = 'items'; ...

Utilizing jQuery for AJAX requests on a timed loop

I'm puzzled by a situation involving an AJAX call within an interval. It seems that the code doesn't work as expected, and I'm wondering why. Initially, I had this code snippet which wasn't functioning properly: setInterval($.ajax({ ...

ExpressJS - The execution of JavaScript in this framework does not strictly follow a top-down approach

Having issues with this particular script not running as expected, particularly in the variable reassignment section. Below is the code snippet and output: // Code to Create New Open Market in the game let latestRowId = 1; var sqlQu ...

Incorporating jquery.prettyPhoto results in the script being displayed on the webpage

When I relocate the following script: <script src="js/jquery.prettyPhoto.js"></script> To this location: <script type="text/javascript"> ... </script> The script starts displaying on the page starting from the bold section on ...

Creating dependent dropdowns using Laravel Inertia Vue: A step-by-step guide

In my AddressController, I have a function called loadCity along with other CRUD functions: public function loadCities(Request $request) { $provinceId = $request->province_id; $cities = Province::where('province_id' ...

The data type 'unknown' cannot be assigned to the type 'any[]', 'Iterable<any>', or (Iterable<any> & any[])

I have been working on creating a custom search filter in my Angular project, and it was functioning properly. However, I encountered an error in my Visual Studio Code. In my previous project, everything was working fine until I updated my CLI, which resul ...

Converting JQuery Object (Ajax-response) to an Array Containing Keys and Values

When I make an $.ajax request, the response I receive looks like this: https://i.sstatic.net/UfnfQ.jpg (please don't worry about the Russian symbols, they are just strings of text:)) I am aware that when PHP sends the request, it is in a simple arr ...

Performing multiple Axios POST requests within a loop in a REACT application with dynamic variables

For my form inputs, I have an array of labels named changing_variable, which depend on the user's selection from a dropdown menu and vary in value. I'm trying to pass this dynamic variable to Axios.post method to correctly input data into my dat ...

Notification system for managing recurring tasks

Situation I am currently working on an application where users are assigned daily tasks such as "wash the window, clean the floor," and so on. Each task has a specific recurrence interval and must be completed at least once within that time frame. For e ...