javascript - 如何在屏幕上滚动时使用移动触摸在 Canvas 上绘制控件

标签 javascript canvas mobile

我正在尝试创建一个移动应用程序,用户可以在其中在 Canvas 上绘制多个控件(矩形)。

如果 Canvas 适合屏幕,它工作正常,但是当我将 Canvas 的高度和宽度增加到屏幕外时,控件绘制得不好(当我增加 Canvas 的大小超过移动屏幕的大小时出现滚动) . 如何在滚动模式下解决这个问题?

Use Chrome Inspect mobile view (iPhone 6/7/8) to test this code Use Pointer drag to draw controls

<html>
<body>
<canvas id="canvas_transparent" height="3000" width="1000" style="border: 1px solid black;"></canvas>
</body>
</html>

<script type="text/javascript">
var canvas = document.getElementById("canvas_transparent");
var ctx = canvas.getContext("2d");
var endX;
var startX;
var endY;
var startY;
var positions = [];
//--------------------------------------------------------
canvas.addEventListener("touchstart", function(e) {
  mousePos = getTouchPos(canvas, e);
  startX = mousePos.x;
  startY = mousePos.y;
  var touch = e.touches[0];
  var mouseEvent = new MouseEvent("mousedown", {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
}, false);
//---------------------------------------------------------
canvas.addEventListener("touchend", function(e) {
  positions.push({
    s_x: startX,
    s_y: startY,
    e_x: endX,
    e_y: endY
  });
  drawAllControls();
  console.log(positions);
  var mouseEvent = new MouseEvent("mouseup", {});
  canvas.dispatchEvent(mouseEvent);
}, false);
//---------------------------------------------------------
canvas.addEventListener("touchmove", function(e) {
  var touch = e.touches[0];
  endX = touch.clientX;
  endY = touch.clientY;
  drawSquare()
  var mouseEvent = new MouseEvent("mousemove", {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
}, false);
//--------------------------------------------------------
// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
  var rect = canvasDom.getBoundingClientRect();
  return {
    x: touchEvent.touches[0].clientX - rect.left,
    y: touchEvent.touches[0].clientY - rect.top
  };
}
//--------------------------------------------------------
function drawSquare() {
  // creating a square
  var w = endX - startX;
  var h = endY - startY;
  var offsetX = (w < 0) ? w : 0;
  var offsetY = (h < 0) ? h : 0;
  var width = Math.abs(w);
  var height = Math.abs(h);
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  ctx.rect(startX + offsetX, startY + offsetY, width, height);
  ctx.fillStyle = "#222222";
  ctx.fill();
  ctx.lineWidth = 2;
  ctx.strokeStyle = 'black';
  ctx.stroke();
}
//----------------------------------------------------------
function drawAllControls() {
  for (var i = 0; i < positions.length; i++) {
    var w = positions[i].e_x - positions[i].s_x;
    var h = positions[i].e_y - positions[i].s_y;
    var offsetX = (w < 0) ? w : 0;
    var offsetY = (h < 0) ? h : 0;
    var width = Math.abs(w);
    var height = Math.abs(h);
    ctx.beginPath();
    ctx.rect(positions[i].s_x + offsetX, positions[i].s_y + offsetY, width, height);
    ctx.fillStyle = "#222222";
    ctx.fill();
    ctx.lineWidth = 2;
    ctx.strokeStyle = 'black';
    ctx.stroke();
  }
}
</script>

enter image description here enter image description here

最佳答案

clientX/clientY 不考虑滚动偏移。我想你想要 pageX/pageY

https://developer.mozilla.org/en-US/docs/Web/API/Touch

// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
  var rect = canvasDom.getBoundingClientRect();
  return {
    x: touchEvent.touches[0].pageX - rect.left,
    y: touchEvent.touches[0].pageY - rect.top
  };
}

关于javascript - 如何在屏幕上滚动时使用移动触摸在 Canvas 上绘制控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48561153/

相关文章:

javascript - 是否可以将 ko.computed 继承到原型(prototype)类?

javascript - 将变量从 objective-c 返回到 javascript

jquery - 对由 Json 数组填充的 JQuery 列表进行排序

mobile - html5 音频的 setVolume 在移动 Android 或 Safari 上不起作用 - 任何解决方法?

javascript - 在不弹出的情况下创建 React App 中的工作箱

javascript - 在子更改事件 Firebase 上更新 react native ListView

wpf - 如何了解 Canvas.Children 中对象的类型

javascript - 获取带有原始图像 alpha 的图像数据?

css - 旋转后鼠标移动不准确

javascript - 在移动设备上可玩的简单 HTML5 游戏使用什么