@bitjson/qr-code

中文文档
1.0.2发布于 2023/02/28MITTypeScriptESM + CJS零依赖近 30 天下载 8204

QR Code Web Component

原始使用文档
文档目录4 节
  1. Full Example
  2. Animations
  3. Animation Previewer
  4. Production build

npm npm downloads Follow Bitjson on Twitter GitHub stars

@bitjson/qr-code

A no-framework, customizable, animate-able, <qr-code> HTML element. It's just a single, self-contained Web Component.

https://user-images.githubusercontent.com/904007/221692053-1f4ab516-0f63-40e4-b1da-1922ba906963.mp4

Try this interactive demo at: qr.bitjson.com

Usage

Import @bitjson/qr-code using your build system or framework (e.g. npm install @bitjson/qr-code), or use the standalone script in your HTML <head> element:

<script src="https://unpkg.com/@bitjson/[email protected]/dist/qr-code.js"></script>

Then use the component anywhere in your HTML <body> element:

<qr-code contents="https://bitjson.com"></qr-code>

Full Example

Here's an example in pure HTML using most features:

<qr-code
  id="qr1"
  contents="https://bitjson.com/"
  module-color="#1c7d43"
  position-ring-color="#13532d"
  position-center-color="#70c559"
  mask-x-to-y-ratio="1.2"
  style="
    width: 200px;
    height: 200px;
    margin: 2em auto;
    background-color: #fff;
  "
>
  <img src="assets/1.2-x-to-y-ratio-icon.svg" slot="icon" />
</qr-code>

<script>
  document.getElementById('qr1').addEventListener('codeRendered', () => {
    document.getElementById('qr1').animateQRCode('MaterializeIn');
  });
</script>

Animations

Animate in, animate on user interactions like URL hits or detected payments, and/or animate out when the QR code interaction is complete.

Several preset animations are available, simply run them with the element's animateQRCode method:

document.getElementById('qr1').animateQRCode('RadialRipple');

Available built-in presets:

  • FadeInTopDown
  • FadeInCenterOut
  • MaterializeIn
  • RadialRipple
  • RadialRippleIn

You can also design your own custom animations! Just pass a function to the qr-code's animateQRCode method, e.g.:

document
  .getElementById('qr1')
  .animateQRCode((targets, _x, _y, _count, entity) => ({
    targets,
    from: entity === 'module' ? Math.random() * 200 : 200,
    duration: 500,
    easing: 'cubic-bezier(.5,0,1,1)',
    web: { opacity: [1, 0], scale: [1, 1.1, 0.5] },
  }));

The built-in presets use this API internally, so review those for guidance and inspiration. Pull request for new presets are welcome!

Animation Previewer

The animation previewer makes fine-tuning animations much easier: try it by cloning this repo and running the live-reloading package script:

git clone https://github.com/bitjson/qr-code.git
cd qr-code
npm ci
npm start

Then work on your animation in src/index.html using the animation previewer (at the bottom right of the window) to test the last-run animation at various speeds, scrub through it manually, or play it in reverse.

Production build

Disable the just-animate player in src/components/qr-code/qr-code.tsx, then build:

npm run build

You can test the built component by pointing the script in index.html to dist/qr-code.js and opening the page via the local filesystem.

版本与发布记录

@bitjson/qr-code 共发布 3 个版本,首次发布于 2023/02/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2023/02/283 年前
1.0.12023/02/283 年前
1.0.02023/02/273 年前

关于 @bitjson/qr-code 的常见问题

@bitjson/qr-code 是什么?

QR Code Web Component

如何安装 @bitjson/qr-code?

在项目目录运行 npm install @bitjson/qr-code,或使用 pnpm add / yarn add / bun add。

@bitjson/qr-code 支持 TypeScript 吗?

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

@bitjson/qr-code 支持 ESM 还是 CommonJS?

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

@bitjson/qr-code 可以商用吗?

@bitjson/qr-code 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bitjson/qr-code 的下载量有多少?

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

@bitjson/qr-code 还在维护吗?

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