-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcanvassaver.js
40 lines (31 loc) · 1.11 KB
/
canvassaver.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
function CanvasSaver(url) {
this.url = url;
this.savePNG = function(cnvs, fname) {
if(!cnvs || !url) return;
fname = fname || 'picture';
var data = cnvs.toDataURL("image/png");
data = data.substr(data.indexOf(',') + 1).toString();
var dataInput = document.createElement("input") ;
dataInput.setAttribute("name", "imgdata") ;
dataInput.setAttribute("value", data);
dataInput.setAttribute("type", "hidden");
var nameInput = document.createElement("input") ;
nameInput.setAttribute("name", 'name') ;
nameInput.setAttribute("value", fname + '.png');
var myForm = document.createElement("form");
myForm.method = 'post';
myForm.action = url;
myForm.appendChild(dataInput);
myForm.appendChild(nameInput);
document.body.appendChild(myForm) ;
myForm.submit() ;
document.body.removeChild(myForm) ;
};
this.generateButton = function (label, cnvs, fname) {
var btn = document.createElement('button'), scope = this;
btn.innerHTML = label;
btn.style['class'] = 'canvassaver';
btn.addEventListener('click', function(){scope.savePNG(cnvs, fname);}, false);
return btn;
};
}