Coder Social home page Coder Social logo

use-posed-wave's Introduction

usePosedWave

A React hook that returns a "react-pose" svg path component animated like a wave.


Waves


What ?

Given a size and amplitude parameters, the hook returns an enhanced component and a "pose" string to pass it. By default, every 3s the wave goes down, morphs a little, then up - indefinitely. Combine multiple waves with different colors to get the gif's result!

Example

function App() {
  // Configure the wave animation and get the component to render.
  // Hint: You could pass the screen width as `x` and any height as `y`
  // to make a full screen width animation.
  const [Wave, wavePose]  = usePosedWave({ size: { x: 200, y: 50 , amplitude: 25 });
  const [Foam, foamPose]  = usePosedWave({ size: { x: 200, y: 50 , amplitude: 35, originOffset: 15 });

  // The component being a SVG path tag, it must be wrapped inside a <svg/>
  // component.
  return (
    <svg width={size.x} height={size.y * 2} style={{ background: "sandybrown" }} >
      {Foam && <Foam pose={foamPose} fill="white" />
      {Wave && <Wave pose={wavePose} fill="teal" />}
    </svg>
  )
}

⚠️ Note: The component will re-render if the "size" parameter changes. Either define it outside the component or with React.useState.

Install

Has react, react-pose and popmotion as --peer dependencies.

yarn add use-posed-wave

or

npm i use-posed-wave

API

const { usePosedWave } = require('use-posed-wave')

  • A react hook that retuns the SVG path component.
  • Takes the following arguments:
    • Required:

      • size: { x: number, y: number } The bounding rect of the animation.
      • amplitude: number The animation's y distance it grows (in pixel).
    • Optional:

      • optionalDuration: number || 3000 The duration for the wave to transit from one state to the other.
      • optionalOffset: number || 0 The y offset to add to the generated path.
      • optionalFade: boolean || false Should the wave fade in and out during the transition?

Todo

  • Make the initial delay optional
  • Return the path component only (without the pose string)
  • Make the wave orientation dynamic (vertical, horizontal)
  • Make it update on "amplitude" change

Related

  • useTimedIndex - Get an index value auto-incremented by 1 every X ms.

Acknowledgement

Without those libraries, this hook would not exists.

Created by

License

MIT

use-posed-wave's People

Contributors

nicobarray avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.