import { QrCode } from './core'; import { getOptions, getQrWidth } from './utils'; import { drawCanvas } from './canvas'; export type TOptions = { margin?: number scale?: number backgroundColor?: string, codeColor?: string, } /** * Returns a string of data base64 * * @param text * @param options */ export const toDataURL = (text: string, options?: TOptions) => { const { margin, scale, backgroundColor, codeColor, errorCorrectionLevel } = getOptions(options) const qr = QrCode.encodeText(text, errorCorrectionLevel); const canvas = drawCanvas(qr, margin, scale, backgroundColor, codeColor) return canvas.toDataURL('image/png') } /** * Returns a string of SVG code for an image depicting the given QR Code, with the given number * of border modules. The string always uses Unix newlines (\n), regardless of the platform. * * @param text * @param options */ export const toSvgString = (text: string, options?: TOptions) => { const { margin, backgroundColor, codeColor, errorCorrectionLevel } = getOptions(options) const qr = QrCode.encodeText(text, errorCorrectionLevel); const qrWidth = getQrWidth(qr.size, margin) let parts: Array = []; for (let y = 0; y < qr.size; y++) { for (let x = 0; x < qr.size; x++) { if (qr.getModule(x, y)) parts.push(`M${x + margin},${y + margin}h1v1h-1z`); } } const background = backgroundColor.alpha !== 0 ? `` : '' const code = codeColor.alpha !== 0 ? `` : '' return `${background}${code}`; }