Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript runs once, CSS handles a 24 hour animation #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2022 Jama
Copyright (c) 2022 Martijn van der Ven

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
30 changes: 22 additions & 8 deletions assets/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
--black: #2d3134;
--gray: #a9aab1;
--beige: #fdf5e6;
--start: 0s;
--fill: var(--green);
}

*,
Expand Down Expand Up @@ -74,6 +76,20 @@ ul {
height: 4.5rem;
width: 4.6rem;
transition: all .5s ease-in;
position: relative;
overflow: hidden;
}

.grid-item span {
background-color: var(--fill);
transition: background-color .5s ease-in;
position: absolute;
inset: 0px;

animation-duration: 86400s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-delay: var(--start);
}

/* homegrown functional CSS framework */
Expand All @@ -86,9 +102,12 @@ div.flex-wrap { flex-wrap: nowrap; }
.items-center { align-items: center !important; }
.justify-around { justify-content: space-around !important; }

.bg-time-passed { background-color: var(--green) !important; }
.bg-time-passed { background-color: var(--green); }

.bg-time-selected { background-color: var(--blue) !important; }
.bg-time-selected {
--fill: var(--blue);
background-color: var(--blue);
}

.text-center { text-align: center; }

Expand All @@ -101,9 +120,4 @@ div.flex-wrap { flex-wrap: nowrap; }
.p-1 { padding: 1rem; }
.p-2 { padding: 2rem; }

.border-b-3 { border-bottom: 0.3rem solid !important; }

.last-grid.bg-time-selected {
background: transparent !important;
background-color: var(--blue) !important;
}
.border-b-3 { border-bottom: 0.3rem solid !important; }
53 changes: 18 additions & 35 deletions assets/voodoo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,33 @@ const createGrid = (rows, cols, container) => {
for (let i = 0; i < (rows * cols); i++) {
let gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
let gridFill = document.createElement('span');
gridItem.appendChild(gridFill);
container.appendChild(gridItem);
};
};

const minutesSinceMidnight = () => {
const secondsSinceMidnight = () => {
const now = new Date();
const midnight = new Date().setHours(0, 0, 0, 0);

return ((now - midnight) / 1000) / 60;
return (now - midnight) / 1000;
}

const fillGrid = () => {
const minutesPassed = minutesSinceMidnight();

const fullBlocks = Math.floor(minutesPassed / 10);

document.querySelectorAll('.grid-container .grid-item').forEach((element, index) => {
if (index + 1 <= fullBlocks) {
element.classList.add('bg-time-passed');
} else {
element.classList.remove('bg-time-passed');
element.style = 'background: transparent'
}
const secondsPassed = secondsSinceMidnight();
document.styleSheets[0].insertRule(`:root { --start: -${secondsPassed}s; }`, 1);

document.querySelectorAll('.grid-container .grid-item span').forEach((element, index, { length }) => {
const increment = 100 / length;
const start = index * increment;
const end = start + increment;
document.styleSheets[0].insertRule(`@keyframes load${index} {
0%, ${start}% { transform: translate(-100%); }
${end}%, 100% { transform: translate(0%); }
}`);
element.style.animationName = `load${index}`;
});

const remainderBlock = (minutesPassed % 10) * 10;
const lastUncoloredGridItem = document.querySelector('.grid-item:not(.bg-time-passed)');

lastUncoloredGridItem.classList.add('last-grid')

lastUncoloredGridItem.style = `background: linear-gradient(to right, var(--green) ${remainderBlock}%, transparent 0%)`;
}

const enableFullScreen = () => {
Expand All @@ -54,26 +50,14 @@ document.addEventListener('DOMContentLoaded', () => {
element.addEventListener('mouseenter', () => {
document.querySelectorAll('.grid-container .grid-item').forEach((element, index) => {
if (index + 1 <= rectangles) {
element.classList.remove('bg-time-passed');
element.classList.add('bg-time-selected');
}
});
});

element.addEventListener('mouseleave', () => {
const minutesPassed = minutesSinceMidnight();
const fullBlocks = Math.floor(minutesPassed / 10);

document.querySelectorAll('.grid-container .grid-item').forEach((element, index) => {
if (index + 1 <= fullBlocks) {
element.style = "background: transparent"
element.classList.add('bg-time-passed');
element.classList.remove('bg-time-selected');
}
else if (index + 1 > fullBlocks) {
element.classList.remove('bg-time-selected');
element.classList.remove('bg-time-passed');
}
document.querySelectorAll('.bg-time-selected').forEach((element) => {
element.classList.remove('bg-time-selected');
});
});
});
Expand All @@ -82,5 +66,4 @@ document.addEventListener('DOMContentLoaded', () => {

enableFullScreen();
fillGrid();
setInterval(fillGrid, 4000);
});