Last active
December 31, 2015 13:09
-
-
Save iolloyd/8449d259c4182bea11f6 to your computer and use it in GitHub Desktop.
Produce random squares given a max number and constraints of box size. Make the squares change colour and move somewhere away from the mouse if mouse over for at least 3 seconds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<!DOCTYPE html> | |
<html> | |
<head> | |
<title>rectangles.html</title> | |
<script type="text/javascript"> | |
var nRectangles = 10, | |
containerWidth = 600, | |
containerHeight = 600, | |
minRectDim = 50, | |
maxRectDim = 100; | |
// Not used in the end since we depend on containerSize | |
function windowSize() { | |
var w = window, | |
d = document, | |
e = d.documentElement, | |
g = d.getElementsByTagName('body')[0], | |
y = w.innerWidth || e.clientWidth || g.clientWidth, | |
x = w.innerHeight|| e.clientHeight|| g.clientHeight; | |
return {'width': x, 'height': y}; | |
} | |
function getRandomColour() { | |
var color = '#'; | |
var choice = 'abcdee0123456789'.split(''); | |
for (var i = 0; i < 6; i++ ) { | |
color += choice[Math.round(Math.random() * 15)]; | |
} | |
return color; | |
} | |
function getRandomLength() { | |
var length = Math.round(Math.random() * maxRectDim) + minRectDim; | |
return length; | |
} | |
function getRandomPos(div, e) { | |
var boxLength = parseInt(div.style.width); | |
var size = windowSize(); | |
var x = Math.round(Math.random() * (containerWidth - boxLength)); | |
var y = Math.round(Math.random() * (containerHeight - boxLength)); | |
var e = e || null; | |
if (e && e.pageX == x && e.pageY == y) { | |
return getRandomPos(div, e); | |
} | |
return {'x': x, 'y': y }; | |
} | |
function createDiv(zIndex){ | |
var div = document.createElement('div'); | |
div.style.position = 'absolute'; | |
length = getRandomLength(); | |
div.style.width = length + 'px'; | |
div.style.height = length + 'px'; | |
var pos = getRandomPos(div); | |
div.style.top = pos['x'] + 'px'; | |
div.style.left = pos['y'] + 'px'; | |
div.style.backgroundColor = getRandomColour(); | |
div.style.zIndex = zIndex; | |
return div; | |
} | |
function createRectangle(zIndex){ | |
var div = createDiv(zIndex); | |
var waitTimer = null; | |
div.onmouseover = function(e){ | |
waitTimer = setTimeout(function(){ | |
div.style.backgroundColor = getRandomColour(); | |
var newPos = getRandomPos(div, e); | |
div.style.left = newPos['x'] + 'px'; | |
div.style.top = newPos['y'] + 'px'; | |
}, 3000); | |
} | |
div.onmouseout = function() { | |
clearTimeout(waitTimer); | |
} | |
document.getElementsByTagName('body')[0].appendChild(div); | |
return div; | |
} | |
function init() { | |
var done = nRectangles; | |
while (done--) { | |
createRectangle(done); | |
} | |
} | |
window.onload = init; | |
</script> | |
</head> | |
<body> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment