-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqr-logo-js.js
More file actions
329 lines (282 loc) · 9.43 KB
/
Copy pathqr-logo-js.js
File metadata and controls
329 lines (282 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const QRCodeGenerator = function (options) {
// Defaults
const defaultOptions = {
value: "https://reactjs.org/",
ecLevel: "M",
enableCORS: false,
size: 150,
quietZone: 10,
bgColor: "#FFFFFF",
fgColor: "#000000",
logoImage: "",
logoWidth: 0,
logoHeight: 0,
logoOpacity: 1,
logoOnLoad: null,
removeQrCodeBehindLogo: false,
logoPadding: 0,
logoPaddingStyle: "square",
eyeRadius: [0, 0, 0],
eyeColor: null,
qrStyle: "squares",
style: null,
id: null,
};
const _ = window._;
const qrGenerator = window.qrcode;
this.options = _.assign({}, defaultOptions, options);
this.canvas = document.createElement("canvas");
this.init();
};
QRCodeGenerator.prototype.init = function () {
// Your initialization logic here
this.update();
};
QRCodeGenerator.prototype.utf16to8 = function (str) {
let out = "",
i,
c;
const len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if (c >= 0x0001 && c <= 0x007f) {
out += str.charAt(i);
} else if (c > 0x07ff) {
out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
} else {
out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
}
}
return out;
};
QRCodeGenerator.prototype.drawRoundedSquare = function (lineWidth, x, y, size, color, radii, fill, ctx) {
ctx.lineWidth = lineWidth;
ctx.fillStyle = color;
ctx.strokeStyle = color;
// Adjust coordinates so that the outside of the stroke is aligned to the edges
y += lineWidth / 2;
x += lineWidth / 2;
size -= lineWidth;
if (!Array.isArray(radii)) {
radii = [radii, radii, radii, radii];
}
// Radius should not be greater than half the size or less than zero
radii = radii.map((r) => {
r = Math.min(r, size / 2);
return r < 0 ? 0 : r;
});
const rTopLeft = radii[0] || 0;
const rTopRight = radii[1] || 0;
const rBottomRight = radii[2] || 0;
const rBottomLeft = radii[3] || 0;
ctx.beginPath();
ctx.moveTo(x + rTopLeft, y);
ctx.lineTo(x + size - rTopRight, y);
if (rTopRight) ctx.quadraticCurveTo(x + size, y, x + size, y + rTopRight);
ctx.lineTo(x + size, y + size - rBottomRight);
if (rBottomRight) ctx.quadraticCurveTo(x + size, y + size, x + size - rBottomRight, y + size);
ctx.lineTo(x + rBottomLeft, y + size);
if (rBottomLeft) ctx.quadraticCurveTo(x, y + size, x, y + size - rBottomLeft);
ctx.lineTo(x, y + rTopLeft);
if (rTopLeft) ctx.quadraticCurveTo(x, y, x + rTopLeft, y);
ctx.closePath();
ctx.stroke();
if (fill) {
ctx.fill();
}
};
QRCodeGenerator.prototype.drawPositioningPattern = function (ctx, cellSize, offset, row, col, color, radii = [0, 0, 0, 0]) {
const lineWidth = Math.ceil(cellSize);
let radiiOuter;
let radiiInner;
if (typeof radii !== "number" && !Array.isArray(radii)) {
radiiOuter = radii.outer || 0;
radiiInner = radii.inner || 0;
} else {
radiiOuter = radii;
radiiInner = radiiOuter;
}
let colorOuter;
let colorInner;
if (typeof color !== "string") {
colorOuter = color.outer;
colorInner = color.inner;
} else {
colorOuter = color;
colorInner = color;
}
let y = row * cellSize + offset;
let x = col * cellSize + offset;
let size = cellSize * 7;
// Outer box
this.drawRoundedSquare(lineWidth, x, y, size, colorOuter, radiiOuter, false, ctx);
// Inner box
size = cellSize * 3;
y += cellSize * 2;
x += cellSize * 2;
this.drawRoundedSquare(lineWidth, x, y, size, colorInner, radiiInner, true, ctx);
};
QRCodeGenerator.prototype.isInPositioninZone = function (col, row, zones) {
return zones.some((zone) => row >= zone.row && row <= zone.row + 7 && col >= zone.col && col <= zone.col + 7);
};
QRCodeGenerator.prototype.transformPixelLengthIntoNumberOfCells = function (pixelLength, cellSize) {
return pixelLength / cellSize;
};
QRCodeGenerator.prototype.isCoordinateInImage = function (col, row, dWidthLogo, dHeightLogo, dxLogo, dyLogo, cellSize, logoImage) {
if (logoImage) {
const numberOfCellsMargin = 2;
const firstRowOfLogo = this.transformPixelLengthIntoNumberOfCells(dxLogo, cellSize);
const firstColumnOfLogo = this.transformPixelLengthIntoNumberOfCells(dyLogo, cellSize);
const logoWidthInCells = this.transformPixelLengthIntoNumberOfCells(dWidthLogo, cellSize) - 1;
const logoHeightInCells = this.transformPixelLengthIntoNumberOfCells(dHeightLogo, cellSize) - 1;
return (
row >= firstRowOfLogo - numberOfCellsMargin &&
row <= firstRowOfLogo + logoWidthInCells + numberOfCellsMargin && // check rows
col >= firstColumnOfLogo - numberOfCellsMargin &&
col <= firstColumnOfLogo + logoHeightInCells + numberOfCellsMargin
); // check cols
} else {
return false;
}
};
QRCodeGenerator.prototype.update = function () {
const {
value,
ecLevel,
enableCORS,
bgColor,
fgColor,
logoImage,
logoOpacity,
logoOnLoad,
removeQrCodeBehindLogo,
qrStyle,
eyeRadius,
eyeColor,
logoPaddingStyle,
} = this.options;
// just make sure that these params are passed as numbers
const size = +this.options.size;
const quietZone = +this.options.quietZone;
const logoWidth = this.options.logoWidth ? +this.options.logoWidth : 0;
const logoHeight = this.options.logoHeight ? +this.options.logoHeight : 0;
const logoPadding = this.options.logoPadding ? +this.options.logoPadding : 0;
const qrCode = qrcode(0, ecLevel);
qrCode.addData(this.utf16to8(value));
qrCode.make();
const canvas = this.canvas;
const ctx = canvas.getContext("2d");
const canvasSize = size + 2 * quietZone;
const length = qrCode.getModuleCount();
const cellSize = size / length;
const scale = window.devicePixelRatio || 1;
canvas.height = canvas.width = canvasSize * scale;
ctx.scale(scale, scale);
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvasSize, canvasSize);
const offset = quietZone;
const positioningZones = [
{ row: 0, col: 0 },
{ row: 0, col: length - 7 },
{ row: length - 7, col: 0 },
];
ctx.strokeStyle = fgColor;
if (qrStyle === "dots") {
ctx.fillStyle = fgColor;
const radius = cellSize / 2;
for (let row = 0; row < length; row++) {
for (let col = 0; col < length; col++) {
if (qrCode.isDark(row, col) && !this.isInPositioninZone(row, col, positioningZones)) {
ctx.beginPath();
ctx.arc(
Math.round(col * cellSize) + radius + offset,
Math.round(row * cellSize) + radius + offset,
(radius / 100) * 75,
0,
2 * Math.PI,
false
);
ctx.closePath();
ctx.fill();
}
}
}
} else {
for (let row = 0; row < length; row++) {
for (let col = 0; col < length; col++) {
if (qrCode.isDark(row, col) && !this.isInPositioninZone(row, col, positioningZones)) {
ctx.fillStyle = fgColor;
const w = Math.ceil((col + 1) * cellSize) - Math.floor(col * cellSize);
const h = Math.ceil((row + 1) * cellSize) - Math.floor(row * cellSize);
ctx.fillRect(Math.round(col * cellSize) + offset, Math.round(row * cellSize) + offset, w, h);
}
}
}
}
// Draw positioning patterns
for (let i = 0; i < 3; i++) {
const { row, col } = positioningZones[i];
let radii = eyeRadius;
let color;
if (Array.isArray(radii)) {
radii = radii[i];
}
if (typeof radii == "number") {
radii = [radii, radii, radii, radii];
}
if (!eyeColor) {
// if not specified, eye color is the same as foreground,
color = fgColor;
} else {
if (Array.isArray(eyeColor)) {
// if array, we pass the single color
color = eyeColor[i];
} else {
color = eyeColor;
}
}
this.drawPositioningPattern(ctx, cellSize, offset, row, col, color, radii);
}
if (logoImage) {
const image = new Image();
if (enableCORS) {
image.crossOrigin = "Anonymous";
}
image.onload = () => {
ctx.save();
const dWidthLogo = logoWidth || size * 0.2;
const dHeightLogo = logoHeight || dWidthLogo;
const dxLogo = (size - dWidthLogo) / 2;
const dyLogo = (size - dHeightLogo) / 2;
if (removeQrCodeBehindLogo || logoPadding) {
ctx.beginPath();
ctx.strokeStyle = bgColor;
ctx.fillStyle = bgColor;
const dWidthLogoPadding = dWidthLogo + 2 * logoPadding;
const dHeightLogoPadding = dHeightLogo + 2 * logoPadding;
const dxLogoPadding = dxLogo + offset - logoPadding;
const dyLogoPadding = dyLogo + offset - logoPadding;
if (logoPaddingStyle === "circle") {
const dxCenterLogoPadding = dxLogoPadding + dWidthLogoPadding / 2;
const dyCenterLogoPadding = dyLogoPadding + dHeightLogoPadding / 2;
ctx.ellipse(dxCenterLogoPadding, dyCenterLogoPadding, dWidthLogoPadding / 2, dHeightLogoPadding / 2, 0, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill();
} else {
ctx.fillRect(dxLogoPadding, dyLogoPadding, dWidthLogoPadding, dHeightLogoPadding);
}
}
ctx.globalAlpha = logoOpacity;
ctx.drawImage(image, dxLogo + offset, dyLogo + offset, dWidthLogo, dHeightLogo);
ctx.restore();
if (logoOnLoad) {
logoOnLoad();
}
};
image.src = logoImage;
}
};
window.QRCodeGenerator = QRCodeGenerator;