简体   繁体   English

未捕获的 TypeError:p5 不是构造函数

[英]Uncaught TypeError: p5 is not a constructor

I have some troubles to start my p5js, since I'm getting this error:我在启动 p5js 时遇到了一些麻烦,因为我收到了这个错误:

Uncaught TypeError: p5 is not a constructor

import * as p5 from 'p5';
export default {
    init() {
        //a P5 moire pattern.
        let s = (sk) => {
            let layers = [];

            // sk.translate(window.innerWidth/2,window.innerHeight/2);
            sk.setup = () =>{
                let gfx = sk.createGraphics(window.innerWidth, window.innerHeight);
                let gfx2;

                sk.createCanvas(window.innerWidth, window.innerHeight);
                sk.angleMode(sk.DEGREES);
                sk.imageMode(sk.CENTER);
                sk.translate(window.innerWidth/2, window.innerHeight/2);
                sk.background(40);
                gfx.stroke(200);
                gfx.strokeWeight(3);
                gfx.line(0, 0, window.innerWidth, 0);
                for(let i=0; i<1000; i++){
                    gfx.point(Math.random() *window.innerWidth, Math.random() *window.innerHeight);
                }

                gfx2 = {...gfx};
                sk.image(gfx,0,0);
                sk.rotate(1);
                sk.image(gfx2,0,0);
            }

        }
        const P5 = new p5(s, document.getElementById('grid'));
    }
}

I my destination index.js the object gets just initialized as myObject.init() , index.html has a div#grid in it.我的目标 index.js object 刚刚初始化为myObject.init()index.html中有一个div#grid I can't see the issue.我看不到问题。 Any help would be appreciated.任何帮助,将不胜感激。

For anybody with the same issue, I took for granted (following p5js docs and other resources about the library) that importing like import * as p5 from 'p5' was correct but it's a named export class so you need to ìmport p5 from 'p5' .对于任何有同样问题的人,我认为理所当然(遵循 p5js 文档和其他关于库的资源) import * as p5 from 'p5'是正确的,但它是一个命名的导出 class 所以你需要ìmport p5 from 'p5' . That worked.那行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM