循环整个 p5js 脚本

发布于 2025-01-10 21:10:27 字数 670 浏览 0 评论 0原文

我想知道是否可以在 p5js Web 编辑器或 VS 代码中多次运行以下代码,并在每次运行时从预定义值数组中更改参数 a、b、A、B、H 并保存 PNG本地图像。我的目标是让脚本运行并生成各种参数不同的绘图。这是我的问题的简化版本,因此不可能在绘制循环中运行循环。有没有办法循环整个脚本?非常感谢。

//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()
  
}

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()
  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手 2025-01-17 21:10:27

您不需要循环整个脚本,只需执行以下操作:

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');
}

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');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文