@flourish/pocket-knife

中文文档
2.3.2发布于 2026/09/01LicenseRef-LICENSEES Module近 30 天下载 1.4万

Flourish module with handy tools

原始使用文档
文档目录14 节
  1. Functions
  2. createScreenshotSVG(target, id)
  3. getTextDimensions(string, font)
  4. getTextDirection()
  5. getTextHeight(string, font)
  6. getTextWidth(string, font)
  7. getUniqueValuesFromBinding(data, binding, index)
  8. hexToColor(hex_string, opacity)
  9. hexToRgba(hex_string, opacity)
  10. isImage(string)
  11. isPale(color_value)
  12. isUrl(string)
  13. remToPx(rem)
  14. wrapStringToLines(label, label_styles, text_max_lines, max_…

Flourish pocket knife 🇨🇭

A module with handy tools that you can use in Flourish templates.

import { isUrl, isImage } from "@flourish/pocket-knife"

Functions

createScreenshotSVG(target, id)

Create a temporary SVG element that can be used in the SVG export. Takes a target element, which it uses to set the boundaries and position of the main svg container; and an id which is used to set a unique id of the SVG element.

This function returns an .addTextElements(els), .addCircleElements(els), .addGradient(el, colorFunction, domain), and .remove() method. .remove() will remove the SVG. addTextElements takes a node list (els) and will convert them to an SVG <text> elements, incorporating the element's font styles and line height. Similarly, addCircleElements takes elements (e.g. divs with a background color) and converts them to SVG <circle> elements. addGradient takes an element, a color function, and a domain; the domain should be an object with a min and max representing the starting and ending values the gradient represents, and the color function should take a number within the domain and return the color which represents that number.

This function can be used in the template's screenshot() function to temporarily create an SVG that will be included in the SVG export.

getTextDimensions(string, font)

Gets dimensions of text. First argument contains the text you want to calculate the width for. The second argument is optional and lets you set font settings for the text in the standard css font property, eg. bold 16px arial. If font is empty, it uses 10px sans-serif. Returns an object with properties width and height.

getTextDirection()

Gets computed text direction on document.body. Returns a "ltr" or "rtl".

getTextHeight(string, font)

Gets height of text. Uses same arguments as getTextDimensions. Returns a number.

getTextWidth(string, font)

Gets width of text. Uses same arguments as getTextDimensions. Returns a number.

getUniqueValuesFromBinding(data, binding, index)

Gets all the unique values in a column associated to a data binding. If it is a multi column binding you can pass in index that is used to work out which of the bound columns it should look at. If you pass it no index and it is a multi column binding it will default to zero.

hexToColor(hex_string, opacity)

Converts hex to d3 color object as described here

hexToColor("#00ff00", 0.5); // {r: 0, g: 255, b: 0, a: 0.5}

hexToRgba(hex_string, opacity)

Converts hex to rgba string

hexToRgba("#00ff00", 0.5); // rgba(0, 255, 0, 0.5)

isImage(string)

Checks if string looks like valid image URL. Returns true or false

isPale(color_value)

Returns if color is pale or not

isUrl(string)

Checks if string looks like valid URL. Returns true or false

remToPx(rem)

Converts value specified in rems to px

wrapStringToLines(label, label_styles, text_max_lines, max_width)

Takes a string, and returns an array of strings where each string is one lines worth of text.

  • Label is a string, that you want to be broken up into lines
  • label_styles is a string that can include font weight, font size and font family in the structure: 'bold 48px serif'. it is what is given to the canvas context font method, and is used to measure the text.
  • text_max_lines is a number that says the maximum amount of lines you want (if you dont have a maximum you can give it null)
  • max_width is a number that states in px the maximum width for the label.

版本与发布记录

@flourish/pocket-knife 共发布 30 个版本,首次发布于 2019/06/11;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.3.2latest2026/09/012 个月前
2.3.12026/05/225 个月前
2.3.02025/03/281.6 年前
2.2.02025/02/201.7 年前
2.1.12025/01/101.8 年前
2.1.02024/12/121.9 年前

关于 @flourish/pocket-knife 的常见问题

@flourish/pocket-knife 是什么?

Flourish module with handy tools

如何安装 @flourish/pocket-knife?

在项目目录运行 npm install @flourish/pocket-knife,或使用 pnpm add / yarn add / bun add。

@flourish/pocket-knife 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flourish__pocket-knife 是否存在。

@flourish/pocket-knife 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@flourish/pocket-knife 可以商用吗?

@flourish/pocket-knife 使用 LicenseRef-LICENSE 许可。声明的许可为 LicenseRef-LICENSE,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@flourish/pocket-knife 的下载量有多少?

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

@flourish/pocket-knife 还在维护吗?

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