在通过JavaScript的Canvas中,如何将对象移动到其他坐标?

时间:2019-03-01 08:02:52

标签: javascript html canvas

我在这里有一个带有控件的简单“游戏”,还有一个重置按钮,用于将绘制的图像(笑脸)重置回中心。我用一个空的圆圈试过了这段代码,我可以通过按钮将其移动而没有任何问题。我继续装饰圆圈,现在无法移动绘制的图像。任何反馈或解决方案将不胜感激。

<!DOCTYPE>
<html lang="en">
<meta charset="UTF-8">
<body>
<button id="reset">Reset</button></br>
<canvas id="myCanvas" width="250" height="250" style="border:1px solid#000000;"></canvas></br>
<button id="left">Left</button>
<button id="up">Up</button>
<button id="down">Down</button>
<button id="right">Right</button>
<script>
    let c, ctx, pos, centre = { x: 95, y: 50 }
    window.onload = function drawSmile(){
      let c = document.getElementById("myCanvas");
      let ctx = c.getContext("2d");
      let centerX = c.width / 2;
      let centerY = c.height / 2;
      let radius = 70;
      let eyeRadius = 10;
      let eyeXOffset = 25;
      let eyeYOffset = 20;

      // draw the yellow circle
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
      ctx.fillStyle = 'yellow';
      ctx.fill();
      ctx.lineWidth = 5;
      ctx.strokeStyle = 'black';
      ctx.stroke();

      // draw the eyes
      ctx.beginPath();
      var eyeX = centerX - eyeXOffset;
      var eyeY = centerY - eyeXOffset;
      ctx.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false);
      var eyeX = centerX + eyeXOffset;
      ctx.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false);
      ctx.fillStyle = 'black';
      ctx.fill();

      // draw the mouth
      ctx.beginPath();
      ctx.arc(centerX, centerY, 50, 0, Math.PI, false);
      ctx.stroke();
      reset();
    }

    function draw(){
      clear();
      ctx.beginPath();
      ctx.arc(centre.x + pos.left, centre.y + pos.top, 40, 0, 2*Math.PI);
      ctx.stroke();
    }

    function clear(){
      ctx.clearRect(0, 0, 2000, 1000);
    } 

    function reset() {
      pos = { left: 0, top: 0 }
      draw();
    }

    function up() {
    ctx.clearRect(0, 0, 2000, 1000);
      pos.top-= 20;
      draw();
    }

    function down() {
      pos.top+= 20;
      draw();
    }

    function left() {  
      pos.left-= 20;
      draw();
    }

    function right() {
      pos.left+= 20;
      draw();
    }

document.getElementById("reset").onclick = reset;
document.getElementById("up").onclick = up;
document.getElementById("down").onclick = down;
document.getElementById("left").onclick = left;
document.getElementById("right").onclick = right;
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

请检查以下代码。它类似于最后一个空圆图,我刚刚包括了正确的位置计算,并且在适当的位置包括了closePath()方法。

let c
, ctx
, pos
, centerX
, centerY
, radius
, eyeRadius
, eyeXOffset
, eyeYOffset

window.onload = function() {
  c = document.getElementById("myCanvas");
  ctx = c.getContext("2d");
  centerX = c.width / 2;
  centerY = c.height / 2;
  
  radius = 70;
  eyeRadius = 10;
  eyeXOffset = 25;
  eyeYOffset = 20;
  
  reset();
}

function drawFace(){
  // Draw the yellow circle
  ctx.beginPath();
  ctx.arc(centerX + pos.left, centerY + pos.top, radius, 0, 2 * Math.PI, false);
  ctx.fillStyle = 'yellow';
  ctx.fill();
  ctx.lineWidth = 5;
  ctx.strokeStyle = 'black';
  ctx.stroke();
  ctx.closePath();
}

function drawEyes(){
  // Draw the eyes
  var eyeX = centerX + pos.left - eyeXOffset;
  var eyeY = centerY + pos.top - eyeYOffset;
  ctx.beginPath();
  ctx.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false);
  ctx.fillStyle = 'black';
  ctx.fill();
  ctx.closePath();
  
  ctx.beginPath();
  eyeX = centerX + pos.left + eyeXOffset;
  ctx.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false);
  ctx.fillStyle = 'black';
  ctx.fill();
  ctx.closePath();
}

function drawMouth(){
  // Draw the mouth
  ctx.beginPath();
  ctx.arc(centerX + pos.left, centerY + pos.top, 50, 0, Math.PI, false);
  ctx.stroke();
  ctx.closePath();
}

function draw() {
  clear();
  drawFace();
  drawEyes();
  drawMouth();
}

function clear() {
  ctx.clearRect(0, 0, c.width, c.height);
}

function reset() {
  pos = {
    left: 0,
    top: 0
  }
  draw();
}

function up() {
  pos.top -= 20;
  draw();
}

function down() {
  pos.top += 20;
  draw();
}

function left() {
  pos.left -= 20;
  draw();
}

function right() {
  pos.left += 20;
  draw();
}

document.getElementById("reset").onclick = reset;
document.getElementById("up").onclick = up;
document.getElementById("down").onclick = down;
document.getElementById("left").onclick = left;
document.getElementById("right").onclick = right;
<!DOCTYPE>
<html lang="en">
<meta charset="UTF-8">

<body>
  <button id="reset">Reset</button><br>
  <canvas id="myCanvas" width="250" height="250" style="border:1px solid#000000;"></canvas><br>
  <button id="left">Left</button>
  <button id="up">Up</button>
  <button id="down">Down</button>
  <button id="right">Right</button>
</body>

</html>