Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. var drawGrid = (function drawGridModule() { // this is the encapsulating scope
  2. var localZoom;
  3. var gridXIterations;
  4. var gridYIterations;
  5. var gridT1ZoomCap = 0.126;
  6. var gridT2ZoomCap = 0.016;
  7. var gridT3ZoomCap = 0.0021;
  8. var gridT4ZoomCap = 0.00011;
  9. var tier1Color;
  10. var tier2Color;
  11. var tier3Color;
  12. var tier4Color;
  13.  
  14. var localResX;
  15. var localResY;
  16.  
  17. var x = grid.x;
  18. var y = grid.y;
  19. var xIndex = grid.xIndex;
  20. var yIndex = grid.yIndex;
  21. var gridSize = grid.cellWidth;
  22.  
  23. return function drawGrid(camera, context) {
  24. grid.x = camera.x;
  25. grid.y = camera.y;
  26. x = -grid.x
  27. y = -grid.y
  28. xIndex = grid.xIndex;
  29. yIndex = grid.yIndex;
  30. gridSize = grid.cellWidth;
  31.  
  32. if (grid.on === true) {//FADES GRID IN AND OUT WHEN ITS ON OR OFF\
  33. grid.alpha += .08
  34. } else {
  35. grid.alpha -= .08;
  36. }
  37. if (grid.alpha > 0) { //DOESN'T DRAW GRID IF GRID IS 100% TRANSPARENT
  38. localResX = camera.resX;
  39. localResY = camera.resY;
  40. localZoom = camera.zoomLevel;
  41. tier1Color = "rgba( 50, 50,180, " + Math.min(0.6, 0.6 * (1 - gridT1ZoomCap / localZoom)) + ")";
  42. tier2Color = "rgba(120,120,255, " + Math.min(0.4, 0.5 * (1 - gridT2ZoomCap / localZoom)) + ")";
  43. tier3Color = "rgba(160,160,255, " + Math.min(0.4, 0.5 * (1 - gridT3ZoomCap / localZoom)) + ")";
  44. tier4Color = "rgba(255,255,255, " + Math.min(0.35, 0.4 * (1 - gridT4ZoomCap / localZoom)) + ")";
  45. gridXIterations = Math.ceil(camera.halfResX / grid.cellWidth / localZoom);
  46. gridYIterations = Math.ceil(camera.halfResY / grid.cellWidth / localZoom);
  47. context.save();
  48. context.globalAlpha = grid.alpha;
  49. context.lineWidth = grid.thickness * Math.min(1, (1 - gridT4ZoomCap / localZoom));
  50.  
  51. while (x > gridSize || x < -gridSize || y < -gridSize || y > gridSize) {
  52. if (x < -gridSize) {
  53. x += gridSize;
  54. xIndex++;
  55. }
  56. else if (x > gridSize) {
  57. x -= gridSize;
  58. xIndex--;
  59. }
  60. if (y < -gridSize) {
  61. y += gridSize;
  62. yIndex++;
  63. }
  64. else if (y > gridSize) {
  65. y -= gridSize;
  66. yIndex--;
  67. }
  68. }
  69.  
  70. //EVERY LINE
  71. if (localZoom > gridT1ZoomCap) {
  72. context.strokeStyle = tier1Color;
  73. for (var i = -gridYIterations; i <= gridYIterations; i++) {
  74. if ((i + yIndex) % 5 !== 0) {
  75. context.beginPath();
  76. context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
  77. context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom); //HORIZONTAL
  78. context.stroke();
  79. }
  80. }
  81. for (i = -gridXIterations; i <= gridXIterations; i++) {
  82. if ((i + xIndex) % 5 !== 0) {
  83. context.beginPath();
  84. context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
  85. context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
  86. context.stroke();
  87. }
  88. }
  89. }
  90. //EVERY 5th LINE
  91. if (localZoom > gridT2ZoomCap) {
  92. context.strokeStyle = tier2Color;
  93. for (i = -gridYIterations - 5 - yIndex % 5 + gridYIterations % 5; i <= gridYIterations; i += 5) {
  94. if ((i + yIndex) % 25 !== 0) {
  95. context.beginPath();
  96. context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
  97. context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom);//HORIZONTAL
  98. context.stroke();
  99. }
  100. }
  101. for (i = -gridXIterations - 5 - xIndex % 5 + gridXIterations % 5; i <= gridXIterations; i += 5) {
  102. if ((i + xIndex) % 25 !== 0) {
  103. context.beginPath();
  104. context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
  105. context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
  106. context.stroke();
  107. }
  108. }
  109. }
  110. //EVERY 25th LINE
  111. if (localZoom > gridT3ZoomCap) {
  112. context.strokeStyle = tier3Color;
  113. for (i = -gridYIterations - 25 - yIndex % 25 + gridYIterations % 25; i <= gridYIterations + 25; i += 25) {
  114. if ((i + yIndex) % 125 !== 0) {
  115. context.beginPath();
  116. context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
  117. context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom);//HORIZONTAL
  118. context.stroke();
  119. }
  120. }
  121. for (i = -gridXIterations - 25 - xIndex % 25 + gridXIterations % 25; i <= gridXIterations + 25; i += 25) {
  122. if ((i + xIndex) % 125 !== 0) {
  123. context.beginPath();
  124. context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
  125. context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
  126. context.stroke();
  127. }
  128. }
  129. }
  130. //EVERY 500th LINE
  131. context.strokeStyle = tier4Color;
  132. for (i = -gridYIterations - 125 - yIndex % 125 + gridYIterations % 125; i <= gridYIterations + 125; i += 125) {
  133. context.beginPath();
  134. context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
  135. context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom);//HORIZONTAL
  136. context.stroke();
  137. }
  138. for (i = -gridXIterations - 125 - xIndex % 125 + gridXIterations % 125; i <= gridXIterations + 125; i += 125) {
  139. context.beginPath();
  140. context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
  141. context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
  142. context.stroke();
  143. }
  144. context.restore();
  145. }
  146. };
  147. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement