npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@plastikmaykr/text-shaper

v0.1.0

Published

Calculate text layout within shapes

Readme

TextShaper

TextShaper is a lightweight, framework-agnostic JavaScript library that calculates precise text layouts within a circular (for now) container. It provides the text lines layout data for you to render using your preferred method (HTML, SVG, Canvas, etc.). The core layout logic is also exposed to enable users to create custom formatting functions if needed.

Table of Contents

Installation

Install TextShaper via npm:

npm install @plastikmaykr/text-shaper

Basic Usage

TextShaper provides both a low-level API and a set of built-in convenience methods. For most use cases, you’ll likely use one of convenience methods for your rendering context of choice.

Convenience Method

The textShaper.svg(text, options) method internally calls the raw layout function and then formats the output for SVG rendering. This design allows you to either use the built-in formatters or create your own if you prefer.

import { textShaper } from '@plastikmaykr/text-shaper';

const text = 'The quick brown fox jumps over the lazy dog.';
const options = {
  // Layout container options (currently only circles are supported)
  size: 200,                 // Diameter of the circle in pixels

  // SizeRange: pass a number
  // or an object with value and an optional minimum value.
  fontSize: { value: 18, min: 12 },

  // Line height options: similarly, a number or { value, min }
  lineHeight: 1.2,

  fontFamily: 'Arial, sans-serif',

  // Ellipsis enabled if text doesn't fit,
  // "…" is the default, but can also be set to a custom string
  ellipsis: true,

  // Coordinate system and alignment settings
  origin: 'center',          // Options: 'center' or 'topleft'
  verticalAlign: 'middle',   // Options: 'top', 'middle', 'bottom'
  horizontalAlign: 'center', // Options: 'start', 'center', 'end'
  textBaseline: 'middle',    // Options: 'top', 'middle', 'bottom'
  textAlign: 'center',       // Options: 'start', 'center', 'end'
};

const svgOutput = textShaper.svg(text, options);

Example output of textShaper.svg() would look like this:

{
  lines: [
    {
      text: 'The quick brown',
      attrs: {
          x: 25,
          y: 35,
          'font-size': 16,
          'font-family': 'Arial, sans-serif',
          'text-anchor': 'middle',
          'dominant-baseline': 'middle',
      }
    },
    // more lines
  ],
  meta: {
    fontSize: 16,
    fontFamily: 'Arial, sans-serif',
    lineHeight: 1.2,
    textAnchor: 'middle',
    dominantBaseline: 'middle',
    overflowed: false,
    modified: true,
    text: {
      full: 'The quick brown fox jumps over the lazy dog.',
      leftover: ''
    }
  }
}

Rendering Example (SVG)

** COMING SOON **

API Reference

textShaperRaw

Lays out text within a circle and returns the raw positioning data.

  • Parameters:
    • text (string): The text to lay out.
    • options (TextShaperOptions): Configuration options (see below).
  • Returns: A TextShaperResultRaw object.

Convenience Methods

The library provides a namespace textShaper with convenience methods:

  • textShaper.svg(text, options): Formats text for SVG rendering.
  • textShaper.html(text, options): Formats text for HTML rendering.
  • textShaper.ctx(text, options): Formats text for Canvas rendering.

These methods are built by combining textShaperRaw with the corresponding formatter function. They serve most users’ needs and ensure consistency while still leaving the door open for custom formatters.

TextShaperOptions

The options object allows you to control layout behavior, numeric values are supposed to be set in pixels:

  • size (number): Diameter of the circle.
  • margin (number): Offset from container's edges.
  • fontSize (number | SizeRange):
    • If a number or { value:number } is provided, the text will be truncated if it doesn’t fit.
    • Providing { value, min } enables the library to shrink the text down to the minimum if needed, before any trucation is applied.
  • lineHeight (number | SizeRange): Works like fontSize.
  • fontFamily (string): Font family to use.
  • origin ("center" | "topleft"): The coordinate system for output.
  • verticalAlign ("top" | "middle" | "bottom"): Vertical text alignment.
  • horizontalAlign ("start" | "center" | "end"): Horizontal text alignment.
  • textBaseline ("top" | "middle" | "bottom"): For SVG and Canvas formatters/renderers.
  • textAlign ("start" | "center" | "end"): For SVG and Canvas formatters/renderers.

TextShaperResultRaw

The raw output returned by textShaperRaw has the following structure:

interface TextLineRaw {
  text: string;
  x: number;
  y: number;
  width: number;
  height: number;
}

interface TextShaperResultRaw {
  lines: TextLineRaw[];
  meta: {
    fontFamily: string;
    fontSize: number;
    lineHeight: number;
    overflowed: boolean;
    modified: boolean;
    origin: 'topleft' | 'center';
    text: {
      full: string;
      leftover: string;
    };
  };
}

This raw output is meant to be transformed by the built-in or custom formatting function into formats suitable for specific rendering context (SVG, HTML, Canvas).

Roadmap

  • Formatters documentation: Documentation on creating custom formatters.
  • Rendering Examples: Demos with code snippets for HTML, SVG and Canvas integrations.
  • Additional Shapes: Support for rectangles (also with rounded corners) and custom shapes.

Contributing

Contributions in the form of discussions to improve this project are welcome. If you have ideas or suggestions please open an issue to start a conversation. Here are some areas where your input would be particularly valuable:

  • Input Object Structure: TextShaperOptions improvements for better usability and intuitive properties naming.
  • Output Formats: Suggest refinements to the output formatters to better meet common use cases.