Giter Club home page Giter Club logo

svg-animation's Introduction

svgAnimation.js is a Snap.svg plugin used to create complex animations from simple SVGs

svgAnimation takes the complexity out of creating Snap.svg animations. Instead of animating in transform strings, callbacks, and milliseconds, svgAnimation allows us to use a simple JSON file to specify keyframes. With it, you can animate translate, rotate, and scaling transformations independently for any given element within an SVG.

example.gif

View this example live in Codepen

Installation

In your HTML file, import Snap, svgAnimation, and any easing libaries. Both the minified and uncompressed (for development) versions are provided.

<!-- Libraries -->
<script src="js/libs/snap.svg.min.js"></script>
<script src="js/libs/snap.svg.easing.min.js"></script>

<!-- Scripts -->
<script src="js/src/svgAnimation.min.js"></script>

Use

To instantiate a new svgAnimation, use the following snippet.

<script>
  (function() {
    var backpack = new svgAnimation({
      canvas:         new Snap('#canvas'),
      svg:            'svg/backpack.svg',
      data:           'json/backpack.json',
      duration:       2000,
      steps:          10
    });
  })();
</script>

Options

  • canvas: Defines the Snap canvas that we'll drawing to, specified by a DOM id
  • svg: Defines the path of the SVG file
  • data: Defines the path of the JSON file
  • duration: Defines the entire duration of an animation
  • steps: Defines the number of steps of an animation

JSON File

The JSON file will hold the keyframes of our animation. A breakdown of the zipper animation is provided as an example.

{
  "animations": [
    {
      "id": "#zipper",
      "keyframes": {
        "translateKeyframes": [
          {
            "step": 6,
            "x": 90,
            "y": 0
          },

          {
            "step": 9,
            "x": 0,
            "y": 0,
            "easing": "easeOutQuint"
          }
        ],

        "rotateKeyframes": [
          {
            "step": 4,
            "angle": 45,
            "cy": "top"
          },

          {
            "step": 6,
            "angle": -45,
            "easing": "easeOutBack"
          },

          {
            "step": 8,
            "angle": 30,
            "easing": "easeOutQuint"
          },

          {
            "step": 10,
            "angle": 0,
            "easing": "easeOutBack"
          }
        ],

        "scaleKeyframes": [
          {
            "step": 4,
            "x": 0,
            "y": 0,
            "cy": "top"
          },

          {
            "step": 6,
            "x": 1,
            "y": 1,
            "easing": "easeOutBack"
          }
        ]
      }
    }
  ]
}

To Do(s)

  • Make event driven
  • Add other SVG animatable properties (path animation, opacity, etc.)
  • Add playback functionality (back, forwards, loop, etc.)

Thanks

Thanks to Rey Bango, Chris Halaska, Matt Harwood, and Rhiana Chan for making this possible.

svg-animation's People

Contributors

hellomichael avatar

Watchers

James Cloos 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.