Add tolerance for mouse proximity to cat image during interaction
This commit is contained in:
41
animeCat.js
41
animeCat.js
@@ -138,6 +138,20 @@ _bindMouseHold() {
|
|||||||
let lastPos = null;
|
let lastPos = null;
|
||||||
let moveDist = 0;
|
let moveDist = 0;
|
||||||
|
|
||||||
|
// Wieviel Pixel Abstand ist ok für "über der Katze"?
|
||||||
|
const CAT_TOLERANCE = 24;
|
||||||
|
|
||||||
|
// Helper: Check if Mouse is "über der Katze" (mit Toleranz)
|
||||||
|
const isMouseNearCat = (e) => {
|
||||||
|
const rect = this.img.getBoundingClientRect();
|
||||||
|
return (
|
||||||
|
e.clientX >= rect.left - CAT_TOLERANCE &&
|
||||||
|
e.clientX <= rect.right + CAT_TOLERANCE &&
|
||||||
|
e.clientY >= rect.top - CAT_TOLERANCE &&
|
||||||
|
e.clientY <= rect.bottom + CAT_TOLERANCE
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const closeEyes = () => {
|
const closeEyes = () => {
|
||||||
this.img.src = this.images.eyesClosed;
|
this.img.src = this.images.eyesClosed;
|
||||||
};
|
};
|
||||||
@@ -147,28 +161,15 @@ _bindMouseHold() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper: Check if Mouse is over the cat image
|
|
||||||
const isMouseOverCat = (e) => {
|
|
||||||
const rect = this.img.getBoundingClientRect();
|
|
||||||
return (
|
|
||||||
e.clientX >= rect.left &&
|
|
||||||
e.clientX <= rect.right &&
|
|
||||||
e.clientY >= rect.top &&
|
|
||||||
e.clientY <= rect.bottom
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
// on mousedown: only if over cat
|
|
||||||
this.img.addEventListener('mousedown', (e) => {
|
this.img.addEventListener('mousedown', (e) => {
|
||||||
if (this._isSpeaking || joyActive) return;
|
if (this._isSpeaking || joyActive) return;
|
||||||
if (!isMouseOverCat(e)) return;
|
if (!isMouseNearCat(e)) return;
|
||||||
|
|
||||||
mouseDown = true;
|
mouseDown = true;
|
||||||
lastPos = { x: e.clientX, y: e.clientY };
|
lastPos = { x: e.clientX, y: e.clientY };
|
||||||
moveDist = 0;
|
moveDist = 0;
|
||||||
closeEyes();
|
closeEyes();
|
||||||
|
|
||||||
// Timer für Joy-Animation, aber NUR wenn genug gestreichelt
|
|
||||||
holdTimer = setTimeout(() => {
|
holdTimer = setTimeout(() => {
|
||||||
if (moveDist >= 75) { // mind. 75px gestreichelt!
|
if (moveDist >= 75) { // mind. 75px gestreichelt!
|
||||||
joyActive = true;
|
joyActive = true;
|
||||||
@@ -176,20 +177,17 @@ _bindMouseHold() {
|
|||||||
joyActive = false;
|
joyActive = false;
|
||||||
reopenEyes();
|
reopenEyes();
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
// Falls NICHT genug gestreichelt: Augen wieder auf
|
|
||||||
else {
|
|
||||||
reopenEyes();
|
reopenEyes();
|
||||||
}
|
}
|
||||||
mouseDown = false;
|
mouseDown = false;
|
||||||
}, 4000);
|
}, 4000);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mausbewegung tracken nur während gedrückt und über der Katze
|
|
||||||
this.img.addEventListener('mousemove', (e) => {
|
this.img.addEventListener('mousemove', (e) => {
|
||||||
if (!mouseDown || joyActive) return;
|
if (!mouseDown || joyActive) return;
|
||||||
if (!isMouseOverCat(e)) {
|
if (!isMouseNearCat(e)) {
|
||||||
// Sofort abbrechen wenn Maus nicht mehr über der Katze
|
// Sofort abbrechen, wenn außerhalb des "Toleranzbereichs"
|
||||||
clearTimeout(holdTimer);
|
clearTimeout(holdTimer);
|
||||||
holdTimer = null;
|
holdTimer = null;
|
||||||
reopenEyes();
|
reopenEyes();
|
||||||
@@ -204,9 +202,8 @@ _bindMouseHold() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wenn Maus losgelassen wird oder verlässt das Bild
|
|
||||||
['mouseup', 'mouseleave'].forEach(evt =>
|
['mouseup', 'mouseleave'].forEach(evt =>
|
||||||
this.img.addEventListener(evt, (e) => {
|
this.img.addEventListener(evt, () => {
|
||||||
if (holdTimer) clearTimeout(holdTimer);
|
if (holdTimer) clearTimeout(holdTimer);
|
||||||
holdTimer = null;
|
holdTimer = null;
|
||||||
if (!joyActive) reopenEyes();
|
if (!joyActive) reopenEyes();
|
||||||
|
|||||||
Reference in New Issue
Block a user