Created
February 4, 2021 11:01
-
-
Save eknkc/f3b2ae59a274da29d8ca70a486658572 to your computer and use it in GitHub Desktop.
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 lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | |
<title>Color Test</title> | |
</head> | |
<script> | |
var listener = function () {}; | |
let running = true; | |
function tick() { | |
let [r, g, b] = [ | |
(Math.random() * 255) | 0, | |
(Math.random() * 255) | 0, | |
(Math.random() * 255) | 0 | |
]; | |
let hexColor = `#${hex(r)}${hex(g)}${hex(b)}`; | |
document.body.style.background = `#${hex(r)}${hex(g)}${hex(b)}`; | |
listener(hexColor); | |
} | |
function hex(c) { | |
return c.toString(16).padStart(2, "0"); | |
} | |
setInterval(function () { | |
if (running) { | |
tick(); | |
} | |
}, 500); | |
window.listenColorChange = function (fn) { | |
listener = fn; | |
}; | |
window.toggleRun = function () { | |
running = !running; | |
}; | |
</script> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment