Embarking on a fresh XR experience

As a newcomer to TypeScript, I am exploring how to create a functionality similar to a "double-click" event for a hand-input controller in my Three.js application. Here is my approach:

- I monitor a click event

- I check the timing between the last click and the current click

- If the time difference is less than a certain threshold, I trigger a custom event

My challenge arises when attempting to dispatch this custom event from my controller, XRTargetRaySpace. While I can successfully dispatch the event using the document object, I am unsure about the workings of the event dispatcher in this specific scenario.

Below is the interface detailing the event map:

https://i.sstatic.net/FwMrH3Vo.png

Here is the snippet of my code:

let deltaTime = 0,
    lastclick = 0,
    MAX_DELTA_TIME = 500;
const doubleClickEvent = new CustomEvent("sandbox:doubleClick", {
    bubbles: true,
    cancelable: true,
    composed: true,
    detail: {
        deltaTime,
    },
});

controller1.addEventListener("squeezestart", (_) => {
    deltaTime = window.performance.now() - lastclick;

    if (deltaTime < MAX_DELTA_TIME) {
        controller1.dispatchEvent(doubleClickEvent);
        lastclick = 0;
    }
    lastclick = window.performance.now();
});
controller2.addEventListener("squeezestart", (_) => {
    deltaTime = window.performance.now() - lastclick;

    if (deltaTime < MAX_DELTA_TIME) {
        controller2.dispatchEvent(doubleClickEvent);
        lastclick = 0;
    }
    lastclick = window.performance.now();
});

My expectation is to have something similar to:

controller1.addEventListener('sandbox:doubleClick', (e) => console.log(e))

where the controller can be XRTargetRaySpace, XRGripSpace, or XRHandSpace.

Answer №1

Updating this topic is important as it may benefit someone in the future. It appears that adding a new event to the threeJs controllers eventmap is not possible. This means that creating something like controller.addEventListener("myCustomEvent", (e) => console.log(e)) where the controller is of type XRTargetRaySpace or XRGripSpace is not supported. However, a workaround is to create a class named MyClass that extends EventDispatcher and contains the controllers. By doing this, it is possible to dispatch a myCustomEvent when a method of this class is invoked. Subsequently, eventListeners can be added to any instance of MyClass to execute a function whenever MyCustomEvent is triggered.

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

What is the best way to submit a form using PHP to send an email, implement Ajax for seamless transitions, and display a message next to the form without any screen refresh?

Upon clicking the submit button, I am looking to achieve two things: (1) Sending the form data via email using PHP without refreshing the page. (2) Displaying a thank you message next to the form utilizing Ajax. Most of the functionality is in pl ...

Developing a way to make vue-custom-element compatible for embedding in external websites

I've been exploring ways to use a component from my Vue website on another site by embedding it in HTML. I came across https://github.com/karol-f/vue-custom-element and found this helpful guide. After installing the npm packages vue-custom-element an ...

Looking for support for glBlendEquationEXT in your WebGL project?

WebGL currently supports ADD, SUBTRACT, and REVERSE_SUBTRACT operations. For more information, visit I am in need of support for MAX and MIN operations in WebGL as well. You can find more information on this idea at Does anyone know of a workaround to ac ...

Having trouble getting the mock module to work with mockImplementation

I have been facing a challenge in properly testing this File. Some tests require mocking the entire module, while others only need specific methods mocked. I have tried various combinations, but currently, for one specific test below, I am attempting the f ...

Create a randomly generated value in a JSON format

{ "description": "AppName", "name": "appName", "partnerProfile": { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19090b1969c90989ddf929e9c">[email protected]</a>", "firstName": "John", ...

The Jest worker has run into 4 child process errors, surpassing the maximum retry threshold

I am a newcomer to Vue and Jest testing, and I keep encountering this error when running a specific test. While I understand that this is a common issue, I am struggling to pinpoint the exact cause of the problem. Here is the error message: Test suite fa ...

Guide to passing arguments to a function within Vue's v-for loop?

Is there a way to pass the parameter {{ item.id }} to my function productos.mostrarModal() within a v-for loop in vue.js? <div v-for="item of articulos"> <a href="#" onclick="productos.mostrarModal()" > <h2>{{item.name}}< ...

What is the most effective method for incorporating CSS using Javascript to target a specific aria-label attribute?

Is there a way to add a CSS property to a specific tag with a particular aria-label on document load? My goal is to change the flex order of a section to 2. I need help using JavaScript to target the aria-label 'block 1' so I can set the order t ...

The linking process in AngularJS is encountering difficulties when trying to interact with

I've already looked at similar questions, and my code seems correct based on the answers provided. It's a very simple beginner code. <html ng-app=""> <head> <title>Assignment</title> <script src=" ...

In JavaScript, when a condition is met, two strings are produced but only the last string is printed

My for loop with nested arrays is working fine, but it should display two strings and only shows the last one. for (i = 0; i < $scope.taskGroups.length; i++) { for (j = 0; j < $scope.taskGroups[i].tasks.length; j++) { ...

The function req.isAuthenticated() always returns false and never evaluates to true

My goal is to create user authentication using the following code: userRouter.post("/login", passport.authenticate("local", { session: false }), (req, res) => { if (req.isAuthenticated()) { const { _id, username } = req.user; const t ...

Include personalized headers to the 'request'

I have configured my express server to proxy my API using the following setup: // Proxy api calls app.use('/api', function (req, res) { let url = config.API_HOST + req.url req.pipe(request(url)).pipe(res) }) In this instance, confi ...

What did I overlook in my AJAX implementation?

When a user selects a value from the dropdown menu, an Ajax call must be made to the server to retrieve some values in JSON format. Below is the Ajax code //AJAX Security $('#ddlSecurityLevel').change(function () { if ($('#ddlSecurityL ...

Using Express to Deliver Static Content to Subdirectories

I am currently using Express to serve a React application that was bootstrapped with Create React App. The project has the following directory structure: |--client/ | |--build/ | | |--static/ | | | |--main.css | | | |--main.js | ...

What is the best way to sort an array based on a person's name?

I have a list of different groups and their members: [ { "label": "Group A", "fields": [ { "value": "color1", "name": "Mike" }, { &quo ...

Display a preloader image while waiting for the content to load using jQuery or AJAX

Hey there, I could really use some help with a little issue. I've been trying to use the click() and load() functions to bring my content into a specific CSS class. $("#feed").click(function(){ $(".homefeed").load("feedcontainer.php"); ...

What is the best way to retrieve the text input fields while using express-fileupload with React?

Front end Uploading a File: <div className="form-group row"> <label htmlFor="directorySSH" className="col-sm-3 col-form-label">Directory: </label> <div className="col-sm-7"> <input ty ...

The payload transmitted from JavaScript to Django Views is devoid of any content

I'm currently working on sending data from JavaScript to Django using ajax. Below is the code snippet I am using for this: var json_name = {'name': 123} $.ajax({ method: 'POST', url: ...

Performance of rendering millions of faces with Three.js using Collada files

Dealing with two .dae files, one containing 1 million faces and the other with 20 million faces, I imported them utilizing ColladaLoader. Interestingly, the 20 million faces file is running smoothly at 30fps, while the 1 million faces file struggles at 2 ...

What could be causing the state to continuously appear as null in my redux application?

Currently, I am in the process of developing a basic contacts application to gain expertise in React and Redux. The main focus right now is on implementing the feature to add a new contact. However, I have encountered an issue where the state being passed ...