aboutsummaryrefslogtreecommitdiff
path: root/src/lib/animations/blink.ts
blob: 623489a282c92e089d73568ad3a6513b875babf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { sineOut } from 'svelte/easing';
import type { AnimationConfig } from 'svelte/animate';

export const blink = function blink(node: HTMLElement, params: AnimationConfig): AnimationConfig {
	const originalWidth = parseFloat(getComputedStyle(node).width);
	const originalHeight = parseFloat(getComputedStyle(node).height);

	return {
		delay: params.delay || 0,
		duration: params.duration || 400,
		easing: params.easing || sineOut,
		css: (t: number): string => {
			const halfWidth = originalWidth / 2;
			const halfHeight = originalHeight / 2;
			const height = Math.round(t <= 0.2 ? (originalHeight * t) / 0.2 : originalHeight);
			const marginY = Math.round(t <= 0.2 ? halfHeight * (1 - t / 0.2) : 0);
			const width = Math.round(t > 0.2 ? ((t - 0.2) / 0.8) * originalWidth : 0);
			const marginX = Math.round(t > 0.2 ? (1 - (t - 0.2) / 0.8) * halfWidth : halfWidth);

			return `width: ${width}px; height: ${height}px; margin: ${marginY}px ${marginX}px`;
		}
	};
};