Created
April 4, 2013 20:12
-
-
Save hidinginabunker/5313918 to your computer and use it in GitHub Desktop.
Changes the background color using setInterval. Pretty old school though, better to use requestAnimationFrame to not completely destroy the battery life of mobile devices
This file contains 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
var intervalTicks = 100; | |
var redValue = 1; | |
var greenValue = 1; | |
var blueValue = 1; | |
var colorChangeMagnitude = 1.01; | |
var backgroundColor = "rgb("+redValue+","+greenValue+","+blueValue+")"; | |
var redFlag = "down"; | |
var greenFlag = "up"; | |
var blueFlag; "up"; | |
window.onload = function() | |
{ | |
initColors(); | |
setInterval(changeBgColor, intervalTicks); | |
} | |
function initColors() { | |
redValue = Math.floor(Math.random() * 255); | |
greenValue = Math.floor(Math.random() * 255); | |
blueValue = Math.floor(Math.random() * 255); | |
changeBgColor(); | |
} | |
function changeBgColor(){ | |
if(redValue < 100) | |
redFlag = "up"; | |
if(redValue > 200) | |
redFlag = "down"; | |
if(redFlag == "up") | |
redValue *= colorChangeMagnitude; | |
if(redFlag == "down") | |
redValue /= colorChangeMagnitude; | |
if(greenValue < 100) | |
greenFlag = "up"; | |
if(greenValue > 200) | |
greenFlag = "down"; | |
if(greenFlag == "up") | |
greenValue *= colorChangeMagnitude; | |
if(greenFlag == "down") | |
greenValue /= colorChangeMagnitude; | |
if(blueValue < 100) | |
blueFlag = "up"; | |
if(blueValue > 200) | |
blueFlag = "down"; | |
if(blueFlag == "up") | |
blueValue *= colorChangeMagnitude; | |
if(blueFlag == "down") | |
blueValue /= colorChangeMagnitude; | |
backgroundColor = "rgb("+Math.floor(redValue)+","+Math.floor(greenValue)+","+Math.floor(blueValue)+")"; | |
document.body.style.background=backgroundColor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment