SVG-Morpheus
JavaScript library enabling SVG icons to morph from one to the other. It implements Material Design's Delightful Details transitions. (THIS PROJECT IS NOT MAINTAINED ANYMORE)
Top Related Projects
BonsaiJS is a graphics library and renderer
The JavaScript library for modern SVG graphics.
JavaScript library to make drawing animation on SVG
Tools for smoother shape animations.
JavaScript library for all kinds of color manipulations
GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web
Quick Overview
SVG-Morpheus is a JavaScript library that enables smooth transitions between SVG icons. It allows developers to morph one icon into another with fluid animations, enhancing the visual appeal and interactivity of web applications. The library is lightweight and easy to integrate into existing projects.
Pros
- Smooth and customizable transitions between SVG icons
- Lightweight and easy to integrate into existing projects
- Supports various easing functions for different animation styles
- Works with both inline SVG and external SVG files
Cons
- Limited to SVG icons only, not suitable for other types of animations
- Requires careful planning of SVG structures for optimal morphing results
- May have performance issues with complex SVG icons or on low-end devices
- Not actively maintained (last update was in 2017)
Code Examples
- Basic icon transition:
var svgMorpheus = new SVGMorpheus('#mySVG');
svgMorpheus.to('icon2', {duration: 1000, easing: 'quint-in'});
This code initializes SVG-Morpheus with an SVG element and morphs it to 'icon2' over 1 second with a quint-in easing.
- Chaining multiple transitions:
var svgMorpheus = new SVGMorpheus('#mySVG');
svgMorpheus.to('icon2', {duration: 500})
.to('icon3', {duration: 500, rotation: 'clockwise'})
.to('icon1', {duration: 500, rotation: 'random'});
This example chains multiple transitions, morphing from the initial state to 'icon2', then to 'icon3' with clockwise rotation, and finally back to 'icon1' with random rotation.
- Using callbacks:
var svgMorpheus = new SVGMorpheus('#mySVG');
svgMorpheus.to('icon2', {
duration: 1000,
easing: 'bounce-out',
rotation: 'none',
callback: function() {
console.log('Transition complete!');
}
});
This code demonstrates the use of a callback function that is executed when the transition is complete.
Getting Started
- Include the SVG-Morpheus library in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/SVG-Morpheus/0.3.2/svg-morpheus.js"></script>
- Create an SVG element with multiple icons:
<svg id="mySVG" style="display: none;">
<symbol id="icon1"><!-- SVG path data for icon1 --></symbol>
<symbol id="icon2"><!-- SVG path data for icon2 --></symbol>
</svg>
- Initialize SVG-Morpheus and trigger a transition:
var svgMorpheus = new SVGMorpheus('#mySVG');
svgMorpheus.to('icon2', {duration: 1000});
This will morph the SVG from its initial state to 'icon2' over 1 second.
Competitor Comparisons
BonsaiJS is a graphics library and renderer
Pros of Bonsai
- More comprehensive library for creating and animating graphics
- Supports both SVG and Canvas rendering
- Offers a wider range of animation and interaction capabilities
Cons of Bonsai
- Larger file size and potentially more complex to use
- Less focused on specific SVG morphing animations
- May have a steeper learning curve for beginners
Code Comparison
SVG-Morpheus:
var myIcons = new SVGMorpheus('#myIconElement');
myIcons.to('icon2', {duration: 1000, easing: 'quint-in'});
Bonsai:
bonsai.run(function() {
var shape = new Circle(100, 100, 50).addTo(stage);
shape.animate('1s', {radius: 100, fill: 'red'});
});
SVG-Morpheus is more focused on morphing between SVG icons, while Bonsai provides a broader set of tools for creating and animating various graphics. SVG-Morpheus has a simpler API for icon transitions, whereas Bonsai offers more flexibility but requires more code for similar effects. Bonsai's ability to work with both SVG and Canvas makes it more versatile, but SVG-Morpheus may be more suitable for projects specifically requiring SVG icon morphing.
The JavaScript library for modern SVG graphics.
Pros of Snap.svg
- More comprehensive SVG manipulation library with a wider range of features
- Better browser compatibility, including support for older versions of Internet Explorer
- Larger community and more frequent updates
Cons of Snap.svg
- Larger file size, which may impact page load times
- Steeper learning curve due to its extensive API
- May be overkill for simple SVG morphing tasks
Code Comparison
SVG-Morpheus:
var myIcons = new SVGMorpheus('#mySVG');
myIcons.to('icon2', {duration: 1000, easing: 'quint-in'});
Snap.svg:
var s = Snap("#svg");
var path1 = s.path("M0,0L10,10");
var path2 = s.path("M0,10L10,0");
path1.animate({ d: path2.attr("d") }, 1000, mina.easeinout);
Both libraries allow for SVG manipulation and animation, but Snap.svg offers more granular control over individual SVG elements. SVG-Morpheus is specifically designed for morphing between SVG icons, making it simpler for this particular use case, while Snap.svg provides a more comprehensive toolkit for general SVG manipulation and animation.
JavaScript library to make drawing animation on SVG
Pros of Vivus
- Lightweight and simple to use
- Supports multiple animation types (delayed, sync, oneByOne, etc.)
- Allows for custom timing functions
Cons of Vivus
- Limited to animating SVG strokes only
- Lacks advanced morphing capabilities
- Fewer customization options for complex animations
Code Comparison
SVG-Morpheus:
var myIcons = new SVGMorpheus('#mySVG');
myIcons.to('icon2', {duration: 1000, easing: 'quint-in'});
Vivus:
new Vivus('mySVG', {duration: 200, type: 'delayed'});
Key Differences
SVG-Morpheus focuses on morphing between different SVG shapes, offering more complex transformation capabilities. It's ideal for transitioning between icons or creating intricate animations.
Vivus specializes in animating SVG strokes, making it perfect for creating drawing effects or simple line animations. It's more lightweight and easier to implement for basic SVG animations.
Both libraries have their strengths, and the choice between them depends on the specific animation requirements of your project. SVG-Morpheus is better suited for complex shape transformations, while Vivus excels at simple, stroke-based animations.
Tools for smoother shape animations.
Pros of Flubber
- More advanced interpolation algorithms for smoother transitions
- Supports interpolation between paths with different numbers of points
- Offers additional utility functions for shape manipulation
Cons of Flubber
- Larger file size and potentially higher performance overhead
- Steeper learning curve due to more complex API
Code Comparison
SVG-Morpheus:
var myMorpheus = new SVGMorpheus('#mysvg');
myMorpheus.to('icon2', {duration: 1000, easing: 'quint-in'});
Flubber:
var interpolator = flubber.interpolate(path1, path2);
d3.select("path")
.transition()
.attrTween("d", function() { return interpolator; });
Summary
Flubber offers more advanced interpolation capabilities and additional utilities, making it suitable for complex morphing scenarios. However, it comes with a larger file size and potentially higher performance overhead. SVG-Morpheus provides a simpler API and lighter footprint, which may be preferable for basic morphing needs. The choice between the two depends on the specific requirements of the project, balancing between advanced features and simplicity.
JavaScript library for all kinds of color manipulations
Pros of chroma.js
- Focused on color manipulation and generation, offering a wide range of color-related functions
- Supports various color spaces and provides color scaling capabilities
- Actively maintained with regular updates and a large community
Cons of chroma.js
- Limited to color-related operations, not designed for SVG manipulation or animation
- May require additional libraries for complex visualizations or animations
- Steeper learning curve for users primarily interested in basic color operations
Code Comparison
SVG-Morpheus (SVG icon animation):
var myIcons = new SVGMorpheus('#myIconElement');
myIcons.to('icon2', {duration: 1000, rotation: 'clockwise'});
chroma.js (Color manipulation):
var color = chroma('#D4F880');
var darkerColor = color.darken();
var scale = chroma.scale(['#fafa6e', '#2A4858']).mode('lch').colors(6);
Summary
While SVG-Morpheus specializes in SVG icon animations and transformations, chroma.js focuses on color manipulation and generation. SVG-Morpheus is ideal for creating smooth transitions between SVG icons, whereas chroma.js excels in color-related tasks such as creating color scales, adjusting color properties, and working with various color spaces. The choice between these libraries depends on the specific requirements of your project, whether it's primarily focused on SVG animations or color manipulations.
GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web
Pros of GSAP
- More comprehensive animation capabilities, including complex timelines and 3D transformations
- Larger community and ecosystem, with extensive documentation and plugins
- Better performance, especially for complex animations
Cons of GSAP
- Steeper learning curve due to its extensive feature set
- Larger file size, which may impact load times for smaller projects
- Commercial license required for some advanced features
Code Comparison
SVG-Morpheus:
var myIcons = new SVGMorpheus('#myIconElement');
myIcons.to('icon2', {duration: 1000, rotation: 'clock'});
GSAP:
gsap.to("#myIconElement", {
duration: 1,
morphSVG: "icon2",
rotation: 360
});
Summary
GSAP offers more powerful animation capabilities and better performance, making it suitable for complex projects. However, it comes with a steeper learning curve and potential licensing costs. SVG-Morpheus is simpler and focused specifically on SVG morphing, which may be preferable for smaller projects or those primarily dealing with icon transitions. The code comparison shows that both libraries offer straightforward syntax for basic morphing animations, with GSAP providing more granular control over animation properties.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
SVG Morpheus
WARNING
THIS PROJECT IS NOT MAINTAINED ANYMORE. You are free to fork it and start a new project.
Overview
JavaScript library enabling SVG icons to morph from one to the other. It implements Material Design's Delightful Details transitions.
Live demo
Installing
Using SVG Morpheus with a CDN
CDN provided by cdnjs
<script src="//cdnjs.cloudflare.com/ajax/libs/SVG-Morpheus/0.3.2/svg-morpheus.js"></script>
Download
You have the following options to install the library:
- Download SVG Morpheus from GitHub.
- Use NPM
npm install svg-morpheus
. - Use Bower
bower install svg-morpheus
.
Add Script
Simply add the svg-morpheus.js script to your website/application. No other scripts are needed. Both the minified and uncompressed (for development) versions are in the /compile folder.
<script src="svg-morpheus.js"></script>
Usage
- Add an icon set SVG to the HTML file where you want to show the morphing icon.
- Create a SVG Morpheus instance for the icon set by calling
new SVGMorpheus(element)
. Object/IFrame/Inline SVG element containing the icon set. Can be a DOM element or a CSS query selector.. For example:
var myIcons = new SVGMorpheus('#myIconSet');
- After initializing, you get a SVGMorpheus object having
to(ID)
function. ID is an id of Icon in the icon set. Use it to morph the icon to another icon in the icon set.
myIcons.to('icon1');
Functions
SVGMorpheus Constructor
Creates a SVGMorpheus instance.
var myIcons = new SVGMorpheus(element, options, callback);
element - Object/IFrame/SVG element containing an icon set. Can be a DOM element or a CSS query selector.
options - Optional. Object specifying default options.
options.iconId - Optional. Id of an icon shown after initialization. Default: last icon in the icon set.
options.duration - Optional. Set a default duration for transition animations, in msec. Default: 750.
options.easing - Optional. Set a default easing for transition animations. Default: quad-in-out.
options.rotation - Optional. Set a default rotation for icon shapes. clock
= clockwise, counterclock
= counterclockwise, random
= randomly set clock/counterclock, none
= no rotation. Default: clock.
callback - Optional. Set a default callback function to call at the animation end.
SVGMorpheus.to()
Morphs the icon to another one.
myIcons.to(iconId, options, callback);
iconId - Id of an icon to transition to.
options - Optional. Object specifying the animation options.
options.duration - Optional. Set a duration for the animation, in msec.
options.easing - Optional. Set an easing for the animation.
options.rotation - Optional. Set a rotation for icon shapes. clock
= clockwise, counterclock
= counterclockwise, random
= randomly set clock/counterclock, none
= no rotation.
callback - Optional. Set a callback function to call at the animation end.
SVGMorpheus.registerEasing()
Registers a custom easing function. SVGMorpheus has a set of predefined easing functions for the morph animation (goes below). If you want to use your own easing, use this function to do that.
myIcons.registerEasing(name, fn);
name - Name of an easing function you want to register.
fn - Easing function. function easing(timing)
gets a float 0<=timing<=1
argument as an input, and outputs float 0<=progress<=1
.
Predefined easing functions
circ-in
, circ-out
, circ-in-out
, cubic-in
, cubic-out
, cubic-in-out
, elastic-in
, elastic-out
, elastic-in-out
, expo-in
, expo-out
, expo-in-out
, linear
, quad-in
, quad-out
, quad-in-out
, quart-in
, quart-out
, quart-in-out
, quint-in
, quint-out
, quint-in-out
, sine-in
, sine-out
, sine-in-out
Icon Set structure
SVG should have the following structure to be a valid icon set:
- 1st tier nodes are
<g>
elements having 'id' attribute. They define icons in the icon set. - 2nd tier nodes are shape elements (
<path>
,circle
,rect
,ellipse
,polygon
,line
). They define the icon graphics.
<svg>
<g id="icon1">
Shape elements
</g>
<g id="icon2">
Shape elements
</g>
</svg>
Example code
Check the Demos directory for examples.
Supported browsers
Chrome Firefox IE >= 10 Safari Opera iOS Safari Android Browser >= 4.4 Chrome for Android
License
See the LICENSE file.
Top Related Projects
BonsaiJS is a graphics library and renderer
The JavaScript library for modern SVG graphics.
JavaScript library to make drawing animation on SVG
Tools for smoother shape animations.
JavaScript library for all kinds of color manipulations
GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot