๐ŸŽ‰ We just launched a new HTML course! Get 3 courses with 33% off! ๐ŸŽ‰
How to Animate Elements in Svelte By Stiffness and Damping

How to Animate Elements in Svelte By Stiffness and Damping

Ferenc Almasi โ€ข 2020 October 18 โ€ข ๐Ÿ“– 2 min read
  • twitter
  • facebook

In Svelte, you can use a spring store to animate certain properties of an element gradually, based on a stiffness and damping parameter:

<script>
    import { spring } from 'svelte/motion';

    const springConfig = {
        stiffness: 0.25,
	damping: 0.1
    };
	
    const rotate = spring(0, springConfig);
    const radius = spring(0, springConfig);

    const transform = () => {
        $rotate = 720;
	$radius = 100;
    };
</script>

<div on:click={transform}
     style="transform: rotate({$rotate}deg); border-radius: {$radius}%">
</div>
Spring.svelte
Copied to clipboard!

You can use the following options in a spring store:

  • stiffness: A value between 0 and 1, where the higher you go, the tighter the spring gets.
  • damping: A value between 0 and 1, where the lower you go, the springier the spring gets.
  • precision: A threshold at which the spring is considered to be at rest. The lower you go, the more precise it will be.

You can find full examples of spring, on the official docs of Svelte, where you can also play around with different values of stiffness and damping.

How to Animate Elements in Svelte By Stiffness and Damping
If you would like to see more Webtips, follow @flowforfrank

Looking into Svelteย 3
Looking to improve your skills? Learn how to build reactive apps with Svelte + Tailwind.
Master Svelte

Resources:

Did you find this page helpful?
๐Ÿ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundreds of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended