🔲 Barcode.WASM Easy-8-Steps (TypeScript)

Barcode.WASMの最小限の使い方サンプルです。

📝 コード(たった8ステップ)

// Step 1: モジュールのインポート
import Module from './barcode.js';

// Step 2: モジュールの初期化
const barcodeModule = await Module();

// Step 3: バーコード(今回はCODE128)インスタンス作成
const barcode = new barcodeModule.Code128();

// Step 4: 指定幅ぴったり調整(省略可)
// 省略した場合、指定幅以内でピクセルにはまる高精度なバーコードを出力します
barcode.setFitWidth(true);

// Step 5: 出力形式設定(PNG出力時は省略可)
barcode.setOutputFormat('svg');  // 'png' または 'svg'

// Step 6: バーコード生成
const barcodeString: string = barcode.draw("Pao@Office 12345!", 600, 150);

// Step 7: 結果をHTMLに埋め込み
// PNG出力時: Base64エンコードされたデータURLが返される
element.innerHTML = '<img src="' + barcodeString + '">';
// SVG出力時: SVG文字列がそのまま返されるので直接埋め込み
// element.innerHTML = barcodeString;

// Step 8: メモリ解放(重要!)
barcode.delete();

🎯 実行サンプル

⏳ WASMモジュール初期化中...