p5.j​​s createCanvas未定义错误.未捕获的ReferenceError [英] p5.js createCanvas not defined error. Uncaught ReferenceError

查看:67
本文介绍了p5.j​​s createCanvas未定义错误.未捕获的ReferenceError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我正在尝试在我的计算机中使用 p5.js 一个简单的应用程序,并包括以下内容:

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

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

我尝试过的事情:

如果我放置调试器并在控制台中查看,我确实会获得p5Color(例如ex)和其他功能.然后脚本就可以很好地加载到页面上了.除了createCanvas不会自动完成,并且在页面中使用时,都会引发上述错误.

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.

为什么?我该如何解决?

推荐答案

p5.js除非在页面上定义了setup()draw(),否则不会加载到全局模式".

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

选项1-强制全局模式. (请注意,如果执行此操作,由于setup()已在页面加载时运行,因此createCanvas()在控制台中不会发挥很大的作用.)

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>

选项2-使用实例模式. (如果您真的想从控制台使用p5.js,这可能是最好的选择.)

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);

这篇关于p5.j​​s createCanvas未定义错误.未捕获的ReferenceError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