https://github.com/weivea/qr-code-wasm
基于wasm 的二维码数据生成生成
https://github.com/weivea/qr-code-wasm
Last synced: 9 months ago
JSON representation
基于wasm 的二维码数据生成生成
- Host: GitHub
- URL: https://github.com/weivea/qr-code-wasm
- Owner: weivea
- License: apache-2.0
- Created: 2021-02-24T03:39:06.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-24T03:42:21.000Z (over 5 years ago)
- Last Synced: 2025-01-16T15:22:09.444Z (over 1 year ago)
- Language: JavaScript
- Size: 103 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE_APACHE
Awesome Lists containing this project
README
# qr-code-wasm
基于wasm 的二维码数据生成生成

## install
```
npm i @weivea/qr-code-wasm
```
## usage
qr-code-wasm 以esm 的方式提供,
下边代码需要webpack打包,webpack目前已经支持 import wasm
可参考: [example](./www)
```javascript
import { QR, Cell } from "qr-code-wasm";
import { memory } from "qr-code-wasm/qr_code_wasm_bg.wasm";
let qr = QR.new("Hello");
let width = qr.width();
const cellsPtr = qr.cells(); // < universe's cells
const cells = new Uint8Array(memory.buffer, cellsPtr, width * width);
let outStr = "";
for (let row = 0; row < width; row++) {
for (let col = 0; col < width; col++) {
let index = row * width + col;
let cell = cells[index] == Cell.Alive ? "◼" : " ";
outStr += cell;
}
outStr += "\n";
}
console.log(outStr);
document.querySelector("#qr").innerText = outStr;
// canvas
const CELL_SIZE = 5; // px
const DEAD_COLOR = "#FFFFFF";
const ALIVE_COLOR = "#000000";
const canvas = document.getElementById("qr-canvas");
canvas.height = CELL_SIZE * width;
canvas.width = CELL_SIZE * width;
const ctx = canvas.getContext("2d");
ctx.beginPath();
for (let row = 0; row < width; row++) {
for (let col = 0; col < width; col++) {
let idx = row * width + col;
ctx.fillStyle = cells[idx] === Cell.Dead ? DEAD_COLOR : ALIVE_COLOR;
ctx.fillRect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
ctx.stroke();
```