質問編集履歴

1

アドバイスによるソース修正

2021/03/11 03:37

投稿

rena_168
rena_168

スコア72

test CHANGED
File without changes
test CHANGED
@@ -91,3 +91,183 @@
91
91
 
92
92
 
93
93
  ```
94
+
95
+
96
+
97
+ 修正後のコードは、以下のようになりますが、onrenderedの中身が実行されないです><
98
+
99
+ ```HTML
100
+
101
+ <!DOCTYPE html>
102
+
103
+ <html lang="ja">
104
+
105
+ <head>
106
+
107
+ <meta charset="UTF-8">
108
+
109
+ <title>JavaScriptで撮るスクリーンショット</title>
110
+
111
+ </head>
112
+
113
+ <body onpageshow="console.log('onpageshow')">
114
+
115
+
116
+
117
+ <hr>
118
+
119
+ <div style="background-color : #AAEEDD"><h1>JavaScriptで撮るスクリーンショット</h1></div>
120
+
121
+ <div id="target">
122
+
123
+ <h2>導入方法と処理概要</h2>
124
+
125
+ <table border="1" width="500" cellspacing="0" cellpadding="5" bordercolor="#333333" style="table-layout: fixed;">
126
+
127
+ <tr>
128
+
129
+ <th bgcolor="#7b9ad0" width="40"><font color="#FFFFFF">No</font></th>
130
+
131
+ <th bgcolor="#7b9ad0" width="230"><font color="#FFFFFF">概要</font></th>
132
+
133
+ <th bgcolor="#7b9ad0" width="230"><font color="#FFFFFF">補足</font></th>
134
+
135
+ </tr>
136
+
137
+ <tr>
138
+
139
+ <td bgcolor="#b1d7e4" width="40" align="right">1</td>
140
+
141
+ <td bgcolor="#FFFFFF" width="230" >html2canvas.jsを読み込む</td>
142
+
143
+ <td bgcolor="#FFFFFF" width="230" ></td>
144
+
145
+ </tr>
146
+
147
+ <tr>
148
+
149
+ <td bgcolor="#b1d7e4" width="40" align="right">2</td>
150
+
151
+ <td bgcolor="#FFFFFF" width="230" >任意のタイミングでhtml2canvas関数を呼ぶ</td>
152
+
153
+ <td bgcolor="#FFFFFF" width="230" >※今回はオンロード処理</td>
154
+
155
+ </tr>
156
+
157
+ <tr>
158
+
159
+ <td bgcolor="#b1d7e4" width="40" align="right">3</td>
160
+
161
+ <td bgcolor="#FFFFFF" width="230" >onrendered 処理にて指定のElementに画像を追記</td>
162
+
163
+ <td bgcolor="#FFFFFF" width="230" >※[img]タグの[src]や、[a]タグの[href]など</td>
164
+
165
+ </tr>
166
+
167
+ </table>
168
+
169
+ </div>
170
+
171
+ <br>
172
+
173
+ <hr>
174
+
175
+ <h3>↓↓ここから画像↓↓ (上の対象のDIVを画像化)<h3>
176
+
177
+ <img src="" id="result" />
178
+
179
+ <h3>↑↑ここまで画像↑↑</h3>
180
+
181
+
182
+
183
+ <hr>
184
+
185
+
186
+
187
+ <a href="" id="ss" download="html_ss.png">スクリーンショット(document.body全体)をダウンロード</a>
188
+
189
+
190
+
191
+ <hr>
192
+
193
+ <h3>注意</h3>
194
+
195
+ <ul>
196
+
197
+ <li>実際にはスクリーンショットを撮っているわけではない</li>
198
+
199
+ <li>html2canvasは、HTML内のDOMやCSSを解釈してCanvas上に描画するライブラリ</li>
200
+
201
+ <li>つまり、レンダリングエンジンに近い動作をする</li>
202
+
203
+ <li>そのため、ブラウザと異なる表示がされる場合がある</li>
204
+
205
+ <li>flashやapplet,iframe(別URL)はうまくキャプチャできない</li>
206
+
207
+ </ul>
208
+
209
+ </div>
210
+
211
+
212
+
213
+
214
+
215
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
216
+
217
+ <script>
218
+
219
+
220
+
221
+ const savepng = () => {
222
+
223
+ //ボタンを押下した際にダウンロードする画像を作る
224
+
225
+ html2canvas(document.body, {
226
+
227
+ onrendered: function (canvas) {
228
+
229
+ //aタグのhrefにキャプチャ画像のURLを設定
230
+
231
+ var imgData = canvas.toDataURL();
232
+
233
+ document.getElementById("ss").href = imgData;
234
+
235
+   document.getElementById("ss").download = new Date().getTime() + '.png';
236
+
237
+ }
238
+
239
+ });
240
+
241
+
242
+
243
+
244
+
245
+ document.getElementById('ss').click();
246
+
247
+ }
248
+
249
+ document.getElementById("ss").addEventListener('click', function(){
250
+
251
+ console.log('OK');
252
+
253
+ });
254
+
255
+
256
+
257
+ window.addEventListener('pageshow', ()=>{
258
+
259
+ setInterval(savepng,60000);
260
+
261
+ });
262
+
263
+ </script>
264
+
265
+
266
+
267
+ </body>
268
+
269
+ </html>
270
+
271
+
272
+
273
+ ```