# Morph

> Morph any SVG into another without the spikes: icons, filled shapes, words, one shape into several.

Source: https://cesarvenzac.com/morph

Package: [@cesarvenzac/morph](https://www.npmjs.com/package/@cesarvenzac/morph) 1.0.3 on npm, MIT licence. Source code: https://github.com/cesarvenzac/morph

```sh
npm install @cesarvenzac/morph
```

*Live example on the page: Every kind. One drawing through every kind of source Morph takes: a Lucide icon, SVG markup with its paint, a word set in Inter. The heart breaks into the word's letters, and they merge into the sun.* Its source:

```tsx
// 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

```tsx
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:

```typescript
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 part
```

## What 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: `MorphIcon` in React, `mount` in the page, `motion` anywhere.
- **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](https://cesarvenzac.com/blog/morphs-without-spikes), and the three ways side by side in [Icon morphs](https://cesarvenzac.com/lab/icon-morphs). The examples use [Lucide](https://lucide.dev)'s icons.

In use on [Brume](https://brume.tools), 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](https://cesarvenzac.com/morph/docs/getting-started.md): Install Morph, make a morph, and draw any moment of it: in React, in the page, or on a server.
- [What it morphs](https://cesarvenzac.com/morph/docs/sources.md): 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](https://cesarvenzac.com/morph/docs/shapes.md): How pieces pair, how holes open and close, what becomes of the shapes one side has more of, and how colours mix.
- [Playing it](https://cesarvenzac.com/morph/docs/playing.md): 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](https://cesarvenzac.com/morph/docs/ways.md): 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](https://cesarvenzac.com/morph/docs/api.md): Every function and type the package exports, entry by entry: the core, React and the DOM.
