Advertisement
KoctrX

Untitled

Aug 22nd, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. snapToGrid(options) {
  2.         let grid = 50;//this.grid;
  3.         const target = options.target;
  4.  
  5.         let incrementOriginX = 0;
  6.         let incrementOriginY = 0;
  7.  
  8.         if (target.originX == 'center') incrementOriginX = (target.width / 2);
  9.         if (target.originY == 'center') incrementOriginY = (target.height / 2);
  10.  
  11.         if (target.padding > 0) {
  12.             incrementOriginX += target.padding / 2;
  13.             incrementOriginY += target.padding / 2;
  14.         }
  15.  
  16.         let left = target.left - incrementOriginX;
  17.         let top = target.top - incrementOriginY;
  18.  
  19.         let gridX = 50, gridY = 50;
  20.         const canvas = options?.target?.canvas || false;
  21.         if (canvas) {
  22.             gridX = (canvas.getWidth() / canvas.getZoom()) / 30;
  23.             gridY = (canvas.getHeight() / canvas.getZoom()) / 30;
  24.         }
  25.  
  26.         let opts = false;
  27.         if (Math.round(left / gridX * 4) % 4 == 0) {
  28.             opts = { ...opts, left: (Math.round(left / gridX) * gridX) + incrementOriginX };
  29.         }
  30.  
  31.         if (Math.round(top / gridY * 4) % 4 == 0) {
  32.             opts = { ...opts, top: (Math.round(top / gridY) * gridY) + incrementOriginY };
  33.         }
  34.  
  35.         if(opts) options.target.set(opts).setCoords();
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement