Creating a CSS3DObject with clickable functionality

Currently, I am utilizing the THREE JS CSS3DRenderer in an attempt to have a CSS3DObject update its position.z when clicked. Below is the code I am working with:

var element = document.createElement("div");
element.style.width = "90px";
element.style.height = "120px";
element.style.backgroundColor = "#5C6167";

var object = new THREE.CSS3DObject(element);
object.position.x = 0;
object.position.y = 0;
object.position.z = 100;
object.addEventListener('click', function (event) {
new TWEEN.Tween(object.position).to({z: 200}, 500).easing(TWEEN.Easing.Quadratic.Out).start();
});
scene.add(object);

Unfortunately, it seems that the CSS3DObject is not recognizing the click event. Any advice or guidance on this matter would be greatly appreciated. Thank you!

Answer №1

If you're looking to manage a click event triggered by a CSS3DObject, here is a method that allows you to access the parent object of the element:

let object = new THREE.CSS3DObject( element );

element.parent = object;

object.element.onclick = function() { this.parent.position.y += 10; };

Version three.js r.65

Answer №2

Have you experimented with using mousedown and mouseup events for your project? Check out this example:

let isMouseDown = false,
    mouseXPos = 0,
    mouseYPos = 0;

function handleMouseDown(event) {
    event.preventDefault();

    isMouseDown = true;
    mouseXPos = event.clientX;
    mouseYPos = event.clientY;
}

function handleMouseUp(event) {
    event.preventDefault();

    isMouseDown = false;
}

function addMouseListeners(canvasElement) {
    canvasElement.addEventListener('mousedown', handleMouseDown, false);
    canvasElement.addEventListener('mouseup', handleMouseUp, false);
}

$(document).ready(function () {
    let canvasElement = document.getElementById("canvas");

    addMouseListeners(canvasElement);

});

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

Unable to retrieve cookies from the client side | Working with NodeJS and JavaScript

Code for Handling Cookies on the Server-Side: res.cookie('test', 'value', { expire: 400000 + Date.now(), httpOnly: false }); res.writeHead(302, { 'Location': 'localhost:4000/test', }); res.end(); Code for Acce ...

Determine if a specific date occurred at least one day ago using momentjs

Is there a way to determine if a specific date is at least one day (24 hours) in the past using momentjs? For example: const currentDate = moment() const isAtLeastOneDayAgo = currentDate.subtract(dateToVerify) > 1 // How can this be done? ...

What is the best way to include text in an editor access notification email?

I've developed a script to assign editors to a spreadsheet, but I'm looking to personalize the emails sent to them. The screenshot below illustrates the step where I can input a message for the email recipients who have been granted access. f ...

Is it possible to successfully pass a parameter from a servlet to a JavaScript function, but encounter issues when trying to view the database

In my view servlet, I am displaying user data from the database in a table. Each row of the table has buttons that allow users to change the cells in that row to text boxes. The issue I am encountering is that when I retrieve the data and loop through to ...

Troubleshooting Puppeteer compatibility issues when using TypeScript and esModuleInterop

When attempting to use puppeteer with TypeScript and setting esModuleInterop=true in tsconfig.json, an error occurs stating puppeteer.launch is not a function If I try to import puppeteer using import * as puppeteer from "puppeteer" My questi ...

The lineup includes ASP.NET, Java, Visual Basic, and AJAX

My ASP.NET application originally had a button that triggered VB.NET code execution on the server when clicked. Recently, there have been changes to the requirements and I've added a new menu that should replace the functionality of the old VB button ...

NPM: Implementing a "post-install" hook that is only executed internally and not for package consumers

Currently, I am in the process of developing an NPM module and would like to automate certain tasks following every npm install while working on the module locally. However, it is crucial that these tasks are not executed when users of my library perform ...

modify the appearance of HTML controls in real time with C#

Is there a way to dynamically add an additional navigation item to my website's menu list when the admin logs in through admin.aspx? The menu list is created using HTML controls such as <ul>...<li>, and I would like the new navigation item ...

Encountering Rendering Issues with EJS Post HTML Conversion

After working on a much larger project for over a month, I'm now six hours into troubleshooting and everything is starting to blend together. Prior to switching to EJS, everything was working perfectly. I'm not looking for someone to solve the ...

What is the rationale behind not storing OAuth2 access tokens as HttpOnly secure cookies? How could this approach be implemented in a Node.js application?

I have a Node.js RESTful api setup where the response token received after posting to /oauth/token typically looks like this: { "refresh_token": "eyJraWQiOiI2...", "token_type": "Bearer", "access_token": "eyJraWQiOiI2Nl...", "expires_in": 3600 } ...

What is the best way to modify a target rotation to align with the camera based on the model's current orientation in Three.js using quaternions?

I have a web application using Three.js with a single model that I rotate using the mouse. I calculate the X/Y movement between mouse events to rotate the model around the specified axis (X, Y, or Z). The camera perspective remains constant throughout this ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

Alpha-Beta Pruning Minimax Algorithm Fails to Determine the Best Move in Tic-Tac-Toe Artificial Intelligence

I have been developing a Tic-Tac-Toe AI system that uses the Alpha-Beta Pruning Minimax algorithm to determine the best move for the AI player (X) on the game board. Unfortunately, I am running into an issue where the algorithm is not returning the correct ...

Ways to verify the existence of a username in WordPress without the need to refresh the page

How can I check if a username exists in the database without refreshing the page in a Wordpress form using AJAX? I've tried implementing AJAX in Wordpress before but it didn't work. Can someone provide me with a piece of helpful code or a link to ...

Keeping track of the toggle state using a cookie

While searching for a way to retain the toggle state, I stumbled upon js-cookie on GitHub. The documentation provides instructions on creating, reading, and deleting a cookie. However, an example would have been really helpful in enhancing my understanding ...

Parallax Effect Slows Down When Scrolling In Web Page

Currently in the process of creating a website with a scrolling parallax effect using Stellar.js on the header and three other sections. However, I'm experiencing lag when scrolling, especially at the top of the page. I've attempted to reduce la ...

Display the image on the right side when a specific number of pixels are available

Looking to have text wrapped around an image? Using float:right; can do the trick. However, is it possible to only float the image to the right when there is more than 200px of space available? For example: _____ |img | | | |_ ...

What is the best way to design a consistent navbar that can be used across multiple HTML pages using Bootstrap 5?

Preferably, I would like to achieve this using only JavaScript without the need for a JavaScript framework in my project. Layout: ./index.html ./about.html ./crew.html ./flights.html ./events.html I am looking to create a consistent navbar across all ...

Issue: User is being redirected from "/login" to "/home" due to a navigation guard. This problem arises in the interaction between Vue-router and Firebase authentication

I'm currently working on an app that utilizes Vue.js and Firebase for authentication. I am trying to implement a verification process using the beforeEach method in my router file to ensure that users can only sign in if their displayName matches a sp ...

Choose an option to adjust the transparency of the image

I'm struggling with a select option and need some assistance. I have a select dropdown list where I want the image opacity to change from 0.3 to 1 when selected or onchange event occurs. The catch is, I can't use the value of the option because i ...