我想要知道的是我如何才能完成这项工作,以便它在位置 100,100 处生成一个红色框,使用一个函数作为我的主要函数,使用另一个函数来绘制图像,但我希望它位于不同的 js 中文件。 2 个 js 文件和 1 个 html 文件。
代码 //HTML代码
<!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);
}
};
最佳答案
看起来 src 之后需要等号。 src=""
Canvas 从头部进入 body
关于javascript - 我在使用 html 连接 js 文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33570722/