@borgar/textbox

中文文档
2.0.0发布于 2026/01/27BSD-3-ClauseTypeScriptESM + CJS零依赖近 30 天下载 5776

A utility to render formatted multi-line rich text.

原始使用文档
文档目录3 节
  1. Installing
  2. API Reference
  3. Limitations:

Textbox

Textbox is a simple library to layout multiline text for display on SVG or Canvas. It can fairly decently line-break and render rich text given some boundaries. It understands simple text, and a small subset of HTML and LaTeX syntaxes. The original purpose of this software is to aid labeling charts.

Take a look at Textbox example and demos if you are curious what this library can do.

Features:

  • Line-break text to fit dimensions.
  • Can overflow text into ... if it doesn't fit designated area.
  • Understands common text features: bold, italic, links, etc...
  • Knows that there is different whitespace like thin, or non-breaking.
  • Tries to be smart about line-breaking before or after certain characters (it can occur after - but not before).
  • Supports hyphenation if text is prepared with soft-hyphens.

Installing

If you don't want to download and build Textbox yourself, the library is also provided as an NPM package:

$ npm install @borgar/textbox

API Reference

For any use, you will need to start by defining a new Textbox instance. You may pass a configuration object as a parameter:

const box = new Textbox({
  font: '12px/14px sans-serif',
  width: Infinity,
  height: Infinity,
  align: 'left',
  valign: 'top',
  x: 0,
  overflow: 'ellipsis',
  parser: 'text',
  createElement: Textbox.createElement
});

Shown here are the defaults, but any or all of the above parameters can be provided. The Textbox instance will provide methods by the same name, along with line-breaking and rendering methods:

const box = new Textbox()
  .font('16px/19px sans-serif')
  .align('center')
  .parser('html')
  .createElement(React.createElement);

Full API documentation can be found in docs/API.md.

Limitations:

  1. In SVG links do not automatically get color or pointer cursor. You will need to add your own styles to these.

    svg text a {
      fill: blue;
      text-decoration: underline;
      cursor: pointer;
    }
    
  2. Multi-word links in SVG text are not necessarily a single entity/element like they are in HTML. If the text in a link is line-broken, then hovering one segment will not cause segment in the next line over to trigger hover styles.

  3. Text justification does not work consistently in all browsers as they have buggy support for the word-spacing property. Google Chrome seems to work fine, Safari works for plain text but incorrectly for formatted text. Firefox is fairly random. Test your output in different browsers before you publish.

  4. Subscript and superscript are bugged in Firefox because they have never implemented the baseline-shift property. The bug report is 20 years old currently, so aaaaaany day now.

版本与发布记录

@borgar/textbox 共发布 14 个版本,首次发布于 2020/01/15;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.0.0latest2026/01/279 个月前
1.8.02024/12/161.9 年前
1.8.0-rc12024/12/161.9 年前
1.8.0-rc02024/12/161.9 年前
1.7.02024/08/092 年前
1.6.02023/03/223 年前

关于 @borgar/textbox 的常见问题

@borgar/textbox 是什么?

A utility to render formatted multi-line rich text.

如何安装 @borgar/textbox?

在项目目录运行 npm install @borgar/textbox,或使用 pnpm add / yarn add / bun add。

@borgar/textbox 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@borgar/textbox 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@borgar/textbox 可以商用吗?

@borgar/textbox 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@borgar/textbox 的下载量有多少?

根据 npm 官方下载统计,@borgar/textbox 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,776 次,日均约 193 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@borgar/textbox 还在维护吗?

最新版本 2.0.0 发布于 2026/01/27(9 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/01/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。