Advertisement
jargon

Roe2Js :: "canvasCursor.js"

Jul 1st, 2024 (edited)
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onmousemove = e => {
  2.    
  3.     canvasViewCursor ( { x: e.pageX, y: e.pageY } );
  4.  
  5. }
  6.  
  7. window.onclick = e => {
  8.    
  9.     canvasViewCursor ( { x: e.pageX, y: e.pageY } );
  10.    
  11.     [ cx, cy ] = [ cx + viewCursor.hover.x, cy + viewCursor.hover.y ];
  12. }
  13.  
  14. function canvasViewCursor ( coords ) {
  15.    
  16.     canvasCursor.x = coords.x;
  17.     canvasCursor.y = coords.y;
  18.        
  19.     var viewRangeMinX = gutterW;
  20.     var viewRangeMinY = gutterH;
  21.    
  22.     var viewRangeMaxX = gutterW + (viewConstraints.viewMaxX - viewConstraints.viewMinX + 1 ) * tileW;
  23.    
  24.     var viewRangeMaxY = gutterH + (viewConstraints.viewMaxY - viewConstraints.viewMinY + 1 ) * tileH;
  25.    
  26.     if (
  27.    
  28.     (canvasCursor.x > viewRangeMinX)
  29.     &&
  30.    
  31.     (canvasCursor.x < viewRangeMaxX)
  32.     &&
  33.  
  34.     (canvasCursor.y > viewRangeMinY)
  35.     &&
  36.        
  37.     (canvasCursor.y < viewRangeMaxY)
  38.    
  39.     )
  40.     {  
  41.         x = canvasCursor.x;
  42.         y = canvasCursor.y;
  43.  
  44.         x -= gutterW;
  45.         y -= gutterH;
  46.  
  47.         x /= tileW;
  48.         y /= tileH;
  49.  
  50.         x = Math.round(x);
  51.         y = Math.round(y);
  52.        
  53.         x += viewConstraints.viewMinX;
  54.         y += viewConstraints.viewMinY;
  55.  
  56.     }else{
  57.         x = 0;
  58.         y = 0;
  59.     }
  60.     if (oobView([x,y]) !== true){
  61.         [x,y] = oobView([x,y]);
  62.     }
  63.    
  64.     [ viewCursor.hover.x, viewCursor.hover.y ] =
  65.         [ x, y ];
  66.    
  67.     console.log({x: x, y: y});
  68.     return;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement