Updated <TrueFocus /> text animation

This commit is contained in:
Utkarsh-Singhal-26
2025-07-12 09:00:15 +05:30
parent 0b97a8c75a
commit d74724c136
3 changed files with 43 additions and 111 deletions

View File

@@ -7,11 +7,11 @@ export const trueFocus: CodeObject = {
usage: `<template> usage: `<template>
<TrueFocus <TrueFocus
sentence="True Focus" sentence="True Focus"
manualMode="false" :manualMode="false"
blurAmount="5" :blurAmount="5"
borderColor="red" borderColor="red"
animationDuration="2" :animationDuration="2"
pauseBetweenAnimations="1" :pauseBetweenAnimations="1"
/> />
</template> </template>

View File

@@ -1,5 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onUnmounted, watch, nextTick, computed } from "vue"; import { motion } from "motion-v";
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
interface TrueFocusProps { interface TrueFocusProps {
sentence?: string; sentence?: string;
@@ -56,7 +57,7 @@ watch(
); );
watch( watch(
[currentIndex, words], [currentIndex, words.value.length],
async () => { async () => {
if (currentIndex.value === null || currentIndex.value === -1) return; if (currentIndex.value === null || currentIndex.value === -1) return;
if (!wordRefs.value[currentIndex.value] || !containerRef.value) return; if (!wordRefs.value[currentIndex.value] || !containerRef.value) return;
@@ -95,6 +96,22 @@ const setWordRef = (el: HTMLSpanElement | null, index: number) => {
} }
}; };
onMounted(async () => {
await nextTick();
if (wordRefs.value[0] && containerRef.value) {
const parentRect = containerRef.value.getBoundingClientRect();
const activeRect = wordRefs.value[0].getBoundingClientRect();
focusRect.value = {
x: activeRect.left - parentRect.left,
y: activeRect.top - parentRect.top,
width: activeRect.width,
height: activeRect.height,
};
}
});
onUnmounted(() => { onUnmounted(() => {
if (interval) { if (interval) {
clearInterval(interval); clearInterval(interval);
@@ -103,16 +120,12 @@ onUnmounted(() => {
</script> </script>
<template> <template>
<div class="focus-container" ref="containerRef"> <div class="relative flex flex-wrap justify-center items-center gap-[1em]" ref="containerRef">
<span <span
v-for="(word, index) in words" v-for="(word, index) in words"
:key="index" :key="index"
:ref="(el) => setWordRef(el as HTMLSpanElement, index)" :ref="(el) => setWordRef(el as HTMLSpanElement, index)"
:class="[ class="relative font-black text-5xl transition-[filter,color] duration-300 ease-in-out cursor-pointer"
'focus-word',
{ manual: manualMode },
{ active: index === currentIndex && !manualMode },
]"
:style="{ :style="{
filter: index === currentIndex ? 'blur(0px)' : `blur(${blurAmount}px)`, filter: index === currentIndex ? 'blur(0px)' : `blur(${blurAmount}px)`,
'--border-color': borderColor, '--border-color': borderColor,
@@ -125,94 +138,27 @@ onUnmounted(() => {
{{ word }} {{ word }}
</span> </span>
<div <motion.div
class="focus-frame" class="top-0 left-0 box-content absolute border-none pointer-events-none"
:animate="{
x: focusRect.x,
y: focusRect.y,
width: focusRect.width,
height: focusRect.height,
opacity: currentIndex >= 0 ? 1 : 0,
}"
:transition="{
duration: animationDuration,
}"
:style="{ :style="{
'--border-color': borderColor, '--border-color': borderColor,
'--glow-color': glowColor, '--glow-color': glowColor,
transform: `translate(${focusRect.x}px, ${focusRect.y}px)`,
width: `${focusRect.width}px`,
height: `${focusRect.height}px`,
opacity: currentIndex >= 0 ? 1 : 0,
transition: `all ${animationDuration}s ease`,
}" }"
> >
<span class="top-left corner"></span> <span class="top-[-10px] left-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-r-0 border-b-0 rounded-[3px] w-4 h-4 transition-none"></span>
<span class="top-right corner"></span> <span class="top-[-10px] right-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-b-0 border-l-0 rounded-[3px] w-4 h-4 transition-none"></span>
<span class="bottom-left corner"></span> <span class="bottom-[-10px] left-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-t-0 border-r-0 rounded-[3px] w-4 h-4 transition-none"></span>
<span class="bottom-right corner"></span> <span class="right-[-10px] bottom-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-t-0 border-l-0 rounded-[3px] w-4 h-4 transition-none"></span>
</div> </motion.div>
</div> </div>
</template> </template>
<style scoped>
.focus-container {
position: relative;
display: flex;
gap: 1em;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.focus-word {
position: relative;
font-size: 3rem;
font-weight: 900;
cursor: pointer;
transition:
filter 0.3s ease,
color 0.3s ease;
}
.focus-word.active {
filter: blur(0);
}
.focus-frame {
position: absolute;
top: 0;
left: 0;
pointer-events: none;
box-sizing: content-box;
border: none;
}
.corner {
position: absolute;
width: 1rem;
height: 1rem;
border: 3px solid var(--border-color, #fff);
filter: drop-shadow(0px 0px 4px var(--border-color, #fff));
border-radius: 3px;
transition: none;
}
.top-left {
top: -10px;
left: -10px;
border-right: none;
border-bottom: none;
}
.top-right {
top: -10px;
right: -10px;
border-left: none;
border-bottom: none;
}
.bottom-left {
bottom: -10px;
left: -10px;
border-right: none;
border-top: none;
}
.bottom-right {
bottom: -10px;
right: -10px;
border-left: none;
border-top: none;
}
</style>

View File

@@ -3,7 +3,6 @@
<TabbedLayout> <TabbedLayout>
<template #preview> <template #preview>
<div class="relative py-6 overflow-hidden demo-container" style="min-height: 200px"> <div class="relative py-6 overflow-hidden demo-container" style="min-height: 200px">
<RefreshButton @click="forceRerender" />
<div :key="key" class="flex flex-col justify-center items-center m-8 pl-6 w-full"> <div :key="key" class="flex flex-col justify-center items-center m-8 pl-6 w-full">
<TrueFocus v-bind="config" /> <TrueFocus v-bind="config" />
</div> </div>
@@ -77,7 +76,6 @@ import Customize from "../../components/common/Customize.vue";
import PreviewColor from "../../components/common/PreviewColor.vue"; import PreviewColor from "../../components/common/PreviewColor.vue";
import PreviewSlider from "../../components/common/PreviewSlider.vue"; import PreviewSlider from "../../components/common/PreviewSlider.vue";
import PreviewSwitch from "../../components/common/PreviewSwitch.vue"; import PreviewSwitch from "../../components/common/PreviewSwitch.vue";
import RefreshButton from "../../components/common/RefreshButton.vue";
import TrueFocus from "../../content/TextAnimations/TrueFocus/TrueFocus.vue"; import TrueFocus from "../../content/TextAnimations/TrueFocus/TrueFocus.vue";
import { trueFocus } from "../../constants/code/TextAnimations/trueFocusCode"; import { trueFocus } from "../../constants/code/TextAnimations/trueFocusCode";
import { useForceRerender } from "@/composables/useForceRerender"; import { useForceRerender } from "@/composables/useForceRerender";
@@ -88,7 +86,7 @@ const manualMode = ref(false);
const blurAmount = ref(5); const blurAmount = ref(5);
const animationDuration = ref(0.5); const animationDuration = ref(0.5);
const pauseBetweenAnimations = ref(1); const pauseBetweenAnimations = ref(1);
const borderColor = ref("#5227FF"); const borderColor = ref("#27FF64");
const config = computed(() => ({ const config = computed(() => ({
sentence: "True Focus", sentence: "True Focus",
@@ -144,15 +142,3 @@ const propData = [
}, },
]; ];
</script> </script>
<style scoped>
.truefocus-demo {
width: 100%;
}
.demo-container {
display: flex;
align-items: center;
justify-content: center;
}
</style>