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

pure-svg-code

v1.0.6

Published

generate qrcode & barcode to svg in pure javascript

Downloads

3,949

Readme

Npm Package Build Status Npm Downloads

pure-svg-code

Generate qrcode and barcode

Feature

  • SVG: can be used in any client support render svg like browser & 小程序 & Node.js;
  • Small: write in pure javascript, no dependency;
  • Typescript: support Typescript;

Install

Install from npm:

npm i pure-svg-code

Import it:

// import both
const {barcode,qrcode,svg2url} = require('pure-svg-code');

// import as you need
const barcode = require('pure-svg-code/barcode');
const qrcode = require('pure-svg-code/qrcode');
const svg2url = require('pure-svg-code/svg2url');

File size:

  • qrcode: minified 17Kb, Gzip 5.7Kb
  • barcode: minified 19Kb, Gzip 6.2Kb

qrcode

const svgString = qrcode({
  content: "http://github.com/",
  padding: 4,
  width: 256,
  height: 256,
  color: "#000000",
  background: "#ffffff",
  ecl: "M"    
})

Output svg content:

<?xml version="1.0" standalone="yes"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
<rect x="0" y="0" width="256" height="256" style="fill:#ffffff;shape-rendering:crispEdges;"/>
<rect x="35.310344827586206" y="35.310344827586206" width="8.827586206896552" height="8.827586206896552" style="fill:#000000;shape-rendering:crispEdges;"/>
<rect x="44.13793103448276" y="35.310344827586206" width="8.827586206896552" height="8.827586206896552" style="fill:#000000;shape-rendering:crispEdges;"/>
...
</svg>

List of options:

  • content - QR Code content, required
  • padding - white space padding, 4 modules by default, 0 for no border
  • width - QR Code width in pixels
  • height - QR Code height in pixels
  • color - color of modules, color name or hex string, e.g. #000000
  • background - color of background, color name or hex string, e.g. white
  • ecl - error correction level: L, M, H, Q

barcode

Set it up and specify your type and options. The following 3 are the only required ones.

var svgString = barcode("9234567890128", "ean13", {width:'50', barWidth:1, barHeight:50});

Output svg content:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 50" >
  <rect width="2.0" height="50" x="0.0" y="0"/>
  <rect width="3.9" height="50" x="3.9" y="0"/>
  ...
</svg>

Support types:

  • codabar
  • code11 (code 11)
  • code39 (code 39)
  • code93 (code 93)
  • code128 (code 128)
  • ean8 (ean 8)
  • ean13 (ean 13)
  • std25 (standard 2 of 5 - industrial 2 of 5)
  • int25 (interleaved 2 of 5)

Support options:

  • barHeight height of svg (default: 30);
  • width width of svg (default: 100);
  • bgColor background color css like (default: 'transparent');
  • color barcode color (default: '#000000');
  • showHRI: should show text under bar;

Use it with <img/> tag

const {qrcode,svg2url} = require('pure-svg-code');
const svgString = qrcode('data');
const url = svg2url(svgString);

// set img element's src to url
imgEle.src = url;

This way can be used for browser & 小程序 or any client can render svg.

Use it in Node.js server

const {qrcode} = require('pure-svg-code');

app.get('/',function(req, res) {
    const svgString = qrcode('data');
    res.send(svgString)
})