/ src / jquery.qrcode.js
jquery.qrcode.js
  1  (function( $ ){
  2  	$.fn.qrcode = function(options) {
  3  		// if options is string, 
  4  		if( typeof options === 'string' ){
  5  			options	= { text: options };
  6  		}
  7  
  8  		// set default values
  9  		// typeNumber < 1 for automatic calculation
 10  		options	= $.extend( {}, {
 11  			render		: "canvas",
 12  			width		: 256,
 13  			height		: 256,
 14  			margin		: 4,
 15  			typeNumber	: -1,
 16  			correctLevel	: QRErrorCorrectLevel.H,
 17                          background      : "#ffffff",
 18                          foreground      : "#000000"
 19  		}, options);
 20  
 21  		var createCanvas	= function(){
 22  			// create the qrcode itself
 23  			var qrcode	= new QRCode(options.typeNumber, options.correctLevel);
 24  			qrcode.addData(options.text);
 25  			qrcode.make();
 26  
 27  			// create canvas element
 28  			var canvas	= document.createElement('canvas');
 29  			canvas.width	= options.width;
 30  			canvas.height	= options.height;
 31  			var ctx		= canvas.getContext('2d');
 32  
 33  			// compute tileW/tileH based on options.width/options.height
 34  			var margin	= options.margin;
 35  			var tileW	= options.width  / (qrcode.getModuleCount() + 2*options.margin);
 36  			var tileH	= options.height / (qrcode.getModuleCount() + 2*options.margin);
 37  
 38  			var isDark = function(x, y) {
 39  				x -= margin; y -= margin;
 40  				if(x < 0 || x >= qrcode.getModuleCount() || y < 0 || y >= qrcode.getModuleCount())
 41  				    return false;
 42  				else
 43  				    return qrcode.isDark(x, y);
 44  			}
 45  
 46  			// draw in the canvas
 47  			for( var row = 0; row < qrcode.getModuleCount()+2*margin; row++ ){
 48  				for( var col = 0; col < qrcode.getModuleCount()+2*margin; col++ ){
 49  					ctx.fillStyle = isDark(row, col) ? options.foreground : options.background;
 50  					var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
 51  					var h = (Math.ceil((row+1)*tileH) - Math.floor(row*tileH));
 52  					ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);  
 53  				}	
 54  			}
 55  			// return just built canvas
 56  			return canvas;
 57  		}
 58  
 59  		// from Jon-Carlos Rivera (https://github.com/imbcmdth)
 60  		var createTable	= function(){
 61  			// create the qrcode itself
 62  			var qrcode	= new QRCode(options.typeNumber, options.correctLevel);
 63  			qrcode.addData(options.text);
 64  			qrcode.make();
 65  			
 66  			// create table element
 67  			var $table	= $('<table></table>')
 68  				.css("width", options.width+"px")
 69  				.css("height", options.height+"px")
 70  				.css("border", "0px")
 71  				.css("border-collapse", "collapse")
 72  				.css('background-color', options.background);
 73  		  
 74  			// compute tileS percentage
 75  			var margin	= options.margin;
 76  			var tileW	= options.width / (qrcode.getModuleCount() + 2*margin);
 77  			var tileH	= options.height / (qrcode.getModuleCount() + 2*margin);
 78  
 79  			var isDark = function(x, y) {
 80  				x -= margin; y -= margin;
 81  				if(x < 0 || x >= qrcode.getModuleCount() || y < 0 || y >= qrcode.getModuleCount())
 82  				    return false;
 83  				else
 84  				    return qrcode.isDark(x, y);
 85  			}
 86  
 87  			// draw in the table
 88  			for(var row = 0; row < (qrcode.getModuleCount() + 2*margin); row++ ){
 89  				var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
 90  				
 91  				for(var col = 0; col < (qrcode.getModuleCount() + 2*margin); col++ ){
 92  					$('<td></td>')
 93  						.css('width', tileW+"px")
 94  						.css('background-color', isDark(row, col) ? options.foreground : options.background)
 95  						.appendTo($row);
 96  				}	
 97  			}
 98  			// return just built canvas
 99  			return $table;
100  		}
101    
102  
103  		return this.each(function(){
104  			var element	= options.render == "canvas" ? createCanvas() : createTable();
105  			$(element).appendTo(this);
106  		});
107  	};
108  })( jQuery );