Skip to content

Render QR codes in the browser

Browser applications can render a QR code as an SVG string, an Image element backed by PNG, WebP, GIF, or JPEG data, or a Canvas element. Choose the output for what the page needs to do next, then insert or compose the returned value with standard browser APIs.

Choose Package Return value Best fit
SVG @qrcodesdk/core string Scalable inline markup and the recommended browser default
Image @qrcodesdk/browser HTMLImageElement A familiar, accessible DOM image backed by PNG, WebP, GIF, or JPEG
Canvas @qrcodesdk/browser HTMLCanvasElement Drawing, compositing, pixel access, or manual PNG export

Start with SVG unless the next browser API specifically needs an Image or Canvas element. SVG stays sharp when resized and does not require Canvas support. Image and Canvas output require both @qrcodesdk/core and @qrcodesdk/browser.

QRCodeSVGRenderer returns complete SVG markup. It is not a DOM node, so insert it into an existing element or pass it to the browser-side template system that owns the page.

Render into a container when the SVG should participate in the page as inline markup:

import {QRCodeSVGRenderer, qrcode} from '@qrcodesdk/core';
const svg = qrcode('https://qrcodesdk.dev').render(
QRCodeSVGRenderer({
accessibility: {ariaLabel: 'Scan to open qrcodesdk.dev'},
}),
);
const container = document.querySelector('#qrcode');
if (container) {
container.innerHTML = svg;
}
<div id="qrcode"></div>

Insert the SVG string returned by QRCodeSVGRenderer() directly. The renderer escapes label text and image URL attributes; it encodes the payload as modules rather than inserting it as text. Do not concatenate unescaped user input into the SVG or its surrounding HTML. For center images, validate uploaded image content before converting it to a data URL; svgCenterImage() checks the URL format but does not inspect the embedded image.

The returned string can be interpolated into HTML generated by a browser-side template:

import {QRCodeSVGRenderer, qrcode} from '@qrcodesdk/core';
const svg = qrcode('https://qrcodesdk.dev').render(
QRCodeSVGRenderer({
accessibility: {ariaLabel: 'Scan to open qrcodesdk.dev'},
}),
);
const html = `
<section>
<h2>Scan this QR code</h2>
${svg}
</section>
`;

Apply layout styles to the inserted root SVG through its container:

#qrcode > svg {
display: block;
width: min(100%, 20rem);
height: auto;
}

The Image element renderers synchronously return a new HTMLImageElement whose src contains the rendered image as a data URL. Use them when the QR code should behave like an ordinary image in the DOM. Pick the renderer by the encoded format:

Renderer Entry point Encoded format
QRCodePNGImageRenderer @qrcodesdk/browser/png PNG, encoded natively by the browser
QRCodeWebPImageRenderer @qrcodesdk/browser/webp Lossless WebP, usually the smallest file
QRCodeGIFImageRenderer @qrcodesdk/browser/gif GIF with 256 colors and binary transparency
QRCodeJPEGImageRenderer @qrcodesdk/browser/jpeg Lossy JPEG on a solid matte color, for destinations that require JPEG

Each format has its own entry point, so a bundle only includes the encoder it imports. WebP, GIF, and JPEG use encoders bundled in the SDK and produce the same bytes in every browser, including Safari, which cannot encode WebP natively. The bundled JPEG encoder also blends transparent pixels onto a white matte, where the browser’s own JPEG encoding would turn them black. All four accept the same options; see the Image element renderers reference for the format differences.

The returned value is already an HTMLImageElement, so it can be appended and styled like any other image:

import {QRCodePNGImageRenderer} from '@qrcodesdk/browser/png';
import {qrcode} from '@qrcodesdk/core';
const image = qrcode('https://qrcodesdk.dev').render(
QRCodePNGImageRenderer({
accessibility: {
alt: 'QR code for qrcodesdk.dev',
ariaLabel: 'Scan to open qrcodesdk.dev',
title: 'QR code for qrcodesdk.dev',
},
}),
);
image.className = 'qrcode';
document.querySelector('#qrcode')?.append(image);
<div id="qrcode"></div>
.qrcode {
display: block;
width: min(100%, 20rem);
height: auto;
}

Read src when another browser API needs the encoded image rather than the element:

import {QRCodePNGImageRenderer} from '@qrcodesdk/browser/png';
import {qrcode} from '@qrcodesdk/core';
const image = qrcode('https://qrcodesdk.dev').render(QRCodePNGImageRenderer());
const response = await fetch(image.src);
const png = await response.blob();
await navigator.clipboard.write([
new ClipboardItem({
'image/png': png,
}),
]);

The Clipboard API requires a secure context and may require a user gesture or permission. Browsers reliably accept only image/png on the clipboard, so use the PNG renderer here.

QRCodeCanvasRenderer synchronously returns a new HTMLCanvasElement. Use Canvas when code needs to draw onto the output, combine it with other graphics, inspect pixels, or export it manually.

Give the Canvas its own accessible name, then append it wherever the QR code should appear:

import {QRCodeCanvasRenderer} from '@qrcodesdk/browser';
import {qrcode} from '@qrcodesdk/core';
const canvas = qrcode('https://qrcodesdk.dev').render(
QRCodeCanvasRenderer({
style: {moduleSize: 8, quietZone: 4},
accessibility: {ariaLabel: 'Scan to open qrcodesdk.dev'},
}),
);
document.querySelector('#qrcode')?.append(canvas);
<div id="qrcode"></div>

Use the QR Canvas as a source for a larger composition:

import {QRCodeCanvasRenderer} from '@qrcodesdk/browser';
import {qrcode} from '@qrcodesdk/core';
const qrCanvas = qrcode('https://qrcodesdk.dev').render(QRCodeCanvasRenderer());
const target = document.querySelector<HTMLCanvasElement>('#target');
const context = target?.getContext('2d');
if (target && context) {
target.width = qrCanvas.width;
target.height = qrCanvas.height;
context.drawImage(qrCanvas, 0, 0);
}

Each render creates a new string or element. When the payload changes, render again and replace the old output so the container does not accumulate QR codes:

import {QRCodePNGImageRenderer} from '@qrcodesdk/browser/png';
import {qrcode} from '@qrcodesdk/core';
const container = document.querySelector('#qrcode');
export function updateQRCode(payload: string) {
const image = qrcode(payload).render(
QRCodePNGImageRenderer({accessibility: {alt: '', ariaLabel: `Scan to open ${payload}`}}),
);
container?.replaceChildren(image);
}

If application state changes frequently, avoid recreating output for unrelated updates. Framework users can instead use the React, Vue, Svelte, or Angular components, which integrate rendering with their normal component lifecycle.

  • Keep the default four-module quiet zone clear of nearby text, borders, and other UI.
  • Give meaningful SVG, Image or Canvas output an accessible label.
  • Use CSS to shrink output responsively, but do not enlarge Image or Canvas output beyond its rendered pixel dimensions. Increase style.moduleSize when more raster resolution is needed.
  • Scan-test the final page at its smallest supported viewport and after any CSS transforms, browser zoom, screenshots, or image compression.

Support QRCodeSDK

If you find QRCodeSDK useful, please consider sponsoring the project!