Random Square Generator

make squares with random colors in random places.

by Bhumika107

HTML

<button id="wrapBtn">
Click me!!
</button>

CSS

.hexagons {
  position:relative;
}
p#circle {
  height: auto;
  width: 200px;
  position: relative;
  border: 1px solid;
  margin: 57px;
}

JavaScript

//Make me randomly return 1 of 2 colors
function pickColor(){
    var colorArray = ["url(http://www.iconsdb.com/icons/preview/deep-pink/hexagon-xxl.png) no-repeat", "url(http://res.cloudinary.com/demo/image/upload/hexagon_sample.png) no-repeat"];
    return colorArray[Math.floor(Math.random() * colorArray.length)];
}

//Make me randomly return a number.  You can pick the range of numbers returned.
var leftPos = [];
var topPos = [];
function pickSize() {
		var randomSize = Math.floor((Math.random()*40)+10) + "px";
    return randomSize;
}
function pickPos(posType){
    
		if(posType == "left") {
      var randomNum = Math.floor((Math.random()*700)+50) + "px";
      while(leftPos.indexOf(randomNum) != -1) {
          var randomNum = Math.floor((Math.random()*700)+50) + "px";
      }
    	leftPos.push(randomNum);
    } else {
      var randomNum = Math.floor((Math.random()*20)+10) + "px";
      while(topPos.indexOf(randomNum) != -1) {
          var randomNum = Math.floor((Math.random()*20)+10) + "px";
      }
    	topPos.push(randomNum);
    }
    return randomNum;
}

function addSquare(){
    var div = document.createElement("div");
    
    //Use pickColor to randomly assign a color to the square's background
    div.style.background = pickColor();
    div.style.backgroundSize = "contain";
    
    // Extra credit! Also set the width and height of the squares with pickPos
		var size = pickSize();
    div.style.height = size;
    div.style.width = size;
    
    div.className += ' hexagons'
    div.style.left=pickPos("left");
    div.style.top=pickPos("top");
    
    $("body").append(div);
}
var count = 0;
var intervalId = window.setInterval(function(){
	addSquare();
  count++;
  if(count > 15) {
    window.clearInterval(intervalId);
  }
},50);

var btn = document.getElementById("wrapBtn")
btn.onclick = function() {
  //$(".hexagons").wrapAll("<p id='circle'></p>");
  $(".hexagons").animate({top:"0px",left:"0px"},2000,function(){});
}