Docs/Primitives/createSpring

createSpring

createSpring returns a new MotionValue that follows its source with spring physics. Pass a MotionValue to track it, or pass a number to create a writable spring you can drive with .set().

import { createMotionValue, createSpring, motion } from 'motion-solidjs'

const x = createMotionValue(0)
const springX = createSpring(x, { stiffness: 200, damping: 22 })

return <motion.div style={{ x: springX }} />
createSpring
move pointer
create-spring.tsxOpen ↗
import { motion, createMotionValue, createSpring, createTransform } from 'motion-solidjs'
import { createSignal, onCleanup, onMount } from 'solid-js'

export const meta = {
  slug: 'create-spring',
  title: 'createSpring',
  category: 'motion-values',
  description: 'Smooth any motion value with a spring — fingers track lag.',
  tag: 'createSpring',
} as const

export default function UseSpringExample() {
  const x = createMotionValue(0)
  const y = createMotionValue(0)
  const springX = createSpring(x, { stiffness: 200, damping: 22 })
  const springY = createSpring(y, { stiffness: 200, damping: 22 })

  const [box, setBox] = createSignal<HTMLDivElement | null>(null)

  onMount(() => {
    const el = box()
    if (!el) return
    const handle = (e: PointerEvent) => {
      const rect = el.getBoundingClientRect()
      x.set(e.clientX - rect.left - rect.width / 2)
      y.set(e.clientY - rect.top - rect.height / 2)
    }
    el.addEventListener('pointermove', handle)
    onCleanup(() => el.removeEventListener('pointermove', handle))
  })

  return (
    <div
      ref={setBox}
      class="relative grid h-56 w-56 place-items-center rounded-3xl border border-border bg-card text-xs text-fg-dim"
    >
      <span>move pointer</span>
      <motion.span
        class="pointer-events-none absolute h-12 w-12 rounded-full bg-grad-rose shadow-glow"
        style={{ x: springX, y: springY }}
      />
    </div>
  )
}

Options

  • stiffness - spring stiffness (default 100)
  • damping - damping coefficient (default 10)
  • mass - particle mass (default 1)
  • velocity - initial velocity
  • restSpeed, restDelta - stop conditions
  • bounce, duration - alternative parameterization