Unusual parameter handling in an ES6 Class

In my class, there is a parameter called values that stores the points representing shapes on a canvas. I want to be able to drag these shapes around by adjusting each point based on how much they have been dragged.

To achieve this functionality, I came up with a plan. When I start dragging (by triggering the mousedown event), I save the initial values of my points in a variable called startValues. Then, as I move the mouse around, I update the values using the startValues and the distance between the starting point and the current mouse position.

However, I encountered an issue where this.startValues keeps getting changed to match this.values every time the cursor moves, and I can't figure out why. Am I overlooking something simple?

Since I store my points as values rather than coordinates (which helps with panning and zooming on the canvas), I convert the values to positions, modify the positions, and then convert them back to values. Below you can see the parent class, Grf, which includes the methods for converting values to positions and vice versa.

Class with the problems

class Test {
    constructor(grf){
        this.grf = grf; 
        this.values = []; 
        this.startValues = []; 
    }

    startMove(p0){ 
        const {grf} = this;

        this.startValues = [...this.values]; 
        this.p0 = p0;

        grf.canvas.addEventListener('mousemove',this.move);
        grf.canvas.addEventListener('mouseup', this.endMove);
    }

    move = (evt) => { 
        const {grf, p0, values, startValues} = this;

        const coords = grf.canvas.getBoundingClientRect();
        const px = evt.clientX - coords.left;
        const py = evt.clientY - coords.top;

        for (let i = 0, iMax = this.values.length; i < iMax; i++){
            values[i][0] = grf.valX(grf.posX(startValues[0]) - (p0[0] - px));
            values[i][1] = grf.valY(grf.posY(startValues[1]) - (p0[1] - py));
        }

        console.log(this.startValues); 
    }

    endMove = (evt) => { 
        const {grf} = this;
        grf.canvas.removeEventListener('mousemove',this.move);
        grf.canvas.removeEventListener('mouseup', this.endMove);
    }
}

The other class

class Grf {
    constructor(canvas){
        this.translateX = 1000;
        this.translateY = 1000;
        this.scaleY = 10.7;
        this.scaleX = 11.2;
        this.canvas = canvas;
    }

    posX (value){
        return (value-this.translateX)*this.scaleX;
    }

    posY (value){
        return (this.canvas.height-(100*(value))-this.translateY)*this.scaleY;
    };

    valX(pos){
        return (pos/this.scaleX) + this.translateX
    }

    valY(pos){
        return (-1)*((pos/this.scaleY) + this.translateY - this.canvas.height)/100
    }

}

Answer №1

When inserting values into the arrays startValues and values within the Test class, it's important to note that you may unintentionally insert the same object in both arrays without actually duplicating it. As a result, both arrays end up holding references to the same instances.

Consider the following example for clarification:

const obj = { a : 10 };
const a = [];
a.push(obj);
const b = [...a]; // creates new array, but with same objects
a[0].a = 20;
console.log(b[0]) // outputs "{ a : 20 }"

To ensure separate instances, you need to create a copy of the object like this:

a.push({...obj})

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

Issue with rendering D3 horizon chart is not displaying

I am attempting to create a horizon chart using D3.js and Horizon.js, inspired by this example from Jason Davies. I am working with fitness tracker data. However, the example by Mike Bostock uses a unique JSON structure and performs some complicated row-t ...

Developing a state object encompassing all the columns of a data table for sorting purposes in React JS

Trying to create a state object to maintain field names and sorting types (ascending/descending). Implementing server-side sorting by passing the column name and sort type (asc or desc). I have a component for a data table with click handlers to determin ...

The key to creating efficient routers in Express: Don't Repeat Yourself!

Currently, I am in the process of developing a web application in the form of a network structure that involves basic CRUD operations. However, I am facing the issue of having overly large router files, prompting me to consider splitting them up. One of t ...

What is the best way to retrieve the slot object property in order to send it to a component for a specific data table row in Vuet

I have defined a vue data property as shown below: data() { orders: [], order: { order_id: null length: null, width: null, } } Additionally, I have implemented a vuetify data table structured like this: <v-data-table v-if=& ...

Is it possible to achieve seamless image transitions in Firefox like it does in Chrome?

