-
Notifications
You must be signed in to change notification settings - Fork 5
/
bundle.js
121 lines (90 loc) · 3.5 KB
/
bundle.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
$ = require("jquery")
var textSignature = {};
textSignature = function(options) {
var self = this;
self.isInitiated = false;
self.imageData = null;
this.init = function() {
//inject custom font face from external url
$('head').find("#text-signature").remove();
var linkElem = $('<link rel="stylesheet">');
linkElem.attr('id', "text-signature");
$('head').append(linkElem);
linkElem.attr('href', options.customFont.url);
if (linkElem[0].addEventListener) {
linkElem[0].addEventListener('load', function() {
// wait abit slonger and call again - if first time
self.generateImage(options);
if (!self.isInitiated) {
setTimeout(function() {
$(options.canvasTargetDom).html(" ");
self.generateImage(options);
}, 2800);
}
self.isInitiated = true;
}, false);
}
}
this.formatInput = function(options) {
//handle errror
if (!options || options == undefined) {
throw "text-singature: parameter cannot be null or empty";
}
if (!options.font) {
throw "text-singature: parameter font cannot be empty";
}
if (!options.textString) {
throw "text-singature: parameter textString cannot be empty";
}
if (typeof options.font === "string") {
return options;
}
options.font = (options.font) || ["12px", "Arial"];
options.font = (options.font).join(" ");
options.fillStyle = (options.color) || "black";
options.textString = options.textString || "Text-Signature !";
options.paddingX = options.paddingX || 0;
options.paddingY = options.paddingY || 0;
return options;
};
this.generateImage = function(options) {
//parameter sanity and defaults
options = this.formatInput(options);
var uniquetime = new Date().getUTCMilliseconds();
//generate canvas
var canvasSelectorDom = $('<canvas></canvas>');
canvasSelectorDom.attr('width', options.width);
canvasSelectorDom.attr('height', options.height);
canvasSelectorDom.attr('id', "text-signature-" + uniquetime);
var context = canvasSelectorDom[0].getContext("2d");
context.font = options.font;
context.fillStyle = options.fillStyle;
context.fillText(options.textString, options.paddingX, options.paddingY);
var dataUrl = canvasSelectorDom[0].toDataURL();
self.imageData = dataUrl;
var img = $('<img>'); //Equivalent: $(document.createElement('img'))
img.attr('src', dataUrl);
img.attr('text-signature-timestamp', uniquetime);
img.attr('id', "text-signature-" + uniquetime);
if (options.canvasTargetDom) {
if (self.isInitiated) {
$(options.canvasTargetDom).html(img);
} else {
$(options.canvasTargetDom).html();
}
} else {
window.open(dataUrl, "text-signature image", "width=600, height=200");
}
};
this.getImageData = function() {
return self.imageData;
};
this.isDomObject = function(obj) {
return (obj.tagName ? "true" : "false");
};
this.ping = function() {
console.log("Yup. text-singature is working");
};
this.init();
};
module.exports = textSignature