Morph
Morph any SVG into another without the spikes: icons, filled shapes, words, one shape into several.
npm install @cesarvenzac/morph// One drawing through every kind of source Morph takes. Every side
// is read into the same shape of thing, so any two morph.
import { morph, svg, text } from "@cesarvenzac/morph";
import { MorphFrame } from "@cesarvenzac/morph/react";
const reel = [
svg(lucideStar), // an icon, written as SVG
svg(heart), // markup with its own paint
text(font, "Morph"), // a word, in a font opentype.js read
svg(sun), // a disc filled, its rays stroked
svg(moon),
];
// Each into the next: split divides one shape among several.
const m = morph(reel[k], reel[k + 1], { extra: "split" });
<MorphFrame morph={m} t={t} />;
Morph turns one SVG drawing into another: icons, filled shapes in their colours, words set in a font, one shape into several. Most morphs look generated, for the same few reasons: points paired by accident, moved in straight lines, corners snapping in. Morph pairs shapes by what they look like, moves each one whole, rounds it halfway and draws it as a few smooth curves, so every frame between two drawings looks drawn.
Use
import { MorphIcon } from "@cesarvenzac/morph/react";
import { Menu, X } from "lucide";
<button
onClick={() => setOpen(!open)}
aria-label={open ? "Close menu" : "Open menu"}
>
<MorphIcon icon={open ? X : Menu} />
</button>;Or draw any moment yourself:
import { morph, svg } from "@cesarvenzac/morph";
const m = morph(svg(heart), svg(star)); // matched once
m.at(0.5); // the frame halfway, a path a partWhat it does
- Any SVG, either side. Lucide's icons as they are, any markup, words set in a font, drawings made by hand; the two needn't match.
- Shapes paired by shape. Pieces pair by what they look like as well as where they are, and holes open and close where they should.
- More shapes on one side. One shape splits into several, or several merge into one, rather than popping in.
- Paint that mixes. Colours mix in OKLab as CSS's
color-mix()does, widths and opacity follow, and a stroke can fill in. - Plays like an interface. A spring that turns back from where it is when it's sent back mid-way:
MorphIconin React,mountin the page,motionanywhere. - Small and plain. TypeScript with no dependencies, its core needing no browser: a frame is path data, for an
<svg>, a canvas or a server.
How it works, measured, is in Morphs without spikes, and the three ways side by side in Icon morphs. The examples use Lucide's icons.
In use on Brume, where every icon that changes is a morph: a call to action turning into what comes next, a download into a tick.
Docs
- Getting started
Install Morph, make a morph, and draw any moment of it: in React, in the page, or on a server.
- What it morphs
Icons, any SVG markup, words set in a font, or drawings made by hand: either side can be any of them, and the two needn't match.
- Shapes and colours
How pieces pair, how holes open and close, what becomes of the shapes one side has more of, and how colours mix.
- Playing it
Spring from one drawing to the next when something changes: MorphIcon in React, mount in the page, motion for any other clock.
- Ways to morph
Soft, even and index, the three ways side by side, the four steps that make soft what it is, and a frame as anchors and handles.
- API reference
Every function and type the package exports, entry by entry: the core, React and the DOM.