0

At a complete loss here for what I assume is a very simple problem, thanks for the help in advance.

I have a HTML doc;

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Sketch</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel="stylesheet" href="style.css">  
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="main.js"></script>
    <script src="https://use.fontawesome.com/1f87fe0ccc.js"></script>
</head>

and here is my main.js file

function _(selector) {
    return document.querySelector(selector);
}
function setup() {
    const canvas = createCanvas(650, 600);
    canvas.parent("canvas-wrapper");
    background = color(255);
}

This should be creating a white canvas on my webpage but nothing is happening and Im concerned I have linked the file incorrectly.

8
  • Is the JavaScript in the same folder as the HTML? Commented Dec 30, 2020 at 14:47
  • 6
    did you call the function? Commented Dec 30, 2020 at 14:48
  • Try checking whether the javascript path is correct first. By doing a console.log() in your js file Commented Dec 30, 2020 at 14:51
  • Did you call the function and are there errors in the developer console or is there a 404 in the network panel? Commented Dec 30, 2020 at 14:52
  • 1
    Because createCanvas is not defined in the "standard JS scope". Where do you take the function from? Maybe that helps: w3schools.com/jsref/dom_obj_canvas.asp Commented Dec 30, 2020 at 15:04

1 Answer 1

1

There are several errors with your code. First off, you need to call all the functions that are supposed to run some code: This is how you do it: function_name(). The second thing you do is to define the function createCanvas, This function name looks similar to the P5JS library's createCanvas function, so I'm assuming that you are trying to use P5JS. Add this to your head tag: <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.min.js"></script>

For more information please visit P5JS Library

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the response, did some readin up on P5JS and it is the library Im using, I've added in the script for it but Im still getting createCanvas is not defined and the js code isnt working how its supposed to.
make sure to put the script tag that links to your "main.js" under the body tag, and not inside the head section. Let me know, if this fixes the problem.
this doesnt work unfortunately, no change. I have a link to font awesome script in my head tag as well underneath the link to P5JS, should I move this one too?
The way you set the background is wrong, You have to move the background to draw function. This code worked for me. function _(selector) { return document.querySelector(selector); } function setup() { let canvas = createCanvas(400, 400); canvas.parent("canvas-wrapper"); } function draw() { background(220) } For more information please visit P5JS get started to see the difference between setup() and draw()
Also, remember to add this to your body tag, inside your HTML code. <div id="canvas-wrapper"></div>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.