From 198fc0210ecdcf43a126ae7efb9893bca6b2b2a1 Mon Sep 17 00:00:00 2001 From: Peter Beard Date: Wed, 19 Nov 2014 02:16:06 -0500 Subject: [PATCH] Added support for drawing directly to tags. --- js/qrcode.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/js/qrcode.js b/js/qrcode.js index 8732a64..713a788 100644 --- a/js/qrcode.js +++ b/js/qrcode.js @@ -464,9 +464,30 @@ var qrcode = function() { } ); }; + _this.drawOnCanvas = function(canvas, cellSize, margin) { + + cellSize = cellSize || 2; + margin = (typeof margin == 'undefined')? cellSize * 4 : margin; + + var size = _this.getModuleCount() * cellSize + margin * 2; + var min = margin; + var max = size - margin; + + drawOnCanvas(canvas, size, size, function(x, y) { + if (min <= x && x < max && min <= y && y < max) { + var c = Math.floor( (x - min) / cellSize); + var r = Math.floor( (y - min) / cellSize); + return _this.isDark(r, c)? 0 : 1; + } else { + return 1; + } + } ); + }; + return _this; }; + //--------------------------------------------------------------------- // qrcode.stringToBytes //--------------------------------------------------------------------- @@ -1627,6 +1648,27 @@ var qrcode = function() { return img; }; + var drawOnCanvas = function(canvas, width, height, getPixel) { + // Set the canvas size + canvas.width = width; + canvas.height = height; + // Get a drawing context + var ctx = canvas.getContext("2d"); + // Fill the background with black + ctx.fillStyle = "#000"; + ctx.fillRect(0,0,width,height); + // Draw the white pixels + ctx.fillStyle = "#fff"; + for(var x = 0; x < width; x++) + { + for(var y = 0; y < height; y++) + { + if(getPixel(x,y)) + ctx.fillRect(x,y,1,1); + } + } + }; + //--------------------------------------------------------------------- // returns qrcode function.