简体   繁体   中英

I've tried setting up my Canvas in JavaScript and it isn't working

I've restarted about 5 times and it's not working. I'm trying to set up my canvas. The first part is the code in the js file and the second code is what I put in the HTML file.

JS:

var canvas;
var ctx;
var w = 1000;
var h = 600;


function setUpCanvas(){
    canvas = document.querySelector("#myCanvas");
    ctx = canvas.getContext("2d");
    canvas.width = w;
    canvas.height = h;
    canvas.style.border = "5px dotted orange";
   

HTML:

 <!DOCTYPE html>
<html>
    <head>
        <link rel ="stylesheet" type = "text/css" href = "m4.css">
    </head>
    <body>
        <div id = "container">
            <h1> module 4 : arrays and loops</h1>
            <canvas id = "myCanvas"></canvas>
        </div>
        <script src = "m4.js"></script>
    </body>
</html>

Simple answer, it works. for proof, visit

https://loading.hermanboxcar5.repl.co

ok if you do ctrl u on that website it will give you the code you did (except my js file is named script.js ). My best guess so far is after the last line of your js put a }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM