1
0
Fork 0
mirror of https://github.com/afkollektiv/afkollektor.de synced 2024-05-06 18:06:45 +00:00

Merge pull request #6 from afkollektiv/peter-konamicode

???
This commit is contained in:
Einstein 2017-11-05 03:09:20 +01:00 committed by GitHub
commit 0609287898
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 0 deletions

View file

@ -1,6 +1,7 @@
index: pages/index.md styles/index.css
mkdir -p build
cp styles/index.css build/index.css
cp scripts/konami-code.js build/k.js
discount-theme pages/index.md \
-o build/index.html \
-t templates/index.theme

104
scripts/konami-code.js Normal file
View file

@ -0,0 +1,104 @@
(function () {
/*
* A map of key codes so that you dont have to use them directly.
*/
var k = {
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39,
B: 66,
A: 65
};
/*
* These are the keys of the konami code (according to wikipedia).
*/
var konamiCode = [ k.UP, k.UP, k.DOWN, k.DOWN, k.LEFT, k.RIGHT, k.LEFT, k.RIGHT, k.B, k.A ];
/*
* In case someone enters the konami code, unleash the seven hells.
*/
onKeyStreak(konamiCode, unleashTheSevenHells);
/*
* Keep a record of keystrokes and call a function when a certain streak is entered.
*/
function onKeyStreak (streak, callback) {
var keysPressed = [];
var eventToListenFor = 'keydown';
var listener = function (e) {
keysPressed.push(e.keyCode);
if (isSuffix(streak, keysPressed)) {
window.removeEventListener(eventToListenFor, listener);
callback();
}
};
window.addEventListener(eventToListenFor, listener);
}
/*
* Check whether listA is a suffix of listB.
*/
function isSuffix (listA, listB) {
if (listA.length > listB.length) {
return false;
}
var index = 0;
for (; index < listA.length; index++) {
if (listA[index] !== listB[listB.length - listA.length + index]) {
console.log(index);
return false;
}
}
return true;
}
/*
* Commit some chaos and make sure to do so at the next frame too.
*/
function unleashTheSevenHells () {
angery();
window.requestAnimationFrame(unleashTheSevenHells);
}
/*
* Be an angry website.
*/
function angery () {
// This returns a list of all elements that match the CSS selector * (which are all).
var allElements = document.querySelectorAll('*');
// Since querySelectorAll doesn't return an array, but something zero-indexed that has a length property, we can use Array's forEach here.
// Welcome to the world of duck typing.
Array.prototype.forEach.call(allElements, makeItPretty);
}
/*
* Makes an element pretty.
*/
function makeItPretty (element) {
element.style.color = randomColour();
element.style.backgroundColor = randomColour();
element.style.textAlign = randomChoice([ 'left', 'center', 'right' ]);
}
function randomChoice (choices) {
return choices[Math.floor(Math.random() * choices.length)];
}
function randomColour () {
var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
return 'rgb(' + r + ',' + g + ',' + b + ')';
}
})();

View file

@ -9,5 +9,6 @@
<div class="afk-container">
<?theme body?>
</div>
<script src="./k.js"></script>
</body>
</html>