1

I am running a p5.js sketch on a flask server. I want to draw several canvases, so i instanced my code like Daniel Schiffman showed in his "9.11: Instance Mode (aka "namespacing") - p5.js Tutorial"-Video, but when i run the code it gives me "Uncaught ReferenceError: p5 is not defined" on the line i instantiate my canvas

var drawCanvas = new p5(firstcanvas);

When i run the code in the p5 web editor it works.

The script tag in my html looks like this

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"
       integrity="sha512-a5hlZKgpC1LVAuKgVeXdP0D9Yfacj0hLtNdzx9zFMkIWRrQyO37KtIPiqArGmVuaBYu3ON6Vt0N3+G/JaLXQYQ=="
       crossorigin="anonymous" referrerpolicy="no-referrer"></script>

so thats the newest version. any idea? thanks

Minimal reproducive example:

var firstCanvas = function(a) {
  let abc;
  a.setup = function() {
    abc = 100;
    a.createCanvas(800, 600);
    a.background(260);
    n = a.createButton("NEW");
    n.position(20, a.height + 225);
    n.mousePressed(clearCanvas);
  }
  a.draw = function() {
    a.fill(10);
    a.rect(a.mouseX, a.mouseY, abc, 50);
  }

  function clearCanvas() {
    a.background(260);
  }
}
var drawCanvas = new p5(firstCanvas);
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"
       integrity="sha512-a5hlZKgpC1LVAuKgVeXdP0D9Yfacj0hLtNdzx9zFMkIWRrQyO37KtIPiqArGmVuaBYu3ON6Vt0N3+G/JaLXQYQ=="
       crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
  • It would be helpful if you included a minimal reproducible example of your issue https://stackoverflow.com/help/minimal-reproducible-example – Paul Wheeler Jun 13 '21 at 20:49

2 Answers2

0

It seems to work fine when I copy that script tag and define a simple firstcanvas function.

function firstcanvas(p5) {
  var x;

  p5.setup = function() {
    p5.createCanvas(400, 400);
    p5.background(51);
    x = p5.width/2;
  }
  
  p5.draw = function() {
    p5.background(51);
    p5.circle(x, p5.height/2, 100);
    x++;
    if (x - 50 >= p5.width) x = -50;
  }
}

var drawCanvas = new p5(firstcanvas);
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"
       integrity="sha512-a5hlZKgpC1LVAuKgVeXdP0D9Yfacj0hLtNdzx9zFMkIWRrQyO37KtIPiqArGmVuaBYu3ON6Vt0N3+G/JaLXQYQ=="
       crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
0

I got it. I did run the html file direct without debugging in PyCharm and got this error. When i gave it a route and started it over the run.py it worked (almost) as it should.