无法实例化p5.js代码(实例模式)

问题描述 投票:0回答:1

更新:问题已解决。如果有人需要帮助/参考,以下是工作中的实例化代码:https://editor.p5js.org/Rod1C/sketches/iaKn9CKCS

我是p5.js的新手,一直在尝试将多个草图加载到网页上。这当然是有问题的,因为我无法使用相同的函数名加载不同的JavaScript文件。对于此类问题,p5有一个称为“实例模式”的东西。

我一直在尝试“实例化”我的代码,这基本上意味着要在这种实例模式下编写它,但是我仍然遇到很多错误-这有点超出我的能力!

这是我的工作p5.js代码(未实例化):https://editor.p5js.org/Rod1C/sketches/bXxGdhRPl

class Particle {

    constructor(l) {
        this.acceleration = createVector(0, 0);
        this.velocity = createVector(0,0);//random(-0.0001, 0.00001), random(-0.001, 0.0001));
        this.position = l ? l.copy() : createVector(Math.random()*(windowWidth*0.8), Math.random()*(windowHeight*0.7),);
        this.home = this.position.copy();
    }

    run() {
        this.update();
        this.display();
    }

    // Method to update position
    update() {
        this.acceleration.x = -0.01*(this.position.x - this.home.x);
        this.acceleration.y = -0.01*(this.position.y - this.home.y);
        this.velocity.add(this.acceleration);
        this.velocity.mult(0.9);
        this.position.add(this.velocity);
        //   lifespan -= 1.0;
    }

    // Method to display
     display() {
        noStroke();//stroke(255, lifespan);
        //fill(255, lifespan);
        var notblue = map(abs(this.velocity.mag()),0,5,27,255); 
        fill(notblue,27,27);
        ellipse(this.position.x, this.position.y, 15, 15);
    }
}

class ParticleSystem {
    constructor(position) {
        this.origin = position.copy();
        this.particles = [];
    }

    addParticle() {
        //this.particles.push(new Particle(this.origin));
        this.particles.push(new Particle());
    }

    run() {
        for (let i = this.particles.length-1; i >= 0; i--) {
            this.particles[i].run();
    //      if (p.isDead()) {
            //    particles.remove(i);
    //      }
        }
    }

    move_away_from(x, y){
        for (let i = 0; i < this.particles.length; i++) {
            let p = this.particles[i];
            let d = dist(x*0.5,y, p.position.x*0.5, p.position.y);
            if( d < 200 ){ 
                p.velocity.x += map(d,0,200,0.5,0.1)*(p.position.x - x);
                p.velocity.y += map(d,0,200,0.5,0.1)*(p.position.y - y);
            }
        }
    }
}

var ps;

function setup() {
    var canvas = createCanvas(windowWidth*0.7, windowHeight*0.7);
    ps = new ParticleSystem(createVector(width/2, 50));
    for (var i=0; i<1200; i++) {
        ps.addParticle();
    }

}

function draw() {
    background(255);
    ps.move_away_from(mouseX, mouseY);
    ps.run();
}

function windowResized() {
  resizeCanvas(windowWidth*0.8, windowHeight*0.7);
}

这就是我实例化的程度,尽管您可以看到我似乎已经死了,因为我似乎无法修复弹出的新错误:https://editor.p5js.org/Rod1C/sketches/E0QS422xy

var sketch = function( p ) { 
class Particle {

    constructor(l) {
        this.acceleration = p.createVector(0, 0); 
        this.velocity = p.createVector(0,0); //random(-0.0001, 0.00001), random(-0.001, 0.0001));
        this.position = l ? l.copy() : createVector(Math.random()*(windowWidth*0.8), Math.random()*(windowHeight*0.7),);
        this.home = this.position.p.copy(); 
    }

    run() {
        this.p.update(); 
        this.p.display() ;
    }

    // Method to update position
    update() {
        this.acceleration.x = -0.01*(this.position.x - this.home.x);
        this.acceleration.y = -0.01*(this.position.y - this.home.y);
        this.velocity.p.add(this.acceleration);
        this.velocity.p.mult(0.9);
        this.position.p.add(this.velocity);
        //   lifespan -= 1.0;
    }

    // Method to display
     display() {
        p.noStroke();
         var notblue = map(abs(this.velocity.mag()),0,5,27,255); 
        p.fill(notblue,27,27);
        p.ellipse(this.position.x, this.position.y, 15, 15);
    }
}

class ParticleSystem {
    constructor(position) {
        this.origin = position.p.copy();
        this.particles = [];
    }

    addParticle() {
        //this.particles.push(new Particle(this.origin));
        this.particles.push(new Particle());
    }

    run() {
        for (let i = this.particles.length-1; i >= 0; i--) {
            this.particles[i].p.run();
         }
        }


    move_away_from(x, y){
        for (let i = 0; i < this.particles.length; i++) {
            let p = this.particles[i];
            let d = p.dist(x*0.5,y, p.position.x*0.5, p.position.y); }
            if( d < 200 ){ 
                p.velocity.x += map(d,0,200,0.5,0.1)*(p.position.x - x);
                p.velocity.y += map(d,0,200,0.5,0.1)*(p.position.y - y);
            }
        }
    }


var ps;

p.setup = function() {
    var canvas = p.createCanvas(p.windowWidth*0.7, p.windowHeight*0.7); 
    ps = new ParticleSystem(p.createVector(p.width/2, 50));
    for (var i=0; i<1200; i++) {
        ps.p.addParticle() }
    }


p.draw = function() {
    p.background(255); 
    ps.p.move_away_from(mouseX, mouseY);
    ps.p.run();
}

p.windowRecreateCanvasd = function() {
  p.recreateCanvasCanvas(windowWidth*0.8, windowHeight*0.7);

}
 };

var godspeed = new p5(sketch);

因此,如果有人可以将我指向正确的方向(告诉我我做错了什么)或解决现有的错误,那就太好了!

注意:我知道我可以通过iFrame嵌入它们,但是这对我想要的效果不起作用。

javascript instantiation p5.js javascript-namespaces
1个回答
0
投票

您的代码包含与使用p变量的方式有关的一些不一致之处。

© www.soinside.com 2019 - 2024. All rights reserved.