回答編集履歴
1
コメントを受けて追記
answer
CHANGED
@@ -1,1 +1,102 @@
|
|
1
|
-
`html2canvas`をクリックイベントの中で実行するのがいいと思います。
|
1
|
+
`html2canvas`をクリックイベントの中で実行するのがいいと思います。
|
2
|
+
|
3
|
+
# コメントを受けて追記
|
4
|
+
|
5
|
+
```html
|
6
|
+
<!DOCTYPE html>
|
7
|
+
<html lang="ja">
|
8
|
+
|
9
|
+
<head>
|
10
|
+
<meta charset="UTF-8">
|
11
|
+
<title>JavaScriptで撮るスクリーンショット</title>
|
12
|
+
</head>
|
13
|
+
|
14
|
+
<body onpageshow="console.log('onpageshow')">
|
15
|
+
|
16
|
+
<hr>
|
17
|
+
<div style="background-color : #AAEEDD">
|
18
|
+
<h1>JavaScriptで撮るスクリーンショット</h1>
|
19
|
+
</div>
|
20
|
+
<div id="target">
|
21
|
+
<h2>導入方法と処理概要</h2>
|
22
|
+
<table border="1" width="500" cellspacing="0" cellpadding="5" bordercolor="#333333" style="table-layout: fixed;">
|
23
|
+
<tr>
|
24
|
+
<th bgcolor="#7b9ad0" width="40">
|
25
|
+
<font color="#FFFFFF">No</font>
|
26
|
+
</th>
|
27
|
+
<th bgcolor="#7b9ad0" width="230">
|
28
|
+
<font color="#FFFFFF">概要</font>
|
29
|
+
</th>
|
30
|
+
<th bgcolor="#7b9ad0" width="230">
|
31
|
+
<font color="#FFFFFF">補足</font>
|
32
|
+
</th>
|
33
|
+
</tr>
|
34
|
+
<tr>
|
35
|
+
<td bgcolor="#b1d7e4" width="40" align="right">1</td>
|
36
|
+
<td bgcolor="#FFFFFF" width="230">html2canvas.jsを読み込む</td>
|
37
|
+
<td bgcolor="#FFFFFF" width="230"></td>
|
38
|
+
</tr>
|
39
|
+
<tr>
|
40
|
+
<td bgcolor="#b1d7e4" width="40" align="right">2</td>
|
41
|
+
<td bgcolor="#FFFFFF" width="230">任意のタイミングでhtml2canvas関数を呼ぶ</td>
|
42
|
+
<td bgcolor="#FFFFFF" width="230">※今回はオンロード処理</td>
|
43
|
+
</tr>
|
44
|
+
<tr>
|
45
|
+
<td bgcolor="#b1d7e4" width="40" align="right">3</td>
|
46
|
+
<td bgcolor="#FFFFFF" width="230">onrendered 処理にて指定のElementに画像を追記</td>
|
47
|
+
<td bgcolor="#FFFFFF" width="230">※[img]タグの[src]や、[a]タグの[href]など</td>
|
48
|
+
</tr>
|
49
|
+
</table>
|
50
|
+
</div>
|
51
|
+
<br>
|
52
|
+
<hr>
|
53
|
+
<h3>↓↓ここから画像↓↓ (上の対象のDIVを画像化)<h3>
|
54
|
+
<img src="" id="result" />
|
55
|
+
<h3>↑↑ここまで画像↑↑</h3>
|
56
|
+
|
57
|
+
<hr>
|
58
|
+
|
59
|
+
<a href="" id="ss" download="html_ss.png">スクリーンショット(document.body全体)をダウンロード</a>
|
60
|
+
|
61
|
+
<hr>
|
62
|
+
<h3>注意</h3>
|
63
|
+
<ul>
|
64
|
+
<li>実際にはスクリーンショットを撮っているわけではない</li>
|
65
|
+
<li>html2canvasは、HTML内のDOMやCSSを解釈してCanvas上に描画するライブラリ</li>
|
66
|
+
<li>つまり、レンダリングエンジンに近い動作をする</li>
|
67
|
+
<li>そのため、ブラウザと異なる表示がされる場合がある</li>
|
68
|
+
<li>flashやapplet,iframe(別URL)はうまくキャプチャできない</li>
|
69
|
+
</ul>
|
70
|
+
</div>
|
71
|
+
|
72
|
+
|
73
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
|
74
|
+
<script>
|
75
|
+
|
76
|
+
const savepng = () => {
|
77
|
+
//ボタンを押下した際にダウンロードする画像を作る
|
78
|
+
html2canvas(document.body, {
|
79
|
+
onrendered: function (canvas) {
|
80
|
+
//aタグのhrefにキャプチャ画像のURLを設定
|
81
|
+
var imgData = canvas.toDataURL();
|
82
|
+
document.getElementById("ss").href = imgData;
|
83
|
+
document.getElementById("ss").download = new Date().getTime() + '.png';
|
84
|
+
document.getElementById('ss').click();
|
85
|
+
}
|
86
|
+
});
|
87
|
+
|
88
|
+
|
89
|
+
}
|
90
|
+
document.getElementById("ss").addEventListener('click', function () {
|
91
|
+
console.log('OK');
|
92
|
+
});
|
93
|
+
|
94
|
+
window.addEventListener('pageshow', () => {
|
95
|
+
setInterval(savepng, 60000);
|
96
|
+
});
|
97
|
+
</script>
|
98
|
+
|
99
|
+
</body>
|
100
|
+
|
101
|
+
</html>
|
102
|
+
```
|