ข้ามไปยังเนื้อหา

SVG renderer

ตัวอย่าง card ที่ใช้ layout ตาม brand-spec

@thai-qr-payment/render ผูก payload builder, QR encoder และ brand assets เข้าด้วยกันเป็น SVG string เดียว ไม่ต้องใช้ DOM และไม่ต้องใช้ canvas — ทำงานได้บนทุก runtime

import { renderThaiQRPayment } from 'thai-qr-payment';
const svg = renderThaiQRPayment({
recipient: '0812345678',
amount: 50,
merchantName: 'Acme Coffee',
amountLabel: '฿ 50.00',
errorCorrectionLevel: 'H',
});

คืน SVG card เต็มรูปแบบ ประกอบด้วย header band ของ Thai QR Payment + sub-mark ของ PromptPay + QR ที่มีกรอบ + label ยอดเงิน

เมื่อต้องการห่อ QR ด้วย design system ของตนเอง:

import { renderThaiQRPaymentMatrix } from 'thai-qr-payment';
const svg = renderThaiQRPaymentMatrix({
recipient: '0812345678',
amount: 50,
size: 320,
quietZone: 4,
});
OptionDefaultหมายเหตุ
themecolor'silhouette' เปลี่ยน brand artwork เป็น path ขาวดำ
merchantNamerender อยู่เหนือ QR
amountLabelrender อยู่ใต้ QR
background#fffพื้นหลังของ card
accent#0a2540สีของข้อความ + สี fill ของ silhouette
headerLogoThai_QR_Payment_Logo-01override ผ่านชื่อใน registry ของ @thai-qr-payment/assets
promptpayLogoPromptPay1override
import { encodeQR } from 'thai-qr-payment';
import { renderCard, renderQRSvg, matrixToPath } from 'thai-qr-payment';
const matrix = encodeQR(wireString, { errorCorrectionLevel: 'H' });
const svg = renderCard(matrix, { merchantName: 'Acme', amountLabel: '฿ 50' });
// หรือลงลึกอีกระดับ:
const justTheQr = renderQRSvg(matrix, { size: 512, quietZone: 4 });
const pathData = matrixToPath(matrix);

ทุก string ที่ถูก interpolate จะผ่าน escapeXmlAttribute ก่อนเขียนลง SVG markup ดังนั้นค่าอย่าง <script>alert(1)</script> ใน merchantName จะถูกแปลงเป็นรูป escape ที่ปลอดภัย พฤติกรรมนี้ได้รับการตรวจสอบด้วย test fixture เฉพาะในแพ็กเกจ render

return new Response(svg, {
headers: { 'content-type': 'image/svg+xml; charset=utf-8' },
});

ใช้งานได้โดยตรงจาก Cloudflare Workers, Vercel Edge, Bun, Deno และ Node โดยไม่ต้องมี glue สำหรับ runtime แต่ละตัว