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 a browser output
Section titled “Choose a browser output”| 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.
SVG string
Section titled “SVG string”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.
Insert it into the DOM
Section titled “Insert it into the DOM”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.
Include it in browser-generated markup
Section titled “Include it in browser-generated markup”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;}Image element
Section titled “Image element”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.
Insert and style it
Section titled “Insert and style it”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;}Use the data URL
Section titled “Use the data URL”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.
Canvas element
Section titled “Canvas element”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.
Insert it into the DOM
Section titled “Insert it into the DOM”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>Draw it into another Canvas
Section titled “Draw it into another Canvas”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);}Replace a rendered QR code
Section titled “Replace a rendered QR code”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 browser output reliable
Section titled “Keep browser output reliable”- 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.moduleSizewhen 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.
Related pages
Section titled “Related pages”Support QRCodeSDK
If you find QRCodeSDK useful, please consider sponsoring the project!