nicco.io/src/lib/components/Progress.svelte

63 lines
1.3 KiB
Svelte
Raw Normal View History

2021-08-02 09:53:08 +02:00
<script lang="ts">
2021-02-08 10:56:38 +01:00
import { spring } from 'svelte/motion'
2021-08-02 09:53:08 +02:00
import { scroll } from '$lib/stores'
2021-02-08 10:56:38 +01:00
let el
const springed = spring(
{ scroll: 0 },
{
2021-02-09 13:51:39 +01:00
stiffness: 0.05,
damping: 0.7,
2021-02-08 10:56:38 +01:00
}
)
function updateState(value) {
2021-02-10 11:23:38 +01:00
const max = 359.99999
2021-02-08 10:56:38 +01:00
const R = 50
let alpha = (360 / 1) * value
2021-02-10 11:23:38 +01:00
alpha = Math.min(alpha, max)
2021-02-08 10:56:38 +01:00
const a = ((90 - alpha) * Math.PI) / 180
const x = R + R * Math.cos(a) * 2
const y = R - R * Math.sin(a) * 2
const center = alpha > 180 ? 1 : 0
2021-08-02 09:53:08 +02:00
const path = `M${R},${-50} A${R * 2},${R * 2},0,${center},1,${x},${y} L${R},${R} L${R},${-R}`
2021-02-08 10:56:38 +01:00
if (el) el.setAttribute('d', path)
}
$: springed.set({ scroll: $scroll })
$: updateState($springed.scroll)
</script>
2021-02-09 13:51:39 +01:00
<div>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" version="1.1">
<path bind:this={el} fill="var(--clr-secondary)" d="" />
</svg>
<span>{$scroll.toFixed(2)}</span>
</div>
2021-02-08 10:56:38 +01:00
<style>
2021-02-09 13:51:39 +01:00
div {
2021-02-10 11:24:33 +01:00
position: absolute;
2021-02-08 10:56:38 +01:00
bottom: 1em;
right: 1em;
2021-02-09 13:51:39 +01:00
pointer-events: none;
text-align: center;
}
span {
display: block;
font-size: 0.5em;
background-color: var(--clr-primary);
height: 1.5em;
}
svg {
2021-02-08 10:56:38 +01:00
border: 0.125em solid var(--clr-primary);
width: 2em;
height: 2em;
2021-02-09 13:51:39 +01:00
position: relative;
top: 0.45em;
2021-02-08 10:56:38 +01:00
}
</style>