To achieve the desired effect, you may need to use some javascript. Visit aditagarwal.com for more information. Styling with CSS: .images-wrapper{ position: fixed; left: 0; top: 80px; bottom: 0; width: 100%; height: 100vh; an ...

Error in Redux-tookit: The store is missing a valid reducer. Ensure that the argument provided to combineReducers is an object containing reducers as values

Uh oh! Looks like there's an error with the Store reducer. The argument passed to combineReducers needs to be an object with valid reducers. I'm having trouble setting up a Store for my app and I can't figure out where I went wrong. Could s ...

In jQuery, you can sort an array of objects based on a specific value pulled from a separate array of objects

Recently, I started learning about jQuery and encountered a problem. I am working with two arrays. var countryArray = [{ Code:'NL', Name:'Netherlands'}, { Code:'BE', Name:'Belgium'}, ...]; var customerArray = [{ Cod ...

Problem encountered when attempting to pass data from parent to child component

While using Vuu.js, I encountered an issue with passing a value from parent to child component. Initially, I had it working perfectly with a provided example. However, as soon as I tried changing the name, the functionality broke. I'm struggling to un ...

Error encountered: Mongodb db.collection.find() function is not successfully retrieving data, whereas the collection.insert() function

Working with node.js/express and utilizing a Mongodb database to store various data sets. The functionality for adding, editing, and deleting data on a webpage is functioning properly. For instance, the code snippet for adding data is as follows: router. ...

What is the most effective way to stop zooming when focusing on form fields on iOS or similar devices when using font sizes of 16px or lower?

It appears that many proposed solutions involve changing the text to 16px or using JavaScript to determine if the phone is an iPhone. However, altering the style may not be the most practical solution and checking for specific devices like iPhones could ...

Incorporating CSS stylesheets into transpiled Vue components within Cypress testing environment

Currently, my setup involves using Cypress with vue-loader to load Vue Single File Components (SFCs) through a webpack embedded preprocessor specifically for Cypress (webpack-preprocessor). Below is the configuration for this setup. const webpack_vue_cypr ...

When defining a stripe in TypeScript using process.env.STRIPE_SECRET_KEY, an error of "string | undefined" is encountered

Every time I attempt to create a new stripe object, I encounter the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string& ...

Leveraging ChartJS alongside JSON for dynamic chart rendering; When attaching JSON data to JavaScript, a blank object is displayed in console.log output

I am currently facing an issue with pushing JSON data into a JavaScript array. If the data were in Python, I believe it would be considered as a regular list. However, when I try to push the data into the array, it appears as "[]" in the console.log. Upon ...

Guide on implementing factory updates to the display

I am attempting to update a reference within my factory in an asynchronous fashion, but I am not seeing the changes reflected in my view. View <div ng-repeat="Message in Current.Messages">{{Message.text}}</div> Controller angular.module(&ap ...

Guide on moving an item from one form and including it as an option in a dropdown menu in another form using

Apologies if this question has been asked before, but I'm having trouble finding a solution! In summary, I am trying to dynamically include a new item in a drop-down menu within a form without losing any current edits that have been made. To provide ...

Executing a javascript function from PHP when a form is submitted: a comprehensive guide

I am looking to trigger a javascript function from PHP when the form is submitted. This function will have access to PHP variables and will use AJAX to send them to a PHP script on another website. The code below serves as an example: <?php .... ..... ...

If an interface property is set as (), what significance does it hold?

While exploring the Vue.js source code located at packages/reactivity/src/effects.ts, I came across this snippet: export interface ReactiveEffectRunner<T = any> { (): T effect: ReactiveEffect } I'm curious, what does () signify in the code ...

Using Node.js to return JSON data containing base64 encoded images

In my database, I store all images as base64 with additional data (creation date, likes, owner, etc). I would like to create a /pictures GET endpoint that returns a JSON object containing the image data, for example: Image Data [{ "creation": 1479567 ...

Basic text deletion script

Trying to find a solution for this problem has been quite the challenge. Despite my efforts in searching and Googling, I have yet to find the perfect answer... Would anyone be able to create a script that can remove all occurrences of <a href="http:// ...

What is the best method for extracting data from a JSON object that is structured differently than a traditional array format?

What is the best way to parse a JSON object with the specified structure? { "cutoffTimes" : { "85c46c49-99b6-47a1-9726-960c8fe6c337" : { "id" : "85c46c49-99b6-47a1-9726-960c8fe6c337", "customer ...