|
19 | 19 | var qrcode = new QRCode(options.typeNumber, options.correctLevel);
|
20 | 20 | qrcode.addData(options.text);
|
21 | 21 | qrcode.make();
|
| 22 | + |
22 | 23 | // create canvas element
|
23 | 24 | var canvas = document.createElement('canvas');
|
24 | 25 | canvas.width = options.width;
|
25 | 26 | canvas.height = options.height;
|
26 | 27 | var ctx = canvas.getContext('2d');
|
| 28 | + |
27 | 29 | // compute tileW/tileH based on options.width/options.height
|
28 | 30 | var tileW = options.width / qrcode.getModuleCount();
|
29 | 31 | var tileH = options.height / qrcode.getModuleCount();
|
| 32 | + |
30 | 33 | // draw in the canvas
|
31 | 34 | for( var row = 0; row < qrcode.getModuleCount(); row++ ){
|
32 | 35 | for( var col = 0; col < qrcode.getModuleCount(); col++ ){
|
|
38 | 41 | return canvas;
|
39 | 42 | }
|
40 | 43 |
|
| 44 | + // from Jon-Carlos Rivera (https://github.com/imbcmdth) |
| 45 | + var createTable = function(){ |
| 46 | + // create the qrcode itself |
| 47 | + var qrcode = new QRCode(options.typeNumber, options.correctLevel); |
| 48 | + qrcode.addData(options.text); |
| 49 | + qrcode.make(); |
| 50 | + |
| 51 | + var $table, $row, $col; |
| 52 | + var row, col; |
| 53 | + var tileS; |
| 54 | + var border_width = (options.width + options.height) / 20; // 10% of the average(width, height) |
| 55 | + |
| 56 | + |
| 57 | + // create table element |
| 58 | + $table = $('<table></table>') |
| 59 | + .css("width", options.width+"px") |
| 60 | + .css("height", options.height+"px") |
| 61 | + .css("border", "0px") |
| 62 | + .css("border-collapse", "collapse") |
| 63 | + .css("margin", border_width+"px") |
| 64 | + .css('background-color', "#ffffff"); |
| 65 | + |
| 66 | + // compute tileS percentage |
| 67 | + tileS = 100 / qrcode.getModuleCount(); |
| 68 | + |
| 69 | + // draw in the table |
| 70 | + for(row = 0; row < qrcode.getModuleCount(); row++ ){ |
| 71 | + $row = $('<tr></tr>').css('height', tileS+"%").appendTo($table); |
| 72 | + |
| 73 | + for(col = 0; col < qrcode.getModuleCount(); col++ ){ |
| 74 | + $col = $('<td></td>').css('width', tileS+"%").appendTo($row); |
| 75 | + $col.css('background-color', qrcode.isDark(row, col) ? "#000000" : "#ffffff"); |
| 76 | + } |
| 77 | + } |
| 78 | + // return just built canvas |
| 79 | + return $table; |
| 80 | + } |
| 81 | + |
| 82 | + |
41 | 83 | return this.each(function(){
|
42 |
| - var canvas = createCanvas(); |
43 |
| - jQuery(canvas).appendTo(this); |
| 84 | + //var element = createCanvas(); |
| 85 | + var element = createTable(); |
| 86 | + jQuery(element).appendTo(this); |
44 | 87 | });
|
45 | 88 | };
|
46 | 89 | })( jQuery );
|
0 commit comments