Zen garden representing Satori's HTML to SVG transformation

Satori: Illuminating the Path from HTML to SVG

The Gray Cat
The Gray Cat

Satori, aptly named after the Zen Buddhist term for enlightenment, is a powerful library that illuminates the path from HTML and CSS to SVG. This innovative tool offers developers a straightforward way to generate dynamic images for various use cases, including Open Graph images and social cards. By leveraging the familiar syntax of JSX, Satori makes the process of creating SVGs from HTML and CSS both intuitive and efficient.

Enlightening Features

Satori comes packed with a range of features that make it a standout choice for developers:

  • JSX Support: Write your layouts using the familiar JSX syntax, making it easy to create complex designs.
  • CSS Subset Implementation: Utilize a comprehensive subset of CSS properties, including flexbox layouts.
  • Font Handling: Support for TTF, OTF, and WOFF font formats, with the ability to embed fonts directly in the SVG.
  • Image Embedding: Easily include images in your designs, with support for various formats and sizing options.
  • Emoji Support: Render custom images for specific graphemes, perfect for including emojis in your generated images.
  • Locale Support: Render text in different locales, ensuring proper character representation across languages.
  • Debug Mode: Visualize bounding boxes to fine-tune your layouts.

Installation and Setup

To begin your journey with Satori, you’ll need to install it in your project. You can do this using npm or yarn:

npm install satori
# or
yarn add satori

Basic Usage: Crafting Your First SVG

Let’s dive into how you can use Satori to create your first SVG. The basic usage is straightforward and resembles writing a React component:

import satori from 'satori';

const svg = await satori(
  <div style={{ color: 'black' }}>Hello, Satori!</div>,
  {
    width: 600,
    height: 400,
    fonts: [
      {
        name: 'Roboto',
        data: robotoArrayBuffer,
        weight: 400,
        style: 'normal',
      },
    ],
  }
);

console.log(svg);
// Output: '<svg ...><path d="..." fill="black"></path></svg>'

In this example, we’re creating a simple div with black text. Satori takes this JSX element and the configuration options, then returns an SVG string.

Advanced Techniques: Mastering Satori’s Capabilities

Styling with CSS

Satori supports a subset of CSS properties, allowing you to style your elements much like you would in a web application. Here’s an example showcasing some supported properties:

const svg = await satori(
  <div style={{
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#f0f0f0',
    width: '100%',
    height: '100%',
    borderRadius: '15px',
    fontFamily: 'Roboto',
    color: '#333',
  }}>
    <h1 style={{ fontSize: '24px', marginBottom: '10px' }}>Welcome to Satori</h1>
    <p style={{ fontSize: '16px', textAlign: 'center' }}>Create beautiful SVGs with ease!</p>
  </div>,
  {
    width: 600,
    height: 400,
    fonts: [
      {
        name: 'Roboto',
        data: robotoArrayBuffer,
        weight: 400,
        style: 'normal',
      },
    ],
  }
);

This example creates a centered layout with a title and description, demonstrating flexbox, text styling, and background styling.

Working with Images

Satori allows you to include images in your SVGs. Here’s how you can embed an image:

const svg = await satori(
  <div style={{ width: '100%', height: '100%', position: 'relative' }}>
    <img
      src="data:image/png;base64,..."
      style={{
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%',
        height: '100%',
        objectFit: 'cover',
      }}
    />
    <h1 style={{
      position: 'absolute',
      top: '50%',
      left: '50%',
      transform: 'translate(-50%, -50%)',
      color: 'white',
      textShadow: '2px 2px 4px rgba(0,0,0,0.5)',
    }}>
      Overlay Text
    </h1>
  </div>,
  {
    width: 600,
    height: 400,
    fonts: [/* ... */],
  }
);

This example overlays text on an image, demonstrating how to work with multiple layers and positioning in Satori.

Handling Emojis and Special Characters

Satori provides a way to handle emojis and special characters through the graphemeImages option:

const svg = await satori(
  <div>Satori is 🚀!</div>,
  {
    width: 600,
    height: 400,
    fonts: [/* ... */],
    graphemeImages: {
      '🚀': 'data:image/svg+xml;base64,...', // Base64 encoded SVG of a rocket
    },
  }
);

This allows you to provide custom images for specific characters, ensuring they render correctly in your SVG.

Optimizing Performance

When working with Satori, especially for generating multiple images, it’s important to optimize for performance. Here are a few tips:

  1. Reuse Font Definitions: Define your fonts once and reuse them across multiple Satori calls.

  2. Use WASM for Better Performance: In browser environments, you can use the WASM version of Satori for improved performance:

import satori, { init } from 'satori/wasm';
import initYoga from 'yoga-wasm-web';

const yoga = initYoga(await fetch('/yoga.wasm').then(res => res.arrayBuffer()));
init(yoga);

// Now use satori as normal
const svg = await satori(/* ... */);
  1. Optimize Image Usage: When including images, use appropriately sized and optimized images to reduce processing time.

Conclusion: Illuminating the Future of Dynamic Image Generation

Satori brings the power of HTML and CSS to SVG generation, opening up new possibilities for creating dynamic, scalable images. Whether you’re generating Open Graph images, creating social media cards, or building any other type of dynamic visual content, Satori provides a flexible and powerful solution.

By mastering Satori, you can streamline your image generation workflow, create more engaging visual content, and enhance your application’s visual appeal. As you continue to explore Satori’s capabilities, you’ll find it to be an invaluable tool in your development toolkit.

For more insights into working with React and SVG, you might find our articles on React SVG and React Icons helpful companions to your Satori journey. Happy coding, and may your SVGs be ever enlightened!