0

I have problem with getImageData in Mozilla. I did a function that after click on canvas get coordinates of mouse. Coordinates are good, but there is problem, that in Mozilla it gives mi bad numbers of getImageDatas - for example in chrome it gives mi red: 10, green: 0, blue: 10 and Mozilla gives red: 60, green: 255, blue: 10... I do not know where is the problem... please, can you help? Here is the function: http://pastebin.com/MjnG0Nbm

Jan Kožušník
  • 683
  • 3
  • 16
  • 30

1 Answers1

0

Maybe your getMousePos() doesn't always give the desired result

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.

Try this instead:

function getMousePosition(canvas, evt) {
    return {
      x: evt.clientX - canvas.offsetLeft + window.scrollX,
      y: evt.clientY - canvas.offsetTop + window.scrollY
    };        
}
devnull69
  • 16,402
  • 8
  • 50
  • 61
  • I tried that function but it gives incorrect numbers - instead of 0,0 in left top corner it gives 159 -1675... – Jan Kožušník Mar 10 '13 at 10:42
  • Sorry, forgot to copy/paste the scrollX and scrollY parts from my example code – devnull69 Mar 10 '13 at 11:22
  • Interesting ... in your first pastebin the method is called as getMousePos(), but in your second pastebin you define it as getMousePosition() – devnull69 Mar 10 '13 at 11:24
  • the first pastebin is before I renamed function to get mouse coordinates... So how it shloud by with the scrollX and scrollY part? Thank you – Jan Kožušník Mar 10 '13 at 14:59