gpt4 book ai didi

javascript - 如何移动 Canvas 图像

转载 作者:行者123 更新时间:2023-12-02 16:47:02 25 4
gpt4 key购买 nike

我制作了以下 HTML 页面,该页面使用 JavaScript 将红色方 block 打印到 Canvas 上,然后允许用户使用向上和向下键上下移动该方 block 。

但是,经过多次尝试,我不确定如何打印可以移动的图像。(而不是我目前拥有的红色方 block )这是我需要帮助的部分。

这是我的可通过键码移动的红色方 block 的代码:

<!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>

非常感谢您的帮助,谢谢! :)

最佳答案

您可以像这样加载图像:

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();

关于javascript - 如何移动 Canvas 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com