如何在 HTML5 中使用键盘箭头键在画布上移动图像
how to move an image on canvas using keyboard arrow key in html5
我正在使用此代码在画布上绘制图像它成功地在画布上绘制了图像 现在我想在画布上移动图像 为此我编写代码 我检查如果按下键盘的右键,我将增加图像的 x 坐标 如果按下左键 我将递减 x 坐标,但图像在画布上没有移动
player = new Image();
player.src = "game_character.png";
context.drawImage(player,player.x * wallDim + wallDim ,player.y * wallDim + wallDim ,50,50);
如何在画布上移动图像
var handleInput = function(event, keyState) {
switch(event.which) {
case 37: { // Left Arrow
keyDown.arrowLeft = keyState;
break;
}
case 38: { // Up Arrow
keyDown.arrowUp = keyState;
break;
}
case 39: { // Right Arrow
keyDown.arrowRight = keyState;
break;
}
case 40: { // Down Arrow
keyDown.arrowDown = keyState;
break;
}
}
}
/**
* physics
*
* This function contains the basic logic for the maze.
*/
var physics = function() {
console.log("physics ");
console.log("first condition "+keyDown.arrowRight +player.x+1);
if(keyDown.arrowLeft && player.x-1 >= 0 && map[player.y][player.x-1] != 1) {
player.x--;
redraw = true;
}
if(keyDown.arrowUp && player.y-1 >= 0 && map[player.y-1][player.x] != 1) {
player.y--;
redraw = true;
}
if(keyDown.arrowRight && player.x+1 < map[0].length && map[player.y][player.x+1] != 1) {
console.log("arrow right");
player.x++;
redraw = true;
}
if(keyDown.arrowDown && player.y+1 < map.length && map[player.y+1][player.x] != 1) {
player.y++;
redraw = true;
}
if(keyDown.arrowRight && player.x+1 >= map[0].length)
{
player.x++;
document.getElementById("canvas_div").style.display="none";
document.getElementById("end_screen_div").style.display="block";
//alert("completed");
}
}
/**
* draw
*
* This function simply draws the current state of the game.
*/
var draw = function() {
// Don't redraw if nothing has changed
if(!redraw)
return;
context.clearRect(0, 0, cols, rows);
context.beginPath();
// Draw the maze
for(var a = 0; a < rows; a++) {
for(var b = 0; b < cols; b++) {
switch(map[a][b]) {
case C.EMPTY: context.fillStyle = colors.empty; break;
case C.WALL: context.fillStyle = colors.wall; break;
}
context.fillRect(b * wallDim, a * wallDim, wallDim, wallDim); // x, y, width, height
}
}
// Draw the player
/* context.fillStyle = colors.player;
context.arc(
player.x * wallDim + wallDim / 2, // x position
player.y * wallDim + wallDim / 2, // y position
wallDim / 2, // Radius
0, // Starting angle
Math.PI * 2, // Ending angle
true // antiClockwise
);*/
player = new Image();
player.src = "game_character.png";
context.drawImage(player,player.x * wallDim + wallDim ,player.y * wallDim + wallDim ,50,50);
var firstplayer=new Image();
firstplayer.src="top_character01.png";
context.drawImage(firstplayer,680,0,60,60);
var secondplayer= new Image();
secondplayer.src="top_character02.png";
context.drawImage(secondplayer,750,0,60,60);
context.fill();
context.closePath();
redraw = false;
}
在 draw 方法中,每次都重新初始化播放器:
player = new Image();
player.src = "game_character.png";
因此,您可以擦除由事件处理程序修改的 player.x。
您应该只在 draw 函数之外初始化播放器一次。你可以像这样移动初始化:
var player = new Image();
player.src = "game_character.png";
var draw = function() {
绝对没有必要在绘制函数中调用player.src = "game_character.png";
。
作为一般规则,在处理动画时,请尝试从绘制函数中删除所有可能的内容,这应该尽可能快。
每次都需要
重新绘制画布。像这样:
function init()
{
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
x = canvas.width / 2; //align to centre of the screen
y = canvas.height / 2; //same as above
speed = 5; //speed for the player to move at
width = 50; //width of the player
height = 50; //height of the player
playerimage = new Image();
playerimage.src = "path/to/image/for/player"; //path to the image to use for the player
canvas.addEventListener("keypress", update);
}
function update(event)
{
if (event.keyCode == 38)
{
y -= speed; //going up
}
if (event.keyCode == 40)
{
y += speed; //going down
}
if (event.keyCode == 37)
{
x -= speed; //going left
}
if (event.keyCode == 39)
{
x += speed; //going right
}
render();
}
function render()
{
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(playerimage, x, y, width, height);
}
我没有测试过它,所以我不知道它是否有效,这里和那里可能会有一些错误。不过它应该可以工作!如果没有别的,它会(希望)让你了解一种你可以去做的方式......
相关文章:
- JS-在一段时间后水平移动图像
- JS-移动图像,暂停设置的时间,然后将图像向后移动
- 如何在kineticjs中移动图像
- 锤击JS平移不移动图像
- 如何使用javascript中的箭头键在画布上移动图像
- 在iPad上使用touchmove移动图像(css3变换)
- 当我在另一个画布上同时拖动另一个图像时,如何移动图像
- 在web应用程序ASP.net中拖放和移动图像
- 如何使用javascript移动图像
- 如何在Javascript中在桌子周围随机移动图像
- 如何使用变换来更改或移动图像
- 如何使用reveal.js、javascript和fragment类移动图像
- 如何在 HTML5 中使用键盘箭头键在画布上移动图像
- 在 Javascript 中以 360° 角度移动图像
- 移动图像,显示值并保存当前值
- 移动图像 ..JavaScript 将变量声明为字符串
- 通过指向 X、Y 坐标来移动图像
- 尝试在单击时移动图像,但是 .click 甚至仅在第一次单击时触发
- 通过单击方向按钮在容器内移动图像
- 使用 JavaScript 移动图像