2013-03-09 36 views
0

我在Mozilla的getImageData有問題。我做了一個函數,點擊畫布後獲取鼠標的座標。座標是好的,但是有一個問題,在Mozilla中,它給出了很多糟糕的getImageDatas數字 - 例如在chrome中,它給出了紅色:10,綠色:0,藍色:10,Mozilla給出紅色:60,綠色:255,藍色:10 ...我不知道問題出在哪裏......請你幫忙嗎? 下面是函數:http://pastebin.com/MjnG0NbmMozilla的getImageDataProblem

+0

沒有人知道一些關於這個問題? – 2013-03-09 20:18:18

+0

你的getMousePos()函數是什麼? – devnull69 2013-03-09 22:30:25

+0

Here:http://pastebin.com/iPet57wV 但是這個功能應該沒問題。它在所有瀏覽器中顯示相同的座標 – 2013-03-10 08:04:17

回答

0

也許你getMousePos()並不總是得到希望的結果

The amount of scrolling that has been done of the viewport area (or any other 
scrollable element) is taken into account when computing the bounding rectangle. 
This means that the top and left property change their values as soon as the 
scrolling position changes (so their values are relative to the viewport and not 
absolute). If this is not the desired behaviour just add the current scrolling 
position to the top and left property (via window.scrollX and window.scrollY) to 
get constant values independent from the current scrolling position. 

試試這個:

function getMousePosition(canvas, evt) { 
    return { 
     x: evt.clientX - canvas.offsetLeft + window.scrollX, 
     y: evt.clientY - canvas.offsetTop + window.scrollY 
    };   
} 
+0

我試過這個功能,但它給出了不正確的數字 - 而不是0,0在左上角它給出了159 -1675 ... – 2013-03-10 10:42:40

+0

對不起,忘了複製/粘貼scrollX和scrollY部件我的示例代碼 – devnull69 2013-03-10 11:22:52

+0

有趣...在您的第一個pastebin中,該方法稱爲getMousePos(),但在第二個pastebin中將其定義爲getMousePosition() – devnull69 2013-03-10 11:24:31