Sorry, the value of pos[i] is currently unknown and cannot be

Looking at this specific array structure:

T1 = [
{
    tipo:"m",
    pos: [
        [0,0]
    ]

    },{
    tipo:"b",
    pos: [
        [0,0.223,0.1,0.5,0.11,0.5]
    ]
    },
    {
    tipo:"m",
    pos: [
        [0,0]
    ]
    }
    ]

My goal is to differentiate types and draw accordingly.

function caminho(c, a) {
c.beginPath();
a.forEach(function(value, i) {
if (value.tipo === "m") {
  c.moveTo(value.pos[i][0], value.pos[i][1]);
  console.log(value.pos);
  } else if (value.tipo === "l") {
       value.pos.forEach(function(pos, j) {
          c.lineTo(pos[0], pos[1]);
    });

    }
    else if(value.tipo === "q") {
      value.pos.forEach(function(pos,j) {
          c.quadraticCurveTo(pos[0], pos[1], pos[2], pos[3]);
    });
}
    else if(value.tipo === "a") {
    value.pos.forEach(function(pos,j) {
        c.arc(pos[0],pos[1],pos[2],pos[3],pos[4]);
    });
}
   else if(value.tipo === "b") {
    value.pos.forEach(function(pos,j) {
        c.bezierCurveTo(pos[0],pos[1],pos[2],pos[3],pos[4],pos[5]);
    })
}
 });
  c.closePath();
  }

I've encountered an issue where removing the last "m" type from T1 resolves the problem; however, I would like to achieve the following result with the T1 structure using arrays:

c2d.beginPath(); 
    c2d.moveTo(0, 0);
    c2d.bezierCurveTo(0,0.223,0.1,0.5,0.11,0.5);
    c2d.moveTo(0, 0);
c2d.closePath();

Answer №1

The variable i represents the index of an item in your array, starting from 0 and incrementing by 1 each time.

In this case, your pos array only contains one element as shown in the question example. Therefore, you need to replace the code snippet:

c.moveTo(value.pos[i][0], value.pos[i][1]);

With:

c.moveTo(value.pos[0][0], value.pos[0][1]);

Here is the full code:

