Error in manipulating drag-and-drop movement using WebGL

I'm currently working on adding camera movement to my WebGL project through drag-and-drop functionality. I've achieved this by calculating the differences in position from the previous event and then adding that value to the current rotation level.

Below is a snippet of the code responsible for this task:

function animate()
{
    // Drag-and-Drop Section
    /*var spdx = 0, spdy = 0;
    spdy =  (HEIGHT / 2 + difference_y) / 400;
    spdx =  (WIDTH / 2 + difference_x) / 400;*/

    if(mouseDown)
    {
         camera.rotation.x += difference_x / 200;
        //camera.rotation.y += difference_y / 200;
    } 

    requestAnimationFrame(animate);
    renderer.shadowMapEnabled = true;
    renderer.shadowMapType = THREE.PCFSoftShadowMap;

    object.rotation.x += 0.005;
    object.rotation.y += 0.01;

    // Render the scene.
    renderer.render(scene, camera);
    //controls.update();

}

function onDocumentMouseMove(event){
    mouseX = event.clientX;
    mouseY = event.clientY;

    difference_x = mouseX - last_position_x;
    difference_y = mouseY - last_position_y;

    last_position_x = mouseX;
    last_position_y = mouseY;
}

The section within the animate() function up to requestAnimationFrame(animate) pertains to handling mouse events. The variable mouseDown is set to true on the mousedown event and reset to false at mouseup. You can access the full code as it appears in the files here, although it may not function correctly within the fiddle.

To accurately demonstrate the issue, I have restricted movement in one direction and provided a test environment here. Feel free to explore and identify any potential problems.

If anyone has insights into where the problem might lie, I would greatly appreciate your input. Thank you.

Answer №1

After some troubleshooting, I discovered that the issue lied within the mouseDown event handler. It was crucial to also keep track of the current coordinates within this function.

function onDocumentMouseDown(event){
    mouseDown = true;
    last_position_x = event.clientX;
    last_position_y = event.clientY;
}

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

When employing $.getScript (jquery), the code fails to run as expected

I'm currently in the process of revamping my website, which now utilizes an AJAX engine. While the engine is generally functioning well, there seems to be some issues with pages not executing JavaScript as intended. Let me elaborate: when the anchor c ...

Personalizing the label of a select input using materialui

Working on customizing a select element using the "styled" method: const StyledSelect = styled(Select)` height: 2rem; color: #fff; border-color: #fff; & .${selectClasses.icon} { color: #fff; } & .${outlinedInputClasses.notchedOutl ...

Instructions for subtracting the value of a cell in column 22 from a cell in column 24 within the same row when a change trigger occurs utilizing Google Apps Script

I need help modifying the script below to only subtract the row on which the change is made, instead of subtracting all rows in the sheet when the on-change trigger executes. var sourceSpreadsheetID = '1r4e4BNKwsmdC2Ry93Mq-N49zj3DAZVpHG21TgTe0FWY&a ...

What is the reason my jQuery AJAX GET call is not receiving a response from the server?

I am encountering an issue where jQuery is supposed to dynamically add HTML to my page, but I am not receiving any response from the server. My JavaScript code sends a GET request with a parameter (bodypart), expecting a response from the server including ...

The reason for the issue stating 'ReferenceError: xmlHttpRequest is not defined' is likely due to the presence of certain IDs

Basically, I am trying to insert values into my database using Ajax in a WordPress custom template called company-update/?update=. This way, the page will not refresh and each record can be inserted individually. I have created a form along with JavaScrip ...

tips for utilizing namespaced getter filtering within a Vuex module in vueJs

In my custom module named ShopItemCategory, I have a Getter getters: { shopItemsCategories: state => state.ShopItemsCategories.data, }, Inside the component, there is a computed function I defined computed: { shopItemsCategories ...

utilizing an arrow function in the same manner as a traditional function

I am a fan of the new arrow ()=>{} syntax and would love to use it wherever possible. I understand that arrow functions point to the outer this context. Is there a way to modify an arrow function so that "this" points to its inner scope? For example, h ...

Inquire about the specific Node-RED version from within a function node

I am seeking to create code within a function node that will check for the Node-RED runtime version number. Depending on the version number, certain parts of the code within this function node may or may not be executed. This is crucial to me as I work wi ...

How can you use plain javascript to drag and drop the cross sign within the box in the grid?

Creating a grid in HTML where clicking on a box will draw an x sign and remove it if clicked again. View the DEMO here. Challenge Description:- I am now looking to implement dragging the cross (x) sign to another grid within the box, but cancelling the ...

There seems to be an issue with the pastebin api createPasteFromFile method as it is showing an error of 'create

I'm currently working on a logging system using node for a twitch chat. The idea is that when you type "!logs user" in the chat, it should upload the corresponding user.txt file to pastebin and provide a link to it in the chat. For this project, I am ...

Tips for preventing unsightly lines between cubes when arranging them in a wall formation

Recently, I have been experimenting with creating a pathfinder using WebGL technology: As part of this project, I am building walls by aligning cubes together. However, an issue that has arisen is the appearance of lines at the boundaries of the cubes. Ca ...

Unit testing in AngularJS: Initializing the controller scope of a directive

Here is the code for a directive with a separate controller using the "controller as" syntax: 'use strict'; angular.module('directives.featuredTable', []) .controller('FeaturedTableCtrl', ['$scope', function ($sco ...

What is the best way to transfer the array from my function and have it returned?

I've been struggling to successfully pass my array to an external function. Can anyone help me with this? You can find the code that I'm working on in jsbin: https://jsbin.com/kugesozawi/edit?js,console,output. The expected result should be passe ...

Creating a time-based scatter plot using chart.js

As I attempt to visualize a scatter plot using chart.js with (x,y) data where x represents a date string, I have noticed that most tutorials and examples available online utilize a function to generate a timestamp for demonstration purposes. However, I am ...

Can a string starting with certain values be omitted?

How can I determine if a value begins with any of the values in an array? var value = "background-color"; var value2 = "--my-variable"; var excludeItems = ["--", "-", "_"]; if (checkForStartsWith(value, excludeItems)) { // Exclude the value } ...

completed() versus finished()

As I was going through the documentation for passport, I came across an interesting observation regarding the use of serialize() and deserialize(). It seems that done() is called without being explicitly returned in one scenario. However, while setting up ...

The Facebook JS SDK is causing an XSS error and returning a "user_denied" response, even though the Auth Popup for FB.Login is not being

I currently have a Facebook canvas app located at Previously, I was using an anchor link to direct users to the OAUTH login flow. After implementing the Facebook JavaScript SDK, I followed this logic: 1) Initialization of the FB API FB.init({ a ...

The Blueimp File Uploader seems to be sending numerous submissions at once

I've been tasked with fixing an issue on our site that I didn't originally build. The previous developer who worked on this project is now occupied with another task, leaving me to figure out what's going wrong. We are utilizing the basic bl ...

Inline CSS for altering the appearance of text within a DIV container

Within the confines of this DIV element... <div name="layer1" style="background-color:lightblue;"> <hr>Site:Downtown DataCenter Device: 2KBS</hr> </div> Hello there, I am utilizing Inline CSS Styling. Is it possible to make the t ...

What advantages does incorporating SSR with dynamic imports bring?

How does a dynamic imported component with ssr: true differ from a normal component import? const DynamicButton = dynamic(() => import('./Button').then((mod) => mod.Button), { ssr: true, }); What are the advantages of one method over the ...