-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.js
232 lines (190 loc) · 6.27 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Created by wushuyi on 2016/8/16 0016.
*/
this.Signature = this.Signature || {};
(function () {
"use strict";
Signature.extend = function (subclass, superclass) {
function o() {
this.constructor = subclass;
}
o.prototype = superclass.prototype;
return (subclass.prototype = new o());
};
Signature.promote = function (subclass, prefix) {
"use strict";
var subP = subclass.prototype, supP = (Object.getPrototypeOf && Object.getPrototypeOf(subP)) || subP.__proto__;
if (supP) {
subP[(prefix += "_") + "constructor"] = supP.constructor; // constructor is not always innumerable
for (var n in supP) {
if (subP.hasOwnProperty(n) && (typeof supP[n] == "function")) {
subP[prefix + n] = supP[n];
}
}
}
return subclass;
};
Signature.proxy = function (fn, context) {
var deletedIds = [];
var slice = deletedIds.slice;
var args, proxy, tmp;
if (typeof context === "string") {
tmp = fn[context];
context = fn;
fn = tmp;
}
// Simulated bind
args = slice.call(arguments, 2);
proxy = function () {
return fn.apply(context || this, args.concat(slice.call(arguments)));
};
// Set the guid of unique handler to the same of original handler, so it can be removed
return proxy;
};
}());
(function () {
"use strict";
function Pen(stage) {
this.count = 0;
this._mouseY = null;
this._mouseY = null;
this._mouseIsDown = null;
this.lastPt = null;
this.stage = stage;
this.shape = null;
this.graphics = null;
this.canvas = this.stage.canvas;
this._history = [];
this._init();
}
var p = Pen.prototype;
p._init = function () {
this._initView();
this._initText();
};
p.penDown = function (evt) {
this._mouseIsDown = true;
this.lastPt = this.shape.globalToLocal(evt.nativeEvent.pageX - this.canvas.offsetLeft
, evt.nativeEvent.pageY - this.canvas.offsetTop);
this.shape.uncache();
this._history.push({pt: this.lastPt, isdown: true});
};
p.penUp = function (evt) {
this._mouseIsDown = false;
this.shape.cache(0, 0, this.canvas.width, this.canvas.height);
};
p.penMove = function (evt) {
this._mouseX = evt.nativeEvent.pageX - this.canvas.offsetLeft;
this._mouseY = evt.nativeEvent.pageY - this.canvas.offsetTop;
};
p.getColor = function () {
return createjs.Graphics.getHSL(
Math.cos((this.count++) * 0.01) * 180,
100,
50,
1.0);
};
p._tick = function () {
if (this._mouseIsDown) {
var color = this.getColor();
this.graphics.setStrokeStyle(6, "round").beginStroke(color);
this.graphics.moveTo(this.lastPt.x, this.lastPt.y);
this.lastPt = this.shape.globalToLocal(this._mouseX, this._mouseY);
this.graphics.lineTo(this.lastPt.x, this.lastPt.y);
this._history.push({pt: this.lastPt});
}
};
p.getJSON = function () {
return JSON.stringify(this._history);
};
p.clean = function () {
this.stage.removeChild(this.shape);
this._initView();
};
p._initView = function () {
this.shape = new createjs.Shape();
this.stage.addChild(this.shape);
this.graphics = this.shape.graphics;
this.count = 0;
this._history = [];
};
p.reDraw = function (data) {
this.clean();
this._removeText();
this._history = data;
for (var i = 0; i < data.length; i++) {
if (data[i].isdown) {
this.lastPt = data[i].pt;
} else {
var color = this.getColor();
this.graphics.setStrokeStyle(6, "round").beginStroke(color);
this.graphics.moveTo(this.lastPt.x, this.lastPt.y);
this.lastPt = data[i].pt;
this.graphics.lineTo(this.lastPt.x, this.lastPt.y);
}
}
};
p._initText = function () {
var text = new createjs.Text("秀一下你的签名", "36px Arial", "#777777");
window.aaa = text
text.x = 250;
text.y = 150;
this.text = text;
var _this = this;
this.stage.addChild(text);
this.stage.on("stagemousedown", function () {
_this._removeText();
}, true);
};
p._removeText = function () {
this.stage.removeChild(this.text);
};
Signature.Pen = Signature.promote(Pen, "Container");
})();
(function () {
"use strict";
function Pad(id) {
this.stage = null;
this.shape = null;
this.pen = null;
this._init(id);
}
var p = Pad.prototype;
p._init = function (id) {
this.stage = new createjs.Stage(id);
this.stage.enableDOMEvents(true);
createjs.Ticker.setFPS(30);
createjs.Ticker.on("tick", Signature.proxy(this._tick, this));
this._initPen();
};
p._initPen = function () {
this.pen = new Signature.Pen(this.stage);
this.stage.on("stagemousemove", Signature.proxy(this.pen.penMove, this.pen));
this.stage.on("stagemousedown", Signature.proxy(this.pen.penDown, this.pen));
this.stage.on("stagemouseup", Signature.proxy(this.pen.penUp, this.pen));
};
p._tick = function (event) {
this.pen._tick();
this.stage.update(event);
};
// 获取json数据
p.getJSON = function () {
return JSON.stringify(this.pen.getJSON());
};
// 获取json文件
p.getJSONFile = function () {
var json = this.getJSON();
var date = new Date();
var file = new File([json], "data.json", {type: "text/plain", lastModified: date});
var down = new DownloadManager();
down.download(file, null, 'data.json');
};
p.reDraw = function (data) {
return this.pen.reDraw(data);
};
// 获取dataURL图片
p.getDataURL = function () {
return this.stage.toDataURL();
};
Signature.Pad = Signature.promote(Pad, "Container");
})();