Added QrCode.drawCanvas() to JavaScript version of the library, by moving it from the demo program.

This commit is contained in:
Nayuki Minase 2016-04-22 16:10:35 +00:00
parent 671237fff2
commit 9a832a200d
2 changed files with 20 additions and 11 deletions

View File

@ -76,17 +76,7 @@ function redrawQrCode() {
var scale = parseInt(document.getElementById("scale-input").value, 10);
if (scale <= 0 || scale > 30)
return;
// Draw QR Code onto canvas
var width = (qr.size + border * 2) * scale;
canvas.width = width;
canvas.height = width;
var ctx = canvas.getContext("2d");
for (var y = -border; y < qr.size + border; y++) {
for (var x = -border; x < qr.size + border; x++) {
ctx.fillStyle = qr.getModule(x, y) == 1 ? "#000000" : "#FFFFFF";
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
}
}
qr.drawCanvas(scale, border, canvas);
canvas.style.removeProperty("display");
} else {
var code = qr.toSvgString(border);

View File

@ -37,6 +37,7 @@
* - Fields int version, size, mask
* - Field QrCode.Ecc errorCorrectionLevel
* - Method getModule(int x, int y) -> int
* - Method drawCanvas(int scale, int border, HTMLCanvasElement canvas) -> void
* - Method toSvgString(int border) -> str
* - Enum Ecc:
* - Constants LOW, MEDIUM, QUARTILE, HIGH
@ -185,6 +186,24 @@ var qrcodegen = new function() {
/*---- Public instance methods ----*/
// Draws this QR Code symbol with the given module scale and number of modules onto the given HTML canvas element.
// The canvas will be resized to a width and height of (this.size + border * 2) * scale. The painted image will be purely
// black and white with no transparent regions. The scale must be a positive integer, and the border must be a non-negative integer.
this.drawCanvas = function(scale, border, canvas) {
if (scale <= 0 || border < 0)
throw "Value out of range";
var width = (size + border * 2) * scale;
canvas.width = width;
canvas.height = width;
var ctx = canvas.getContext("2d");
for (var y = -border; y < size + border; y++) {
for (var x = -border; x < size + border; x++) {
ctx.fillStyle = this.getModule(x, y) == 1 ? "#000000" : "#FFFFFF";
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
}
}
};
// Based on the given number of border modules to add as padding, this returns a
// string whose contents represents an SVG XML file that depicts this QR Code symbol.
this.toSvgString = function(border) {