[英]Brackets doesn't load my canvas
When I'm referencing my JavaScript file to my index.html
file (two separate files in the same folder), and I load the html file in a browser, the drawing i made in the javascript file doesnt show in the canvas.当我将我的 JavaScript 文件引用到我的
index.html
文件(同一文件夹中的两个单独文件),并且我在浏览器中加载 html 文件时,我在 javascript 文件中制作的绘图不会显示在画布中。
Here is this HTML
code:这是这个
HTML
代码:
<!DOCTYPE html>
<html>
<head>
<title>snake</title>
<script language="javascript" type="text/javascript" src="snake.js"></script>
</head>
<body>
<canvas id="dots" height="500" width="500"></canvas>
</body>
</html>
and here is the JavaScript:这是 JavaScript:
function snake() {
var canvas = document.getElementById("dots");
var snakehead = canvas.getContext('2d');
snakehead.beginPath();
snakehead.arc(100, 75, 50, 0, 2 * Math.PI);
snakehead.stroke();
snakehead.fillStyle = "#2776ff";
snakehead.fill();
}
snake();
in JS-fiddle the code works, it looks like this: https://jsfiddle.net/j2ny6gaf/72/在 JS-fiddle 代码工作,它看起来像这样: https : //jsfiddle.net/j2ny6gaf/72/
I'm also getting some errors from JSLint.我也从 JSLint 得到了一些错误。
"Missing use of 'strict' statement, on the var canvas = document.getElementById "Combine this with the previous var statement var snake = canvasgetcontext “在 var canvas = document.getElementById 上缺少使用 'strict' 语句”将其与之前的 var 语句 var snake = canvasgetcontext 结合起来
and a ERROR;和一个错误; "Document is not defined.[no-undef]
“文档未定义。[no-undef]
Try using the script tag尝试使用脚本标签
<script type="text/javascript" src="snake.js"></script>
Inside the head section.头部内部。
尝试在 head 部分中添加您的脚本(script src=“file-name.js” /script)并尝试添加画布大小。
试试
window.onload = function snake() {...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.