@veaba/qrcode-wasm 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +320 -0
- package/dist/index.d.ts +170 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +347 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +137 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +15 -0
- package/dist/types.js.map +1 -0
- package/package.json +47 -0
- package/pkg/LICENSE +21 -0
- package/pkg/README.md +320 -0
package/pkg/README.md
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# @veaba/qrcode-wasm
|
|
2
|
+
|
|
3
|
+
WebAssembly QRCode 生成器 - 基于 Rust 编译,提供优秀的 QRCode 生成性能。
|
|
4
|
+
|
|
5
|
+
## 简介
|
|
6
|
+
|
|
7
|
+
基于 Rust 实现并编译为 WebAssembly 的 QRCode 生成器,提供接近原生的执行速度。
|
|
8
|
+
|
|
9
|
+
与 `@veaba/qrcode-js` 提供**完全一致的 API**,方便在两个包之间无缝切换。
|
|
10
|
+
|
|
11
|
+
## 特性
|
|
12
|
+
|
|
13
|
+
- ⚡ **高性能** - Rust + WASM 提供接近原生的执行速度
|
|
14
|
+
- ⚡ **并行计算** - 支持多线程并行生成(需配置)
|
|
15
|
+
- 💾 **可选缓存** - LRU 缓存支持,重复文本性能提升 10 倍+
|
|
16
|
+
- 🎨 **丰富样式** - 支持 10+ 种个性样式
|
|
17
|
+
- 📦 **批量生成** - 支持批量异步生成
|
|
18
|
+
- 🖼️ **SVG 输出** - 生成矢量图形,清晰锐利
|
|
19
|
+
- 🔧 **TypeScript 支持** - 包含类型定义文件
|
|
20
|
+
- 🔄 **API 统一** - 与 `@veaba/qrcode-js` 完全一致的 API
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @veaba/qrcode-wasm
|
|
26
|
+
# 或
|
|
27
|
+
pnpm add @veaba/qrcode-wasm
|
|
28
|
+
# 或
|
|
29
|
+
yarn add @veaba/qrcode-wasm
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 使用方法
|
|
33
|
+
|
|
34
|
+
### 基础用法(统一 API)
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import init, { QRCodeCore, QRErrorCorrectLevel } from '@veaba/qrcode-wasm';
|
|
38
|
+
|
|
39
|
+
// 初始化 WASM(只需一次)
|
|
40
|
+
await init();
|
|
41
|
+
|
|
42
|
+
// 创建 QRCode 实例(与 qrcode-js 相同的 API)
|
|
43
|
+
const qr = new QRCodeCore('Hello World', QRErrorCorrectLevel.H);
|
|
44
|
+
|
|
45
|
+
// 获取 SVG
|
|
46
|
+
const svg = qr.toSVG(256);
|
|
47
|
+
|
|
48
|
+
// 插入到页面
|
|
49
|
+
document.getElementById('qrcode').innerHTML = svg;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 使用缓存(推荐)
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import init, {
|
|
56
|
+
generateRoundedQRCodeCached,
|
|
57
|
+
clearQRCodeCache,
|
|
58
|
+
getCacheStats
|
|
59
|
+
} from '@veaba/qrcode-wasm';
|
|
60
|
+
|
|
61
|
+
await init();
|
|
62
|
+
|
|
63
|
+
// 第一次生成会缓存
|
|
64
|
+
const svg1 = generateRoundedQRCodeCached('https://example.com', 256, 8);
|
|
65
|
+
|
|
66
|
+
// 第二次生成直接从缓存读取,速度提升 10 倍+
|
|
67
|
+
const svg2 = generateRoundedQRCodeCached('https://example.com', 256, 8);
|
|
68
|
+
|
|
69
|
+
// 查看缓存状态
|
|
70
|
+
console.log(getCacheStats()); // { size: 1, maxSize: 100, keys: [...] }
|
|
71
|
+
|
|
72
|
+
// 清空缓存
|
|
73
|
+
clearQRCodeCache();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 样式化二维码
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import init, {
|
|
80
|
+
generateWechatStyleQRCode,
|
|
81
|
+
generateDouyinStyleQRCode,
|
|
82
|
+
generateCyberpunkStyleQRCode
|
|
83
|
+
} from '@veaba/qrcode-wasm';
|
|
84
|
+
|
|
85
|
+
await init();
|
|
86
|
+
|
|
87
|
+
// 微信风格
|
|
88
|
+
const wechatQR = generateWechatStyleQRCode('https://weixin.qq.com', 256);
|
|
89
|
+
|
|
90
|
+
// 抖音风格
|
|
91
|
+
const douyinQR = generateDouyinStyleQRCode('https://douyin.com', 256);
|
|
92
|
+
|
|
93
|
+
// 赛博朋克风格
|
|
94
|
+
const cyberQR = generateCyberpunkStyleQRCode('https://example.com', 256);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 批量生成
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import init, { generateBatchQRCodes, generateBatchQRCodesCached } from '@veaba/qrcode-wasm';
|
|
101
|
+
|
|
102
|
+
await init();
|
|
103
|
+
|
|
104
|
+
const texts = [
|
|
105
|
+
'https://example.com/1',
|
|
106
|
+
'https://example.com/2',
|
|
107
|
+
// ... 更多
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
// 非缓存版本
|
|
111
|
+
const svgs = generateBatchQRCodes(texts, { size: 256 });
|
|
112
|
+
|
|
113
|
+
// 缓存版本
|
|
114
|
+
const svgsCached = generateBatchQRCodesCached(texts, { size: 256 });
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 异步生成
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import init, { generateQRCodeAsync, generateBatchAsync } from '@veaba/qrcode-wasm';
|
|
121
|
+
|
|
122
|
+
await init();
|
|
123
|
+
|
|
124
|
+
// 单个异步生成
|
|
125
|
+
const result = await generateQRCodeAsync('https://example.com', {
|
|
126
|
+
size: 256,
|
|
127
|
+
cache: true // 启用缓存
|
|
128
|
+
});
|
|
129
|
+
console.log(result.svg);
|
|
130
|
+
|
|
131
|
+
// 批量异步生成
|
|
132
|
+
const results = await generateBatchAsync(texts, {
|
|
133
|
+
size: 256,
|
|
134
|
+
cache: true
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## 底层 WASM API(可选)
|
|
139
|
+
|
|
140
|
+
如果需要更底层的控制,可以直接使用 WASM 原生 API:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import init, { QRCodeWasm, CorrectLevel } from '@veaba/qrcode-wasm';
|
|
144
|
+
|
|
145
|
+
await init();
|
|
146
|
+
|
|
147
|
+
// 使用底层 WASM API
|
|
148
|
+
const qr = new QRCodeWasm();
|
|
149
|
+
qr.make_code('https://github.com/veaba/qrcodes');
|
|
150
|
+
const svg = qr.get_svg();
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### 使用 QRCodeGenerator(可复用实例)
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import init, { QRCodeGenerator, CorrectLevel } from '@veaba/qrcode-wasm';
|
|
157
|
+
|
|
158
|
+
await init();
|
|
159
|
+
|
|
160
|
+
// 创建可复用的生成器
|
|
161
|
+
const gen = new QRCodeGenerator();
|
|
162
|
+
|
|
163
|
+
// 设置选项
|
|
164
|
+
gen.set_options(256, 256, CorrectLevel.H);
|
|
165
|
+
|
|
166
|
+
// 生成单个 QRCode
|
|
167
|
+
gen.generate('https://github.com/veaba/qrcodes/1');
|
|
168
|
+
const svg1 = gen.get_svg();
|
|
169
|
+
|
|
170
|
+
// 复用同一实例生成另一个(性能更好)
|
|
171
|
+
gen.generate('https://github.com/veaba/qrcodes/2');
|
|
172
|
+
const svg2 = gen.get_svg();
|
|
173
|
+
|
|
174
|
+
// 批量生成
|
|
175
|
+
const texts = ['url1', 'url2', 'url3'];
|
|
176
|
+
const svgs = gen.generate_batch(texts);
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Canvas 渲染
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import init, { CanvasRenderer } from '@veaba/qrcode-wasm';
|
|
183
|
+
|
|
184
|
+
await init();
|
|
185
|
+
|
|
186
|
+
// 创建 Canvas 渲染器
|
|
187
|
+
const renderer = new CanvasRenderer(256, 256);
|
|
188
|
+
|
|
189
|
+
// 设置颜色(RGBA)
|
|
190
|
+
renderer.set_colors(
|
|
191
|
+
0, 0, 0, 255, // 深色: 黑色
|
|
192
|
+
255, 255, 255, 255 // 浅色: 白色
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// 渲染 QRCode
|
|
196
|
+
const pixelData = renderer.render('https://github.com/veaba/qrcodes', CorrectLevel.H);
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## API
|
|
200
|
+
|
|
201
|
+
### 统一 API(与 qrcode-js 一致)
|
|
202
|
+
|
|
203
|
+
| 类/函数 | 说明 |
|
|
204
|
+
|---------|------|
|
|
205
|
+
| `QRCodeCore` | 核心 QRCode 类 |
|
|
206
|
+
| `QRErrorCorrectLevel` | 纠错级别枚举 |
|
|
207
|
+
| `generateRoundedQRCode` / `generateRoundedQRCodeCached` | 圆角二维码 |
|
|
208
|
+
| `generateGradientQRCode` / `generateGradientQRCodeCached` | 渐变二维码 |
|
|
209
|
+
| `generateWechatStyleQRCode` / `generateWechatStyleQRCodeCached` | 微信风格 |
|
|
210
|
+
| `generateDouyinStyleQRCode` / `generateDouyinStyleQRCodeCached` | 抖音风格 |
|
|
211
|
+
| `generateBatchQRCodes` / `generateBatchQRCodesCached` | 批量生成 |
|
|
212
|
+
| `generateQRCodeAsync` | 异步生成 |
|
|
213
|
+
| `getCachedQRCode` | 获取缓存 |
|
|
214
|
+
| `clearQRCodeCache` | 清空缓存 |
|
|
215
|
+
|
|
216
|
+
### 底层 WASM API
|
|
217
|
+
|
|
218
|
+
| 方法 | 说明 | 返回值 |
|
|
219
|
+
|------|------|--------|
|
|
220
|
+
| `new()` | 创建实例 | `QRCodeWasm` |
|
|
221
|
+
| `with_options(w, h, level)` | 带选项创建 | `QRCodeWasm` |
|
|
222
|
+
| `make_code(text)` | 生成 QRCode | `void` |
|
|
223
|
+
| `get_svg()` | 获取 SVG | `string` |
|
|
224
|
+
| `get_module_count()` | 获取模块数 | `number` |
|
|
225
|
+
| `get_modules_json()` | 获取模块数据 | `string` |
|
|
226
|
+
| `is_dark(row, col)` | 判断模块颜色 | `boolean` |
|
|
227
|
+
|
|
228
|
+
## 与 @veaba/qrcode-js 的对比
|
|
229
|
+
|
|
230
|
+
| 特性 | @veaba/qrcode-wasm | @veaba/qrcode-js |
|
|
231
|
+
|------|-------------------|------------------|
|
|
232
|
+
| 性能 | ⚡⚡⚡ 最快 | ⚡⚡ 快 |
|
|
233
|
+
| 包大小 | ~45KB | ~15KB |
|
|
234
|
+
| 初始化 | 需要异步 | 即时 |
|
|
235
|
+
| 兼容性 | 现代浏览器 | IE11+ |
|
|
236
|
+
| API | 统一 ✅ | 统一 ✅ |
|
|
237
|
+
|
|
238
|
+
选择 `@veaba/qrcode-wasm`:
|
|
239
|
+
- ✅ 追求极致性能
|
|
240
|
+
- ✅ 高频批量生成
|
|
241
|
+
- ✅ 现代浏览器环境
|
|
242
|
+
|
|
243
|
+
选择 `@veaba/qrcode-js`:
|
|
244
|
+
- ✅ 需要支持 IE11 等旧浏览器
|
|
245
|
+
- ✅ 对包大小敏感
|
|
246
|
+
- ✅ 不想处理 WASM 的异步初始化
|
|
247
|
+
|
|
248
|
+
## 性能对比
|
|
249
|
+
|
|
250
|
+
| 场景 | @veaba/qrcode-js | @veaba/qrcode-wasm | 提升 |
|
|
251
|
+
|------|------------------|-------------------|------|
|
|
252
|
+
| 单条生成 | ~9,000 ops/s | ~15,000 ops/s | **1.7x** |
|
|
253
|
+
| SVG 输出 | ~9,800 ops/s | ~17,000 ops/s | **1.7x** |
|
|
254
|
+
| 缓存命中 | ~500,000 ops/s | ~500,000 ops/s | 相同 |
|
|
255
|
+
|
|
256
|
+
*测试环境:Chrome 120, Intel i7-1165G7*
|
|
257
|
+
|
|
258
|
+
## 开发
|
|
259
|
+
|
|
260
|
+
### 环境要求
|
|
261
|
+
|
|
262
|
+
- Rust
|
|
263
|
+
- wasm-pack
|
|
264
|
+
- Node.js
|
|
265
|
+
|
|
266
|
+
### 构建
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
# 安装 wasm-pack
|
|
270
|
+
cargo install wasm-pack
|
|
271
|
+
|
|
272
|
+
# 构建 WASM
|
|
273
|
+
wasm-pack build --target web
|
|
274
|
+
|
|
275
|
+
# 构建 Node.js 版本
|
|
276
|
+
wasm-pack build --target nodejs
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### 运行测试
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
# Rust 测试
|
|
283
|
+
cargo test
|
|
284
|
+
|
|
285
|
+
# TypeScript 测试
|
|
286
|
+
npm test
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### 构建发布版本
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
# 优化构建
|
|
293
|
+
wasm-pack build --release --target web
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## 性能测试
|
|
297
|
+
|
|
298
|
+
基准测试已迁移到 `bench/frontend-benchmark`:
|
|
299
|
+
|
|
300
|
+
```bash
|
|
301
|
+
cd bench/frontend-benchmark
|
|
302
|
+
node benchmark.cjs # 测试 qrcode-js
|
|
303
|
+
npx tsx wasm.benchmark.ts # 测试 qrcode-wasm
|
|
304
|
+
cargo run --bin benchmark # 测试 Rust 原生性能
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## 相关包
|
|
308
|
+
|
|
309
|
+
- `@veaba/qrcode-js` - 纯 JavaScript 版本(API 一致)
|
|
310
|
+
- `@veaba/qrcode-node` - Node.js 版本
|
|
311
|
+
- `@veaba/qrcode-bun` - Bun 运行时版本
|
|
312
|
+
- `@veaba/qrcode-shared` - 共享核心库
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
MIT
|
|
317
|
+
|
|
318
|
+
## 历史迁移
|
|
319
|
+
|
|
320
|
+
- **2026-02-06**: 基准测试从 `packages/qrcode-wasm/benchmark/` 和 `packages/qrcode-wasm/src/bin/benchmark.rs` 迁移至 `bench/frontend-benchmark/`,保持包目录干净。
|