Created
August 28, 2013 10:25
-
-
Save SoftCreatR/6364568 to your computer and use it in GitHub Desktop.
QRCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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