[SOLVED] Adding createButton() causes an error

#1

Error:
Uncaught ReferenceError: createButton is not defined

Things I’ve tried to fix:

  1. I tried replacing the script tag to the CDN version of p5 8.0 thinking maybe I was not using the latest code base, but this did not work
  2. Tried running code on a local server, also did not work got same error
  3. Tried… button = createButton(“this is a button”)… got same error.
  4. Tried pasting in sample code from the reference documents and this did not work either.

Thanks for any help on this one, code below

var bgColor;

function setup(){
    createCanvas(400,400);
    createButton("this is a button");  //Adding this gives an error.
    bgColor = color(200);
}

function changeColor(){
    bgColor = color(random(255));
}

function mousePressed(){
    changeColor();
}

function draw(){
    background(bgColor);
    fill(255,0,0);
        
}

HTML FILE

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="p5js/p5.dom.min.js"></script>
    <script src="p5js/p5.min.js"></script>
    <script src="p5js/p5.sound.min.js"></script>
    <!-- <link rel="stylesheet" href="style.css"> -->
    
    <title>Sketch</title>
</head>
<body>
    <h1>Hello World</h1>
    <p>Super duper this is some text on the screen</p>
    <p>Yeah and this is some more text on the screen</p>
    <script src="sketch.js"></script> 
</body>
</html>
1 Like
#2

Switching the order of the script tags in the HTML solved this…
p5 needed to come before the dom…

<script src="p5js/p5.min.js"></script>
<script src="p5js/p5.dom.min.js"></script>
3 Likes