我正在慢慢学习一些基本的 HTML 5 和 Javascript。我编写了一个 Javascript 文件,该文件假设显示图像,然后通过按右键更改图像的位置。图像加载,但当我按右时它不会改变位置。我究竟做错了什么?
var canvas = document.createElement("canvas");
canvas.width = 800;
canvas.height = 600;
canvas.style = "border:2px solid blue";
canvas.addEventListener('keydown', doKeyDown, true);
var ctx = canvas.getContext("2d");
document.body.appendChild( canvas );
var guy1Image = new Image();
guy1Image.onload = function () {
ctx.drawImage( guy1Image, x, y );
};
guy1Image.src = "img/1.png";
var x = 0;
var y = 0;
function doKeyDown( e ) {
if ( e.keyCode == 39 ) {
x = 128;
y = 128;
}
}