javascript - 鼠标事件到 ipad 兼容性的触摸事件

标签 javascript html canvas mouseevent touch-event

我有这个 child 绘画应用程序,它使用鼠标事件在图像上作画。但是我需要将鼠标事件转换为触摸,以便它可以在 ipad 上工作。请有人可以向我解释如何执行此操作。我的应用程序代码与此示例代码非常相似 http://cool-php-tutorials.blogspot.co.uk/2011/05/simple-html5-drawing-app-with-saving.html .

P.S 我对 javascript 的了解不是高级水平,所以如果您能向我展示工作代码或示例,将会有很大帮助

我的鼠标事件代码如下。请将此功能从鼠标隐藏到触摸..请自从 2 天以来我一直坚持这个..:|

var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();

// binding events to the canvas
$('#drawingCanvas').mousedown(function(e){
  var mouseX = e.pageX - this.offsetLeft;
  var mouseY = e.pageY - this.offsetTop;

  paint = true; // start painting
  addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);

  // always call redraw
  redraw();
});

$('#drawingCanvas').mousemove(function(e){
  if(paint){
    addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
    redraw();
  }
});

// when mouse is released, stop painting, clear the arrays with dots
$('#drawingCanvas').mouseup(function(e){
  paint = false;

  clickX = new Array();
  clickY = new Array();
  clickDrag = new Array();
});

// stop painting when dragged out of the canvas
$('#drawARobot').mouseleave(function(e){
  paint = false;
});


// The function pushes to the three dot arrays
function addClick(x, y, dragging)
{
  clickX.push(x);
  clickY.push(y);
  clickDrag.push(dragging);
}



// this is where actual drawing happens
// we add dots to the canvas


function redraw(){

  for(var i=0; i < clickX.length; i++)
  {  
    context.beginPath();
    if(clickDrag[i] && i){
      context.moveTo(clickX[i-1], clickY[i-1]);
     }else{
       context.moveTo(clickX[i]-1, clickY[i]);
     }
     context.lineTo(clickX[i], clickY[i]);
     context.closePath();
     context.stroke();
  }
}

最佳答案

你可以这样映射它:

$('#drawingCanvas').bind("mousedown touchstart", function(e){

$('#drawingCanvas').bind("mousemove touchmove", function(e){

$('#drawingCanvas').bind("mouseup touchend", function(e){

然后根据需要微调代码。

关于javascript - 鼠标事件到 ipad 兼容性的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17613710/

相关文章:

html - CSS定位——两个元素并排

javascript - 如何将图像添加到电子邮件的 html 正文 (Go)

php - 在生成整个页面(由 PHP 动态生成)之前,如何预加载 .css 和 .js 文件

javascript 事件、onfocus

javascript - 尝试将一个文件夹返回到 index.html 时如何修复 "Error Loading Page"

php - 通过 PHP 中的提交输入显示数组值

javascript - 使用后如何从 GPU 清理和卸载 WebGL Canvas 上下文?

android - 如何通过Canvas DrawBitmap绘制位图的一部分

javascript - 使用内置 Magento 表单验证将单选框设置为必填条目

javascript - 谷歌地图折线不显示和抛出错误