Sparkles

A configurable sparkles component that can be used as a background or as a standalone component.

Aceternity

Installation

Install Dependencies

npm i svelte-motion clsx tailwind-merge

Add util file

src/lib/utils/cn.ts
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
        
export function cn(...inputs: ClassValue[]) {
    return twMerge(clsx(inputs));
}

Copy the source code

src/lib/components/ui/Sparkles/Sparkles.svelte
<script lang="ts">
	import { cn } from '@/utils';
	import { Motion } from 'svelte-motion';
	let randomMove = () => Math.random() * 4 - 2;

	export let minSize: number = 0.6;
	export let maxSize: number = 1.5;
	export let speed: number = 3;
	export let particleColor: string = '#ffffff';
	export let particleDensity: number | undefined = 200;
	export let className: string | undefined = undefined;

	function getRandomValue() {
		return minSize + Math.random() * (maxSize - minSize);
	}
</script>

<div class={cn('relative h-48', className)}>
	<div class="absolute inset-0">
		{#each [...Array(particleDensity)] as _, i (`star-${i}`)}
			<Motion
				let:motion
				animate={{
					top: `calc(${Math.random() * 100}% + ${randomMove()}px)`,
					left: `calc(${Math.random() * 100}% + ${randomMove()}px)`,
					opacity: Math.random(),
					scale: [1, 1.2, 0]
				}}
				transition={{
					duration: Math.random() * 10 + speed,
					repeat: Infinity,
					ease: 'linear'
				}}
			>
				<span
					use:motion
					class="inline-block"
					style={`position: absolute; width: ${getRandomValue()}px; height: ${getRandomValue()}px; background-color: ${particleColor}; border-radius: 50%; top: ${Math.random() * 100}%; left: ${Math.random() * 100}%;`}
				></span>
			</Motion>
		{/each}
	</div>
</div>
src/lib/components/ui/Sparkles/index.ts
import Sparkles from './Sparkles.svelte';

export { Sparkles };

Props

Sparkles
Prop Type Description
id string | undefined The id of the sparkles component.
className string | undefined The class name of the sparkles component.
background string | undefined The background color of the sparkles component.
particleSize number | undefined The size of the particles.
minSize number | undefined The minimum size of the particles.
maxSize number | undefined The maximum size of the particles.
speed number | undefined The speed of the particles.
particleColor string | undefined The color of the particles.
particleDensity number | undefined The density of the particles.