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

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/11 00:55
2021/03/11 00:57
2021/03/11 01:04
2021/03/11 01:12
2021/03/11 01:24
2021/03/11 01:30
2021/03/11 01:42
2021/03/11 01:52
2021/03/11 02:18
2021/03/11 02:28
2021/03/11 02:31
2021/03/11 02:42
2021/03/11 02:54
2021/03/11 04:16
2021/03/11 04:36
2021/03/12 04:09
2021/03/12 06:13
2021/03/12 06:16
2021/03/12 06:17 編集
2021/03/12 06:19
2021/03/12 06:23
2021/03/12 06:25
2021/03/12 06:31
2021/03/12 06:34
2021/03/12 06:39
2021/03/12 06:43
2021/03/12 06:46
2021/03/12 06:53
2021/03/12 08:04
2021/03/12 08:10