I tried to learn more JavaScript
Posted: Sun Jun 05, 2011 12:10 pm
So last night I gave my self a JavaScript challenge, this was the result http://jacekk.co.uk/scripts/js_squares/squares.html. cool right
Works best in Chrome, for some reason firefox does not randomize the array very well.
And the code for interested people
Works best in Chrome, for some reason firefox does not randomize the array very well.
And the code for interested people
xhjs.window.onload(function () { var screen_size = xhjs.window.get_size(); var screen_centre = new Array(screen_size[0] / 2, screen_size[1] / 2); var xmax = Math.ceil(screen_size[0] / 20); var ymax = Math.ceil(screen_size[1] / 20); var logo_radius = Math.min(screen_size[0], screen_size[1]) / 2.5; var triangle_radius = logo_radius * 0.4; var i, x, y, bx, by, dx, dy, mdy, mdx, ts, R, row, box, boxes, r, g, b, interval; document.body.style.overflow = 'hidden'; document.body.style.margin = '0px'; document.body.style.padding = '0px'; boxes = new Array(); for (y = 0; y < ymax; ++y) { row = xhjs.create('div'); row.style.cssFloat = 'left'; row.style.width = '100%'; row.style.height = '20px'; for (x = 0; x < xmax; ++x) { box = xhjs.create('div'); box.style.visibility = 'hidden'; box.style.cssFloat = 'left'; box.style.width = '20px'; box.style.height = '20px'; xhjs.append(row, box); boxes[boxes.length] = box; } xhjs.append(document.body, row); } for (i = 0; i < boxes.length; ++i){ bx = boxes.offsetLeft + 10; by = boxes.offsetTop + 10; mdx = dx = screen_centre[0] - bx; mdy = dy = screen_centre[1] - by; if (mdx < 0) mdx *= -1; if (mdy < 0) mdy *= -1; ts = 0.6 * dx; R = Math.sqrt(Math.pow(mdx, 2) + Math.pow(mdy, 2)); if (by > (screen_centre[1] - ts - triangle_radius) && by < (screen_centre[1] + ts + triangle_radius) && bx > screen_centre[0] - triangle_radius) { r = Math.round(Math.random() * 80) + 170; g = Math.round(Math.random() * 80) + 170; b = Math.round(Math.random() * 80) + 170; } else if (R < logo_radius) { r = Math.round(Math.random() * 100) + 100; g = Math.round(Math.random() * 80); b = Math.round(Math.random() * 80); } else { r = Math.round(Math.random() * 220); g = Math.round(Math.random() * 255); b = Math.round(Math.random() * 255); } boxes.style.backgroundColor = 'rgb(' + r + ', ' + g + ', ' + b + ')'; } boxes.sort(function () { return Math.round(5 - (Math.random() * 10)); }); interval = setInterval(function () { if (boxes.length === 0) { clearInterval(interval); } else { boxes.pop().style.visibility = 'visible'; } }, 5); });