function caminho(c, a) {
  c.beginPath();
  a.forEach(function(value, i) {
    if (value.tipo === "m") {
      c.moveTo(value.pos[0][0], value.pos[0][1]);
      console.log(value.pos);
    } else if (value.tipo === "l") {
      value.pos.forEach(function(pos, j) {
        c.lineTo(pos[0], pos[1]);
      });
    } else if (value.tipo === "q") {
      value.pos.forEach(function(pos, j) {
        c.quadraticCurveTo(pos[0], pos[1], pos[2], pos[3]);
      });
    } else if (value.tipo === "a") {
      value.pos.forEach(function(pos, j) {
        c.arc(pos[0], pos[1], pos[2], pos[3], pos[4]);
      });
    } else if (value.tipo === "b") {
      value.pos.forEach(function(pos, j) {
        c.bezierCurveTo(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
      });
    }
  });
  c.closePath();
}

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

Do not allow navigation to another page until the form has been submitted

As part of my project, I have implemented a feature where new users are required to change their password upon logging into their account for the first time. For new users, the Change Password screen is displayed immediately after login. The menu options ...

What is the best way to selectively create transparency for a specific color in a three.js canvas?

I'm working with a three.js canvas that I generated using liquidfun.js. However, I'm encountering an issue where the canvas is not transparent. I want to make a specific color in the canvas fully transparent, such as the whitish background. Is t ...

retrieve JSON information using jQuery

Hi everyone, I'm trying to figure out how to retrieve JSON data using an AJAX call. I attempted the following method, but it doesn't seem to be working :P First off, I created a JavaScript file where I stored my JSON data: var food = [ ...

Different JavaScript entities with identical attributes (labels)

Even though JavaScript doesn't have tangible objects, I'm struggling to differentiate between them. Let's say we have two objects called Apple and Orange defined as follows: function Apple(){ this.name = "Apple"; } and function Orang ...

AWS Lambda encountered an issue: Task terminated unexpectedly prior to finishing the operation

I've scoured various Stack Overflow threads for solutions to my issue, but no luck so far. As a beginner in this field, I might not be implementing the suggested fixes correctly. Here's my code, and any help would be greatly appreciated. var mys ...

Unable to apply ng-class when condition is met after ng-click

I am currently experiencing an issue with ng-class. When I click, I pass a variable and then check if it is true in ng-class. If it is indeed true, I append the "col-xs-6" class. Here is what I have attempted: <div ng-class="{'col-xs-6': myV ...

The progress bar for Ng-file-upload does not function properly when used in conjunction with offline

Using ng-file-upload for file uploading in my home has been successful, as I am able to upload files without any issues. However, I encountered a problem where the progress bar only appears when offlinejs is disabled in the index.html file. It seems that ...

Transitioning from AngularJS version 1.5.0 to 1.5.8

My bower.json file contains various dependencies, including AngularJS at version 1.5.0. I am looking to update AngularJS to version 1.5.8 without causing issues for my team members. I attempted to use bower install angular#1.5.8 --save, but this resulted ...

Having issues with Jquery and duplicating tables functionality not functioning as expected

I am facing an issue with two external jQuery files. One file allows me to clone the last row of a table, while the other is supposed to retrieve the id of a select tag based on a class assigned to it. However, the second script only works for the original ...

Issue with forEach function not executing in Next.js useEffect

I am struggling to present all the 'posts' from my database in separate divs. The code successfully retrieves the posts and stores them in an array called posts. However, I encounter a problem when trying to loop through the posts using the forEa ...

Incorporating jQuery tooltips within a dynamically refreshing jQuery div

Having trouble with jQuery tooltips (using Tipsy) on a dynamically included page. The tooltips work well on regular pages, but when I include the page through PHP and set up auto-refresh using jQuery, the tooltips stop working properly. The issue seems to ...

decoding JSON without the need for jQuery

Is there a way to convert the result retrieved from an ajax call into a JavaScript array without relying on jQuery? Alternatively, would it suffice to simply loop through the JSON array without converting it into a JavaScript array? Currently, I just nee ...

Unable to save text to file in both Javascript and PHP

I'm facing an issue with my website signup form. It consists of fields for email and password. The HTML triggers a JavaScript function which, in turn, calls PHP code to save the email to a file on the server. While the JavaScript seems to be functioni ...

Is there a different option instead of relying on promises for asynchronous requests?

Let's consider a scenario where we have a basic front end application (perhaps using Angular) and a back end app. When the front end app performs a get request, in most cases, the Angular repository will initiate an $http.get request which will return ...

A guide to creating a dynamic ngTemplateOutlet in Angular

Below is the HTML code I attempted: HTML <ng-container [ngTemplateOutlet]="room"></ng-container> <ng-template #room1> test 1 </ng-template> <ng-template #room2> test 2 </ng-template> <ng-template # ...

The subsequent code still running even with the implementation of async/await

I'm currently facing an issue with a function that needs to resolve a promise before moving on to the next lines of code. Here is what I expect: START promise resolved line1 line2 line3 etc ... However, the problem I'm encountering is that all t ...

Achieve video lazy loading in JavaScript or jQuery without relying on any external plugins

I've been experimenting with lazy loading videos using jQuery. While I've had success with lazy loading images and background-images using the same technique, I ran into issues when trying to apply it to videos. Here's what I've tried s ...

Verify the search input to see if the value matches in the array and display the corresponding response

As a beginner in Javascript, I have recently started learning the language in the past few weeks. What I am looking for: I want to create a search bar where users can enter their postcode and then the script will search through an array to find if the po ...

Prevent closure of Bootstrap offcanvas on backdrop click in a Blazor .NET application

Trying to implement an offcanvas window form and ensuring it stays open only when the user clicks the close button. Following client requirements, I need to call the offcanvas window via C# with IJSRuntime injection instead of directly from HTML. Operatin ...

Passing a variable through an ajax request upon successful completion

Is there a way I can include the variable 'schedule_id[i]' in the result of this call? Can I also add this variable to the data object? Here's my code: for (var i = 0; i < schedule_id.length; i++) { //Making an AJAX request $.a ...