GitHub repository

Sine Wave Animations

Sine wave animations create smooth, periodic motion perfect for creating oscillating, rhythmic, and cyclical animation behavior.

  • 🐦️ Smooth oscillating motion
  • 🐦️ Precise frequency control
  • 🐦️ Perfect for looping animations
  • 🐦️ Real-time parameter updates

Basic Usage Create a simple oscillating animation using createSineWave:

import { createSineWave } from 'ararajs'
function OscillatingBox() {
  const [motion] = createSineWave({
    amplitude: 50,    // Oscillates 50px in each direction
    frequency: 0.5,   // Complete cycle every 2 seconds
    offset: 100      // Center of oscillation at 100px
  })

  return (
    <div style={{
        transform: `translateX(${motion.position}px)`
      }}>
      🔵
    </div>
  )

}

Configuration Options Sine waves can be customized through several parameters:

type SineWaveOptions = {
  offset: number    // Center position of the oscillation (default: 0)
  frequency: number // Oscillations per second (default: 1)
  amplitude: number // Size of the oscillation (default: 1)
  phase: number     // Starting point in the cycle in radians (default: 0) (2 * Math.PI is 1 turn)
}

Understanding Parameters

Offset - The center point around which the oscillation occurs Frequency - How many complete cycles occur per second Amplitude - How far the motion deviates from the offset Phase - Where in the cycle the animation starts (in radians)

Visual Parameter Guide Here’s how different parameters affect the motion:

// Center oscillation
const centered = {
  offset: 0,
  amplitude: 50,
  frequency: 1
}
// Shifted right
const shifted = {
  offset: 100,  // Moves entire oscillation right
  amplitude: 50,
  frequency: 1
}
// Fast, small oscillation
const fast = {
  offset: 0,
  amplitude: 10,  // Smaller movement
  frequency: 4    // 4 cycles per second
}
// Slow, wide oscillation
const wide = {
  offset: 0,
  amplitude: 200, // Large movement
  frequency: 0.2  // One cycle every 5 seconds
}

Example: Sine Wave 2D Section titled Example: Sine Wave 2D

Reactive Parameters Sine waves can respond to changing parameters in real-time:

function ReactiveOscillation() {
    const [frequency, setFrequency] = createSignal(1)
    const [motion] = createSineWave(() => ({
        frequency: frequency(),
        amplitude: 50
    }))

    return (
      <>
        <input
          type="range"
          min="0.1"
          max="5"
          step="0.1"
          value={frequency()}
          onInput={(e) => setFrequency(Number(e.target.value))}
        />
          <div style={{
            transform: `translateX(${motion.position}px)`
          }}>
          🔵
          </div>
      </>
    )
}