forked from legalthings/angular-signature
/
signature.js
117 lines (96 loc) · 3.5 KB
/
signature.js
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
/*
* https://github.com/legalthings/signature-pad-angular
* Copyright (c) 2015 ; Licensed MIT
*/
angular.module('signature', []);
angular.module('signature').directive('signaturePad', ['$window', '$timeout',
function ($window, $timeout) {
'use strict';
var EMPTY_IMAGE = 'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=';
return {
restrict: 'EA',
replace: true,
template: (
'<div class="signature" ng-style="{height: height + \'px\', width: width + \'px\'}">' +
'<canvas ng-mouseup="updateModel()" style="width: 100%; height: 100%;"></canvas>' +
'</div>'
),
scope: {
accept: '=',
clear: '=',
dataurl: '=?',
isEmpty: '=?',
height: '@',
width: '@'
},
controller: [
'$scope',
function ($scope) {
$scope.isEmpty = function () {
return $scope.signaturePad.isEmpty();
};
$scope.accept = function () {
var signature = {};
if (!$scope.signaturePad.isEmpty()) {
signature.dataUrl = $scope.signaturePad.toDataURL();
signature.isEmpty = false;
} else {
signature.dataUrl = EMPTY_IMAGE;
signature.isEmpty = true;
}
return signature;
};
$scope.updateModel = function () {
var result = $scope.accept();
$scope.dataurl = result.isEmpty ? undefined : result.dataUrl;
};
$scope.$watch(['width', 'height'], $scope.onResize);
}
],
link: function (scope, element) {
var canvas = element.find('canvas')[0];
var parent = element;
var onDevicePixelRatioChange = $window.matchMedia('(-webkit-device-pixel-ratio:1)');
scope.signaturePad = new SignaturePad(canvas);
if (scope.signature && !scope.signature.$isEmpty && scope.signature.dataUrl) {
scope.signaturePad.fromDataURL(scope.signature.dataUrl);
}
function updateScale() {
var ctx = canvas.getContext('2d');
var scaleWidth = canvas.width / parent[0].offsetWidth;
var scaleHeight = canvas.height / parent[0].offsetHeight;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(scaleWidth, scaleHeight);
}
scope.clear = function() {
var ctx = canvas.getContext('2d');
ctx.setTransform(1, 0, 0, 1, 0, 0);
scope.signaturePad.clear();
scope.dataurl = undefined;
updateScale();
}
scope.onResize = function () {
var ratio = Math.max($window.devicePixelRatio || 1, 1);
var newWidth = Math.round(scope.width * ratio);
var newHeight = Math.round(scope.height * ratio);
if (canvas.width !== newWidth || canvas.height !== newHeight) {
canvas.width = newWidth;
canvas.height = newHeight;
scope.signaturePad.clear();
}
updateScale();
};
$timeout(scope.onResize);
var resizeCallback = scope.onResize.bind(this);
angular.element($window).bind('resize', resizeCallback);
onDevicePixelRatioChange.addListener(resizeCallback);
scope.$on('$destroy', function removeListeners() {
angular.element($window).unbind('resize', resizeCallback);
onDevicePixelRatioChange.removeListener(resizeCallback);
});
}
};
}
]);
// Backward compatibility
angular.module('ngSignaturePad', ['signature']);