回答編集履歴
1
コメントを受けて追記
test
CHANGED
@@ -1 +1,203 @@
|
|
1
1
|
`html2canvas`をクリックイベントの中で実行するのがいいと思います。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
# コメントを受けて追記
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```html
|
10
|
+
|
11
|
+
<!DOCTYPE html>
|
12
|
+
|
13
|
+
<html lang="ja">
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
<head>
|
18
|
+
|
19
|
+
<meta charset="UTF-8">
|
20
|
+
|
21
|
+
<title>JavaScriptで撮るスクリーンショット</title>
|
22
|
+
|
23
|
+
</head>
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
<body onpageshow="console.log('onpageshow')">
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
<hr>
|
32
|
+
|
33
|
+
<div style="background-color : #AAEEDD">
|
34
|
+
|
35
|
+
<h1>JavaScriptで撮るスクリーンショット</h1>
|
36
|
+
|
37
|
+
</div>
|
38
|
+
|
39
|
+
<div id="target">
|
40
|
+
|
41
|
+
<h2>導入方法と処理概要</h2>
|
42
|
+
|
43
|
+
<table border="1" width="500" cellspacing="0" cellpadding="5" bordercolor="#333333" style="table-layout: fixed;">
|
44
|
+
|
45
|
+
<tr>
|
46
|
+
|
47
|
+
<th bgcolor="#7b9ad0" width="40">
|
48
|
+
|
49
|
+
<font color="#FFFFFF">No</font>
|
50
|
+
|
51
|
+
</th>
|
52
|
+
|
53
|
+
<th bgcolor="#7b9ad0" width="230">
|
54
|
+
|
55
|
+
<font color="#FFFFFF">概要</font>
|
56
|
+
|
57
|
+
</th>
|
58
|
+
|
59
|
+
<th bgcolor="#7b9ad0" width="230">
|
60
|
+
|
61
|
+
<font color="#FFFFFF">補足</font>
|
62
|
+
|
63
|
+
</th>
|
64
|
+
|
65
|
+
</tr>
|
66
|
+
|
67
|
+
<tr>
|
68
|
+
|
69
|
+
<td bgcolor="#b1d7e4" width="40" align="right">1</td>
|
70
|
+
|
71
|
+
<td bgcolor="#FFFFFF" width="230">html2canvas.jsを読み込む</td>
|
72
|
+
|
73
|
+
<td bgcolor="#FFFFFF" width="230"></td>
|
74
|
+
|
75
|
+
</tr>
|
76
|
+
|
77
|
+
<tr>
|
78
|
+
|
79
|
+
<td bgcolor="#b1d7e4" width="40" align="right">2</td>
|
80
|
+
|
81
|
+
<td bgcolor="#FFFFFF" width="230">任意のタイミングでhtml2canvas関数を呼ぶ</td>
|
82
|
+
|
83
|
+
<td bgcolor="#FFFFFF" width="230">※今回はオンロード処理</td>
|
84
|
+
|
85
|
+
</tr>
|
86
|
+
|
87
|
+
<tr>
|
88
|
+
|
89
|
+
<td bgcolor="#b1d7e4" width="40" align="right">3</td>
|
90
|
+
|
91
|
+
<td bgcolor="#FFFFFF" width="230">onrendered 処理にて指定のElementに画像を追記</td>
|
92
|
+
|
93
|
+
<td bgcolor="#FFFFFF" width="230">※[img]タグの[src]や、[a]タグの[href]など</td>
|
94
|
+
|
95
|
+
</tr>
|
96
|
+
|
97
|
+
</table>
|
98
|
+
|
99
|
+
</div>
|
100
|
+
|
101
|
+
<br>
|
102
|
+
|
103
|
+
<hr>
|
104
|
+
|
105
|
+
<h3>↓↓ここから画像↓↓ (上の対象のDIVを画像化)<h3>
|
106
|
+
|
107
|
+
<img src="" id="result" />
|
108
|
+
|
109
|
+
<h3>↑↑ここまで画像↑↑</h3>
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
<hr>
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
<a href="" id="ss" download="html_ss.png">スクリーンショット(document.body全体)をダウンロード</a>
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
<hr>
|
122
|
+
|
123
|
+
<h3>注意</h3>
|
124
|
+
|
125
|
+
<ul>
|
126
|
+
|
127
|
+
<li>実際にはスクリーンショットを撮っているわけではない</li>
|
128
|
+
|
129
|
+
<li>html2canvasは、HTML内のDOMやCSSを解釈してCanvas上に描画するライブラリ</li>
|
130
|
+
|
131
|
+
<li>つまり、レンダリングエンジンに近い動作をする</li>
|
132
|
+
|
133
|
+
<li>そのため、ブラウザと異なる表示がされる場合がある</li>
|
134
|
+
|
135
|
+
<li>flashやapplet,iframe(別URL)はうまくキャプチャできない</li>
|
136
|
+
|
137
|
+
</ul>
|
138
|
+
|
139
|
+
</div>
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
|
146
|
+
|
147
|
+
<script>
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
const savepng = () => {
|
152
|
+
|
153
|
+
//ボタンを押下した際にダウンロードする画像を作る
|
154
|
+
|
155
|
+
html2canvas(document.body, {
|
156
|
+
|
157
|
+
onrendered: function (canvas) {
|
158
|
+
|
159
|
+
//aタグのhrefにキャプチャ画像のURLを設定
|
160
|
+
|
161
|
+
var imgData = canvas.toDataURL();
|
162
|
+
|
163
|
+
document.getElementById("ss").href = imgData;
|
164
|
+
|
165
|
+
document.getElementById("ss").download = new Date().getTime() + '.png';
|
166
|
+
|
167
|
+
document.getElementById('ss').click();
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
});
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
document.getElementById("ss").addEventListener('click', function () {
|
180
|
+
|
181
|
+
console.log('OK');
|
182
|
+
|
183
|
+
});
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
window.addEventListener('pageshow', () => {
|
188
|
+
|
189
|
+
setInterval(savepng, 60000);
|
190
|
+
|
191
|
+
});
|
192
|
+
|
193
|
+
</script>
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
</body>
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
</html>
|
202
|
+
|
203
|
+
```
|