Commit 2c3587509d

2c3587509dbc7fd230908e733414a827c6cc9562

parent: d412924605

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-07-09 04:39 UTC

tako

Layout: unified · split

docs/.vitepress/theme/Layout.vue +3 −104
@@ -7,12 +7,10 @@
77-->
88<script setup lang="ts">
99import {
10 customStorageEventName,
1110 useEventListener,
1211 useStorage,
1312 useThrottleFn
1413} from '@vueuse/core'
15import { usePreferredReducedMotion } from '@vueuse/core'
1614import { getScrollOffset, useData, useRoute } from 'vitepress'
1715import type { DefaultTheme as Theme } from 'vitepress'
1816import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue'
@@ -35,9 +33,9 @@ import NotFoundComponent from './components/NotFound.vue'
3533import { NolebaseEnhancedReadabilitiesScreenMenu } from './components/settings'
3634import NavActions from './components/NavActions.vue'
3735import SidebarCard from './components/SidebarCard.vue'
38import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey, TakodachiStorageKey } from './constants'
36import TakodachiChaser from './components/TakodachiChaser.vue'
37import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants'
3938import { useEffects } from './composables/useEffects'
40import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from './math'
4139
4240const route = useRoute()
4341const { frontmatter, isDark, site, theme } = useData()
@@ -794,8 +792,6 @@ const breadcrumbs = computed(() => {
794792 return buildBreadcrumbs(sidebar, currentPath)
795793})
796794
797// Respect user's reduced motion preferences
798const prefs = usePreferredReducedMotion()
799795const { effectsEnabled } = useEffects()
800796
801797watchEffect(() => {
@@ -984,80 +980,6 @@ watch(
984980 { immediate: true }
985981)
986982
987const speed = 2
988const snapThreshold = 5 // px
989
990const takodachiRef = ref<HTMLImageElement | null>(null)
991let mousePos: Vec2D | null = null
992let target: Vec2D | null = null
993let offset: Vec2D = [0, 0]
994
995const position = ref({
996 x: 0,
997 y: 0
998})
999
1000const updateMousePos = (e: MouseEvent | TouchEvent) => {
1001 if (e instanceof MouseEvent) {
1002 mousePos = [e.clientX, e.clientY]
1003 } else {
1004 mousePos = [e.touches[0].clientX, e.touches[0].clientY]
1005 }
1006}
1007
1008const takodachiDisable = ref<(() => void) | null>(null)
1009const takodachiToggledOn = useStorage(TakodachiStorageKey, false)
1010
1011const reloadTakodachi = () => {
1012 takodachiDisable.value?.()
1013
1014 if (prefs.value === 'reduce') return
1015 const saved = takodachiToggledOn
1016 if (saved.value === false) return
1017
1018 window.addEventListener('mousemove', updateMousePos)
1019 window.addEventListener('touchstart', updateMousePos)
1020
1021 const chaser = takodachiRef!.value
1022 if (chaser) {
1023 offset = v2smul([chaser.clientWidth, chaser.clientHeight], 0.5)
1024 setTimeout(() => {
1025 chaser.classList.remove('opacity-0')
1026 }, 1000)
1027 }
1028
1029 const intervalId = setInterval(() => {
1030 if (!mousePos) return
1031
1032 const currentPos: Vec2D = [
1033 position.value.x + offset[0],
1034 position.value.y + offset[1]
1035 ]
1036
1037 target = target ?? currentPos
1038
1039 const diff = v2sub(mousePos, currentPos)
1040
1041 target = v2mag(diff) < snapThreshold
1042 ? mousePos
1043 : v2add(target, v2smul(v2norm(diff), speed))
1044
1045 const finalVec = v2sub(target, offset)
1046
1047 position.value = { x: finalVec[0], y: finalVec[1] }
1048 }, 10)
1049
1050 takodachiDisable.value = () => {
1051 chaser?.classList.add('opacity-0')
1052
1053 window.removeEventListener('mousemove', updateMousePos)
1054 window.removeEventListener('touchstart', updateMousePos)
1055 clearInterval(intervalId)
1056
1057 takodachiDisable.value = null
1058 }
1059}
1060
1061983function initTitleOnly() {
1062984 document.querySelectorAll('.custom-block').forEach(block => {
1063985 const children = [...block.children]
@@ -1368,18 +1290,6 @@ onMounted(() => {
13681290 ) return
13691291 requestAnimationFrame(() => (hamburger as HTMLElement)?.click())
13701292 })
1371
1372 // Storage changed in other documents.
1373 useEventListener(window, 'storage', () => {
1374 reloadTakodachi()
1375 })
1376
1377 // Storage changed in the same document.
1378 useEventListener(window, customStorageEventName, () => {
1379 reloadTakodachi()
1380 })
1381
1382 reloadTakodachi()
13831293})
13841294
13851295onUnmounted(() => {
@@ -1388,7 +1298,6 @@ onUnmounted(() => {
13881298 tocMutationObserver?.disconnect()
13891299 cancelAnimationFrame(outlineFollowRaf)
13901300 cancelAnimationFrame(tocRaf)
1391 takodachiDisable.value && takodachiDisable.value()
13921301 window.removeEventListener(BOOKMARK_CHANGE_EVENT, syncTocBookmarkStates)
13931302})
13941303</script>
@@ -1396,17 +1305,7 @@ onUnmounted(() => {
13961305<template>
13971306 <Layout>
13981307 <template #layout-top>
1399 <img
1400 id="takodachi"
1401 ref="takodachiRef"
1402 src="/takodachi.webp"
1403 alt="Takodachi"
1404 class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500"
1405 :style="{
1406 left: `${position.x}px`,
1407 top: `${position.y}px`
1408 }"
1409 />
1308 <TakodachiChaser />
14101309 <!-- Home sidebar menu -->
14111310 <template v-if="isHome">
14121311 <Transition name="home-fade">
docs/.vitepress/theme/components/TakodachiChaser.vue added +95
@@ -0,0 +1,95 @@
1<script setup lang="ts">
2import { useStorage } from '@vueuse/core'
3import { onMounted, onUnmounted, ref, watch } from 'vue'
4
5import { TakodachiStorageKey } from '../constants'
6import { v2add, v2mag, v2norm, v2smul, v2sub, type Vec2D } from '../math'
7
8const speed = 2
9const snapThreshold = 5 // px
10
11const takodachiRef = ref<HTMLImageElement | null>(null)
12let mousePos: Vec2D | null = null
13let target: Vec2D | null = null
14let offset: Vec2D = [0, 0]
15
16const position = ref({ x: 0, y: 0 })
17
18const updateMousePos = (e: MouseEvent | TouchEvent) => {
19 if (e instanceof MouseEvent) {
20 mousePos = [e.clientX, e.clientY]
21 } else {
22 mousePos = [e.touches[0].clientX, e.touches[0].clientY]
23 }
24}
25
26const takodachiToggledOn = useStorage(TakodachiStorageKey, false)
27let disable: (() => void) | null = null
28
29const reload = () => {
30 disable?.()
31
32 if (!takodachiToggledOn.value) return
33
34 window.addEventListener('mousemove', updateMousePos)
35 window.addEventListener('touchstart', updateMousePos)
36
37 const chaser = takodachiRef.value
38 if (chaser) {
39 offset = v2smul([chaser.clientWidth, chaser.clientHeight], 0.5)
40 setTimeout(() => {
41 chaser.classList.remove('opacity-0')
42 }, 1000)
43 }
44
45 const intervalId = setInterval(() => {
46 if (!mousePos) return
47
48 const currentPos: Vec2D = [
49 position.value.x + offset[0],
50 position.value.y + offset[1]
51 ]
52
53 target = target ?? currentPos
54
55 const diff = v2sub(mousePos, currentPos)
56
57 target = v2mag(diff) < snapThreshold
58 ? mousePos
59 : v2add(target, v2smul(v2norm(diff), speed))
60
61 const finalVec = v2sub(target, offset)
62
63 position.value = { x: finalVec[0], y: finalVec[1] }
64 }, 10)
65
66 disable = () => {
67 chaser?.classList.add('opacity-0')
68
69 window.removeEventListener('mousemove', updateMousePos)
70 window.removeEventListener('touchstart', updateMousePos)
71 clearInterval(intervalId)
72
73 disable = null
74 }
75}
76
77watch(takodachiToggledOn, reload)
78
79onMounted(reload)
80onUnmounted(() => disable?.())
81</script>
82
83<template>
84 <img
85 id="takodachi"
86 ref="takodachiRef"
87 src="/takodachi.webp"
88 alt="Takodachi"
89 class="pointer-events-none fixed absolute z-[9999] h-10 w-10 opacity-0 transition-opacity duration-500"
90 :style="{
91 left: `${position.x}px`,
92 top: `${position.y}px`
93 }"
94 />
95</template>