Home > Web Front-end > H5 Tutorial > body text

Pure JS QR code generation plug-in based on canvas

黄舟
Release: 2017-01-19 13:44:42
Original
1385 people have browsed it

Brief Tutorial

qrious is a pure JS QR code generation plug-in based on HTML5 Canvas. Various QR codes can be quickly generated through qrious.js. You can control the size and color of the QR code, and you can also Base64 encode the generated QR code.

Installation

You can install the qrious.js QR code plug-in through bower or npm.

$ npm install --save qrious
$ bower install --save qrious
Copy after login

How to use

To use this QR code generation plug-in, you need to introduce the qrious.js file into the page.

<script type="text/javascript" src="js/qrious.js"></script>
Copy after login

HTML structure

Use a element as a container for QR code images.

<canvas id="qr"></canvas>
Copy after login

Initialization plug-in

You can instantiate an object instance through the QRious() method.

(function() {
  const qr = new QRious({
    element: document.getElementById(&#39;qr&#39;),
    value: &#39;http://www.htmleaf.com/&#39;
  })
})()
Copy after login

If you use it in Node.js, the code is as follows:

const express = require(&#39;express&#39;)
const QRious = require(&#39;qrious&#39;)
 
const app = express()
 
app.get(&#39;/qr&#39;, (req, res) => {
  const qr = new QRious({ value: &#39;http://www.htmleaf.com/&#39; })
 
  res.end(new Buffer(qr.toDataURL(), &#39;base64&#39;))
})
 
app.listen(3000)
Copy after login

Configuration parameters

The available configuration parameters of qrious.js QR code plug-in are as follows:

Pure JS QR code generation plug-in based on canvas

For example:

const qr = new QRious()
qr.background = &#39;#000&#39;
qr.foreground = &#39;#fff&#39;
qr.level = &#39;H&#39;
qr.size = 500
qr.value = &#39;http://www.htmleaf.com/&#39;
Copy after login

Or pass it in the constructor:

const qr = new QRious({
  background: &#39;#000&#39;,
  foreground: &#39;#fff&#39;,
  level: &#39;H&#39;,
  size: 500,
  value: &#39;http://www.htmleaf.com/&#39;
})
Copy after login

You can set the QR code used to generate the QR code in the element parameter DOM element. The DOM element must be a element or an Pure JS QR code generation plug-in based on canvas element.

const qr = new QRious({
  element: document.querySelector(&#39;canvas&#39;),
  value: &#39;http://www.htmleaf.com/&#39;
})
 
qr.canvas.parentNode.appendChild(qr.image)
Copy after login

toDataURL([mime]) method

The toDataURL([mime]) method can generate the URI of the Base64 encoded data of the QR code. If you do not specify a MIME Type, the default value will be used as the mime type.

const qr = new QRious({
  value: &#39;http://www.htmleaf.com/&#39;
})
 
console.log(qr.toDataURL())
//=> "data:image/png;base64,iVBOR...AIpqDnseH86KAAAAAElFTkSuQmCC"
console.log(qr.toDataURL(&#39;image/jpeg&#39;))
//=> "data:image/jpeg;base64,/9j/...xqAqIqgKFAAAAAq3RRQAUUUUAf/Z"
Copy after login

The above is the content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template