Developing controls for vertical movement

if ( upDownMovement == true) {  
                        SELECTED.position.y = mouse.y * scale;
                              }

In my attempt to create a vertical movement control that only allows for direct up and down motion along the Y-axis, I discovered a critical flaw in my implementation. The issue lies in the discontinuity between the initial mouse position and the model's original position, resulting in a noticeable "jump" at the beginning of the movement.

I am now seeking guidance on the correct approach to implementing this type of control. If there are any libraries or resources available that excel in this area, I would greatly appreciate any recommendations.

One potential solution I'm considering is modifying the code to read as follows: SELECTED.position.y = SELECTED.position.y + mouse.y * scale; however, I am open to suggestions and feedback from experienced developers.

Answer №1

It is important to capture the initial mouse position when upDownMovement becomes true and use it as a reference point. Here is an example:

if (upDownMovement == true) {
  mouse_y_reference = mouse_y_reference || mouse.y;
  SELECTED.position.y += (mouse.y - mouse_y_reference) * scale;
} else {
  mouse_y_reference = null;
}

For a smoother movement, consider using an animation library like tween.js with the following approach:

mouse_y_reference = mouse_y_reference || mouse.y; // ensure this variable is declared in a higher scope
var toY = SELECTED.position.y + (mouse.y - mouse_y_reference) * scale;
new TWEEN.Tween(SELECTED.position)
  .to({ y: toY }, 100) // adjust the duration as needed
  .easing( TWEEN.Easing.Elastic.InOut )
  .onComplete(function() {
    mouse_y_reference = null;
  })
  .start();

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

Encountering issues with tesseract callbacks in nodejs due to validation errors

I encountered the following error: fs.js:132 throw new ERR_INVALID_CALLBACK(); ^ TypeError [ERR_INVALID_CALLBACK]: Callback must be a function at makeCallback (fs.js:132:11) at Object.fs.unlink (fs.js:1002:14) at /home/bakedpanda/Documents/BTP/ ...

Deactivate Mongoose connection in Node.js after completing tasks

Here is a mongoose script I have been using to connect to the local database and perform some operations. However, I am facing an issue with disconnecting the connection after use. const mongoose = require('mongoose'); const db = mongoose.connec ...

Reveal the array on the frontend in a row each time using Javascript

My array contains the following items: let array=["First", "Second"] I want to display each item on a separate line (without commas), like this: First Second I attempted splitting the array using: let data.split("\n") However, the output was: ...

Asynchronously retrieving results in MongoDB

My task involves fetching all users from the users collection. app.post('/login', function(req,res,next){ users = self._db.get('users', {}) }) Below is the function in my database class: this.get = function( col, opt ) { ...

Turn off a feature

I'm having trouble disabling a tooltip that is being utilized from this specific website. The code for setting and calling the tooltip looks like this: this.tooltip = function(){....} $(document).ready(function(){ tooltip(); }); I've attempte ...

Transform Python list into a JavaScript array

Seeking quick assistance from experts as I face an intriguing challenge that has me stumped. In a Python .psp file, I have a list mylist[] that is populated at runtime and a JavaScript function that requires a list to dynamically create a form object. The ...

Why is there a blank white space under my image in Safari?

I am struggling to understand why Safari is adding a white space at the bottom of the image on my current website project. Interestingly, Firefox and Chrome display it correctly. The image should stay fixed at the bottom of the page even when resized. He ...

Employ node's gm library in combination with promises and buffers

Using gm with Bluebird has been a bit tricky for me. Initially, I tried this approach: var gm = require('gm'); var bluebird = require('bluebird'); gm = bluebird.promisifyAll(gm); However, when attempting to execute the following code: ...

Objects within objects in TypeScript

Looking to nest JavaScript objects in TypeScript? Here's how: let endpoints = { auth: { login: "http://localhost:8079/auth/login" } }; If you try the following code, it won't work as expected: private endpoints: Object = { ...

Embracing unconventional attributes within AngularJS

After converting my model from Java to JavaScript using GWT 2.7, I have a set of properties with getValue() and setValue() methods that are not obfuscated. I am interested in using these properties in {{}} expressions, especially for data binding with ngM ...

Tips on passing a variable into a controller using jQuery AJAX from the URL

Can you help me figure out how to pass an id to a function in the controller? I keep getting an error saying it cannot find the method in the controller. Here's my view: function Delete1(id) { if (confirm("Are you sure?")) { $. ...

Creating Bi-directional Computed Properties with Vue.js Class Components

Is it feasible to utilize two-way computed properties in vuejs while working with class-style components? For my specific scenario, involving a basic vuex store application, is there a method to connect the store values to a select using v-model? The vuex ...

When working with THREE.js in Electron, web development tools seem to vanish into thin air

Exploring electron is fairly new to me (if you know of any good documentation, please leave it in the comments) and I've encountered an issue that has left me puzzled: Everything seems fine until I load the THREE.js library. At that point, even thoug ...

What is the process for obtaining a Facebook page ID using the Facebook page name?

Can someone please explain the process of obtaining a Facebook page ID from the Facebook page name? I noticed on this website that it displays the number of likes based on the Facebook page name. If anyone has any insight on how to achieve this, I would ...

Discover the hex codes for automatically generated shades in the Material UI Color Palette

When working with my material ui theme, I want to incorporate different shades of my defined colors. Initially, I only define the main color: const theme = createMuiTheme({ palette: { primary: { main: "#C5FFA1", }, ...

Error: The property 'condition' is not defined and cannot be read

I've been diving into the world of React and I encountered a roadblock while working on a weather application. Below are snippets from my component and app.js files: import React from 'react' const WeatherCard = (props) => { ret ...

What is the process for creating URL query parameters?

What is the process for generating parameters after node?_=xxxxxx? If I am using a Python script to access the URL, how can I retrieve these parameters? edit: I apologize for not providing enough information. This is my first post as a novice. I am atte ...

Using jQuery to bind the "CTRL+A" key combination to exclusively selecting a specific region

My goal is to modify the CTRL+A shortcut so that instead of selecting all text, it will only select specific content within containers with a class of xyz. Unfortunately, I have not been successful in getting this functionality to work. Even attempting to ...

The blend of Combination and Advanced Timeline triggers a "ReferenceError: Highcharts is not defined" error

Replication Steps First, download HIGHCHARTS 4.1.9 from http://www.highcharts.com/download Next, open the file index.html Then, click on Combinations > Advanced timeline An error message is displayed in Firebug and the chart does not appear: Referenc ...

Is there a way to utilize JSON parse for converting deeply nested keys from underscore to camelCase?

A JSON string containing an object is on my mind: const str = `[{"user_id":"561904e8-6e45-5012-a9d8-e2ff8761acf6","email_addr":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa0a3a6a5bc8aaca ...