4.1.4 • Published 7 months ago

@oleksii-pavlov/qr-codes v4.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

@oleksii-pavlov/qr-codes

Overview

This library provides a flexible and powerful solution for generating QR codes, customizing their design, and downloading them as images. It is designed to handle a variety of use cases, such as encoding messages, setting error correction levels, and exporting QR codes in different file formats.


Installation

To install the library, run the following command:

npm install @oleksii-pavlov/qr-codes

Usage

Importing the Library

import { QRCode, Printer, Downloader } from '@oleksii-pavlov/qr-codes'

Generating a QR Code

To generate a QR code, use the QRCode.create method. It accepts a configuration object with the message to encode and an optional minimalErrorCorrection parameter.

Example:

const qrMatrix = QRCode.create({
  message: 'Hello, QR Code!',
  minimalErrorCorrection: 'M' // Optional: L, M, Q, H
})

console.log(qrMatrix) // Matrix representation of the QR Code

See Configuration Options


Rendering QR Code

You can use the Printer class to render the QR code

Example:

const qrMatrix = QRCode.create({
  message: 'Hello, QR Code!',
  minimalErrorCorrection: 'M'
})

const printer = new Printer({
  lightColor: '#FFFFFF',
  darkColor: '#000000',
})

const inject = printer.getInjectorBySelector('#container')
inject(qrMatrix)

See Printer Config Options for configuring Printer

See Injection Options


Downloading QR Codes

The Downloader class allows you to download QR codes as image files in various formats (e.g., PNG, JPEG, WebP).

Example:

const qrMatrix = QRCode.create({
  message: 'Hello, QR Code!',
  minimalErrorCorrection: 'M' // or use ERROR_CORRECTION_M from this package
})

const printer = new Printer({
  lightColor: '#FFFFFF',
  darkColor: '#000000',
  output: 'canvas' // or use canvasEngine from this package
})

const downloader = new Downloader({
  fileName: 'my-qr-code',
  fileType: 'png' // or use fileTypePNG
})

// create a canvas containing QR Code
const canvasElement = printer.print(qrCodeMatrix)

// download canvas as PNG
downloader.download(canvasElement)

See Configuration Options

See Download Options


Configuration Options

QRCodeConfig

PropertyTypeDescription
messagestringThe message to encode in the QR code.
minimalErrorCorrectionErrorCorrection(Optional) Error correction level (L, M, Q, H).

Injection options

  • getInjectorBySelector(selector: string) - returns injection callback by selector (created element will fill container)
  • getInjectorByElement(element: HTMLElement) - returns injection callback by element (created element will fill container)
  • injectContent<T extends HTMLElement>(container: T, content: QRCodeContent): void - injects QR Code content to specified container (fills container)
  • injectElement<C extends HTMLElement, E extends Element>(container: C, element: E): void - injects Graphic Element (Canvas or SVG) to container (fills container)
  • print<T extends Element>(content: QRCodeContent, size?: number): T - creates Graphic Element (Canvas or SVG) with given size

PrinterConfig

PropertyTypeDescription
lightColorstringThe background color of the QR code.
darkColorstringThe foreground color of the QR code.
outputEngineTokenThe output mode
paddingCellsnumberThe padding around the QR code.
designDesignTokenThe design pattern for QR code customization.
resolutionIncreaseCoefficientnumberThe resolution scaling factor.

Downloader Config

PropertyTypeDescription
fileNamestringThe name of the downloaded file.
fileTypeFileTypeThe file format (e.g., png, jpeg, webp).

Download options

  • download<T extends Element>(element: T): void - downloads image from given element (considering fileType)
  • downloadFromCanvas(canvas: HTMLCanvasElement): void - downloads image from canvas
  • downloadFromCanvasContainer(container: HTMLElement): void - downloads image from element that contains canvas
  • downloadFromSVG(svg: SVGSVGElement): void - downloads image from svg
  • downloadFromSVGContainer(container: HTMLElement): void - downloads image from element that contains svg

Exported Constants

File Types

  • fileTypePNG
  • fileTypeJPEG
  • fileTypeWebp
  • fileTypeSVG

Designs (for rendering)

  • designClassic
  • designCircles
  • designLiquid
  • designLiquidOil
  • designOil

EngineToken

  • canvasEngine
  • svgEngine

Error Correction Levels

  • ERROR_CORRECTION_L
  • ERROR_CORRECTION_M
  • ERROR_CORRECTION_Q
  • ERROR_CORRECTION_H
4.1.4

7 months ago

4.1.3

7 months ago

4.1.2

7 months ago

4.1.1

7 months ago

4.1.0

7 months ago

4.0.0

7 months ago

3.0.0

7 months ago

2.2.0

7 months ago

2.1.16

7 months ago

2.1.15

7 months ago

2.1.14

7 months ago

2.1.13

7 months ago

2.1.12

8 months ago

2.1.11

8 months ago

2.1.10

8 months ago

2.1.9

10 months ago

2.1.8

10 months ago

2.1.7

10 months ago

2.1.6

10 months ago

2.1.5

10 months ago

2.1.4

10 months ago

2.1.3

10 months ago

2.1.1

10 months ago

2.1.0

10 months ago

2.0.0

10 months ago

1.1.0

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.2.8

10 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago