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

135
Views
Looping an entire p5js script

I was wondering if it is possible to run the following code on the p5js web editor or within VS code multiple times and change the parameters a, b, A, B, H from an array of predefined values every time it runs and save the PNG image locally. My aim is to let the script run and generate a wide range of plots with these parameters varying. This is a simplified version of my problem so running the loop within the draw loop is not possible. Is there a way to loop the entire script? Many thanks in advance.

//fixed
Sb = 0.3;
Vb = 1;
// noprotect

t = 0;
dt = 0.1 / 2;
t2=0

//want to vary
a = 0.03;
b = 0.04;
A = 20;
B = 20;
H = 0.15;

function setup() {
  createCanvas(1000, 1000, WEBGL);
  colorMode(HSB, 1);

  console.log(width);
  background(H, Sb, Vb);
}

function draw() {
  
  for (i = 0; t < 200 * TAU; i++) {
    W = A * sin(a * t) * B * sin(b * t );
   
  
    strokeWeight(2);
    point(t-width/2,W)

    

    t += dt;
  }
 saveCanvas(join(['Im', 1], '_'), 'png')
  noLoop()
  
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You don't need to loop the whole script, do something like this:

function setup(){

  createCanvas(1000, 1000, WEBGL);
  colorMode(HSB, 1);

  background("white");
  
  iteration(0.03,0.04,20,20,0.15);
  
}

function iteration(a, b, A, B, H){

  const Sb = 0.3;
  const Vb = 1;
  let t = 0;
  const dt = 0.1 / 2;
  const t2 = 0

  background(H, Sb, Vb);

  for (let i = 0; t < 200 * TAU; i++) {
    W = A * sin(a * t) * B * sin(b * t );

    strokeWeight(2);
    point(t-width/2,W)

    t += dt;
  }
  saveCanvas(join(['Im', 1], '_'), 'png');
}
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!