Created
March 6, 2016 03:50
-
-
Save nurfarazi/02460a9514499d71376c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// typeNumber < 1 for automatic calculation | |
options = $.extend( {}, { | |
render : "canvas", | |
width : 256, | |
height : 256, | |
typeNumber : -1, | |
correctLevel : QRErrorCorrectLevel.H | |
}, options); | |
var createCanvas = function(){ | |
// create the qrcode itself | |
var qrcode = new QRCode(options.typeNumber, options.correctLevel); | |
qrcode.addData(options.text); | |
qrcode.make(); | |
// create canvas element | |
var canvas = document.createElement('canvas'); | |
canvas.width = options.width; | |
canvas.height = options.height; | |
var ctx = canvas.getContext('2d'); | |
// compute tileW/tileH based on options.width/options.height | |
var tileW = options.width / qrcode.getModuleCount(); | |
var tileH = options.height / qrcode.getModuleCount(); | |
// draw in the canvas | |
for( var row = 0; row < qrcode.getModuleCount(); row++ ){ | |
for( var col = 0; col < qrcode.getModuleCount(); col++ ){ | |
ctx.fillStyle = qrcode.isDark(row, col) ? "#000000" : "#ffffff"; | |
ctx.fillRect( col*tileW, row*tileH, tileW, tileH ); | |
} | |
} | |
// return just built canvas | |
return canvas; | |
} | |
// from Jon-Carlos Rivera (https://github.com/imbcmdth) | |
var createTable = function(){ | |
// create the qrcode itself | |
var qrcode = new QRCode(options.typeNumber, options.correctLevel); | |
qrcode.addData(options.text); | |
qrcode.make(); | |
// create table element | |
var $table = $('<table></table>') | |
.css("width", options.width+"px") | |
.css("height", options.height+"px") | |
.css("border", "0px") | |
.css("border-collapse", "collapse") | |
.css('background-color', "#ffffff"); | |
// compute tileS percentage | |
var tileW = options.width / qrcode.getModuleCount(); | |
var tileH = options.height / qrcode.getModuleCount(); | |
// draw in the table | |
for(var row = 0; row < qrcode.getModuleCount(); row++ ){ | |
var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table); | |
for(var col = 0; col < qrcode.getModuleCount(); col++ ){ | |
$('<td></td>') | |
.css('width', tileW+"px") | |
.css('background-color', qrcode.isDark(row, col) ? "#000000" : "#ffffff") | |
.appendTo($row); | |
} | |
} | |
// return just built canvas | |
return $table; | |
} | |
return this.each(function(){ | |
var element = options.render == "canvas" ? createCanvas() : createTable(); | |
jQuery(element).appendTo(this); | |
}); | |
}; | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment