web-difarft/app/components/CustomCursor.vue

172 lines
2.3 KiB
Vue

<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref } from "vue";
const x = ref(0);
const y = ref(0);
const visible = ref(false);
const handleMouseMove = (event: MouseEvent) => {
x.value = event.clientX;
y.value = event.clientY;
visible.value = true;
};
const handleMouseLeave = () => {
visible.value = false;
};
onMounted(() => {
window.addEventListener("mousemove", handleMouseMove, {
passive: true,
});
document.documentElement.addEventListener(
"mouseleave",
handleMouseLeave,
);
});
onBeforeUnmount(() => {
window.removeEventListener(
"mousemove",
handleMouseMove,
);
document.documentElement.removeEventListener(
"mouseleave",
handleMouseLeave,
);
});
</script>
<template>
<div
class="custom-cursor"
:class="{ 'is-visible': visible }"
:style="{
left: `${x}px`,
top: `${y}px`,
}"
>
<div class="crosshair">
<span class="horizontal" />
<span class="vertical" />
<span class="center" />
</div>
<div class="coordinates">
<span>X {{ x }}</span>
<span>Y {{ y }}</span>
</div>
</div>
</template>
<style scoped>
.custom-cursor {
position: fixed;
left: 0;
top: 0;
width: 0;
height: 0;
z-index: 2147483647;
pointer-events: none;
opacity: 0;
transform: translate3d(0, 0, 0);
transition: opacity 120ms ease;
}
.custom-cursor.is-visible {
opacity: 1;
}
.crosshair {
position: absolute;
left: 0;
top: 0;
width: 22px;
height: 22px;
transform: translate(-50%, -50%);
}
.horizontal,
.vertical {
position: absolute;
display: block;
background: var(--text);
opacity: 0.85;
}
.horizontal {
left: 0;
top: 10px;
width: 22px;
height: 1px;
}
.vertical {
left: 10px;
top: 0;
width: 1px;
height: 22px;
}
.center {
position: absolute;
left: 50%;
top: 50%;
width: 3px;
height: 3px;
border-radius: 9999px;
background: var(--text);
transform: translate(-50%, -50%);
}
.coordinates {
position: absolute;
left: 14px;
top: 14px;
display: flex;
flex-direction: column;
gap: 1px;
white-space: nowrap;
font-family: monospace;
font-size: 9px;
line-height: 1.2;
letter-spacing: 0.04em;
color: var(--text-muted);
opacity: 0.9;
}
@media (max-width: 767px) {
.custom-cursor {
display: none;
}
}
</style>