Takumi

Output formats

Choose PNG, JPEG, WebP, ICO, raw pixels, or SVG output and set its quality, resolution, and cancellation.

render() returns encoded bytes, and the format option chooses the encoder.

Picking a format

Choose an image encoder
import {  } from "takumi-js";

const  = < ="w-full h-full grid place-items-center bg-white">Hello Takumi</>;

const  = await (, { : 1200, : 630, : "png" });
const  = await (, { : 1200, : 630, : "jpeg", : 80 });
const  = await (, { : 1200, : 630, : "webp", : 80 });
FormatAlphaqualitylosslessNotes
png (default)YesNoNoLossless. Use it for transparency or exact pixels.
jpegNoYesNoLossy. Draw an opaque background, since JPEG has no alpha channel.
webpYesYesYesLossless unless you set quality. lossless: true wins.
icoYesNoNoFavicon file. At most 256 × 256 pixels.
rawYesNoNoUncompressed RGBA bytes, width × height × 4.

quality runs from 0 to 100 and defaults to 75. Formats marked No in the quality column ignore it.

On the WebAssembly backend, WebP is always lossless. quality and lossless are accepted but have no effect on WebP. quality still applies to JPEG.

Raw frames

format: "raw" skips encoding and returns the RGBA buffer, which you can feed to ffmpeg or another encoder. The Animation page streams raw frames into ffmpeg to make a video.

Device pixel ratio

devicePixelRatio sets how many image pixels each CSS pixel takes. width and height stay the image size in pixels. To render a 1200 × 630 design at twice the resolution, double the size and set the ratio to 2:

Render at twice the resolution
import {  } from "takumi-js";

const  = await (< ="w-full h-full text-6xl">Hello Takumi</>, {
  : 2400,
  : 1260,
  : 2, 
});

The result is a 2400 × 1260 image. Every CSS length draws twice as large, so the layout matches a 1200 × 630 render at ratio 1.

With width: 1200 and height: 630 at ratio 2, the image stays 1200 × 630 and the layout gets only 600 × 315 CSS pixels.

Dithering

dithering adds ordered Bayer noise to gradient fills before they drop to 8-bit color, which removes visible banding. It applies to render(), including raw output. It accepts these values:

  • none (default)
  • ordered-bayer
Reduce gradient banding
import {  } from "takumi-js";

const  = await (< ="w-full h-full bg-linear-to-r from-slate-900 to-slate-700" />, {
  : 1200,
  : 630,
  : "ordered-bayer", 
});

Animated output

renderAnimation() encodes a sequence of scenes as WebP, APNG, or GIF. See Animation.

Vector SVG

renderSvg() returns an SVG document string instead of a bitmap. It draws real <rect>, <path>, gradients, glyph outlines, and embedded images from the same layout.

Render PNG and SVG
import { ,  } from "takumi-js";

const  = <>Hello Takumi</>;

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

SVG is a vector format, so these raster options do not apply to it:

  • format
  • quality
  • lossless
  • dithering
  • drawDebugBorder
  • devicePixelRatio

Cancellation

Pass an AbortSignal as signal to cancel a render. Takumi checks it before work starts and again after fonts and images load. An aborted signal stops the render with the signal's reason, and it also cancels image fetches that are still running.

Cancel with the request
import {  } from "takumi-js/response";

export function (: ) {
  return new (<>Hello Takumi</>, {
    : 1200,
    : 630,
    : ., 
  });
}

Last updated on