我试图在我的画布上获取背景图片,但是当我检查我的控制台时,我收到此错误并且背景未显示:
" game.js:60 Uncaught InvalidStateError:无法执行' drawImage' on' CanvasRenderingContext2D&#39 ;:提供的HTMLImageElement位于'破坏'州。 "
函数StartGame在HTML body onload中链接。
有谁知道我怎么能解决这个问题?
谢谢:)
这是我的JSFIDDLE:
[http://jsfiddle.net/Nielsvangils/v9hL9d3k/8/][1]
HTML:
<html>
<head>
<title>Game</title>
</head>
<body onload="startGame()">
<canvas id="gamefield" width="800" height="600" style="border:1px solid #000000"></canvas>
<script src="game.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js"></script>
<p>Jump: press <b>SPACE</b></p>
</body>
</html>
使用Javascript:
var canvas = document.getElementById("gamefield");
ctx = canvas.getContext("2d");
var background = new Image();
background.src = "images/gamingbackground2.png";
function startGame()
{
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(background,0,0);
}