画布绘制触摸未绘制

时间:2019-10-13 08:32:06

标签: javascript html5-canvas touch-event

如果我在lineTo()和moveTo()上输入位置,我有一行,但是如果我给touchstart和touchmove位置没有任何反应,并且我有bot console erreur来帮助我

touchStart(e){
  this.touchDessiner(e.changedTouches[0].pageX, e.changedTouches[0].pageY)
  console.log(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
}
touchMove(e){
  e.preventDefault();
  this.touchDessiner(e.changedTouches[0].pageX, e.changedTouches[0].pageY)
  console.log(e.changedTouches[0].pageX, e.changedTouches[0].pageY)
}

  touchDessiner(x, y){
    this.cont.lineWidth = 2;
    this.cont.strokeStyle = "#000";
    this.cont.beginPath();
    this.cont.moveTo(x, y);
    this.cont.lineTo(x, y);
    this.cont.stroke();
  }

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

以下是绘制线条的正确顺序:

在TouchStart上:
 1.开始新的路径(从画布上抬起笔)
 2.将笔移到此处

在TouchMove上:
 3.在笔仍触摸画布的情况下,将笔移到此处

canvas = document.getElementById("can");
cont = canvas.getContext("2d");

function touchStart(e){
  this.cont.beginPath();
  this.cont.moveTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
}

function touchMove(e){
  e.preventDefault();
  this.touchDessiner(e.changedTouches[0].pageX, e.changedTouches[0].pageY)
}

function touchDessiner(x, y){
  this.cont.lineWidth = 2;
  this.cont.strokeStyle = "#000";
  this.cont.lineTo(x, y);
  this.cont.stroke();
}

window.addEventListener("touchstart", touchStart);
window.addEventListener("touchmove", touchMove);
<!DOCTYPE html>
<html>
<body>
  
  canvas
  <canvas id = "can" style = "border: 1px solid black; position:absolute; left:0px; top:0px;"> </canvas>

</body>
</html>