(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, background : "#ffffff", foreground : "#000000" }, 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) ? options.foreground : options.background; var w = (math.ceil((col+1)*tilew) - math.floor(col*tilew)); var 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; } // 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 = $('
') .css("width", options.width+"px") .css("height", options.height+"px") .css("border", "0px") .css("border-collapse", "collapse") .css('background-color', options.background); // 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 = $('').css('height', tileh+"px").appendto($table); for(var col = 0; col < qrcode.getmodulecount(); col++ ){ $('') .css('width', tilew+"px") .css('background-color', qrcode.isdark(row, col) ? options.foreground : options.background) .appendto($row); } } // return just built canvas return $table; } return this.each(function(){ var element = options.render == "canvas" ? createcanvas() : createtable(); $(element).appendto(this); }); }; })( jquery );