teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2021/03/11 03:37

投稿

rena_168
rena_168

スコア73

title CHANGED
File without changes
body CHANGED
@@ -44,4 +44,94 @@
44
44
 
45
45
 
46
46
 
47
+ ```
48
+
49
+ 修正後のコードは、以下のようになりますが、onrenderedの中身が実行されないです><
50
+ ```HTML
51
+ <!DOCTYPE html>
52
+ <html lang="ja">
53
+ <head>
54
+ <meta charset="UTF-8">
55
+ <title>JavaScriptで撮るスクリーンショット</title>
56
+ </head>
57
+ <body onpageshow="console.log('onpageshow')">
58
+
59
+ <hr>
60
+ <div style="background-color : #AAEEDD"><h1>JavaScriptで撮るスクリーンショット</h1></div>
61
+ <div id="target">
62
+ <h2>導入方法と処理概要</h2>
63
+ <table border="1" width="500" cellspacing="0" cellpadding="5" bordercolor="#333333" style="table-layout: fixed;">
64
+ <tr>
65
+ <th bgcolor="#7b9ad0" width="40"><font color="#FFFFFF">No</font></th>
66
+ <th bgcolor="#7b9ad0" width="230"><font color="#FFFFFF">概要</font></th>
67
+ <th bgcolor="#7b9ad0" width="230"><font color="#FFFFFF">補足</font></th>
68
+ </tr>
69
+ <tr>
70
+ <td bgcolor="#b1d7e4" width="40" align="right">1</td>
71
+ <td bgcolor="#FFFFFF" width="230" >html2canvas.jsを読み込む</td>
72
+ <td bgcolor="#FFFFFF" width="230" ></td>
73
+ </tr>
74
+ <tr>
75
+ <td bgcolor="#b1d7e4" width="40" align="right">2</td>
76
+ <td bgcolor="#FFFFFF" width="230" >任意のタイミングでhtml2canvas関数を呼ぶ</td>
77
+ <td bgcolor="#FFFFFF" width="230" >※今回はオンロード処理</td>
78
+ </tr>
79
+ <tr>
80
+ <td bgcolor="#b1d7e4" width="40" align="right">3</td>
81
+ <td bgcolor="#FFFFFF" width="230" >onrendered 処理にて指定のElementに画像を追記</td>
82
+ <td bgcolor="#FFFFFF" width="230" >※[img]タグの[src]や、[a]タグの[href]など</td>
83
+ </tr>
84
+ </table>
85
+ </div>
86
+ <br>
87
+ <hr>
88
+ <h3>↓↓ここから画像↓↓ (上の対象のDIVを画像化)<h3>
89
+ <img src="" id="result" />
90
+ <h3>↑↑ここまで画像↑↑</h3>
91
+
92
+ <hr>
93
+
94
+ <a href="" id="ss" download="html_ss.png">スクリーンショット(document.body全体)をダウンロード</a>
95
+
96
+ <hr>
97
+ <h3>注意</h3>
98
+ <ul>
99
+ <li>実際にはスクリーンショットを撮っているわけではない</li>
100
+ <li>html2canvasは、HTML内のDOMやCSSを解釈してCanvas上に描画するライブラリ</li>
101
+ <li>つまり、レンダリングエンジンに近い動作をする</li>
102
+ <li>そのため、ブラウザと異なる表示がされる場合がある</li>
103
+ <li>flashやapplet,iframe(別URL)はうまくキャプチャできない</li>
104
+ </ul>
105
+ </div>
106
+
107
+
108
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
109
+ <script>
110
+
111
+ const savepng = () => {
112
+ //ボタンを押下した際にダウンロードする画像を作る
113
+ html2canvas(document.body, {
114
+ onrendered: function (canvas) {
115
+ //aタグのhrefにキャプチャ画像のURLを設定
116
+ var imgData = canvas.toDataURL();
117
+ document.getElementById("ss").href = imgData;
118
+   document.getElementById("ss").download = new Date().getTime() + '.png';
119
+ }
120
+ });
121
+
122
+
123
+ document.getElementById('ss').click();
124
+ }
125
+ document.getElementById("ss").addEventListener('click', function(){
126
+ console.log('OK');
127
+ });
128
+
129
+ window.addEventListener('pageshow', ()=>{
130
+ setInterval(savepng,60000);
131
+ });
132
+ </script>
133
+
134
+ </body>
135
+ </html>
136
+
47
137
  ```