|
//Set up variables |
|
const main = document.getElementById("main"); |
|
var col = getComputedStyle(document.documentElement).getPropertyValue('--cols') |
|
var allShapes = document.getElementsByClassName("▲"); |
|
var activeShapes = document.getElementsByClassName("▲ is--active"); |
|
|
|
//Set click event on container to refresh content |
|
main.addEventListener('click', randomize); |
|
|
|
function generateShapes(){ |
|
//Loop through all shapes |
|
for(i = 0; i < allShapes.length; i++) { |
|
//Set random number |
|
var d = Math.random(); |
|
//Set current shape |
|
var shape = allShapes[i]; |
|
//Activate shapes with increasing likelihood according to the iteration shape (i), dom order (col), and random number (d) |
|
if( i < (col*4) && d < .1 || |
|
i > (col*4) && i < (col*8) && d < .2 || |
|
i > (col*8) && i < (col*12) && d < .3 || |
|
i > (col*12) && i < (col*16) && d < .4 || |
|
i > (col*16) && i < (col*20) && d < .5 || |
|
i > (col*20) && i < (col*24) && d < .6 || |
|
i > (col*24) && i < (col*28) && d < .7 || |
|
i > (col*28) && i < (col*32) && d < .8 || |
|
i > (col*32) && i < (col*36) && d < .9 || |
|
i > (col*36) && i < (col*40) && d < 1 ) { |
|
shape.classList += " is--active"; |
|
} |
|
//If activated, set opacity to the random number |
|
if(shape.classList.contains("is--active")) { |
|
shape.setAttribute("fill","rgba(255,255,255," + d + ")"); |
|
} |
|
} |
|
} |
|
|
|
//Reset all active shaps to default fill and classes |
|
function clearAll() { |
|
while (activeShapes[0]) { |
|
activeShapes[0].setAttribute("fill","rgba(255,255,255,0)"); |
|
activeShapes[0].classList.remove('is--active') |
|
} |
|
} |
|
|
|
//Clear all active shapes and randomize |
|
function randomize() { |
|
clearAll(); |
|
generateShapes(); |
|
} |
|
|
|
//Generate shapes on initial page load |
|
generateShapes(); |