我制作了以下HTML页面,该页面使用javascript在画布上打印红色方块,然后允许用户使用向上和向下键上下移动方块。
然而,经过多次尝试,我不确定如何打印可以随后移动的图像。(而不是我目前的红色方块)这是我需要帮助的部分。
以下是我可以使用密钥代码移动的红色方块的代码:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Javascript game</title>
<style>
canvas{
}
</style>
</head>
<body>
<script>
var WIDTH = 800, HEIGHT = 600;
var context, keystate, canvas;
var DownArrow = 40, UpArrow = 38;
var square;
square =
{
x: null,
y: null,
width: 200,
height: 150,
update: function()
{
if (keystate[UpArrow]) this.y -= 7;
if (keystate[DownArrow]) this.y += 7;
},
draw: function()
{
context.fillRect(this.x,this.y,this.width,this.height);
}
}
function main()
{
canvas = document.createElement('canvas');
canvas.width = WIDTH;
canvas.height = HEIGHT;
context = canvas.getContext("2d");
document.body.appendChild(canvas);
keystate = {};
document.addEventListener("keydown", function (e) {
keystate[e.keyCode] = true;
});
document.addEventListener("keyup", function (e) {
delete keystate[e.keyCode];
});
init();
var anim = function()
{
update();
draw();
window.requestAnimationFrame(anim,canvas);
}
window.requestAnimationFrame(anim,canvas);
}
function init()
{
square.x = WIDTH / 2;
square.y = HEIGHT / 2;
}
function update()
{
square.update();
}
function draw()
{
context.fillRect(0, 0, WIDTH, HEIGHT);
context.save();
context.fillStyle = "#f00";
square.draw();
context.restore();
}
main();
</script>
</body>
</html>
非常感谢帮助,谢谢! :)
答案 0 :(得分:2)
您可以加载如下图像:
var img=new Image();
img.onload=function(){
init();
}
img.src='sun.png';
让图像有时间加载......这是img.onload
的全部内容,这一点非常重要。只有在图像完全加载并准备好使用后才会调用onload函数。
加载完图片后,您只需将context.drawImage
替换为context.fillRect
即可获得可移动图片。
祝你好运,这里是示例代码和演示:
var WIDTH = 800, HEIGHT = 600;
var context, keystate, canvas;
var DownArrow = 40, UpArrow = 38;
var square;
// create a new image object
var img=new Image();
// tell the image to call init() after it's fully loaded
img.onload=function(){
init();
}
// tell the image where to get its source
img.src='https://dl.dropboxusercontent.com/u/139992952/multple/sun.png';
square =
{
x: null,
y: null,
width: 200,
height: 150,
update: function()
{
if (keystate[UpArrow]) this.y -= 7;
if (keystate[DownArrow]) this.y += 7;
},
draw: function()
{
// draw the image instead of the rectangle
// context.fillRect(this.x,this.y,this.width,this.height);
context.drawImage(img,this.x,this.y);
}
}
function main()
{
canvas = document.createElement('canvas');
canvas.width = WIDTH;
canvas.height = HEIGHT;
context = canvas.getContext("2d");
document.body.appendChild(canvas);
keystate = {};
document.addEventListener("keydown", function (e) {
keystate[e.keyCode] = true;
});
document.addEventListener("keyup", function (e) {
delete keystate[e.keyCode];
});
// Now this is done after the image has fully loaded
// init();
var anim = function()
{
update();
draw();
window.requestAnimationFrame(anim,canvas);
}
window.requestAnimationFrame(anim,canvas);
}
function init()
{
square.x = WIDTH / 2;
square.y = HEIGHT / 2;
}
function update()
{
square.update();
}
function draw()
{
context.fillRect(0, 0, WIDTH, HEIGHT);
context.save();
context.fillStyle = "#f00";
square.draw();
context.restore();
}
main();
&#13;