简体   繁体   English

通过JavaScript使用Canvas,如何使对象通过箭头键移动?

[英]Using Canvas via JavaScript, how can I make an object move via arrow keys?

I'm just starting out with a game that I'm trying to do for a fun little project. 我刚开始尝试一款有趣的小项目游戏。 I'm still new to canvas and was just wondering how I would be able to make this red square move via arrow keys? 我还不熟悉画布,只是想知道如何通过箭头键使这个红色方块移动? I understand the keyup event but wasn't exactly sure how to incorporate it with canvas. 我了解keyup事件,但不确定如何将其与canvas合并。 Thanks in advance 提前致谢

<canvas id="myCanvas" height="400" width="400" style="border: 1px solid black"></canvas>

let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.translate(120,120);
ctx.lineTo(0,0);
ctx.lineTo(160,0);
ctx.lineTo(160,160);
ctx.lineTo(0,160);
ctx.lineTo(0,0);
ctx.fillStyle = "red";
ctx.fill();
ctx.stroke();

document.addEventListener('keydown', function(event) {
console.log(event.keyCode);
if (event.keyCode == 37) {
  ctx.clearRect(0, 0, c.width, c.height);
  ctx.translate(0,0);
}
  • First: you have to rerender the canvas every time something changed (at least the part that changed). 第一:每次更改(至少更改的部分)时,都必须重新渲染画布。

  • Second although keydown re-fires while you keep the button pressed, you'd want a more consistent update cycle; 其次,尽管在按住按钮的同时按下了keydown键,但您希望有一个更一致的更新周期。 like requestAnimationFrame or setInterval . requestAnimationFramesetInterval And then later you can also account for the little inconsistencies in these intervals by taking the time since the last function call/update into account. 然后,您还可以考虑自上次函数调用/更新以来的时间,以解决这些时间间隔中的微小不一致问题。

  • Then you'd want to introduce a keyState that stores the current state of multiple buttons, so that you can check that outside of the actual keydown/keyup event. 然后,您想引入一个keyState来存储多个按钮的当前状态,以便您可以在实际的keydown / keyup事件之外进行检查。

     const isKeyDown = {}; document.onkeydown = document.onkeyup = function(e){ isKeyDown[e.which] = isKeyDown[e.key] = e.type === "keydown"; }; 
  • and as a second step, you could introduce velocity to the object with friction or gravity. 第二步,可以通过摩擦或重力将速度引入对象。 So your button presses don't determine the actual movement, but "act a force" onto the current movement of your object. 因此,按下按钮并不能确定实际的运动,而是“施加力”到对象的当前运动上。

I've putted the shape you draw in a function so that I can call it again after you clear the canvas on key down. 我将您绘制的形状放在函数中,以便在清除按键上的画布后可以再次调用它。 Also since you are translating the context you need to clear from (-120, -120). 另外,由于您正在翻译上下文,因此需要从(-120,-120)中清除。 I hope it helps. 希望对您有所帮助。

 let canvas = document.getElementById("myCanvas"); let ctx = canvas.getContext("2d"); ctx.translate(120,120); myShape() document.addEventListener('keydown', function(event) { console.log(event.keyCode); if (event.keyCode == 37) { ctx.clearRect(-120, -120, canvas.width + 120, canvas.height + 120); ctx.translate(-120,-120); myShape() }}); function myShape(){ ctx.beginPath(); ctx.lineTo(0,0); ctx.lineTo(160,0); ctx.lineTo(160,160); ctx.lineTo(0,160); ctx.lineTo(0,0); ctx.fillStyle = "red"; ctx.fill(); ctx.stroke();} 
 <canvas id="myCanvas" height="400" width="400" style="border: 1px solid black"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在通过JavaScript的Canvas中,如何将对象移动到其他坐标? - In Canvas via JavaScript, How Can I move an object to a different coordinate? 通过JavaScript使用Canvas,如何使圆移动到单击它的位置? - Using Canvas via JavaScript, How Can I make a circle move to where I click it? 如何使用 Javascript 中的箭头键移动此 object? - How can I move this object with the arrow keys in Javascript? 在通过 JavaScript 的 Canvas 中,如何获取对象的坐标,然后将该对象移动到不同的坐标? - In Canvas via JavaScript, How can I get the coordinates of an object and then move that object to a different coordinate? 如何使用javascript中的箭头键在画布上移动图像 - How to move image on canvas using arrow keys in javascript 如何使用JavaScript使用箭头键移动对象 - How to move an object using arrow keys using javascript 如何使用箭头键或 WASD 使 object(图像)在 canvas 周围平滑移动? - how to make an object (image) smoothly move around a canvas with arrow keys or WASD? 如何使用箭头键在画布中平滑移动对象 - How to use arrow keys to move object smoothly in canvas 如何通过箭头键连续/平滑地移动元素? - How to move an element via arrow keys continuously/smoothly? jQuery-如何处理可通过箭头键更新的字段? - JQuery - How to deal with field that can be updated via arrow keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM