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 β€’ Read time 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:

Copied to clipboard!
<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

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

Resources:

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.