可以解决由于高速引起的鬼影效应吗?

时间:2018-01-31 07:14:03

标签: javascript canvas graphics

有这个令人讨厌的重影吗?移动物体时效果很高"高"速度和html画布。

由于每速度的fps越来越低,这在高速时是否正常? 图形中有这个术语吗? 这是JS的事吗? 可以解决吗?

(使用wasd移动) https://codepen.io/anon/pen/eVNmmm

function drawPlayerAtPosition(pos) {
  ctx.arc(pos.x, pos.y, 30, 0, Math.PI * 2)
  ctx.fill()
}

const playerPos = new Point(centerPoint.x, centerPoint.y)
const playerVelocity = new Point(0, 0)
const playerSpeed = 5
requestAnimationFrame(animate)


function animate() {

  ctx.clearRect(0, 0, innerWidth, innerHeight)
  ctx.beginPath()

  movePlayer()
  drawPlayerAtPosition(playerPos)

  ctx.stroke()
  requestAnimationFrame(animate)
}

addEventListener('keydown', e => {

  if (e.keyCode == 87) {
    playerVelocity.y = -playerSpeed
  }
  if (e.keyCode == 83) {
    playerVelocity.y = playerSpeed
  }
  if (e.keyCode == 68) {
    playerVelocity.x = playerSpeed
  }
  if (e.keyCode == 65) {
    playerVelocity.x = -playerSpeed
  }
})

 addEventListener('keyup', e => {

  if (e.keyCode == 87) {
    playerVelocity.y = 0
  }
  if (e.keyCode == 83) {
    playerVelocity.y = 0
  }
  if (e.keyCode == 68) {
    playerVelocity.x = 0
  }
  if (e.keyCode == 65) {
    playerVelocity.x = 0
  }
})

function movePlayer() {
  playerPos.x += playerVelocity.x
  playerPos.y += playerVelocity.y
}

0 个答案:

没有答案