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

Store neko position across pages #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
60 changes: 50 additions & 10 deletions oneko.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
if (isReducedMotion) return;

const nekoEl = document.createElement("div");
let persistPosition = true;

let nekoPosX = 32;
let nekoPosY = 32;

let mousePosX = 0;
let mousePosY = 0;

Expand Down Expand Up @@ -85,6 +86,34 @@
};

function init() {
let nekoFile = "./oneko.gif"
const curScript = document.currentScript
if (curScript && curScript.dataset.cat) {
nekoFile = curScript.dataset.cat
}
if (curScript && curScript.dataset.persistPosition) {
if (curScript.dataset.persistPosition === "") {
persistPosition = true;
} else {
persistPosition = JSON.parse(curScript.dataset.persistPosition.toLowerCase());
}
}

if (persistPosition) {
let storedNeko = JSON.parse(window.localStorage.getItem("oneko"));
if (storedNeko !== null) {
nekoPosX = storedNeko.nekoPosX;
nekoPosY = storedNeko.nekoPosY;
mousePosX = storedNeko.mousePosX;
mousePosY = storedNeko.mousePosY;
frameCount = storedNeko.frameCount;
idleTime = storedNeko.idleTime;
idleAnimation = storedNeko.idleAnimation;
idleAnimationFrame = storedNeko.idleAnimationFrame;
nekoEl.style.backgroundPosition = storedNeko.bgPos;
}
}

nekoEl.id = "oneko";
nekoEl.ariaHidden = true;
nekoEl.style.width = "32px";
Expand All @@ -96,20 +125,31 @@
nekoEl.style.top = `${nekoPosY - 16}px`;
nekoEl.style.zIndex = 2147483647;

let nekoFile = "./oneko.gif"
const curScript = document.currentScript
if (curScript && curScript.dataset.cat) {
nekoFile = curScript.dataset.cat
}
nekoEl.style.backgroundImage = `url(${nekoFile})`;

document.body.appendChild(nekoEl);

document.addEventListener("mousemove", function (event) {
mousePosX = event.clientX;
mousePosY = event.clientY;
});


if (persistPosition) {
window.addEventListener("beforeunload", function (event) {
window.localStorage.setItem("oneko", JSON.stringify({
nekoPosX: nekoPosX,
nekoPosY: nekoPosY,
mousePosX: mousePosX,
mousePosY: mousePosY,
frameCount: frameCount,
idleTime: idleTime,
idleAnimation: idleAnimation,
idleAnimationFrame: idleAnimationFrame,
bgPos: nekoEl.style.backgroundPosition
}));
});
}

window.requestAnimationFrame(onAnimationFrame);
}

Expand All @@ -124,8 +164,8 @@
lastFrameTimestamp = timestamp;
}
if (timestamp - lastFrameTimestamp > 100) {
lastFrameTimestamp = timestamp
frame()
lastFrameTimestamp = timestamp;
frame();
}
window.requestAnimationFrame(onAnimationFrame);
}
Expand Down