Skip to content

Instantly share code, notes, and snippets.

@SoftCreatR
Created August 28, 2013 10:25
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/6364568 to your computer and use it in GitHub Desktop.
Save SoftCreatR/6364568 to your computer and use it in GitHub Desktop.
QRCode
(function ($) {
$.fn.qrcode = function (options) {
// if options is string,
if (typeof options === 'string') {
options = {
text: options
};
}
// set default values
options = $.extend({}, {
width: 100,
height: 100,
background: "#ffffff",
foreground: "#000000"
}, options);
var create = function () {
// create the qrcode itself
var qrcode = new QRCode(-1, QRErrorCorrectLevel.H),
canvas,
ctx,
tileW = 0,
tileH = 0,
row = 0,
col = 0,
w = 0,
h = 0;
qrcode.addData(options.text);
qrcode.make();
// 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 just built canvas
return canvas;
}
return this.each(function () {
var element = create();
jQuery(element).appendTo(this);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment