Dynamic Dot Layout Mapping in Vue.js and Canvas: A Problematic Coordinate Issue

My current project involves using Vue and HTML canvas to create a responsive "link game". You can view a sample of my work in the image below:

However, I've encountered a strange issue. I'm trying to determine if a line drawn between two dots is considered "legitimate". Here's an illustration of what constitutes a "legitimate" connection:

In order to achieve this goal, I've been utilizing a variable to store information about each dot's location:

this.Dotlocation[[RowID,ColID,GroupID],[x_location,y_location]]

Unfortunately, no matter what I do, the information doesn't seem to be accurate. Here's the output I'm getting:

[ 
 // id,col,group
 [ [ 3, 2, 4 ], [ 631.4, 417.43 ] ], 
 [ [ 3, 1, 3 ], [ 495, 417.43 ] ], 
 ...
 ]

There are a couple of questions that arise from this data:

  1. I don't understand why the IDs are out of order.
  2. The grouping and columns appear to be incorrect; it appears to be related to the final column.

You can find the lengthy code that I've been working with below for reference:

DrawImgOnCanvas(question,context1){
    // Code block here
},

Here is the input data being used:

question:[  
['assets/GamePic/Cat.png','assets/GamePic/Cat.png','assets/GamePic/Cat.png'],
['assets/GamePic/Cat.png','assets/GamePic/Cat.png','assets/GamePic/Cat.png','assets/GamePic/Cat.png','assets/GamePic/Cat.png'],  
['assets/GamePic/Cat.png','assets/GamePic/Cat.png']
],
    
Thank you for taking the time to read through this. I look forward to your response.

[1]: https://i.sstatic.net/MmxjB.png
[2]: https://i.sstatic.net/MoZX6.png

Answer №1

The sequencing of the order is not as expected due to the asynchronous nature of loading images. This means that there is no guarantee that all the code within the onload function will execute in the anticipated order.

Furthermore, since the values for col and group may change before the onload event is triggered, it explains why these values are not what was initially expected.

To ensure that the code behaves as intended, the implementation of async/await is necessary. Only a couple of modifications are required, which are denoted by:

// ==========================

It's worth noting that the onload block is now aligned differently, but this does not constitute a change in the code structure.

// ==========================
// make the function "async"
async DrawImgOnCanvas(question, context1) {
    let Column_Amount = question.length;
    var onchangegroup = false;
    let Column_ID = 1;
    this.Group = 1;
    for (let col = 0; col < question.length; col++) {
        const Max_Img_Size = this.CountMaxImgSize(this.QuestionDataStructure[col].length);
        let RWD_Colum_Gap = this.CountRWDYGap(question[col]);
        for (let Dot_Row_ID = 0; Dot_Row_ID < question[col].length; Dot_Row_ID++) {
            let Img = new Image();
            let Image_Url = icon;
            Img.src = Image_Url;
            let Img_Size = this.ResizeRWDImg(Max_Img_Size, Img);
            let x = this.Min_border + Max_Img_Size.Max_Width * col + this.RWD_Gap_Width * col;
            let y = this.Min_border + RWD_Colum_Gap + Max_Img_Size.Max_Height * Dot_Row_ID;

            // ========================================
            // This section waits until the image has finished loading
            //
            await new Promise((resolve) => (Img.onload = resolve));
            context1.drawImage(Img, x, y, Img_Size.New_Width, Img_Size.New_Height);
            context1.beginPath();
            if (col == 0) {
                context1.arc(x + Img_Size.New_Width + this.Min_border, y + Img_Size.New_Height / 2, this.DotRadius, 0, Math.PI * 2, true);
                this.DotLocation.push([[Dot_Row_ID, Column_ID, this.Group], [x + Img_Size.New_Width + this.Min_border,y + Img_Size.New_Height / 2]]);
                context1.fillStyle = "black";
                context1.fill();
            } else if (col == Column_Amount - 1) {
                context1.arc(x - this.Min_border, y + Img_Size.New_Height / 2, this.DotRadius, 0, Math.PI * 2, true);
                this.DotLocation.push([[Dot_Row_ID, Column_ID, this.Group], [x - this.Min_border, y + Img_Size.New_Height / 2]]);
                context1.fillStyle = "black";
                context1.fill();
            } else {
                //Right
                context1.arc(x + Img_Size.New_Width + this.Min_border, y + Img_Size.New_Height / 2, this.DotRadius, 0, Math.PI * 2, true);
                this.DotLocation.push([[Dot_Row_ID, Column_ID + 1, this.Group + 1], [x + Img_Size.New_Width + this.Min_border,y + Img_Size.New_Height / 2]]);
                //Left
                context1.arc(x - this.Min_border, y + Img_Size.New_Height / 2, this.DotRadius, 0, Math.PI * 2, true);
                this.DotLocation.push([[Dot_Row_ID, Column_ID, this.Group], [x - this.Min_border, y + Img_Size.New_Height / 2]]);
                context1.fillStyle = "black";
                context1.fill();
            }
            context1.closePath();
        }
        if (col != 0) {
            this.Group++;
            Column_ID += 2;
        }
    }
},

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

The issue I'm facing is that the ng-click functionality in Angular JS is not functioning properly when the

Upon loading the page, my JavaScript function creates a tree structure from JSON data and generates a list of anchor tags which look like this: <a ng-click="shareParent(4619)">Some data</a> Despite associating the ng-click directive with the ...

Combine Immer and NgRx reducer for improved state management

Upon analyzing redux and ngrx, it appears that immer is the preferred library for creating a copy of the state before storing it. In following the example provided by immer, I implemented the following code in my reducer: on(exampleActions.updateExample ...

Script for running a React app with Prettier and eslint in a looping fashion

My Create React App seems to be stuck in a compile loop, constantly repeating the process. Not only is this behavior unwanted, but it's also quite distracting. The constant compiling occurs when running the default npm run start command. I suspect t ...

How can I attach the input value that has been selected to another div using Ajax AutoComplete for jQuery?

Greetings, I am currently utilizing the Ajax AutoComplete for jQuery library. Within this library, there are 2 demos available. In the first demo titled "Ajax auto-suggest sample (start typing country name)", when a country is selected from the dropdown, ...

Generate tables and rows dynamically

I am looking for guidance on dynamically creating a table and adding rows to it. I have successfully created a table with one row containing form fields, but I am unsure how to add additional rows. Any examples or suggestions on how this can be implemented ...

The YDN-DB plugin for full text search is currently blocking certain queries

A new plugin has been integrated to enable full-text search capabilities with YDN-DB. Although full text queries are now functioning properly, existing queries intended to retrieve a list of data are no longer operational. For instance: db_mob_audit.from ...

I attempted to utilize the prop to transfer an object, but unfortunately, the value was not received

I am working with a child component that accepts a todo object. Vue.component('child', { props: ['todo'], template: '<span>{{ todo.text }}</span>' }) var vm = new Vue({ el: '# ...

Encountering issues with reading the length property error within the jsonstore function in the MobileFirst

Hello, I'm a newcomer to the world of Javascript and MobileFirst. I am currently attempting to retrieve and display a list of JSON values: [{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}] However, when trying ...

What is the best way to connect an HTML image to effortlessly switch between displaying and hiding an element (specifically, a table) using JavaScript?

Apologies if my question is a bit confusing and poorly written, as I am new to coding and Stack Overflow. What I am trying to achieve is the ability to click on each image and have a different table pop up for each planet with facts (an example of the Merc ...

How to access a particular tab in Bootstrap 5 using an external link

Is there a way to direct users to a specific tab upon clicking a link on another page? Check out the example below: <div class="products-btn"> <a href="products.html#pills-profile">view all</a> </div> On Pro ...

Enhance the annotation of JS types for arguments with default values

Currently, I am working within a code base that predominantly uses JS files, rather than TS. However, I have decided to incorporate tsc for type validation. In TypeScript, one method of inferring types for arguments is based on default values. For example ...

HTTPInterceptor failing to capture incoming HTTP requests from external widget in Angular

Within my application, I incorporate a third-party UI widget obtained from the npm registry as a dependency. This widget makes a GET request using the axios module when integrated into my app's user interface. However, I have observed that the HTTP G ...

What is the reason behind the for of loop breaking within an async function instead of properly awaiting the execution?

Update 2: I made changes to use setTimeOut instead, which fixed the issue. Check out the accepted answer for details on what was causing the error. The code below is now functioning properly. async function getSlices() { const imgBuffs = await sliceImg ...

What is the best way to retrieve JavaScript Values through Selenium?

Currently, I am working with Java selenium client and running this code snippet. The variable PAGE_NUMBER is assigned a value; however, when using selenium, I'm unable to retrieve it: String script = "var cellValue = selenium.browserbot.getUserWindow ...

Script for Ajax not working properly

I am facing a problem with my ajax request where it still loads the php script instead of executing its function without refreshing. I suspect there might be an issue with my ajax. Can someone please review the ajax script and point out any errors? HTML ...

Retrieving and securely storing information using fetch() on authenticated REST services

Currently, I have successfully set up a React application that communicates with a REST backend which is built using Python and Flask. The specific functionality I have achieved involves downloading data from a database and saving it as a CSV file through ...

Tips for interacting with a modal using Bootstrap Vue

I have implemented a sophisticated modal in its own component. It operates on a duplicate of the supplied model to enable the user to cancel the action. <template> <b-modal v-if="itemCopy" v-model="shown" @cancel="$emit('input& ...

The RxJS scan operator has the ability to retrieve only the most recently pushed partial data

I am facing a challenge with managing a BehaviorSubject. My goal is to update it by passing a Partial<Type> and I have implemented the following code structure. The use of scan helps in merging new and old data seamlessly. personUpdates$ = new Behavi ...

Issue with React Google Maps persists with StandaloneSearchBox in code causing it to be stuck on "Loading" status

I am developing a React JS page where I want to enable companies to register on my website and add their locations for rentals, purchases, etc. The main feature I need is to implement Markers on the map. While I have successfully integrated Google Maps ont ...

Passing the title of a page as data to a component in Next.js

I am currently working on setting a custom title for each page within my next.js project. Here is an example of a simple Layout: const MainLayout = props => { return ( <Layout style={{ minHeight: "100vh" }}> <Head> < ...