Skip to content

marking qr code as downloadable image #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ Size in pixels
#### image
- If you want to render qr code into image element set this attribute to `true`.

#### download
- Mark an image as downloadable

#### fileName
- Name of downloaded image

# Contributing

Contributions are welcome. Please make a pull request against canary branch, use [Git Commit Message Conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#heading=h.uyo6cb12dt6w) and do not bump versions. Also include tests.
Expand Down
33 changes: 24 additions & 9 deletions src/angular-qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@

return $scope.checkInputMode(inputMode, text) ? inputMode : '';
};

$scope.getFileName = function() {
return $scope.fileName || 'qrcode.png';
};
}])
.directive('qr', ['$timeout', '$window', function($timeout, $window){

Expand All @@ -84,7 +88,8 @@
inputMode: '=',
size: '=',
text: '=',
image: '='
image: '=',
fileName: '='
},
controller: 'QrCtrl',
link: function postlink(scope, element, attrs){
Expand All @@ -93,15 +98,19 @@
throw new Error('The `text` attribute is required.');
}

var canvas = element.find('canvas')[0];
var $canvas = element.find('canvas');
var canvas = $canvas[0];
var canvas2D = !!$window.CanvasRenderingContext2D;

var link = 'download' in attrs ? document.createElement('a') : '';

scope.TYPE_NUMBER = scope.getTypeNumeber();
scope.TEXT = scope.getText();
scope.CORRECTION = scope.getCorrection();
scope.SIZE = scope.getSize();
scope.INPUT_MODE = scope.getInputMode(scope.TEXT);
scope.canvasImage = '';
scope.FILE_NAME = scope.getFileName();

var draw = function(context, qr, modules, tile){
for (var row = 0; row < modules; row++) {
Expand All @@ -114,7 +123,7 @@
}
};

var render = function(canvas, value, typeNumber, correction, size, inputMode){
var render = function(canvas, value, typeNumber, correction, size, inputMode, fileName){
var trim = /^\s+|\s+$/g;
var text = value.replace(trim, '');

Expand All @@ -132,44 +141,50 @@
draw(context, qr, modules, tile);
scope.canvasImage = canvas.toDataURL() || '';
}

if (link) {
link.download = fileName;
link.href = canvas.toDataURL() || '';
element.wrap(link);
}
};

render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE);
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.FILE_NAME);

$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);
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.FILE_NAME);
}
});

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);
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.FILE_NAME);
}
});

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);
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.FILE_NAME);
}
});

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);
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.FILE_NAME);
}
});

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);
render(canvas, scope.TEXT, scope.TYPE_NUMBER, scope.CORRECTION, scope.SIZE, scope.INPUT_MODE, scope.FILE_NAME);
}
});
});
Expand Down