npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dfts-qrcode

v1.1.0

Published

tiny and simple-to-use Javascript / TypeScript QR-Code generator library. Fully type-safe and esm module compatible

Downloads

278

Readme

dfts-qrcode

dfts-qrcode is a tiny and simple-to-use Javascript / TypeScript QR-Code generator library. Fully type-safe and esm module compatible.

npm version NPM npm bundle size

Installation

npm:

npm install dfts-qrcode@latest

pnpm:

pnpm install dfts-qrcode@latest

Usage

HTMLImageElement

Without center image

import { generateQrCodeImage } from 'dfts-qrcode';

const { image, dataUrl } = generateQrCodeImage('data');

Options:

  • generateOptions
  • generateWithAccessibleOptions

With center image

import { generateQrCodeImage$ } from 'dfts-qrcode';

generateQrCodeImage$('data', {image: {src: './assets/logo.png'}}).then(({image, dataUrl}) => {
  ...
})

Options:

  • generateOptions
  • generateWithAccessibleOptions
  • generateWithImageOptions

HTMLCanvasElement

Without center image

import { generateQrCodeCanvas } from 'dfts-qrcode';

const qrcode = generateQrCodeCanvas('data');

Options:

  • generateOptions

With center image

import { generateQrCodeCanvas$ } from 'dfts-qrcode';

generateQrCodeCanvas$('data', {image: {src: './assets/logo.png'}}).then((qrcode) => {
  ...
})

Options:

  • generateOptions
  • generateWithImageOptions

SVG

Without center image

import { generateQrCodeSVG } from 'dfts-qrcode';

const { svg, dataUrl } = generateQrCodeSVG('data');

Options:

  • generateOptions
  • generateWithAccessibleOptions

Without center image as string

import { generateQrCodeSVGString } from 'dfts-qrcode';

const svgString = generateQrCodeSVGString('data');

Options:

  • generateOptions
  • generateWithAccessibleOptions

With center image

import { generateQrCodeSVG$ } from 'dfts-qrcode';

generateQrCodeSVG$('data', {image: {src: './assets/logo.png'}}).then(({ svg, dataUrl }) => {
  ...
})

Options:

  • generateOptions
  • generateWithAccessibleOptions
  • generateWithImageOptions

QR-Code Matrix

import { generateQrCodeMatrix } from 'dfts-qrcode';

const qrcodeMatrix = generateQrCodeMatrix('data');

Options:

  • generateMatrixOptions

Options

import { ColorValueHex } from './types';

export type QRCodeVersion =
  | -1
  | 0
  | 1
  | 2
  | 3
  | 4
  | 5
  | 6
  | 7
  | 8
  | 9
  | 10
  | 11
  | 12
  | 13
  | 14
  | 15
  | 16
  | 17
  | 18
  | 19
  | 20
  | 21
  | 22
  | 23
  | 24
  | 25
  | 26
  | 27
  | 28
  | 29
  | 30
  | 31
  | 32
  | 33
  | 34
  | 35
  | 36
  | 37
  | 38
  | 39
  | 40;

export type QRCodeErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H';

export type ColorValueHex = `#${string}`;

export type generateMatrixOptions = {
  // If undefined or -1, automatically calculated
  version?: QRCodeVersion;

  // If undefined, automatically calculated
  mode?: 'numeric' | 'alphanumeric' | 'octet';

  // Defaults to L
  errorCorrectionLevel?: QRCodeErrorCorrectionLevel;

  // If undefined or -1, automatically calculated
  mask?: number;
};

export type generateOptions = generateMatrixOptions & {
  // defaults to 5, size in px
  size?: number;

  // defaults to 4, margin in px
  margin?: number;

  colors?: {
    // defaults to '#FFFFFF', hex color
    colorLight?: ColorValueHex;

    // defaults to '#00000', hex color
    colorDark?: ColorValueHex;
  };
};

export type generateWithImageOptions = {
  image?: {
    src?: string;
    height?: number;
    width?: number;
  };
};

export type generateWithAccessibleOptions = {
  alt?: string;
  ariaLabel?: string;
  title?: string;
};

Error correction level

Error correction capability allows to successfully scan a QR Code even if the symbol is dirty or damaged. Four levels are available to choose according to the operating environment.

Higher levels offer a better error resistance but reduce the symbol's capacity. If the chances that the QR Code symbol may be corrupted are low (for example if it is showed through a monitor) is possible to safely use a low error level such as Low or Medium.

Possible levels are shown below:

| Level | Error resistance | | ---------------- | :--------------: | | L (Low) | ~7% | | M (Medium) | ~15% | | Q (Quartile) | ~25% | | H (High) | ~30% |

The percentage indicates the maximum amount of damaged surface after which the symbol becomes unreadable.

Error level can be set through options.errorCorrectionLevel property. If not specified, the default value is L.

import { generateQrCodeMatrix } from 'dfts-qrcode';

generateQrCodeMatrix('data', { errorCorrectionLevel: 'M' });

QR Code capacity

Capacity depends on symbol version and error correction level. Also encoding modes may influence the amount of storable data.

The QR Code versions range from version 1 to version 40. Each version has a different number of modules (black and white dots), which define the symbol's size. For version 1 they are 21x21, for version 2 25x25 e so on. Higher is the version, more are the storable data, and of course bigger will be the QR Code symbol.

The table below shows the maximum number of storable characters in each encoding mode and for each error correction level.

| Mode | L | M | Q | H | | ------------ | ---- | ---- | ---- | ---- | | Numeric | 7089 | 5596 | 3993 | 3057 | | Alphanumeric | 4296 | 3391 | 2420 | 1852 | | Byte | 2953 | 2331 | 1663 | 1273 |

QR Code version can be set through options.version property. If no version is specified, the more suitable value will be used. Unless a specific version is required, this option is not needed.

import { generateQrCodeMatrix } from 'dfts-qrcode';

generateQrCodeMatrix('data', { errorCorrectionLevel: 'M', mode: 'octet', version: 1 });

Encoding modes

Modes can be used to encode a string in a more efficient way. A mode may be more suitable than others depending on the string content. A list of supported modes are shown in the table below:

| Mode | Characters | Compression | | ------------ | ---------------------------------------------------------- | ----------------------------------------- | | Numeric | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | 3 characters are represented by 10 bits | | Alphanumeric | 0–9, A–Z (upper-case only), space, $, %, *, +, -, ., /, : | 2 characters are represented by 11 bits | | Byte | Characters from the ISO/IEC 8859-1 character set | Each characters are represented by 8 bits |