Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var drawGrid = (function drawGridModule() { // this is the encapsulating scope
- var localZoom;
- var gridXIterations;
- var gridYIterations;
- var gridT1ZoomCap = 0.126;
- var gridT2ZoomCap = 0.016;
- var gridT3ZoomCap = 0.0021;
- var gridT4ZoomCap = 0.00011;
- var tier1Color;
- var tier2Color;
- var tier3Color;
- var tier4Color;
- var localResX;
- var localResY;
- var x = grid.x;
- var y = grid.y;
- var xIndex = grid.xIndex;
- var yIndex = grid.yIndex;
- var gridSize = grid.cellWidth;
- return function drawGrid(camera, context) {
- grid.x = camera.x;
- grid.y = camera.y;
- x = -grid.x
- y = -grid.y
- xIndex = grid.xIndex;
- yIndex = grid.yIndex;
- gridSize = grid.cellWidth;
- if (grid.on === true) {//FADES GRID IN AND OUT WHEN ITS ON OR OFF\
- grid.alpha += .08
- } else {
- grid.alpha -= .08;
- }
- if (grid.alpha > 0) { //DOESN'T DRAW GRID IF GRID IS 100% TRANSPARENT
- localResX = camera.resX;
- localResY = camera.resY;
- localZoom = camera.zoomLevel;
- tier1Color = "rgba( 50, 50,180, " + Math.min(0.6, 0.6 * (1 - gridT1ZoomCap / localZoom)) + ")";
- tier2Color = "rgba(120,120,255, " + Math.min(0.4, 0.5 * (1 - gridT2ZoomCap / localZoom)) + ")";
- tier3Color = "rgba(160,160,255, " + Math.min(0.4, 0.5 * (1 - gridT3ZoomCap / localZoom)) + ")";
- tier4Color = "rgba(255,255,255, " + Math.min(0.35, 0.4 * (1 - gridT4ZoomCap / localZoom)) + ")";
- gridXIterations = Math.ceil(camera.halfResX / grid.cellWidth / localZoom);
- gridYIterations = Math.ceil(camera.halfResY / grid.cellWidth / localZoom);
- context.save();
- context.globalAlpha = grid.alpha;
- context.lineWidth = grid.thickness * Math.min(1, (1 - gridT4ZoomCap / localZoom));
- while (x > gridSize || x < -gridSize || y < -gridSize || y > gridSize) {
- if (x < -gridSize) {
- x += gridSize;
- xIndex++;
- }
- else if (x > gridSize) {
- x -= gridSize;
- xIndex--;
- }
- if (y < -gridSize) {
- y += gridSize;
- yIndex++;
- }
- else if (y > gridSize) {
- y -= gridSize;
- yIndex--;
- }
- }
- //EVERY LINE
- if (localZoom > gridT1ZoomCap) {
- context.strokeStyle = tier1Color;
- for (var i = -gridYIterations; i <= gridYIterations; i++) {
- if ((i + yIndex) % 5 !== 0) {
- context.beginPath();
- context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
- context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom); //HORIZONTAL
- context.stroke();
- }
- }
- for (i = -gridXIterations; i <= gridXIterations; i++) {
- if ((i + xIndex) % 5 !== 0) {
- context.beginPath();
- context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
- context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
- context.stroke();
- }
- }
- }
- //EVERY 5th LINE
- if (localZoom > gridT2ZoomCap) {
- context.strokeStyle = tier2Color;
- for (i = -gridYIterations - 5 - yIndex % 5 + gridYIterations % 5; i <= gridYIterations; i += 5) {
- if ((i + yIndex) % 25 !== 0) {
- context.beginPath();
- context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
- context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom);//HORIZONTAL
- context.stroke();
- }
- }
- for (i = -gridXIterations - 5 - xIndex % 5 + gridXIterations % 5; i <= gridXIterations; i += 5) {
- if ((i + xIndex) % 25 !== 0) {
- context.beginPath();
- context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
- context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
- context.stroke();
- }
- }
- }
- //EVERY 25th LINE
- if (localZoom > gridT3ZoomCap) {
- context.strokeStyle = tier3Color;
- for (i = -gridYIterations - 25 - yIndex % 25 + gridYIterations % 25; i <= gridYIterations + 25; i += 25) {
- if ((i + yIndex) % 125 !== 0) {
- context.beginPath();
- context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
- context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom);//HORIZONTAL
- context.stroke();
- }
- }
- for (i = -gridXIterations - 25 - xIndex % 25 + gridXIterations % 25; i <= gridXIterations + 25; i += 25) {
- if ((i + xIndex) % 125 !== 0) {
- context.beginPath();
- context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
- context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
- context.stroke();
- }
- }
- }
- //EVERY 500th LINE
- context.strokeStyle = tier4Color;
- for (i = -gridYIterations - 125 - yIndex % 125 + gridYIterations % 125; i <= gridYIterations + 125; i += 125) {
- context.beginPath();
- context.moveTo(0, localResY / 2 + (y + (gridSize * i)) * localZoom);
- context.lineTo(localResX, localResY / 2 + (y + (gridSize * i)) * localZoom);//HORIZONTAL
- context.stroke();
- }
- for (i = -gridXIterations - 125 - xIndex % 125 + gridXIterations % 125; i <= gridXIterations + 125; i += 125) {
- context.beginPath();
- context.moveTo(localResX / 2 + (x + (gridSize * i)) * localZoom, 0);
- context.lineTo(localResX / 2 + (x + (gridSize * i)) * localZoom, localResY); //Vertical
- context.stroke();
- }
- context.restore();
- }
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement