mirror of
https://github.com/DavidHDev/vue-bits.git
synced 2026-03-07 06:29:30 -07:00
[ REFACT ] : CircularText Text Animation
This commit is contained in:
@@ -31,7 +31,7 @@
|
|||||||
"lucide-vue-next": "^0.548.0",
|
"lucide-vue-next": "^0.548.0",
|
||||||
"mathjs": "^14.6.0",
|
"mathjs": "^14.6.0",
|
||||||
"matter-js": "^0.20.0",
|
"matter-js": "^0.20.0",
|
||||||
"motion-v": "^1.5.0",
|
"motion-v": "^1.10.2",
|
||||||
"ogl": "^1.0.11",
|
"ogl": "^1.0.11",
|
||||||
"postprocessing": "^6.37.6",
|
"postprocessing": "^6.37.6",
|
||||||
"primeicons": "^7.0.0",
|
"primeicons": "^7.0.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref, watchEffect, onUnmounted } from 'vue';
|
import { animate, Motion, MotionValue, useMotionValue } from 'motion-v';
|
||||||
import { Motion } from 'motion-v';
|
import { computed, onMounted, watch } from 'vue';
|
||||||
|
|
||||||
interface CircularTextProps {
|
interface CircularTextProps {
|
||||||
text: string;
|
text: string;
|
||||||
@@ -10,87 +10,59 @@ interface CircularTextProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<CircularTextProps>(), {
|
const props = withDefaults(defineProps<CircularTextProps>(), {
|
||||||
text: '',
|
|
||||||
spinDuration: 20,
|
spinDuration: 20,
|
||||||
onHover: 'speedUp',
|
onHover: 'speedUp',
|
||||||
className: ''
|
className: ''
|
||||||
});
|
});
|
||||||
|
|
||||||
const letters = computed(() => Array.from(props.text));
|
const letters = computed(() => Array.from(props.text));
|
||||||
const isHovered = ref(false);
|
const rotation: MotionValue<number> = useMotionValue(0);
|
||||||
|
|
||||||
const currentRotation = ref(0);
|
let currentAnimation: ReturnType<typeof animate> | null = null;
|
||||||
const animationId = ref<number | null>(null);
|
|
||||||
const lastTime = ref<number>(Date.now());
|
|
||||||
const rotationSpeed = ref<number>(0);
|
|
||||||
|
|
||||||
const getCurrentSpeed = () => {
|
const startRotation = (duration: number) => {
|
||||||
if (isHovered.value && props.onHover === 'pause') return 0;
|
currentAnimation?.stop();
|
||||||
|
const start = rotation.get();
|
||||||
|
|
||||||
const baseDuration = props.spinDuration;
|
currentAnimation = animate(rotation, start + 360, {
|
||||||
const baseSpeed = 360 / baseDuration;
|
duration,
|
||||||
|
ease: 'linear',
|
||||||
|
repeat: Infinity
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (!isHovered.value) return baseSpeed;
|
onMounted(() => {
|
||||||
|
startRotation(props.spinDuration);
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => [props.spinDuration, props.text],
|
||||||
|
() => {
|
||||||
|
startRotation(props.spinDuration);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleHoverStart = () => {
|
||||||
|
if (!props.onHover) return;
|
||||||
|
|
||||||
switch (props.onHover) {
|
switch (props.onHover) {
|
||||||
case 'slowDown':
|
case 'slowDown':
|
||||||
return baseSpeed / 2;
|
startRotation(props.spinDuration * 2);
|
||||||
|
break;
|
||||||
case 'speedUp':
|
case 'speedUp':
|
||||||
return baseSpeed * 4;
|
startRotation(props.spinDuration / 4);
|
||||||
|
break;
|
||||||
|
case 'pause':
|
||||||
|
currentAnimation?.stop();
|
||||||
|
break;
|
||||||
case 'goBonkers':
|
case 'goBonkers':
|
||||||
return baseSpeed * 20;
|
startRotation(props.spinDuration / 20);
|
||||||
default:
|
break;
|
||||||
return baseSpeed;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getCurrentScale = () => {
|
|
||||||
return isHovered.value && props.onHover === 'goBonkers' ? 0.8 : 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
const animate = () => {
|
|
||||||
const now = Date.now();
|
|
||||||
const deltaTime = (now - lastTime.value) / 1000;
|
|
||||||
lastTime.value = now;
|
|
||||||
|
|
||||||
const targetSpeed = getCurrentSpeed();
|
|
||||||
|
|
||||||
const speedDiff = targetSpeed - rotationSpeed.value;
|
|
||||||
const smoothingFactor = Math.min(1, deltaTime * 5);
|
|
||||||
rotationSpeed.value += speedDiff * smoothingFactor;
|
|
||||||
|
|
||||||
currentRotation.value = (currentRotation.value + rotationSpeed.value * deltaTime) % 360;
|
|
||||||
|
|
||||||
animationId.value = requestAnimationFrame(animate);
|
|
||||||
};
|
|
||||||
|
|
||||||
const startAnimation = () => {
|
|
||||||
if (animationId.value) {
|
|
||||||
cancelAnimationFrame(animationId.value);
|
|
||||||
}
|
|
||||||
lastTime.value = Date.now();
|
|
||||||
rotationSpeed.value = getCurrentSpeed();
|
|
||||||
animate();
|
|
||||||
};
|
|
||||||
|
|
||||||
watchEffect(() => {
|
|
||||||
startAnimation();
|
|
||||||
});
|
|
||||||
|
|
||||||
startAnimation();
|
|
||||||
|
|
||||||
onUnmounted(() => {
|
|
||||||
if (animationId.value) {
|
|
||||||
cancelAnimationFrame(animationId.value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleHoverStart = () => {
|
|
||||||
isHovered.value = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleHoverEnd = () => {
|
const handleHoverEnd = () => {
|
||||||
isHovered.value = false;
|
startRotation(props.spinDuration);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLetterTransform = (index: number) => {
|
const getLetterTransform = (index: number) => {
|
||||||
@@ -104,28 +76,24 @@ const getLetterTransform = (index: number) => {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Motion
|
<Motion
|
||||||
:animate="{
|
tag="div"
|
||||||
rotate: currentRotation,
|
:class="[
|
||||||
scale: getCurrentScale()
|
'm-0 mx-auto rounded-full w-[200px] h-[200px] relative font-black text-white text-center cursor-pointer origin-center',
|
||||||
|
className
|
||||||
|
]"
|
||||||
|
:style="{
|
||||||
|
rotate: rotation
|
||||||
}"
|
}"
|
||||||
:transition="{
|
:initial="{
|
||||||
rotate: {
|
rotate: 0
|
||||||
duration: 0
|
|
||||||
},
|
|
||||||
scale: {
|
|
||||||
type: 'spring',
|
|
||||||
damping: 20,
|
|
||||||
stiffness: 300
|
|
||||||
}
|
|
||||||
}"
|
}"
|
||||||
:class="`m-0 mx-auto rounded-full w-[200px] h-[200px] relative font-black text-white text-center cursor-pointer origin-center ${props.className}`"
|
|
||||||
@mouseenter="handleHoverStart"
|
@mouseenter="handleHoverStart"
|
||||||
@mouseleave="handleHoverEnd"
|
@mouseleave="handleHoverEnd"
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
v-for="(letter, i) in letters"
|
v-for="(letter, i) in letters"
|
||||||
:key="i"
|
:key="i"
|
||||||
class="absolute inline-block inset-0 text-2xl transition-all duration-500 ease-[cubic-bezier(0,0,0,1)]"
|
class="inline-block absolute inset-0 text-2xl transition-all duration-500 ease-[cubic-bezier(0,0,0,1)]"
|
||||||
:style="{
|
:style="{
|
||||||
transform: getLetterTransform(i),
|
transform: getLetterTransform(i),
|
||||||
WebkitTransform: getLetterTransform(i)
|
WebkitTransform: getLetterTransform(i)
|
||||||
|
|||||||
@@ -1,28 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<TabbedLayout>
|
<TabbedLayout>
|
||||||
<template #preview>
|
<template #preview>
|
||||||
<div class="demo-container h-[400px] overflow-hidden">
|
<div class="h-[400px] overflow-hidden demo-container">
|
||||||
<CircularText
|
<CircularText :key="rerenderKey" :text="text" :spin-duration="spinDuration" :on-hover="onHover" />
|
||||||
:key="rerenderKey"
|
|
||||||
text="VUE * BITS * IS * AWESOME * "
|
|
||||||
:spin-duration="spinDuration"
|
|
||||||
:on-hover="onHover"
|
|
||||||
class-name="text-blue-500"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Customize>
|
<Customize>
|
||||||
<div class="flex gap-4 flex-wrap">
|
<PreviewText title="Text" v-model="text" />
|
||||||
<button
|
<PreviewSelect title="On Hover" v-model="onHover" :options="hoverOptions" />
|
||||||
class="text-xs bg-[#0b0b0b] rounded-[10px] border border-[#1e3721] hover:bg-[#1e3721] text-white h-8 px-3 transition-colors"
|
<PreviewSlider title="Spin Duration (s)" v-model="spinDuration" :min="1" :max="60" :step="1" />
|
||||||
@click="toggleOnHover"
|
|
||||||
>
|
|
||||||
On Hover:
|
|
||||||
<span class="text-[#a1a1aa]"> {{ onHover }}</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<PreviewSlider title="Spin Duration (s)" v-model="spinDuration" :min="1" :max="50" :step="1" />
|
|
||||||
</Customize>
|
</Customize>
|
||||||
|
|
||||||
<PropTable :data="propData" />
|
<PropTable :data="propData" />
|
||||||
@@ -41,45 +27,58 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue';
|
import CliInstallation from '@/components/code/CliInstallation.vue';
|
||||||
import TabbedLayout from '../../components/common/TabbedLayout.vue';
|
import CodeExample from '@/components/code/CodeExample.vue';
|
||||||
import PropTable from '../../components/common/PropTable.vue';
|
import Dependencies from '@/components/code/Dependencies.vue';
|
||||||
import Dependencies from '../../components/code/Dependencies.vue';
|
import Customize from '@/components/common/Customize.vue';
|
||||||
import CliInstallation from '../../components/code/CliInstallation.vue';
|
import PreviewSelect from '@/components/common/PreviewSelect.vue';
|
||||||
import CodeExample from '../../components/code/CodeExample.vue';
|
import PreviewSlider from '@/components/common/PreviewSlider.vue';
|
||||||
import Customize from '../../components/common/Customize.vue';
|
import PreviewText from '@/components/common/PreviewText.vue';
|
||||||
import PreviewSlider from '../../components/common/PreviewSlider.vue';
|
import PropTable from '@/components/common/PropTable.vue';
|
||||||
import CircularText from '../../content/TextAnimations/CircularText/CircularText.vue';
|
import TabbedLayout from '@/components/common/TabbedLayout.vue';
|
||||||
import { circularText } from '@/constants/code/TextAnimations/circularTextCode';
|
|
||||||
import { useForceRerender } from '@/composables/useForceRerender';
|
import { useForceRerender } from '@/composables/useForceRerender';
|
||||||
|
import { circularText } from '@/constants/code/TextAnimations/circularTextCode';
|
||||||
|
import CircularText from '@/content/TextAnimations/CircularText/CircularText.vue';
|
||||||
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
const { rerenderKey } = useForceRerender();
|
||||||
|
|
||||||
|
const text = ref('VUE*BITS*COMPONENTS*');
|
||||||
const onHover = ref<'slowDown' | 'speedUp' | 'pause' | 'goBonkers'>('speedUp');
|
const onHover = ref<'slowDown' | 'speedUp' | 'pause' | 'goBonkers'>('speedUp');
|
||||||
const spinDuration = ref(20);
|
const spinDuration = ref(20);
|
||||||
const { rerenderKey, forceRerender } = useForceRerender();
|
|
||||||
|
|
||||||
const hoverOptions: Array<'slowDown' | 'speedUp' | 'pause' | 'goBonkers'> = [
|
const hoverOptions = [
|
||||||
'slowDown',
|
{ label: 'Slow Down', value: 'slowDown' },
|
||||||
'speedUp',
|
{ label: 'Speed Up', value: 'speedUp' },
|
||||||
'pause',
|
{ label: 'Pause', value: 'pause' },
|
||||||
'goBonkers'
|
{ label: 'Go Bonkers', value: 'goBonkers' }
|
||||||
];
|
];
|
||||||
|
|
||||||
const toggleOnHover = () => {
|
|
||||||
const currentIndex = hoverOptions.indexOf(onHover.value);
|
|
||||||
const nextIndex = (currentIndex + 1) % hoverOptions.length;
|
|
||||||
onHover.value = hoverOptions[nextIndex];
|
|
||||||
forceRerender();
|
|
||||||
};
|
|
||||||
|
|
||||||
const propData = [
|
const propData = [
|
||||||
{ name: 'text', type: 'string', default: '""', description: 'The text content to display in a circular pattern.' },
|
{
|
||||||
{ name: 'spinDuration', type: 'number', default: '20', description: 'Duration of one full rotation in seconds.' },
|
name: 'text',
|
||||||
|
type: 'string',
|
||||||
|
default: "''",
|
||||||
|
description: 'The text to display in a circular layout.'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'spinDuration',
|
||||||
|
type: 'number',
|
||||||
|
default: '20',
|
||||||
|
description: 'The duration (in seconds) for one full rotation.'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'onHover',
|
name: 'onHover',
|
||||||
type: 'string',
|
type: "'slowDown' | 'speedUp' | 'pause' | 'goBonkers'",
|
||||||
default: '"speedUp"',
|
default: 'undefined',
|
||||||
description: 'Hover behavior: "slowDown", "speedUp", "pause", or "goBonkers".'
|
description:
|
||||||
|
"Specifies the hover behavior variant. Options include 'slowDown', 'speedUp', 'pause', and 'goBonkers'."
|
||||||
},
|
},
|
||||||
{ name: 'className', type: 'string', default: '""', description: 'Additional class names to style the component.' }
|
{
|
||||||
|
name: 'className',
|
||||||
|
type: 'string',
|
||||||
|
default: "''",
|
||||||
|
description: 'Optional additional CSS classes to apply to the component.'
|
||||||
|
}
|
||||||
];
|
];
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user