7

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?

Software Mechanic
  • 974
  • 2
  • 9
  • 25
  • Ahem.. Any comments about why the downvote? – Software Mechanic Dec 29 '14 at 21:30
  • You were probably downvoted because it's not clear what you're asking. ("Any namespacing issues am not aware of?" - is not really an answerable question.) I edited the question and provided an answer for you... hope it helps! – wxactly Jan 12 '15 at 05:22
  • @wxactly: It does help.I'll accept the answer. Yeah, my js understanding was lacking to frame the question better then. Thanks. – Software Mechanic Jan 14 '15 at 02:44

1 Answers1

12

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);
wxactly
  • 2,400
  • 1
  • 26
  • 42
  • 2
    Thanks. The [P5 Getting Started](https://p5js.org/get-started/) shows these functions but really fails to explain the significance. – Ed Randall Jan 25 '20 at 20:22