2012-06-25 95 views
1

我對觸摸處理程序有一個小問題...它有時候在觸摸時有時候不能工作,它在繪製完後不能讀取我的數據,而是用直線繪製,所以我想知道是什麼問題以及我做錯了什麼?請幫助me..i已經把我的代碼到jsfiddle..please幫我(http://jsfiddle.net/Frebu/1/)ontouch事件問題

function touchHandler(event) { 
     var touches = event.changedTouches, 
     first = touches[0], 
     type = ""; 
     switch (event.type) { 
      case "touchstart": type = "mousedown"; break; 
      case "touchmove": type = "mousemove"; break; 
      case "touchend": type = "mouseup"; break; 
      default: return; 
     } 

     var simulatedEvent = document.createEvent("MouseEvent"); 
     simulatedEvent.initMouseEvent(type, true, true, window, 1, 
         first.screenX, first.screenY, 
         first.clientX, first.clientY, false, 
         false, false, false, 0/*left*/, null); 

     first.target.dispatchEvent(simulatedEvent); 
     event.preventDefault(); 
    } 

    function init(id) { 
     document.getElementById(id).addEventListener("touchstart", touchHandler, true); 
     document.getElementById(id).addEventListener("touchmove", touchHandler, true); 
     document.getElementById(id).addEventListener("touchend", touchHandler, true); 
    } 
$(document).ready(function() { 
     init('myCanvas'); 
});​ 
+0

這裏是我的整個代碼段.http://jsfiddle.net/Frebu/1/ – newbie

+0

你在'HTML中打錯了'ontouchstart'作爲'oontouchstart',但我不認爲這是問題所在。某處存在堆棧溢出。 –

+0

亞..這不是一個問題,我只是不知道爲什麼有時工作得很好,現在它不能讀取和生成觸摸設備上的繪圖數據,如ipad – newbie

回答

1

我recomendation是使用hammer.js,這是一個偉大觸摸庫(在瀏覽器上回退到鼠標)。

http://eightmedia.github.com/hammer.js/

除此之外,我沒有測試小提琴Chrome上模擬觸摸事件的任何問題。而且奇怪,代碼似乎很好。

+0

亞..奇怪,你能從textarea獲得數據嗎? – newbie