Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/ninja.qrcode.js
248 views
1
(function (ninja) {
2
var qrC = ninja.qrCode = {
3
// determine which type number is big enough for the input text length
4
getTypeNumber: function (text) {
5
var lengthCalculation = text.length * 8 + 12; // length as calculated by the QRCode
6
if (lengthCalculation < 72) { return 1; }
7
else if (lengthCalculation < 128) { return 2; }
8
else if (lengthCalculation < 208) { return 3; }
9
else if (lengthCalculation < 288) { return 4; }
10
else if (lengthCalculation < 368) { return 5; }
11
else if (lengthCalculation < 480) { return 6; }
12
else if (lengthCalculation < 528) { return 7; }
13
else if (lengthCalculation < 688) { return 8; }
14
else if (lengthCalculation < 800) { return 9; }
15
else if (lengthCalculation < 976) { return 10; }
16
return null;
17
},
18
19
createCanvas: function (text, sizeMultiplier) {
20
sizeMultiplier = (sizeMultiplier == undefined) ? 2 : sizeMultiplier; // default 2
21
// create the qrcode itself
22
var typeNumber = qrC.getTypeNumber(text);
23
var qrcode = new QRCode(typeNumber, QRCode.ErrorCorrectLevel.H);
24
qrcode.addData(text);
25
qrcode.make();
26
var width = qrcode.getModuleCount() * sizeMultiplier;
27
var height = qrcode.getModuleCount() * sizeMultiplier;
28
// create canvas element
29
var canvas = document.createElement('canvas');
30
var scale = 10.0;
31
canvas.width = width * scale;
32
canvas.height = height * scale;
33
canvas.style.width = width + 'px';
34
canvas.style.height = height + 'px';
35
var ctx = canvas.getContext('2d');
36
ctx.scale(scale, scale);
37
// compute tileW/tileH based on width/height
38
var tileW = width / qrcode.getModuleCount();
39
var tileH = height / qrcode.getModuleCount();
40
// draw in the canvas
41
for (var row = 0; row < qrcode.getModuleCount() ; row++) {
42
for (var col = 0; col < qrcode.getModuleCount() ; col++) {
43
ctx.fillStyle = qrcode.isDark(row, col) ? "#000000" : "#ffffff";
44
ctx.fillRect(col * tileW, row * tileH, tileW, tileH);
45
}
46
}
47
// return just built canvas
48
return canvas;
49
},
50
51
// show QRCodes with canvas
52
// parameter: keyValuePair
53
// example: { "id1": "string1", "id2": "string2"}
54
// "id1" is the id of a div element where you want a QRCode inserted.
55
// "string1" is the string you want encoded into the QRCode.
56
showQrCode: function (keyValuePair, sizeMultiplier) {
57
for (var key in keyValuePair) {
58
var value = keyValuePair[key];
59
try {
60
if (document.getElementById(key)) {
61
document.getElementById(key).innerHTML = "";
62
document.getElementById(key).appendChild(qrC.createCanvas(value, sizeMultiplier));
63
}
64
}
65
catch (e) { }
66
}
67
}
68
};
69
})(ninja);
70