The effectiveness of mouseup and mousemove events diminishes when employed in an external module

Currently, I am immersed in a three.js project that incorporates standard orbit controls on an object, specifically a particle cloud. Everything runs smoothly when this logic is embedded within my root class along with all the three.js code. However, my goal is to separate this logic into an external class for better organization.

Unfortunately, when I achieve this separation, the 'mousemove' and 'mouseup' events fail to trigger. Oddly enough, the touch events function flawlessly and the orbit behaves as intended.

I have experimented with using 'preventDefault()' in the calls, but it doesn't seem to yield any significant changes. Substituting 'window' for 'document' since we are now in a module also did not resolve the issue.

What could I be overlooking? The structure of the module is as follows:


var orbitElement;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

var Controls = {

    enableOrbiting: function(target) {
        orbitElement = target;
        document.addEventListener( 'mousedown', this.onDocumentMouseDown, false );
        document.addEventListener( 'touchstart', this.onDocumentTouchStart, false );
        document.addEventListener( 'touchmove', this.onDocumentTouchMove, false );
    },

    onDocumentMouseDown: function( event ) {
        document.addEventListener( 'mousemove', this.onDocumentMouseMove, false );
        document.addEventListener( 'mouseup', this.onDocumentMouseUp, false );
        document.addEventListener( 'mouseout', this.onDocumentMouseOut, false );
        mouseXOnMouseDown = event.clientX - windowHalfX;
        targetRotationOnMouseDown = targetRotation;
    },
    
    // More functions...

}

module.exports = Controls;

Answer №1

this may not be what you anticipate.

If you invoke controls.enableOrbiting(), then within the enableOrbiting function, the context of this is the same as controls. However, when you attach this.onDocumentMouseDown as an event listener, the context of this within the onDocumentMouseDown method refers to the event emitter, which is document, and not controls.

UPDATE: Ensure to bind to Controls once it has been assigned.

You can bind each function to Controls, like so:

var Controls = {}
Controls.onDocumentMouseDown = function (event) {
 ...
}.bind(Controls);

By doing this, the context of this will consistently be Controls, regardless of how the this keyword is used in the method invocation.

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

Using the Vue.js Spread Operator in place of Vue.set or Vue.delete

I'm exploring ways to utilize the spread operator for adding or removing object properties in a manner that preserves reactivity. Within a Vuex mutation, this code snippet is successful: Vue.set(state.sportTypes.sports, sportName, sportProperties) H ...

The Discord OAuth2 bot fails to assign roles to authenticated users

While working on OAuth2 login for my website, I encountered an issue. After a user successfully logs in through OAuth2, the bot should assign a role to them on my Discord server. However, when I tested the login process myself, the bot returned an error me ...

I am unable to display the service response in the Angular component

I'm facing an issue with displaying data in an angular component from a service. The process from the service to the component seems fine, but when I try to use the variable in HTML, it doesn't show the result. For this project, I am using the M ...

Create a downloadable document in AngularJS containing specific data

I need to collect user input values such as name, address, and phone number. Once the values are entered, I want to dynamically generate a downloadable MS Word document file on button click using AngularJS. How can I accomplish this task? Is it feasible t ...

I am struggling to figure out how to make the responsive menu close

#switch image var up = 'https://image.flaticon.com/icons/svg/149/149187.svg'; var down = 'https://image.flaticon.com/icons/svg/128/128397.svg'; $('#resNavToggle').click(function() { if ($('.resNavIcon').attr( ...

Executing a function that includes showModalDialog outside of an iframe might display the incorrect page

This website structure looks like: `--main.html `--dialog.html `--folder `--iframe.html The code for each page is as follows: main.html: <html> <head> <script type="text/javascript"> function t ...

Node.js allows you to seamlessly upload multiple images from various form fields or input types simultaneously

<form action="/upload-images" method="post" enctype="multipart/form-data"> <input type="file" name="image1" /> <input type="file" name="image2" /> <input type= ...

Using React.js to Retrieve Data from the Server without AJAX Calls

Currently, I am in the process of creating a web application using Express.js for the back-end and React.js for the front-end. Prior to using React.js, I utilized EJS templating and followed a similar workflow on the back-end as shown below: var express = ...

Reversing the sequence of code in JavaScript - react native

I'm currently working on tracking the number of times a button is pressed within one second. For the most part, it's functioning correctly as it tracks and displays the count. The issue arises when it displays the button press count from the pr ...

What is the best method for sending XML data to a URL using JavaScript within an Adobe AIR application?

I am currently developing an application that involves downloading an XML string from a URL and then posting it to another URL. I have managed to successfully download the XML string and can manipulate it using alerts, however, I am struggling with posting ...

Guide to setting up a new user in Sanity with their profile pictures

Hey there, I am new to working with Sanity and currently tackling a personal project. My main query is regarding how to add a user along with their profile image (uploaded as a file from their device) to the Sanity Database. I need all the details entered ...

The Bulma calendar date input fails to display the pre-filled start date

I am facing a challenge while trying to integrate the Bulma calendar into my project, particularly when it comes to setting a default start date. According to the documentation, I am using the following method: <input type="date" data-start-date="10/2 ...

What are some helpful tools for organizing data from an External API in NodeJS and Express?

One of the tasks I'm currently working on involves connecting to an external API through my backend system. Data flow : External API -> My backend -> Client side Typically, I would use modules such as request or http to facilitate this p ...

Tips on assigning html actions/variables to a flask submit button

I am facing an issue with my Flask app where the form submission process takes a while for the backend to process. In order to prevent users from refreshing or resubmitting, I want to display a loading gif during this time. The current HTML code for my fl ...

Mixing success and error states can lead to confusion when using jQuery and Express together

I've been struggling with a simple question that's been on my mind for quite some time. Despite my searches, I haven't found a similar query, so I apologize if it seems too basic or repetitive. The scenario involves an API route (Express-ba ...

Launching URLs from JSON data in the system browser - Apache Cordova

I am facing an issue with opening links from a JSON in the system browser on both Android and iOS devices. The link generated by the function appears as: {{item.addressLink}}, instead of bit.ly/xyzxyz Here is what I currently have: <a href="#" ng-cl ...

Radio button is selected but textbox does not display

Struggling to get the radio button to display the textbox as intended, but unfortunately encountering issues. Here is the HTML code in the JSP page: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> ...

How can I dynamically load a 3D model (in JSON format) at the current location of the mouse using Three.JS?

I'm currently working on a feature that involves loading a 3D model based on the mouse's position. Utilizing jQuery drag and drop functionality for this purpose has helped me load the model onto the canvas successfully, but I'm facing issues ...

What is the best way to bypass TS1192 when incorporating @types/cleave.js into my Angular application?

Using cleave.js (^1.5.2) in my Angular 6 application, along with the @types/cleave.js package (^1.4.0), I encountered a strange issue. When I run ng build to compile the application, it fails and shows the following errors: ERROR in src/app/app.component. ...

Maximizing Efficiency: Using a Single Prototype-Instance Across Multiple HTML Pages

I have defined some Javascript "Classes" in separate files using the Prototype function as shown below: function Playlist(id, name){ this.id = id; this.name = name; } Playlist.prototype.getid = function(){return this.id;} Playlist.prototype.getn ...