Guest User

Untitled

a guest
Jul 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. canvas.onmousemove = function(e)
  2. {
  3. var x = e.offsetX;
  4. var y = e.offsetY;
  5. var cx = canvas.width;
  6. var cy = canvas.height;
  7. if(x <= 0.1*cx && y <= 0.1*cy)
  8. {
  9. alert("Upper Left");
  10. //Move "viewport" to up and left (if possible)
  11. }
  12. //Additional Checks for location
  13. }
  14.  
  15. var isDown = false; // whether mouse is pressed
  16. var startCoords = []; // 'grab' coordinates when pressing mouse
  17. var last = [0, 0]; // previous coordinates of mouse release
  18.  
  19. canvas.onmousedown = function(e) {
  20. isDown = true;
  21.  
  22. startCoords = [
  23. e.offsetX - last[0], // set start coordinates
  24. e.offsetY - last[1]
  25. ];
  26. };
  27.  
  28. canvas.onmouseup = function(e) {
  29. isDown = false;
  30.  
  31. last = [
  32. e.offsetX - startCoords[0], // set last coordinates
  33. e.offsetY - startCoords[1]
  34. ];
  35. };
  36.  
  37. canvas.onmousemove = function(e)
  38. {
  39. if(!isDown) return; // don't pan if mouse is not pressed
  40.  
  41. var x = e.offsetX;
  42. var y = e.offsetY;
  43.  
  44. // set the canvas' transformation matrix by setting the amount of movement:
  45. // 1 0 dx
  46. // 0 1 dy
  47. // 0 0 1
  48.  
  49. ctx.setTransform(1, 0, 0, 1,
  50. x - startCoords[0], y - startCoords[1]);
  51.  
  52. render(); // render to show changes
  53.  
  54. }
Add Comment
Please, Sign In to add comment