Updated to use Svelte 5 runes

This commit is contained in:
Justin Edmund 2024-07-12 16:56:01 -04:00
parent 1f2cf71e21
commit 8c31d08064
2 changed files with 58 additions and 58 deletions

View file

@ -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() {

View file

@ -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)
}
})
</script>