无法正确地将这组函数转换为工作类(p5.js)

时间:2019-01-07 19:07:43

标签: javascript class oop object p5.js

我正在将一组创建树的函数转换为一个类,但是如果函数创建一个大的分支树,则该类版本无法像在函数中那样正确地绘制树。我对javascript和oop的经验不是很高,因此在这里发现问题非常令人沮丧和困难,如果有人可以指出我需要对类进行的更改以便能够正确显示,我将不胜感激。

我并排比较了代码,试图找出是否存在任何差异,但是我觉得问题是由于类中函数和方法的差异所致,这使我很难识别。

我要修复的代码可以在这里找到:https://editor.p5js.org/remcqueen/sketches/S1ETz7WfV

代码的预期结果与函数版本相同,可以在以下位置找到:https://editor.p5js.org/remcqueen/sketches/B1HdvWbzN

应生成完整的无叶树。但是当前显示的内容完全不同。

感谢所有看到此问题以帮助解决此问题的人。

这是我要修复的课程:

class createTree {

  constructor() {
    this.tree = createGraphics(width-10, height-10);
    this.n = 0;
  }

  draw() {
    this.tree.beginShape();
    this.tree.noStroke();
    this.tree.background(0,0);
    for (this.i = 0; this.i < 3; this.i++) {
        this.tree.fill(map(this.i, 0, 2, 60, 20));
        this.branch(width/2, height, 70, -HALF_PI, 150, 0);
    }
    this.tree.endShape();
    image(this.tree, 5, 5);
  }


  branch(x, y, bSize, theta, bLength, pos) {
    this.x = x;
    this.y = y;
    this.bSize = bSize;
    this.theta = theta;
    this.bLength = bLength;
    this.pos = pos;
    this.n += 0.01;
    this.diam = lerp(this.bSize, 0.7 * this.bSize, this.pos / this.bLength);
    this.diam *= map(noise(this.n), 0, 1, 0.4, 1.6);

    this.tree.ellipse(this.x, this.y, this.diam, this.diam);
    if (this.bSize > 0.6) {
        if (this.pos < this.bLength) {
            this.x += cos(this.theta + random(-PI / 10, PI / 10));
            this.y += sin(this.theta + random(-PI / 10, PI / 10));
            this.branch(this.x, this.y, this.bSize, this.theta, this.bLength, this.pos + 1);
        } else {
            this.drawLeftBranch = random(1) > 0.1;
            this.drawRightBranch = random(1) > 0.1;
            if (this.drawLeftBranch) this.branch(this.x, this.y, random(0.5, 0.7) * this.bSize, this.theta - random(PI / 15, PI / 5), random(0.6, 0.8) * this.bLength, 0);
            if (this.drawRightBranch) this.branch(this.x, this.y, random(0.5, 0.7) * this.bSize, this.theta + random(PI / 15, PI / 5), random(0.6, 0.8) * this.bLength, 0);

            if (!this.drawLeftBranch && !this.drawRightBranch) {
                this.tree.push()
                this.tree.translate(this.x, this.y);
                this.tree.rotate(this.theta);
                this.tree.quad(0, -this.diam / 2, 2 * this.diam, -this.diam / 6, 2 * this.diam, this.diam / 6, 0, this.diam / 2);
                this.tree.pop();
            }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您正在整个实例中设置实例变量,而您真正想要的是函数本地变量。在实例上设置变量时,它们将在递归过程中共享,这不是您想要的。例如,您需要函数的所有参数在函数本地:

将它们添加到this会破坏一切:

branch(x, y, bSize, theta, bLength, pos) {
   this.x = x;  // <-- don't do that
   this.y = y;
 // etc
}

这是一个清理过的代码段:

var a;
function setup() {
  createCanvas(900, 700);
  colorMode(HSB);
  noLoop();
  noStroke();
  a = new createTree();
  a.draw();
}

class createTree {

  constructor() {
    this.tree = createGraphics(width, height);
    this.n = 0;
  }

  draw() {
    this.tree.beginShape();
    this.tree.noStroke();
    this.tree.background(0,0);
		
    for (let i = 0; i < 3; i++) {
        this.tree.fill(map(i, 0, 2, 60, 20));
        this.branch(width/2, height, 70, -HALF_PI, 150, 0);
    }
    this.tree.endShape();
    image(this.tree, 5, 5);
  }


  branch(x, y, bSize, theta, bLength, pos) {
    
    this.n += 0.01;
    let diam = lerp(bSize, 0.7 * bSize, pos / bLength);
    diam *= map(noise(this.n), 0, 1, 0.4, 1.6);

    this.tree.ellipse(x, y, diam, diam);
    if (bSize > 0.6) {
        if (pos < bLength) {
            x += cos(theta + random(-PI / 10, PI / 10));
            y += sin(theta + random(-PI / 10, PI / 10));
            this.branch( x, y, bSize, theta, bLength, pos + 1);
        } else {
            let drawLeftBranch = random(1) > 0.1;
            let drawRightBranch = random(1) > 0.1;
            if (drawLeftBranch) this.branch(x, y, random(0.5, 0.7) * bSize, theta - random(PI / 15, PI / 5), random(0.6, 0.8) * bLength, 0);
            if (drawRightBranch) this.branch(x, y, random(0.5, 0.7) * bSize, theta + random(PI / 15, PI / 5), random(0.6, 0.8) * bLength, 0);

            if (!drawLeftBranch && !drawRightBranch) {
                this.tree.push()
                this.tree.translate(x, y);
                this.tree.rotate(theta);
                this.tree.quad(0, -diam / 2, 2 * diam, -diam / 6, 2 * diam, diam / 6, 0, diam / 2);
                this.tree.pop();
            }
        }
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>