Canvas element renderer
When to use
Section titled “When to use”Use QRCodeCanvasRenderer when browser code needs to draw, composite, inspect, or manually export
raster output through the Canvas API.
Minimal example
Section titled “Minimal example”import {QRCodeCanvasRenderer} from '@qrcodesdk/browser';import {qrcode} from '@qrcodesdk/core';
const canvas = qrcode('https://qrcodesdk.dev').render(QRCodeCanvasRenderer());See the Canvas element section of Render QR codes in the browser for insertion and compositing examples.
Styled example
Section titled “Styled example”import {QRCodeCanvasRenderer} from '@qrcodesdk/browser';import {qrcode} from '@qrcodesdk/core';import {circle, finder} from '@qrcodesdk/core/style';
const canvas = qrcode('https://qrcodesdk.dev').render( QRCodeCanvasRenderer({ style: {moduleSize: 8, modules: circle(), finder: finder({center: circle(), color: '#2563eb'})}, }),);See Customize appearance for every color, shape, and finder option.
Return value
Section titled “Return value”The renderer synchronously returns a new HTMLCanvasElement. Its pixel width and height are both
style.moduleSize × (matrix width + 2 × style.quietZone). RGBA colors retain their alpha.
Renderer-specific options
Section titled “Renderer-specific options”QRCodeCanvasRendererOptions accepts the
shared visual style and a prepared center image:
| Option | Type | Default | Effect |
|---|---|---|---|
style |
QRCodeVisualStyle |
shared defaults | Applies graphical dimensions, colors, and shapes |
accessibility.ariaLabel |
string |
undefined |
Sets an aria-label attribute |
accessibility.title |
string |
undefined |
Sets a title attribute |
centerImage |
QRCodeCanvasCenterImage |
undefined |
Draws a center image from canvasCenterImage() |
Center image
Section titled “Center image”canvasCenterImage(source, layout?) from @qrcodesdk/browser creates the centerImage value for
Canvas and Image element output in every format:
import {QRCodeCanvasRenderer, canvasCenterImage} from '@qrcodesdk/browser';import {qrcode} from '@qrcodesdk/core';
const logo = new Image();logo.src = '/logo.png';await logo.decode();
const canvas = qrcode('https://qrcodesdk.dev') .errorCorrection('H') .render(QRCodeCanvasRenderer({centerImage: canvasCenterImage(logo, {size: 0.3})}));| Parameter | Type | Default | Effect |
|---|---|---|---|
source |
CanvasImageSource |
required | Loaded browser image source |
layout.size |
number |
0.3 |
Image box as a fraction of matrix width |
layout.padding |
number |
1 |
Cleared border around the image, in modules |
layout.clearBackground |
boolean |
true |
Removes every module the image or its padding touches |
Renderer-specific constraints
Section titled “Renderer-specific constraints”- The renderer is synchronous. An image source must be loaded and expose positive intrinsic dimensions before rendering; unloaded or zero-sized sources throw.
- The center-image source may be any ready
CanvasImageSource, such as anHTMLImageElement, another canvas, or anImageBitmap. - Pass a
centerImagecreated withcanvasCenterImage(); TypeScript checks the factory result type.canvasCenterImage()validates its layout when it is called. - A non-empty
ariaLabel, or a non-emptytitlewhenariaLabelis blank, setsrole="img"andaria-labelon the Canvas element. The Canvas is decorative only when both are blank. For more accessibility controls, use the Image element renderers for native image attributes. - Option values are copied when
QRCodeCanvasRenderer()is called. The center image retains its source by identity and checks it when rendering.
Related guides
Section titled “Related guides”- Customize appearance for shared styling and scan reliability.
- Add a center image for loading browser image sources.
- Render QR codes in the browser for DOM insertion and layout.
- Download or save for
toBlob()export.
Related pages
Section titled “Related pages”Support QRCodeSDK
If you find QRCodeSDK useful, please consider sponsoring the project!