import { random, range } from 'lodash';
import './reset.css';
import './styles.css';
const PRECONFIGURED_PARTICLES = [
{
angle: 210,
distance: 55,
},
{
angle: 234,
distance: 30,
},
{
angle: 225,
distance: 75,
},
{
angle: 240,
distance: 50,
},
];
window.addEventListener('click', (event) => {
const x = event.clientX;
const y = event.clientY;
const motionEnabled = window.matchMedia(
'(prefers-reduced-motion: no-preference)'
).matches;
if (!motionEnabled) {
return;
}
const particles = [];
range(4).forEach((index) => {
const particle = document.createElement('img');
particle.setAttribute('alt', '');
particle.setAttribute(
'src',
'https://sandpack-bundler.vercel.app/img/wand-sparkle.svg'
);
particle.setAttribute('aria-hidden', 'true');
particle.classList.add('star');
particle.style.top = y + 'px';
particle.style.left = x + 'px';
let { angle, distance } = PRECONFIGURED_PARTICLES[index];
angle += random(-8, 8);
distance += random(-5, 5);
const rotation = random(-45, -270);
const size = random(13, 18);
particle.style.setProperty('--size', size + 'px')
particle.style.setProperty('--angle', angle + 'deg');
particle.style.setProperty('--distance', distance + 'px');
particle.style.setProperty('--rotation', rotation + 'deg');
particle.style.setProperty(
'--pop-duration',
normalize(distance, 20, 85, 1000, 1400) +
random(-300, 300) + 'ms'
);
particle.style.setProperty(
'--fade-duration',
random(700, 900) + 'ms'
);
particle.style.setProperty(
'--fade-delay',
normalize(distance, 20, 85, 250, 500) + 'ms'
);
document.body.appendChild(particle);
particles.push(particle);
});
window.setTimeout(
() => {
particles.forEach((particle) => {
particle.remove();
});
},
3000
);
});
const normalize = (
number,
currentScaleMin,
currentScaleMax,
newScaleMin,
newScaleMax
) => {
const standardNormalization =
(number - currentScaleMin) / (currentScaleMax - currentScaleMin);
return (
(newScaleMax - newScaleMin) * standardNormalization + newScaleMin
);
};