Skip to content

Commit 58c8167

Browse files
committed
src/jquery.qrcode.js including <table> build from imbcmdth (Jon-Carlos Rivera)
1 parent af5e6ef commit 58c8167

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/jquery.qrcode.js

+45-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
var qrcode = new QRCode(options.typeNumber, options.correctLevel);
2020
qrcode.addData(options.text);
2121
qrcode.make();
22+
2223
// create canvas element
2324
var canvas = document.createElement('canvas');
2425
canvas.width = options.width;
2526
canvas.height = options.height;
2627
var ctx = canvas.getContext('2d');
28+
2729
// compute tileW/tileH based on options.width/options.height
2830
var tileW = options.width / qrcode.getModuleCount();
2931
var tileH = options.height / qrcode.getModuleCount();
32+
3033
// draw in the canvas
3134
for( var row = 0; row < qrcode.getModuleCount(); row++ ){
3235
for( var col = 0; col < qrcode.getModuleCount(); col++ ){
@@ -38,9 +41,49 @@
3841
return canvas;
3942
}
4043

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+
4183
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);
4487
});
4588
};
4689
})( jQuery );

0 commit comments

Comments
 (0)