Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="grid" style="z-index: 5;top:40px;position:absolute;"></div>

<button id="blocks">Blocks</button>
<button id="diamonds">Diamonds</button>
<button id="hex">Hex</button>
              
            
!

CSS

              
                
              
            
!

JS

              
                $(document).ready(function(){
  grid('blocks', 10, 10, 800, 800);
  
  $("#blocks").click(function(){
    grid('blocks', 10, 10, 800, 800);
  });
  $("#diamonds").click(function(){
    grid('diamonds', 10, 10, 800, 800);
  });
  $("#hex").click(function(){
    grid('hex', 10, 10, 800, 800);
  });
  
  
});

function grid(type, w, h, totalW, totalH){
	
	var $this = this;
	this.type = type || 'blocks';// blocks, diamonds, hexagons
	this.blockW = w || 25;
	this.blockH = h || 25;
	this.container;
  $('#grid').empty();
  
		this.container = document.createElement('div');
		this.container.style.position = 'absolute';
		this.container.style.width = '100%';
		this.container.style.height = '100%';
		this.container.id = 'gridContainer';
		
  
		var c = document.createElement("canvas");
	    c.width  = totalW;
	    c.height = totalH;
	    
		var totalW = totalW || $(document).width();
		var totalH = totalH || $(document).height();
		
	if(this.type == 'blocks'){
		
	    var mapGridCanvas = c.getContext("2d");
	    mapGridCanvas.clearRect(0, 0, c.width, c.height);
	    mapGridCanvas.globalAlpha = 1;
	    mapGridCanvas.strokeStyle = "#1e1e1e";
	    mapGridCanvas.lineWidth = 1;
    	mapGridCanvas.beginPath();
    	var x = 0;
    	var y = 0;
		for(var i = 0;i < Math.round(totalW/blockW); i++){
				
				x = i*blockW;
				y = 0;
				mapGridCanvas.moveTo(x,y);
				mapGridCanvas.lineTo(x,y+totalH);
				
		}
		
		for(var j = 0; j < Math.round(totalH/blockH); j++){
				
				x = 0;
				y = j*blockH;
				
				mapGridCanvas.moveTo(x,y);
				mapGridCanvas.lineTo(x+totalW,y);
				
				
		}
		
		mapGridCanvas.stroke();
		this.container.appendChild(c);
		document.getElementById('grid').appendChild(this.container);
		
	}
	else if(this.type == 'hex'){
		
		var c = drawHexGrid({}, c);
		this.container.appendChild(c);
		document.getElementById('grid').appendChild(this.container);
	}
	else if(this.type == 'diamonds'){
		
		
	    var mapGridCanvas = c.getContext("2d");
	    mapGridCanvas.clearRect(0, 0, c.width, c.height);
	    mapGridCanvas.globalAlpha = 1;
	    mapGridCanvas.strokeStyle = "#1e1e1e";
	    mapGridCanvas.lineWidth = 1;
    	mapGridCanvas.beginPath();
    	var x = 0;
    	var y = 0;
        var z = 0;
    	var counter = 0;
		for(var i = 0; i < Math.round(totalH/blockH); i++){
			
			var z = counter;
			while(x <= blockW*Math.round(totalW/blockW)){
				
				if(z%2 == 0){
					mapGridCanvas.moveTo( x, y+blockW );
					mapGridCanvas.lineTo( x+blockW, y );
				}
				else{
					mapGridCanvas.moveTo(x,y);
					mapGridCanvas.lineTo(x+blockW, y+blockW);
				}
					
				x += blockW;	
				z += 1;
			}
			
			x = 0;
			y = y + blockW;
			counter += 1;
			
		}
		
		mapGridCanvas.stroke();
		this.container.appendChild(c);
		document.getElementById('grid').appendChild(this.container);
		
	}
	

	function drawHexGrid(opts,c) {
		
		var alpha 		= opts.alpha || 1;
		var color 		= opts.color || '#1e1e1e';
		var lineWidth  	= opts.lineWidth || 1;
		var radius 		= opts.radius || 20;
		
	    
	    var mapGridCanvas = c.getContext("2d");
	    mapGridCanvas.clearRect(0, 0, c.width, c.height);
	    mapGridCanvas.globalAlpha = alpha;
	    mapGridCanvas.strokeStyle = color;
	    mapGridCanvas.lineWidth = lineWidth;
	
	    //length of line
	    r = radius;
	    part = 60;
	    hexSize = r*Math.sqrt(3);
	    yHexSize = r*Math.sqrt(2.25);
	    xHexes = 2000 / hexSize;
	    yHexes = 2000 / yHexSize;
	
	    mapGridCanvas.beginPath();
	
	    //loop through hex "rows" and every other row shift
	    for (xGrid=0;xGrid<=xHexes;xGrid++){
	        for (yGrid=0;yGrid<=yHexes;yGrid++){
	            if (yGrid % 2 == 0) {
	                //even row
	                shiftX = hexSize/2;
	            }
	            else {
	                //odd row
	                shiftX=0;
	            }
	            for (i=0;i<=6;i++) {
	                var a = i * part - 90;
	                x = r * Math.cos(a * Math.PI / 180)+xGrid*hexSize+shiftX;
	                y = r * Math.sin(a * Math.PI / 180)+yGrid*yHexSize;
	                if (i == 0) {
	                    mapGridCanvas.moveTo(x,y);
	                }
	                else {
	                    mapGridCanvas.lineTo(x,y);
	                }
	            }
	        }
	    }
	    mapGridCanvas.stroke();
	    
	    return c;
	}
	
	function removeGrid(){
		
		document.removeChild(this.container);
	}
};

              
            
!
999px

Console