Build in Hero to home and animate stuff correctly
This commit is contained in:
+66
-40
@@ -20,6 +20,15 @@
|
||||
<svg id="barTemplate" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 8.47 8.47"><path fill="none" stroke="#3d2f1f" stroke-linecap="round" stroke-linejoin="round" stroke-width="0.8" d="m1.5 4.23.23-.1a6 6 0 0 1 5.01 0l.23.1" style="paint-order:markers stroke fill"/></svg>
|
||||
</span>
|
||||
</h1> -->
|
||||
|
||||
<header id="hero">
|
||||
<h1>nikology.dev
|
||||
<span class="bird-wrap">
|
||||
<button id="birdButton"><img src="birb.svg" alt=""></button>
|
||||
<svg id="barTemplate" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 8.47 8.47"><path fill="none" stroke="#3d2f1f" stroke-linecap="round" stroke-linejoin="round" stroke-width="0.8" d="m1.5 4.23.23-.1a6 6 0 0 1 5.01 0l.23.1" style="paint-order:markers stroke fill"/></svg>
|
||||
</span>
|
||||
</h1>
|
||||
</header>
|
||||
|
||||
<!-- Nav Bar -->
|
||||
<nav>
|
||||
@@ -54,7 +63,7 @@
|
||||
<script>
|
||||
const cache = new Map();
|
||||
|
||||
async function loadFragment(file) {
|
||||
async function loadApp(file) {
|
||||
if (!cache.has(file)) {
|
||||
const res = await fetch(file);
|
||||
if (!res.ok) {
|
||||
@@ -71,68 +80,85 @@
|
||||
return { status: 200, content: cache.get(file)};
|
||||
}
|
||||
|
||||
async function render() {
|
||||
async function fetchApp() {
|
||||
const path = location.pathname;
|
||||
document.body.classList.toggle('home', path === '/');
|
||||
const file = `/pages${path === '/' ? '/home' : path}.html`;
|
||||
|
||||
const result = await loadFragment(file);
|
||||
|
||||
if (result.content) {
|
||||
document.getElementById('app').innerHTML = result.content;
|
||||
return await loadApp(file);
|
||||
}
|
||||
|
||||
async function commit(appFile) {
|
||||
if (appFile.content) {
|
||||
document.getElementById('app').innerHTML = appFile.content;
|
||||
} else {
|
||||
const errorResult = await loadFragment('/pages/error.html');
|
||||
const errorResult = await loadApp('/pages/error.html');
|
||||
document.getElementById('app').innerHTML = errorResult.content;
|
||||
document.getElementById('error-code').textContent = result.status;
|
||||
document.getElementById('error-code').textContent = appFile.status;
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('click', e => {
|
||||
async function navigate() {
|
||||
const result = await fetchApp();
|
||||
await commit(result);
|
||||
}
|
||||
|
||||
document.addEventListener('click', async e => {
|
||||
const link = e.target.closest('[data-link]');
|
||||
if (link) {
|
||||
e.preventDefault();
|
||||
if (link.pathname === location.pathname) return;
|
||||
history.pushState(null, '', link.href);
|
||||
render();
|
||||
|
||||
const currentApp = document.getElementById('app');
|
||||
currentApp.classList.add('fading');
|
||||
|
||||
const fetchedApp = fetchApp();
|
||||
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
await commit(await fetchedApp);
|
||||
currentApp.classList.remove('fading')
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('popstate', render);
|
||||
render().then(() => {
|
||||
|
||||
window.addEventListener('popstate', navigate);
|
||||
navigate().then(() => {
|
||||
requestAnimationFrame(() => document.body.classList.add('ready'))
|
||||
});
|
||||
|
||||
// const DISTANCE = 25
|
||||
// const birdIcon = document.getElementById("birdButton");
|
||||
// const birdSound = new Audio("SnowBuntingSound.ogg");
|
||||
// const template = document.getElementById("barTemplate");
|
||||
// const wrap = document.querySelector(".bird-wrap");
|
||||
const DISTANCE = 40
|
||||
const birdIcon = document.getElementById("birdButton");
|
||||
const birdSound = new Audio("SnowBuntingSound.ogg");
|
||||
const template = document.getElementById("barTemplate");
|
||||
const wrap = document.querySelector(".bird-wrap");
|
||||
|
||||
// function spawnBar(distance, angle, delay = 0) {
|
||||
// const clone = template.cloneNode(true);
|
||||
// clone.removeAttribute("id");
|
||||
// clone.classList.add("bar");
|
||||
// clone.style.setProperty("--angle", `${angle}deg`);
|
||||
// clone.style.setProperty("--distance", `${-distance}px`);
|
||||
// clone.style.setProperty("--delay", `${delay}s`);
|
||||
// wrap.appendChild(clone);
|
||||
function spawnBar(distance, angle, delay = 0) {
|
||||
const clone = template.cloneNode(true);
|
||||
clone.removeAttribute("id");
|
||||
clone.classList.add("bar");
|
||||
clone.style.setProperty("--angle", `${angle}deg`);
|
||||
clone.style.setProperty("--distance", `${-distance}px`);
|
||||
clone.style.setProperty("--delay", `${delay}s`);
|
||||
wrap.appendChild(clone);
|
||||
|
||||
// clone.addEventListener("animationend", () => clone.remove());
|
||||
// }
|
||||
clone.addEventListener("animationend", () => clone.remove());
|
||||
}
|
||||
|
||||
// function birdSoundAnim() {
|
||||
// const leftRandom = (Math.random() * 40) + 40
|
||||
// const rightRandom = (Math.random() * 40) + 40
|
||||
// spawnBar(DISTANCE, -leftRandom, 0.2)
|
||||
// spawnBar(DISTANCE, -leftRandom, 0.4)
|
||||
// spawnBar(DISTANCE, rightRandom, 0.6)
|
||||
// spawnBar(DISTANCE, rightRandom, 0.8)
|
||||
// }
|
||||
function birdSoundAnim() {
|
||||
const leftRandom = (Math.random() * 20) + 80
|
||||
const rightRandom = (Math.random() * 20) + 40
|
||||
spawnBar(DISTANCE, -leftRandom, 0.2)
|
||||
spawnBar(DISTANCE, -leftRandom, 0.4)
|
||||
spawnBar(DISTANCE, -rightRandom, 0.6)
|
||||
spawnBar(DISTANCE, -rightRandom, 0.8)
|
||||
}
|
||||
|
||||
// birdIcon.addEventListener("click", (e) => {
|
||||
// if (!birdSound.paused) return;
|
||||
// birdSound.play();
|
||||
// birdSoundAnim()
|
||||
// });
|
||||
birdIcon.addEventListener("click", (e) => {
|
||||
if (!birdSound.paused) return;
|
||||
birdSound.play();
|
||||
birdSoundAnim()
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user