简体   繁体   中英

I have trouble connecting js files using html

What I want do find out is how I can make this work so it makes a red box at location 100,100, using a function as my main function and another function to draw the image but I want it to be in a different js file. 2 js files and 1 html file.

代码截图

Code //HTML code

        <!DOCTYPE html>
<html lang ="en">
    <head>
        <meta charset="UTF-8">
        <title>Prufa Einar</title>

    </head>
    <body>
        <canvas id = "myCanvas" width="600" height = "600"
            style ="border: 1px solid black;">
        <script type = "text/javascript" src "draw.js"></script>
        <script type = "text/javascript" src "main.js"></script>

        <script type = "text/javascript">
            main();
        </script>

    </body>
</html>
//first js.file
var canvas;
var ctx;
var main = function()
{
        canvas = document.createElement("canvas");
        ctx = canvas.getContext("2d");
        document.body.appendChild(canvas);
        draw(ctx);
};
//second js file
var draw = function(ctx)
{
    {
        ctx.fillStyle = "Red";
        ctx.fillRect(100,100,20,20);
    }
};

Looks like you need equal sign after src. src=""

and canvas out of head into body instead

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