From 8c31d0806468018e9d18d5db9e8cfd500dfae478 Mon Sep 17 00:00:00 2001 From: Justin Edmund <383021+jedmund@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:56:01 -0400 Subject: [PATCH] Updated to use Svelte 5 runes --- src/lib/components/Avatar.svelte | 51 +++++++++++++------ src/lib/components/SVGHoverEffect.svelte | 65 +++++++++--------------- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/lib/components/Avatar.svelte b/src/lib/components/Avatar.svelte index fa15d4c..64d0109 100644 --- a/src/lib/components/Avatar.svelte +++ b/src/lib/components/Avatar.svelte @@ -2,29 +2,48 @@ import { onMount } from 'svelte' import { spring } from 'svelte/motion' - let isHovering = false - let isBlinking = false + let isHovering = $state(false) + let isBlinking = $state(false) const scale = spring(1, { stiffness: 0.1, damping: 0.125 }) - $: isHovering ? scale.set(1.25) : scale.set(1) + $effect(() => { + if (isHovering) { + scale.set(1.25) + } else { + scale.set(1) + } + }) - function blink() { - isBlinking = true - setTimeout(() => { - isBlinking = false - if (Math.random() < 0.45) { - setTimeout(() => { - isBlinking = true - setTimeout(() => { - isBlinking = false - }, 150) - }, 100) - } - }, 150) + function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)) + } + + function setBlinkState(state) { + isBlinking = state + } + + async function singleBlink(duration) { + setBlinkState(true) + await sleep(duration) + setBlinkState(false) + } + + async function doubleBlink() { + await singleBlink(50) + await sleep(100) + await singleBlink(150) + } + + async function blink() { + await singleBlink(150) + + if (Math.random() < 0.45) { + await doubleBlink() + } } function startBlinking() { diff --git a/src/lib/components/SVGHoverEffect.svelte b/src/lib/components/SVGHoverEffect.svelte index c07a263..5319ae2 100644 --- a/src/lib/components/SVGHoverEffect.svelte +++ b/src/lib/components/SVGHoverEffect.svelte @@ -2,59 +2,46 @@ import { onMount } from 'svelte' import { spring } from 'svelte/motion' - export let SVGComponent - export let backgroundColor = '#f0f0f0' - export let maxMovement = 20 - export let smoothness = 0.1 - export let containerHeight = '300px' - export let bounceStiffness = 0.1 - export let bounceDamping = 0.4 + const { + SVGComponent, + backgroundColor = '#f0f0f0', + maxMovement = 20, + containerHeight = '300px', + stiffness = 0.15, + damping = 0.8 + } = $props() - let container - let svg - let isHovering = false - let animationFrame + let container = $state(null) + let svg = $state(null) const position = spring( { x: 0, y: 0 }, { - stiffness: bounceStiffness, - damping: bounceDamping + stiffness, + damping } ) - $: if (svg) { - svg.style.transform = `translate(calc(-50% + ${$position.x}px), calc(-50% + ${$position.y}px))` - } + $effect(() => { + if (svg) { + const { x, y } = $position + svg.style.transform = `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))` + } + }) function handleMouseMove(event) { - isHovering = true const rect = container.getBoundingClientRect() const x = event.clientX - rect.left const y = event.clientY - rect.top - const targetX = (x / rect.width - 0.5) * 2 * maxMovement - const targetY = (y / rect.height - 0.5) * 2 * maxMovement - - position.set({ x: targetX, y: targetY }, { hard: false }) + position.set({ + x: (x / rect.width - 0.5) * 2 * maxMovement, + y: (y / rect.height - 0.5) * 2 * maxMovement + }) } function handleMouseLeave() { - isHovering = false - position.set({ x: 0, y: 0 }, { hard: false }) - } - - function updateSVGPosition() { - if (isHovering) { - const currentX = $position.x - const currentY = $position.y - position.update((pos) => ({ - x: pos.x + (currentX - pos.x) * smoothness, - y: pos.y + (currentY - pos.y) * smoothness - })) - } - - animationFrame = requestAnimationFrame(updateSVGPosition) + position.set({ x: 0, y: 0 }) } onMount(() => { @@ -65,12 +52,6 @@ svg.style.top = '50%' svg.style.transform = 'translate(-50%, -50%)' } - - updateSVGPosition() - - return () => { - cancelAnimationFrame(animationFrame) - } })