p5.js createCanvas not defined error. Uncaught ReferenceError

微笑、不失礼 提交于 2020-05-15 00:58:22

问题


Problem:

I am trying to use p5.js in my simple app, and including it thus:

<script src="static/js/p5.js"> </script>

What I've tried:

If I put a debugger and look in the console, I do get the functions for p5Color (for ex) and others. And the script gets loaded on to the page fine. Except createCanvas doesn't auto-complete and when used in the page, throws above error.

Why? How can I work around this?


回答1:


p5.js won't load into "global mode" unless it sees setup() or draw() defined on the page.

Option 1 - force global mode. (Note that if you do this, createCanvas() won't do you much good in the console, since setup() will have already run on page load.)

<script src="static/js/p5.js"> </script>
<script>
    function setup() {
        //...
    }
</script>

Option 2 - use instance mode. (This is probably your best bet if you really want to use p5.js from the console.)

var s = function( sketch ) {
  sketch.setup = function() {
    sketch.createCanvas(700, 410);
    //...
  };
};    
var myp5 = new p5(s);


来源:https://stackoverflow.com/questions/27694616/p5-js-createcanvas-not-defined-error-uncaught-referenceerror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!