Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. function mouseGridPosition(e,
  2. {
  3. pixel,
  4. w,
  5. h
  6. }) {
  7. // w and h are the width and height of the canvas
  8. var { canvas, context: ctx } = getCanvasAndContext();
  9.  
  10. var mX = e.offsetX;
  11. var mY = e.offsetY;
  12.  
  13. var gridOffsetX = Math.floor( (mX / canvas.width) * pixel);
  14. var gridOffsetY = Math.floor( (mY / canvas.height) * pixel);
  15. var x = (gridOffsetX/pixel) * w;
  16. var y = (gridOffsetY/pixel) * h;
  17.  
  18. var data = {
  19. left: x,
  20. top: y,
  21. right: x + w/pixel,
  22. bottom: y + h/pixel,
  23. canvasWidth: w,
  24. canvasHeight: h,
  25. pixel,
  26. canvas,
  27. context: ctx,
  28. width: w/pixel,
  29. height: h/pixel,
  30. rawMouseX: mX,
  31. rawMouseY: mY
  32. };
  33. data.centerX = data.left + (data.width / 2);
  34. data.centerY = data.top + (data.height / 2);
  35.  
  36. return data;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement