使用するhtml2canvasスクリーンショットをオブジェクトに保存するにはどうすればよいですか?私はデモを探索し、関数は次のようにスクリーンショットが生成されて生成することが確認されました:html2canvasを使用してスクリーンショットをアップロードするには?
$(window).ready(function() {
('body').html2canvas();
});
私がやってみたことは何
$(window).ready(function() {
canvasRecord = $('body').html2canvas();
dataURL = canvasRecord.toDataURL("image/png");
dataURL = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
upload(dataURL);
});
されると、私は、その後にそれを渡します私のupload()
機能。私が抱えている問題は、html2canvas()
ライブラリでスクリーンショットがどこに作られているのか、どの関数がそれを返すのか分かりません。私はthisの答えを使用してキャンバスオブジェクトを変換しようとしましたが(私はこれを行う必要があるとは確信していませんが)。
私はちょうど私が何をする必要があるか理解するのに役立ち(特にbebrawさん@)がimgurにファイルをアップロードする方法についてquestionを尋ね、と回答。
upload()
機能はImgur例APIヘルプからです:
function upload(file) {
// file is from a <input> tag or from Drag'n Drop
// Is the file an image?
if (!file || !file.type.match(/image.*/)) return;
// It is!
// Let's build a FormData object
var fd = new FormData();
fd.append("image", file); // Append the file
fd.append("key", "mykey"); // Get your own key: http://api.imgur.com/
// Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
xhr.onload = function() {
// Big win!
// The URL of the image is:
JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
}
関数 'upload'は何ですか?渡された 'dataURL'はMIMEタイプのない普通の' base64'文字列です。それは意図されていますか? –
@RobW 'upload()'関数を追加しました.-私は 'dataURL'関数が何をしているのか分かりません。この例ではキャンバスをイメージに変換するために使用したので、追加しました(私が知る限り)。 – djq
正しい方法は 'var canvasRecord = new html2canvas(document.body).canvas;'結果と[この回答]を組み合わせることです(http://stackoverflow.com/a/5303242/938089?sending-images-from-canvas -elements-ajax-and-php-filesを使用しています)。 –