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

@canvas-components/barcode

v0.1.5

Published

Canvas barcode drawing and export utilities.

Readme

@canvas-components/barcode

@canvas-components/barcode 是一个纯原生的 Canvas 条形码绘制与导出工具包,适合在表格单元格、卡片、票据、打印标签和离屏导出场景中使用。

功能作用

  • 支持 code128code128bcode128ccode39itfean13ean8upca
  • 支持直接绘制到 CanvasRenderingContext2D
  • 支持导出为 SVG、Data URL、Blob
  • 支持自定义文本、颜色、间距、透明度和导出倍率

安装方法

pnpm add @canvas-components/barcode

最低环境要求:

  • Node.js >= 18.12.0
  • pnpm >= 9.0.0

使用方法

import {
  drawBarcodeToCanvas,
  exportBarcodeToSvg,
} from "@canvas-components/barcode";

const canvas = document.querySelector("canvas")!;
const ctx = canvas.getContext("2d")!;

drawBarcodeToCanvas({
  ctx,
  x: 0,
  y: 0,
  width: 240,
  height: 72,
  value: "123456789012",
  format: "code128",
  displayValue: true,
});

const svg = exportBarcodeToSvg({
  value: "123456789012",
  width: 240,
  height: 72,
});

属性说明

DrawBarcodeToCanvasOptions

| 属性 | 类型 | 说明 | | --- | --- | --- | | ctx | CanvasRenderingContext2D \| OffscreenCanvasRenderingContext2D | 目标画布上下文 | | x / y | number | 绘制起点 | | width / height | number | 绘制区域尺寸 | | value | string | 原始编码值 | | format | BarcodeFormat | 条形码制式,默认 code128 | | color | string | 条纹前景色 | | backgroundColor | string | 背景色 | | padding | number | 内边距 | | opacity | number | 透明度 | | displayValue | boolean | 是否显示可读文本 | | text | string | 自定义可读文本 | | fontSize | number | 文本字号 | | fontWeight | string | 文本字重 | | fontFamily | string | 文本字体 | | textColor | string | 文本颜色 | | textGap | number | 条纹与文本间距 |

BarcodePattern

| 属性 | 类型 | 说明 | | --- | --- | --- | | format | BarcodeFormat | 实际使用的编码制式 | | value | string | 原始输入值 | | text | string | 最终可读文本 | | codeValues | number[] | 中间编码值 | | modules | number[] | 绘制用模块宽度序列 | | moduleCount | number | 总模块数 |

ExportBarcodeToImageOptions

继承 DrawBarcodeToCanvasOptions 的大部分导出相关字段,另补充:

| 属性 | 类型 | 说明 | | --- | --- | --- | | pixelRatio | number | 位图导出倍率 | | type | "image/png" \| "image/jpeg" \| "image/webp" | 导出图片类型 | | quality | number | 图片质量 | | canvas | ExportCanvasLike | 外部复用 canvas |

方法说明

| 方法 | 签名 | 说明 | | --- | --- | --- | | encodeBarcodePattern | (value, format?) => BarcodePattern | 把原始值编码为可绘制 pattern | | drawBarcodeToCanvas | (options) => void | 直接绘制到 canvas | | exportBarcodeToSvg | (options) => string | 导出 SVG 字符串 | | exportBarcodeToDataUrl | (options) => Promise<string> | 导出图片 Data URL | | exportBarcodeToBlob | (options) => Promise<Blob> | 导出图片 Blob |

适用场景

  • 物流面单、仓储标签
  • 表格中的条形码单元格
  • 打印前预览与离屏导出
  • 前端纯浏览器生成编码图片