From 8fba3e557b77d313e19c2456fab6bdc87fcdefb2 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 3 Jul 2015 14:00:45 +0200 Subject: [PATCH 1/2] feat: allow dark and light colors to be customized --- src/angular-qr.js | 65 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/src/angular-qr.js b/src/angular-qr.js index f333687..39073fe 100644 --- a/src/angular-qr.js +++ b/src/angular-qr.js @@ -27,6 +27,18 @@ return $scope.size || 250; }; + $scope.getSize = function(){ + return $scope.size || 250; + }; + + $scope.getDarkColor = function(){ + return $scope.darkColor || '#000'; + }; + + $scope.getLightColor = function(){ + return $scope.lightColor || '#FFF'; + }; + $scope.isNUMBER = function(text){ var ALLOWEDCHARS = /^[0-9]*$/; return ALLOWEDCHARS.test(text); @@ -77,14 +89,16 @@ return { restrict: 'E', - template: '', + template: '', scope: { typeNumber: '=', correctionLevel: '=', inputMode: '=', size: '=', text: '=', - image: '=' + image: '=', + darkColor: '=', + lightColor: '=' }, controller: 'QrCtrl', link: function postlink(scope, element, attrs){ @@ -101,20 +115,24 @@ scope.CORRECTION = scope.getCorrection(); scope.SIZE = scope.getSize(); scope.INPUT_MODE = scope.getInputMode(scope.TEXT); - scope.canvasImage = ''; + scope.DARK_COLOR = scope.getDarkColor(); + scope.LIGHT_COLOR = scope.getLightColor(); + scope.canvasImage = 'http://lorempixel.com/500/500/'; - var draw = function(context, qr, modules, tile){ + var draw = function(context, qr, modules, tile, darkColor, lightColor){ for (var row = 0; row < modules; row++) { for (var col = 0; col < modules; col++) { var w = (Math.ceil((col + 1) * tile) - Math.floor(col * tile)), h = (Math.ceil((row + 1) * tile) - Math.floor(row * tile)); - context.fillStyle = qr.isDark(row, col) ? '#000' : '#fff'; + context.fillStyle = qr.isDark(row, col) ? darkColor : lightColor; context.fillRect(Math.round(col * tile), Math.round(row * tile), w, h); } } }; - var render = function(canvas, value, typeNumber, correction, size, inputMode){ + var render = function(canvas, value, typeNumber, correction, size, inputMode, darkColor, lightColor){ + console.log("darkColor: "+darkColor); + console.log("lightColor: "+lightColor); var trim = /^\s+|\s+$/g; var text = value.replace(trim, ''); @@ -129,47 +147,66 @@ canvas.width = canvas.height = size; if (canvas2D) { - draw(context, qr, modules, tile); - scope.canvasImage = canvas.toDataURL() || ''; + draw(context, qr, modules, tile, darkColor, lightColor); + scope.canvasImage = canvas.toDataURL(); } }; - render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE); + var rerender = function() { + render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.DARK_COLOR, scope.LIGHT_COLOR); + }; + + rerender(); $timeout(function(){ scope.$watch('text', function(value, old){ if (value !== old) { scope.TEXT = scope.getText(); scope.INPUT_MODE = scope.getInputMode(scope.TEXT); - render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE); + rerender(); } }); scope.$watch('correctionLevel', function(value, old){ if (value !== old) { scope.CORRECTION = scope.getCorrection(); - render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE); + rerender(); } }); scope.$watch('typeNumber', function(value, old){ if (value !== old) { scope.TYPE_NUMBER = scope.getTypeNumeber(); - render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE); + rerender(); } }); scope.$watch('size', function(value, old){ if (value !== old) { scope.SIZE = scope.getSize(); - render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE); + rerender(); } }); scope.$watch('inputMode', function(value, old){ if (value !== old) { scope.INPUT_MODE = scope.getInputMode(scope.TEXT); - render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE); + rerender(); + } + }); + + scope.$watch('darkColor', function(value, old){ + if (value !== old) { + console.log("darkColor changed: "+value); + scope.DARK_COLOR = scope.getDarkColor(); + rerender(); + } + }); + + scope.$watch('lightColor', function(value, old){ + if (value !== old) { + scope.LIGHT_COLOR = scope.getLightColor(); + rerender(); } }); }); From db9ff15581b986ee9b3621fe8b5a66948225ce06 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 3 Jul 2015 15:23:15 +0200 Subject: [PATCH 2/2] feat(demo): allow changing light and dark colors --- demo/index.html | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/demo/index.html b/demo/index.html index c13258a..12df721 100644 --- a/demo/index.html +++ b/demo/index.html @@ -11,8 +11,8 @@