Javascript 如何将移动按钮控件添加到html画布

Javascript 如何将移动按钮控件添加到html画布,javascript,canvas,touch-event,Javascript,Canvas,Touch Event,我正在做一个基本的画布游戏,里面有太空入侵者,除了移动按钮控制外,所有的东西都在运行。我找不到一种方法将触摸控制与按键代码的使用方式结合起来 可在此处找到工作版本: 当前输入的处理方式如下: handleInput: function() { if (isKeyDown(LEFT_KEY)) { this.xVel = -175; } else if (isKeyDown(RIGHT_KEY)) { this.xVel = 175; } else this.xVel

我正在做一个基本的画布游戏,里面有太空入侵者,除了移动按钮控制外,所有的东西都在运行。我找不到一种方法将触摸控制与按键代码的使用方式结合起来

可在此处找到工作版本:

当前输入的处理方式如下:

handleInput: function() {
  if (isKeyDown(LEFT_KEY)) {
    this.xVel = -175;
  } else if (isKeyDown(RIGHT_KEY)) {
    this.xVel = 175;
  } else this.xVel = 0;

  if (wasKeyPressed(SHOOT_KEY)) {
    if (this.bulletDelayAccumulator > 0.5) {
      this.shoot();
      this.bulletDelayAccumulator`enter code here` = 0;
    }
  }