Skip to content

Instantly share code, notes, and snippets.

@SoftCreatR
Created August 28, 2013 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SoftCreatR/6364576 to your computer and use it in GitHub Desktop.
Save SoftCreatR/6364576 to your computer and use it in GitHub Desktop.
QRCode
/**
* QRCode generator
*
* @author Sascha Greuel
* @copyright 2010-2013 Sascha Greuel
* @license Creative Commons BY-SA <http://creativecommons.org/licenses/by-sa/3.0/>
*/
var qrgen = function () {
// Get options
var options = arguments[1];
/**
* Set options
*/
options = function () {
var i = 0,
key = '';
for (i = 1; i < arguments.length; i += 1) {
for (key in arguments[i]) {
arguments[i].hasOwnProperty(key) && (arguments[0][key] = arguments[i][key])
}
}
return arguments[0]
}({}, {
text: (typeof options === 'string' ? options : (typeof options === 'undefined' ? 'QR-Code-Plugin - Copyright 2013 by SoftCreatR.de' : options)),
width: 100,
height: 100,
background: '#ffffff',
foreground: '#000000'
}, options);
// create qrcode
var qrcode = new QRCode(-1, QRErrorCorrectLevel.M);
qrcode.addData(options.text.replace(/^\s+|\s+$/g, ''));
qrcode.make();
/**
* Create canvas object
*/
var createCanvas = function () {
var tileW = tileH = row = col = w = h = 0,
canvas = ctx = {};
// create canvas element
canvas = document.createElement('canvas');
canvas.width = options.width;
canvas.height = options.height;
ctx = canvas.getContext('2d');
// compute tileW/tileH based on options.width/options.height
tileW = options.width / qrcode.getModuleCount();
tileH = options.height / qrcode.getModuleCount();
// draw in the canvas
for (row = 0; row < qrcode.getModuleCount(); row += 1) {
for (col = 0; col < qrcode.getModuleCount(); col += 1) {
ctx.fillStyle = (qrcode.isDark(row, col) ? options.foreground : options.background), w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW), h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW), ctx.fillRect(Math.round(col * tileW), Math.round(row * tileH), w, h)
}
}
// return built canvas
return canvas
}
/**
* Create table object
*/
var createTable = function () {
var tileW = tileH = rows = cols = 0,
table = row = col = {};
// create table element
table = document.createElement('table');
table.style.width = options.width + 'px';
table.style.height = options.height + 'px';
table.style.border = '0px';
table.style.borderCollapse = 'collapse';
table.style.backgroundColor = options.background;
// compute tileW/tileH based on options.width/options.height
tileW = options.width / qrcode.getModuleCount();
tileH = options.height / qrcode.getModuleCount();
// draw in the table
for (rows = 0; rows < qrcode.getModuleCount(); rows += 1) {
for (row = document.createElement('tr'), row.style.height = tileH + 'px', table.appendChild(row), cols = 0; cols < qrcode.getModuleCount(); cols += 1) {
col = document.createElement('td'), col.style.width = tileW + 'px', col.style.backgroundColor = (qrcode.isDark(rows, cols) ? options.foreground : options.background), row.appendChild(col)
}
}
// return built table
return table;
}
// output qrcode based on the browsers capabilities
try {
document.createElement('canvas').getContext('2d');
arguments[0].appendChild(createCanvas());
} catch (e) {
arguments[0].appendChild(createTable());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment