Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
subtraction in p5.js if statement only occurs once instead of repeating

I have a p5.js animation here that is meant to animate the artwork then reverse the animation, however while it works for my other artworks, the logic doesn't work here as the if statement doesn't get triggered due to the subtraction for my mask only occurs once. Right now only have of the animation works, you can view it here: https://editor.p5js.org/theruantan/sketches/8g583LF8j

The main issue begins at line 116 and it is at line 124 where the code is unable to reach this if statement.

//Start of reversal for masks
for (let i = beachSwitches.length - 1; i >= 0; i--) {
          if (beachSwitches[i] == 0) {
            if (beachMasks[i] > 0) {
              beachMasks[i] -= 20;
              animating = true;
              //console.log("beachMasks");
              console.log(beachMasks[i])
            }
            if (beachMasks[i] <= 300) {
              // Begin reversing the next bar
              beachSwitches[i - 1] = 0;
              
              // To see if the code is able to reach here. 
              console.log(beachSwitches[i - 1] + "This is triggered");
              
            }
          }
}

beachMarks[i] should be able to be <= 300 but it's stuck at 780.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have recently started working with p5.js but just based on reading the code and understanding what you are trying to do what I can see is in the line you mentioned i.e 116 , if you log inside and outside you will understand why its only running once. Inside the 116 lines for loop, you are checking for beachSwitches[i]==0, which only happens once(at the last index of that array) because the beachSwitches gets modified above, also you haven't defined anything in the else part for this condition. Which is what is beachSwtiches[i] == 0 is NOT TRUE. In my screenshot below, you might get a rough idea. Hope this helps in some manner! Cheers! Screenshot of logs I took from line 116 to 124

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!