初学者关于p5js print() 函数的问题

发布于 2025-01-12 23:01:24 字数 419 浏览 0 评论 0原文

我在 GitHub 上看到了一些已关闭的帖子,以及关于随机返回的偶尔未解决的类似问题,但我可以得到一个很好的解释,解释为什么在定义对象的简单 print() 命令上该对象是似乎在循环中打印在控制台上:

在此处输入图像描述

我刚刚开始使用 p5js,并且不了解 javascript。所以它可能是命令放置的地方(function setup()function draw()),我仍然不确定为什么它们默认出现在编辑器起始页上,否则我需要指定我希望结果只打印一次(?)。

I have seen some closed posts on GitHub, and the occasional unsolved similar question on random returns, but I can get a good explanation as to why on a simple print() command of a defined object the object is being printed on the console seemingly in a loop:

enter image description here

I just started using p5js, and don't know javascript. So it may be where the commands are placed (function setup() or function draw()), which I am still unsure why they are defaulted on the editor start page, or else I need to specify that I want the result printed just once (?).

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

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

发布评论

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

评论(1

遥远的绿洲 2025-01-19 23:01:24

发生这种情况是因为 draw() 在循环上运行。在幕后, 库正在使用 requestAnimationFrame 调用绘制()

您可以将 print 语句移至 setup() 函数内,该函数仅在脚本开始时运行一次。

function setup() {
  createCanvas(400, 400);
  c = createVector(0,0);
  print(c.toString());
}

function draw() {
  //print(c.toString());
  background(220);
}

或者,如果您愿意,可以设置条件打印块,如下所示:

var printed = false;
function setup() {
  createCanvas(400, 400);
  c = createVector(0,0);
  //print(c.toString());
}

function draw() {
  if (!printed) {
    print(c.toString());
    printed = true;
  }
  background(220);
}

注意:还可以考虑使用p5.Vector 对象用于更清晰的输出。

如果您需要从 draw() 函数中调用的函数获取输出,您可以应用相同的原则,如下所示:

var printed = false;

function setup(){
  createCanvas(400, 400);
  c = createVector(0,0);
  print(c);
}

function draw() {
  background(220);
}

function beingCalledWithinDraw(){
  if (!printed) {
    print(c);
    printed = true;
  }
}

可运行示例:

var printed = false;

function setup() {
  createCanvas(400, 400);
  c = createVector(0, 0);
  //print(c.toString());
}

function draw() {
  background(220);
  beingCalledWithinDraw();
}

function beingCalledWithinDraw() {
  if (!printed) {
    print(c.toString());
    printed = true;
  }
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>

This happens because draw() runs on a loop. Behind the scenes, the library is using requestAnimationFrame to call draw().

You could move the print statement to be within the setup() function, which only runs once at the start of the script.

function setup() {
  createCanvas(400, 400);
  c = createVector(0,0);
  print(c.toString());
}

function draw() {
  //print(c.toString());
  background(220);
}

Or if you prefer, you can set up a conditional print block as follows:

var printed = false;
function setup() {
  createCanvas(400, 400);
  c = createVector(0,0);
  //print(c.toString());
}

function draw() {
  if (!printed) {
    print(c.toString());
    printed = true;
  }
  background(220);
}

Note: also consider using the toString method in the p5.Vector object for cleaner output.

If you need to get output from a function being called from within the draw() function, you can apply the same principles as follows:

var printed = false;

function setup(){
  createCanvas(400, 400);
  c = createVector(0,0);
  print(c);
}

function draw() {
  background(220);
}

function beingCalledWithinDraw(){
  if (!printed) {
    print(c);
    printed = true;
  }
}

Runnable example:

var printed = false;

function setup() {
  createCanvas(400, 400);
  c = createVector(0, 0);
  //print(c.toString());
}

function draw() {
  background(220);
  beingCalledWithinDraw();
}

function beingCalledWithinDraw() {
  if (!printed) {
    print(c.toString());
    printed = true;
  }
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>

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