Skip to content

Instantly share code, notes, and snippets.

@JeffreyBPetersen
Last active August 11, 2016 02:24
Show Gist options
  • Save JeffreyBPetersen/ecd62c39b01502a7c6655c555626e7cb to your computer and use it in GitHub Desktop.
Save JeffreyBPetersen/ecd62c39b01502a7c6655c555626e7cb to your computer and use it in GitHub Desktop.
I named this "noip" for no particular reason.
body {
align-items: center;
display: flex;
justify-content: center;
margin: 0;
min-height: 100vh;
}
#noip {
background-color: #000;
border-radius: 25vmin;
height: 50vmin;
width: 50vmin;
}
<!doctype html>
<html>
<head>
<link href='noip.css' rel='stylesheet'></link>
<script src='noip.js' defer></script>
<title>noip</title>
<body>
<div id='noip'></div>
</body>
</html>
let position = {
r: 0,
g: 0,
b: 0
};
let velocity = {
r: 0,
g: 0,
b: 0
};
let charge = {
r: 0,
g: 0,
b: 0
}
noip = () => {
let channel = ['r','g','b'][Math.floor(Math.random()*3)];
if(position[channel] == 0) velocity[channel]++;
else if(position[channel] == 255) velocity[channel]--;
else velocity[channel] += (Math.floor(Math.random()*2) - 1) * 2 + 1;
charge.r += Math.abs(velocity.r);
charge.g += Math.abs(velocity.g);
charge.b += Math.abs(velocity.b);
if(charge.r > charge.g && charge.r > charge.b) channel = 'r';
else if(charge.g > charge.b) channel = 'g';
else channel = 'b';
if(velocity[channel] > 0) position[channel]++;
else if(velocity[channel] < 0) position[channel]--;
if(position[channel] == 0 || position[channel] == 255) velocity[channel] = 0;
charge[channel] = 0;
document.getElementById('noip').style.backgroundColor = `rgb(${position.r},${position.g},${position.b})`;
window.requestAnimationFrame(noip);
}
toggle = () => {
document.body.style.backgroundColor = document.body.style.backgroundColor == 'rgb(0, 0, 0)' ? '#FFF' : '#000';
velocity.r *= -1;
velocity.g *= -1;
velocity.b *= -1;
position.r = (position.r - 128) * -1 + 127;
position.g = (position.g - 128) * -1 + 127;
position.b = (position.b - 128) * -1 + 127;
document.getElementById('noip').style.backgroundColor = `rgb(${position.r},${position.g},${position.b})`;
}
main = () => {
document.getElementById('noip').addEventListener('click', toggle);
noip();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment