.fillStyle 不起作用,控制台中没有错误
.fillStyle not working, no errors in console
这是我的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Rogue Game</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body onload="canvasGame()">
<canvas id="myCanvas" width="800" height ="800">
<p>Sorry your browser does not support canvas!</p>
</canvas>
</body>
</html>
和Javascript:
function canvasGame() {
canvas = document.getElementById("myCanvas");
if(canvas.getContext) {
ctx = canvas.getContext("2d");
ctx.fillStyle = "white";
ctx.fill();
}
}
我得到的控制台是:
file:///Users/darceymckelvey/Documents/Rogue/css/style.cssfile:///Users/darceymckelvey/Documents/Rogue/js/main.js获取https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js [HTTP/2.0 304 未修改 60ms]file:///Users/darceymckelvey/Documents/Rogue/css/style.css
我的画布不是白色的,它只是我在 CSS 中设置身体的颜色。
脚本中缺少"var"。您也没有任何对象要填充颜色。因此,您必须创建一个才能用颜色填充它。有关详细信息,请参阅有关 fill() 的文档。下面是一个有效的示例:
<html>
<head>
<meta charset="utf-8" />
<title>Rogue Game</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> </script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body onload="canvasGame()">
<canvas id="myCanvas" width="800" height ="800">
<p>Sorry your browser does not support canvas!</p>
</canvas>
<script>
function canvasGame() {
var canvas = document.getElementById("myCanvas");
if(canvas.getContext) {
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.rect(20, 20, 150, 100);
ctx.fillStyle = "white";
ctx.fill();
}
}
</script>
</body>
</html>
相关文章:
- 回复'js'仅当请求有错误时(否则使用html)
- 表单已发送,但验证有错误
- 如果文件不存在,fs.watch是否有错误处理程序
- Meteor上有错误的同步问题
- 表单验证:如果有错误,不要't使手风琴动起来
- 我有错误文档.注册元素未被细化
- 如何转换为特定于 json 的类 c#?我有错误
- 抄写员 JS 错误 - 控制台.log未定义
- 如果当前输入有错误或警告,则禁用所有下一个表单输入字段
- 通过有错误的 Ajax-Request 打印出 HTML 会阻止进一步的 JS 工作
- 脸书应用程序有错误
- XDomainRequest 总是有错误
- HTML5 Canvas游戏有错误,点击时第一个拼图块会变成不同的块
- Page_ClientValidate返回 false,但没有一个验证器有错误
- 量角器由中继器命令中是否有错误
- Jquery 验证器 - 表单被保存但有错误
- Node.js Visual Studio工具如何获取错误/控制台消息以输出VS
- 任何人看到我的重置功能有错误
- 当尝试用另一个facebook用户登录时,它说控制台日志中有错误
- 在Javascript中,我想添加一个值到数组,但在控制台我有错误"失踪;statement"之前