This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
/
signature.js
153 lines (123 loc) · 4.43 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* https://github.com/legalthings/signature-pad-angular
* Copyright (c) 2015 ; Licensed MIT
*/
angular.module('signature', []);
angular.module('signature').directive('signaturePad', ['$interval', '$timeout', '$window',
function ($interval, $timeout, $window) {
'use strict';
var signaturePad, element, EMPTY_IMAGE = 'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=';
return {
restrict: 'EA',
replace: true,
template: '<div class="signature" style="width: 100%; max-width:{{width}}px; height: 100%; max-height:{{height}}px;"><canvas style="display: block; margin: 0 auto;" ng-mouseup="onMouseup()" ng-mousedown="notifyDrawing({ drawing: true })"></canvas></div>',
scope: {
accept: '=?',
clear: '=?',
dataurl: '=?',
height: '@',
width: '@',
notifyDrawing: '&onDrawing',
},
controller: [
'$scope',
function ($scope) {
$scope.accept = function () {
return {
isEmpty: $scope.dataurl === EMPTY_IMAGE,
dataUrl: $scope.dataurl
};
};
$scope.onMouseup = function () {
$scope.updateModel();
// notify that drawing has ended
$scope.notifyDrawing({ drawing: false });
};
$scope.updateModel = function () {
/*
defer handling mouseup event until $scope.signaturePad handles
first the same event
*/
$timeout().then(function () {
$scope.dataurl = $scope.signaturePad.isEmpty() ? EMPTY_IMAGE : $scope.signaturePad.toDataURL();
});
};
$scope.clear = function () {
$scope.signaturePad.clear();
$scope.dataurl = EMPTY_IMAGE;
};
$scope.$watch("dataurl", function (dataUrl) {
if (!dataUrl || $scope.signaturePad.toDataURL() === dataUrl) {
return;
}
$scope.setDataUrl(dataUrl);
});
}
],
link: function (scope, element, attrs) {
var canvas = element.find('canvas')[0];
var parent = canvas.parentElement;
var scale = 0;
var ctx = canvas.getContext('2d');
var width = parseInt(scope.width, 10);
var height = parseInt(scope.height, 10);
canvas.width = width;
canvas.height = height;
scope.signaturePad = new SignaturePad(canvas);
scope.setDataUrl = function(dataUrl) {
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(1, 1);
scope.signaturePad.clear();
scope.signaturePad.fromDataURL(dataUrl);
$timeout().then(function() {
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(1 / scale, 1 / scale);
});
};
var calculateScale = function() {
var scaleWidth = Math.min(parent.clientWidth / width, 1);
var scaleHeight = Math.min(parent.clientHeight / height, 1);
var newScale = Math.min(scaleWidth, scaleHeight);
if (newScale === scale) {
return;
}
var newWidth = width * newScale;
var newHeight = height * newScale;
canvas.style.height = Math.round(newHeight) + "px";
canvas.style.width = Math.round(newWidth) + "px";
scale = newScale;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(1 / scale, 1 / scale);
};
var resizeIH = $interval(calculateScale, 200);
scope.$on('$destroy', function () {
$interval.cancel(resizeIH);
resizeIH = null;
});
angular.element($window).bind('resize', calculateScale);
scope.$on('$destroy', function () {
angular.element($window).unbind('resize', calculateScale);
});
calculateScale();
element.on('touchstart', onTouchstart);
element.on('touchend', onTouchend);
function onTouchstart() {
scope.$apply(function () {
// notify that drawing has started
scope.notifyDrawing({ drawing: true });
});
}
function onTouchend() {
scope.$apply(function () {
// updateModel
scope.updateModel();
// notify that drawing has ended
scope.notifyDrawing({ drawing: false });
});
}
}
};
}
]);
// Backward compatibility
angular.module('ngSignaturePad', ['signature']);