You're reading the v2 beta docs. For the stable release, switch to v1 →
TakumiTakumi

Images & emoji

Load images, cache resources, and render emoji.

External Images

Takumi fetches external images in src attributes and CSS background-image / mask-image.

Add a caching layer by passing resourcesOptions.cache to render.

import {  } from "takumi-js";

const  = < ="https://example.com/image.png" />;

const  = new <string, ArrayBuffer>();

const  = await (, {
  : {
    ,
  },
});

Pre-fetched Images

Provide images by key so the renderer doesn't have to fetch them itself. Each key can be used in any src field or in the background-image / mask-image CSS properties.

import {  } from "takumi-js/response";

export function () {
  return new (< />, {
    : [
      {
        : "my-logo",
        : () => ("/logo.png").(() => .()),
      },
      {
        : "background",
        : () => ("/background.png").(() => .()),
      },
    ],
  });
}

function () {
  return (
    <
      ={{
        : "url(background)",
      }}
    >
      < ="my-logo" />
    </>
  );
}

Decode caching

Takumi caches each decoded image so a repeated src is decoded once. Two independent layers exist:

LayerStoresSet with
Byte cacheFetched bytesresourcesOptions.cache
Decode cacheDecoded pixelsper-image cache

The per-image cache mode takes two values:

  • auto (default): cache the decoded image; the entry is evictable.
  • none: skip the cache. Use it for a one-off image you won't render again, to avoid holding its pixels in memory.
import {  } from "takumi-js/response";

export function () {
  return new (< />, {
    : [
      {
        : "banner",
        : () => ("/banner.png").(() => .()),
        : "none", 
      },
    ],
  });
}

Emoji

Dynamic fetching

ImageResponse accepts a satori-compatible emoji option. The providers are twemoji, blobmoji, noto, openmoji, fluent, and fluentFlat.

import {  } from "takumi-js/response";

export function () {
  return new (< ="flex justify-center items-center text-3xl">Hello 👋😁</>, {
    : "twemoji", 
  });
}

Manual extraction

ImageResponse calls the extractEmojis helper for you. The helper splits emoji from text into image nodes pointing at a CDN. Run the steps yourself to fetch emoji bytes alongside other resources.

import {  } from "takumi-js/helpers/emoji";
import {  } from "takumi-js/helpers/jsx";
import { ,  } from "takumi-js/helpers";
import {  } from "takumi-js/node";

let {  } = await (< ="flex justify-center items-center text-3xl">Hello 👋😁</>);
 = (, "twemoji");

const  = ();
const  = await ();

const  = new ();

const  = await .(, {
  ,
});

COLR / bitmap fonts

Takumi renders COLR (Color Layer) fonts, the format behind packs like Twemoji-COLR. A COLR file is much smaller than a bitmap emoji font like Noto Color Emoji. The renderer draws emoji from the loaded glyphs, with no network request.

Two steps:

  • Register the font through fonts.
  • Set emoji: "from-font".
Edit on GitHub

Last updated on

On this page