-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneko.js
531 lines (468 loc) · 15 KB
/
oneko.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// modified oneko.js from https://github.com/kyrie25/spicetify-oneko/blob/main/oneko.js
// oneko.js: https://github.com/adryd325/oneko.js
(async function oneko() {
const nekoEl = document.createElement("div");
let nekoPosX = 32,
nekoPosY = 32,
mousePosX = 0,
mousePosY = 0,
frameCount = 0,
idleTime = 0,
idleAnimation = null,
idleAnimationFrame = 0,
forceSleep = false,
grabbing = false,
grabStop = true,
nudge = false,
kuroNeko = false,
variant = "classic";
function parseLocalStorage(key, fallback) {
try {
const value = JSON.parse(localStorage.getItem(`oneko:${key}`));
console.log(key, value);
return typeof value === typeof fallback ? value : fallback;
} catch (e) {
console.error(e);
return fallback;
}
}
const nekoSpeed = 10,
variants = [
["classic", "Classic"],
["dog", "Dog"],
["tora", "Tora"],
["maia", "Maia (maia.crimew.gay)"],
["vaporwave", "Vaporwave (nya.rest)"],
],
spriteSets = {
idle: [[-3, -3]],
alert: [[-7, -3]],
scratchSelf: [
[-5, 0],
[-6, 0],
[-7, 0],
],
scratchWallN: [
[0, 0],
[0, -1],
],
scratchWallS: [
[-7, -1],
[-6, -2],
],
scratchWallE: [
[-2, -2],
[-2, -3],
],
scratchWallW: [
[-4, 0],
[-4, -1],
],
tired: [[-3, -2]],
sleeping: [
[-2, 0],
[-2, -1],
],
N: [
[-1, -2],
[-1, -3],
],
NE: [
[0, -2],
[0, -3],
],
E: [
[-3, 0],
[-3, -1],
],
SE: [
[-5, -1],
[-5, -2],
],
S: [
[-6, -3],
[-7, -2],
],
SW: [
[-5, -3],
[-6, -1],
],
W: [
[-4, -2],
[-4, -3],
],
NW: [
[-1, 0],
[-1, -1],
],
}, // Get keys with 2 or more sprites
keys = Object.keys(spriteSets).filter((key) => spriteSets[key].length > 1),
usedKeys = new Set();
function create() {
variant = parseLocalStorage("variant", "classic");
kuroNeko = parseLocalStorage("kuroneko", false);
if (!variants.some((v) => v[0] === variant)) {
variant = "classic";
}
nekoEl.id = "oneko";
nekoEl.style.width = "32px";
nekoEl.style.height = "32px";
nekoEl.style.position = "fixed";
// nekoEl.style.pointerEvents = "none";
nekoEl.style.backgroundImage = `url('https://raw.githubusercontent.com/kyrie25/spicetify-oneko/main/assets/oneko/oneko-classic.gif')`;
nekoEl.style.imageRendering = "pixelated";
nekoEl.style.left = `${nekoPosX - 16}px`;
nekoEl.style.top = `${nekoPosY - 16}px`;
nekoEl.style.filter = kuroNeko ? "invert(100%)" : "none";
// Render Oneko below Spicetify's Popup Modal
nekoEl.style.zIndex = "99";
document.body.appendChild(nekoEl);
window.addEventListener("mousemove", (e) => {
if (forceSleep) return;
mousePosX = e.clientX;
mousePosY = e.clientY;
});
window.addEventListener("resize", () => {
if (!forceSleep) return;
// If neko is outside the window and is forced to sleep, wake her up
if (
nekoPosX - window.innerWidth > 32 ||
nekoPosY - window.innerHeight > 32 ||
// Also when she is about to go outside the window
mousePosX - window.innerWidth > 32 ||
mousePosY - window.innerHeight > 32
) {
forceSleep = false;
resetIdleAnimation();
}
});
// Handle dragging of the cat
nekoEl.addEventListener("mousedown", (e) => {
if (e.button !== 0) return;
grabbing = true;
let startX = e.clientX;
let startY = e.clientY;
let startNekoX = nekoPosX;
let startNekoY = nekoPosY;
let grabInterval;
const mousemove = (e) => {
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
const absDeltaX = Math.abs(deltaX);
const absDeltaY = Math.abs(deltaY);
// Scratch in the opposite direction of the drag
if (absDeltaX > absDeltaY && absDeltaX > 10) {
setSprite(deltaX > 0 ? "scratchWallW" : "scratchWallE", frameCount);
} else if (absDeltaY > absDeltaX && absDeltaY > 10) {
setSprite(deltaY > 0 ? "scratchWallN" : "scratchWallS", frameCount);
}
if (
grabStop ||
absDeltaX > 10 ||
absDeltaY > 10 ||
Math.sqrt(deltaX ** 2 + deltaY ** 2) > 10
) {
grabStop = false;
clearTimeout(grabInterval);
grabInterval = setTimeout(() => {
grabStop = true;
nudge = false;
startX = e.clientX;
startY = e.clientY;
startNekoX = nekoPosX;
startNekoY = nekoPosY;
}, 150);
}
nekoPosX = startNekoX + e.clientX - startX;
nekoPosY = startNekoY + e.clientY - startY;
nekoEl.style.left = `${nekoPosX - 16}px`;
nekoEl.style.top = `${nekoPosY - 16}px`;
};
const mouseup = () => {
grabbing = false;
nudge = true;
resetIdleAnimation();
window.removeEventListener("mousemove", mousemove);
window.removeEventListener("mouseup", mouseup);
};
window.addEventListener("mousemove", mousemove);
window.addEventListener("mouseup", mouseup);
});
nekoEl.addEventListener("contextmenu", (e) => {
e.preventDefault();
kuroNeko = !kuroNeko;
localStorage.setItem("oneko:kuroneko", kuroNeko);
nekoEl.style.filter = kuroNeko ? "invert(100%)" : "none";
});
nekoEl.addEventListener("dblclick", () => {
forceSleep = !forceSleep;
nudge = false;
if (!forceSleep) {
resetIdleAnimation();
return;
}
// If Full App Display is on, sleep on its progress bar instead
const fullAppDisplay = document.getElementById("fad-progress");
if (fullAppDisplay) {
mousePosX = fullAppDisplay.getBoundingClientRect().right - 16;
mousePosY = fullAppDisplay.getBoundingClientRect().top - 12;
return;
}
// Get the far right and top of the progress bar
const progressBar = document.querySelector(
".main-nowPlayingBar-center .playback-progressbar"
);
const progressBarRight = progressBar.getBoundingClientRect().right;
const progressBarTop = progressBar.getBoundingClientRect().top;
const progressBarBottom = progressBar.getBoundingClientRect().bottom;
// Make the cat sleep on the progress bar
mousePosX = progressBarRight - 16;
mousePosY = progressBarTop - 8;
// Get the position of the remaining time
const remainingTime = document.querySelector(
".main-playbackBarRemainingTime-container"
);
const remainingTimeLeft = remainingTime.getBoundingClientRect().left;
const remainingTimeBottom = remainingTime.getBoundingClientRect().bottom;
const remainingTimeTop = remainingTime.getBoundingClientRect().top;
// Get the position of elapsed time
const elapsedTime = document.querySelector(
".playback-bar__progress-time-elapsed"
);
const elapsedTimeRight = elapsedTime.getBoundingClientRect().right;
const elapsedTimeLeft = elapsedTime.getBoundingClientRect().left;
// If the remaining time is on top right of the progress bar, make the cat sleep to the a little bit to the left of the remaining time
// Theme compatibility
if (
remainingTimeLeft < progressBarRight &&
remainingTimeTop < progressBarBottom &&
progressBarTop - remainingTimeBottom < 32
) {
mousePosX = remainingTimeLeft - 16;
// Comfy special case
if (Spicetify.Config.current_theme === "Comfy") {
mousePosY = progressBarTop - 14;
}
// Move the cat to the left of elapsed time if it is too close to the remaining time (Nord theme)
if (remainingTimeLeft - elapsedTimeRight < 32) {
mousePosX = elapsedTimeLeft - 16;
}
}
});
window.onekoInterval = setInterval(frame, 100);
}
function getSprite(name, frame) {
return spriteSets[name][frame % spriteSets[name].length];
}
function setSprite(name, frame) {
const sprite = getSprite(name, frame);
nekoEl.style.backgroundPosition = `${sprite[0] * 32}px ${sprite[1] * 32}px`;
}
function resetIdleAnimation() {
idleAnimation = null;
idleAnimationFrame = 0;
}
function idle() {
idleTime += 1;
// every ~ 20 seconds
if (
idleTime > 10 &&
Math.floor(Math.random() * 200) == 0 &&
idleAnimation == null
) {
let avalibleIdleAnimations = ["sleeping", "scratchSelf"];
if (nekoPosX < 32) {
avalibleIdleAnimations.push("scratchWallW");
}
if (nekoPosY < 32) {
avalibleIdleAnimations.push("scratchWallN");
}
if (nekoPosX > window.innerWidth - 32) {
avalibleIdleAnimations.push("scratchWallE");
}
if (nekoPosY > window.innerHeight - 32) {
avalibleIdleAnimations.push("scratchWallS");
}
idleAnimation =
avalibleIdleAnimations[
Math.floor(Math.random() * avalibleIdleAnimations.length)
];
}
if (forceSleep) {
avalibleIdleAnimations = ["sleeping"];
idleAnimation = "sleeping";
}
switch (idleAnimation) {
case "sleeping":
if (idleAnimationFrame < 8 && nudge && forceSleep) {
setSprite("idle", 0);
break;
} else if (nudge) {
nudge = false;
resetIdleAnimation();
}
if (idleAnimationFrame < 8) {
setSprite("tired", 0);
break;
}
setSprite("sleeping", Math.floor(idleAnimationFrame / 4));
if (idleAnimationFrame > 192 && !forceSleep) {
resetIdleAnimation();
}
break;
case "scratchWallN":
case "scratchWallS":
case "scratchWallE":
case "scratchWallW":
case "scratchSelf":
setSprite(idleAnimation, idleAnimationFrame);
if (idleAnimationFrame > 9) {
resetIdleAnimation();
}
break;
default:
setSprite("idle", 0);
return;
}
idleAnimationFrame += 1;
}
function frame() {
frameCount += 1;
if (grabbing) {
grabStop && setSprite("alert", 0);
return;
}
const diffX = nekoPosX - mousePosX;
const diffY = nekoPosY - mousePosY;
const distance = Math.sqrt(diffX ** 2 + diffY ** 2);
// Cat has to sleep on top of the progress bar
if (
forceSleep &&
Math.abs(diffY) < nekoSpeed &&
Math.abs(diffX) < nekoSpeed
) {
// Make the cat sleep exactly on the top of the progress bar
nekoPosX = mousePosX;
nekoPosY = mousePosY;
nekoEl.style.left = `${nekoPosX - 16}px`;
nekoEl.style.top = `${nekoPosY - 16}px`;
idle();
return;
}
if ((distance < nekoSpeed || distance < 48) && !forceSleep) {
idle();
return;
}
idleAnimation = null;
idleAnimationFrame = 0;
if (idleTime > 1) {
setSprite("alert", 0);
// count down after being alerted before moving
idleTime = Math.min(idleTime, 7);
idleTime -= 1;
return;
}
direction = diffY / distance > 0.5 ? "N" : "";
direction += diffY / distance < -0.5 ? "S" : "";
direction += diffX / distance > 0.5 ? "W" : "";
direction += diffX / distance < -0.5 ? "E" : "";
setSprite(direction, frameCount);
nekoPosX -= (diffX / distance) * nekoSpeed;
nekoPosY -= (diffY / distance) * nekoSpeed;
nekoPosX = Math.min(Math.max(16, nekoPosX), window.innerWidth - 16);
nekoPosY = Math.min(Math.max(16, nekoPosY), window.innerHeight - 16);
nekoEl.style.left = `${nekoPosX - 16}px`;
nekoEl.style.top = `${nekoPosY - 16}px`;
}
create();
function getRandomSprite() {
let unusedKeys = keys.filter((key) => !usedKeys.has(key));
if (unusedKeys.length === 0) {
usedKeys.clear();
unusedKeys = keys;
}
const index = Math.floor(Math.random() * unusedKeys.length);
const key = unusedKeys[index];
usedKeys.add(key);
return [getSprite(key, 0), getSprite(key, 1)];
}
function setVariant(arr) {
console.log(arr);
variant = arr[0];
localStorage.setItem("oneko:variant", `"${variant}"`);
nekoEl.style.backgroundImage = `url('https://raw.githubusercontent.com/kyrie25/spicetify-oneko/main/assets/oneko/oneko-${variant}.gif')`;
}
// Popup modal to choose variant
function pickerModal() {
const container = document.createElement("div");
container.className = "oneko-variant-container";
const style = document.createElement("style");
// Each variant is a 64x64 sprite
style.innerHTML = `
.oneko-variant-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.oneko-variant-button {
width: 64px;
height: 64px;
margin: 8px;
cursor: pointer;
background-size: 800%;
border-radius: 25%;
transition: background-color 0.2s ease-in-out;
background-position: var(--idle-x) var(--idle-y);
image-rendering: pixelated;
}
.oneko-variant-button:hover, .oneko-variant-button-selected {
background-color: var(--spice-main-elevated);
}
.oneko-variant-button:hover {
background-position: var(--active-x) var(--active-y);
}
`;
container.appendChild(style);
const [idle, active] = getRandomSprite();
function variantButton(variantEnum) {
const div = document.createElement("div");
div.className = "oneko-variant-button";
div.id = variantEnum[0];
div.style.backgroundImage = `url('https://raw.githubusercontent.com/kyrie25/spicetify-oneko/main/assets/oneko/oneko-${variantEnum[0]}.gif')`;
div.style.setProperty("--idle-x", `${idle[0] * 64}px`);
div.style.setProperty("--idle-y", `${idle[1] * 64}px`);
div.style.setProperty("--active-x", `${active[0] * 64}px`);
div.style.setProperty("--active-y", `${active[1] * 64}px`);
div.onclick = () => {
setVariant(variantEnum);
document
.querySelector(".oneko-variant-button-selected")
?.classList.remove("oneko-variant-button-selected");
div.classList.add("oneko-variant-button-selected");
};
if (variantEnum[0] === variant) {
div.classList.add("oneko-variant-button-selected");
}
Spicetify.Tippy(div, {
...Spicetify.TippyProps,
content: variantEnum[1],
});
return div;
}
for (const variant of variants) {
container.appendChild(variantButton(variant));
}
return container;
}
while (!Spicetify.Mousetrap) {
await new Promise((r) => setTimeout(r, 100));
}
Spicetify.Mousetrap.bind("o n e k o", () => {
Spicetify.PopupModal.display({
title: "Choose your neko",
// Render the modal new every time it is opened
content: pickerModal(),
});
});
})();