java - Processing sketch 在 Java 模式下工作正常,但在 Processing.js 下不行

标签 java javascript webgl processing processing.js

我喜欢发布我在网站上使用 Processing.js 制作的一些 Processing 草图。然而,我制作的这个特别的在 JavaScript 模式下不能正常工作。起初我以为是因为它使用了通用的 ArrayList 类,而通用类直到最近才在 Processing 中得到支持。但后来我想起我制作的另一个使用相同类的草图在 Processing.js 中运行良好。

这是给我带来问题的代码:

ArrayList<Point> line;

class Point
{
  float x, y, z;
  Point(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
  Point copy() { return new Point(x, y, z); }
}

Point cursor;

void setup()
{
  line = new ArrayList<Point>();
  size(400, 400, P3D);
  cursor = new Point(width/2, height/2, 0);
  line.add(cursor.copy());
  frameRate(60);
}

void draw()
{
  background(0);
  camera();
  noSmooth();

  float coefficient = 0.05;
  cursor.x = map(coefficient, 0.0, 1.0, cursor.x, mouseX);
  cursor.y = map(coefficient, 0.0, 1.0, cursor.y, mouseY);

  if (mousePressed && (mouseButton == LEFT)) cursor.z -= 5.0;
  if (mousePressed && (mouseButton == RIGHT)) cursor.z += 5.0;
  if (mousePressed && (mouseButton == CENTER)) {
    cursor = new Point(width/2, height/2, 0);
    line.clear();
  }

  line.add(cursor.copy());

  rotate(sin(frameCount / 60.0) * 0.2, 0, 1, 0);
  noFill();
  stroke(255, 255, 0);
  pushMatrix();
  translate(200, 200, 0);
  box(400, 400, 400);
  popMatrix();

  // Draw the line
  stroke(0, 255, 0);
  Point last = null;
  for (Point p : line) {
    if (last != null) {
      line(last.x, last.y, last.z, p.x, p.y, p.z);
    }
    last = p;
  }

  // Draw the cursor
  float radius = 24;
  stroke(255, 0, 0);
  line(cursor.x - radius, cursor.y, cursor.z, cursor.x + radius, cursor.y, cursor.z);
  line(cursor.x, cursor.y - radius, cursor.z, cursor.x, cursor.y + radius, cursor.z);
  line(cursor.x, cursor.y, cursor.z - radius, cursor.x, cursor.y, cursor.z + radius);
}

here是无法正常工作的实时页面。

是什么导致了这个问题,更重要的是,我该如何解决它?

最佳答案

一件重要的事情是不要给 API 中也使用的草图变量名称,所以一个名为 line 的变量不是一个好主意,因为它可能会覆盖 line( ) 函数。

JavaScript 没有函数名和变量名的分离;他们共享同一个命名空间。参见 http://processingjs.org/articles/p5QuickStart.html#variablenamingcare有关这方面的更多信息。

关于java - Processing sketch 在 Java 模式下工作正常,但在 Processing.js 下不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22872625/

相关文章:

java - LibGDX - Gdx.graphics.getWidth() 如何返回显示表面的宽度?

java - 将 double 对转换为 double 组

javascript - 使用函数写入 .js 文件

javascript - 如何创建在不同页面上有不同问题的测验?

javascript - 如何将纹理应用于自定义几何体

java - 将 null 传递给首选 String 而不是 Object 的方法

java - 将回车 <CR> 和 CTRL-Z 添加到 Java 中的字符串

javascript - 需要关于过滤器图标编码的建议

webgl - 使用 gl.uniform1fv 上传数组的最大大小是多少?

opengl-es - 在WebGL2中使用drawBuffers时遇到一些问题