canvas2png.js
1.18 KB
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
/*
* canvas2png.js
*
* Copyright (c) 2010-2013 Shinya Muramatsu
* Released under the MIT License
* http://flashcanvas.net/
*/
(function(doc) {
var scripts = doc.getElementsByTagName("script");
var script = scripts[scripts.length - 1];
var url = script.getAttribute("src").replace(/[^\/]+$/, "save.php");
window.canvas2png = function(canvas, filename) {
var tagName = canvas.tagName.toLowerCase();
if (tagName !== "canvas") {
return;
}
if (typeof FlashCanvas !== "undefined") {
FlashCanvas.saveImage(canvas, filename);
} else {
var action = url;
if (filename) {
action += "?filename=" + filename;
}
var form = doc.createElement("form");
var input = doc.createElement("input");
form.setAttribute("action", action);
form.setAttribute("method", "post");
input.setAttribute("type", "hidden");
input.setAttribute("name", "dataurl");
input.setAttribute("value", canvas.toDataURL());
doc.body.appendChild(form);
form.appendChild(input);
form.submit();
form.removeChild(input);
doc.body.removeChild(form);
}
}
})(document);