์ด ์ค๋ฅ์ ์๋ฏธ๋ ๋ฌด์์ ๋๊น? ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ๊ณ ์น๋ ๋ฐฉ๋ฒ
๋ ๋๋งํ๋ ค๋ ์์๊ฐ Document DOM ๋ด์ ์์ง ์์ต๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ ๋๊น?
0.5๋ ๊ด์ฐฎ์ง๋ง 1๋ก ์ ๊ทธ๋ ์ด๋ํ๋ฉด ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
jsfiddle์์ ์์ ๋ฅผ ๊ณต์ ํด ์ฃผ์ธ์.
HTML ๋ณธ๋ฌธ ๋ค์์ html2canvas js(๋ฒ์ 1) ๋ฐ ๋ด ์ฑ js๋ฅผ ๋ก๋ํ๋ ค๊ณ ์๋ํ๋๋ฐ ์ด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.
๋๊ตฌ๋ ์ง์ด ๋ฌธ์ ์ ๋ํ ์์ ์ฌํญ์ด ์์ต๋๊น? ASP.Net MVC cshtml ํ์ด์ง์์ ์ฌ์ฉํ๋ ๋์ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
jQuery๋ฅผ ์ฌ์ฉํ์ฌ DIV ์ฌ์ฉ์ ์ฐพ๋ ๊ฒฝ์ฐ
html2canvas($("#์์")[0]).then(ํจ์(์บ๋ฒ์ค) {
$("#์์ ์ถ๋ ฅ").append(์บ๋ฒ์ค);
});
๊ทธ๋ฌ๋ html2canvas๋ฅผ ์ฌ์ฉํ์ฌ Google ์ง๋์ ์ด๋ฏธ์ง๋ฅผ ์ฐ์ ์ ์์ต๋๋ค(๋ชจ๋ ์ง๋ ์ด๋ฏธ์ง๊ฐ ์ค์ง ์์). cshtml์์ ๋์ผํ ์์ ์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋๊ฐ ๋์์ค ์ ์์ต๋๊น ???
@cjcortez @RaghavPrabhu ์์ smartbepl์ ์๋ฃจ์
์ jQuery ๋๋ ๋จ์ผ Element
๋์ ์์ ๋ชฉ๋ก์ ์ ๊ณตํ๋ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๊ณ ๊ฐ์ ํ ์ ์๋๋ก ์์ ํฉ๋๊น?
์ด๊ฒ์ ๋๋ฅผ ์ํด ์๋ํฉ๋๋ค!
html2canvas($('#div').get(0)).then( ํจ์(์บ๋ฒ์ค) {
console.log(์บ๋ฒ์ค);
});
๋๋ถ์ ํจ๊ณผ๊ฐ ์์๋ค
@jeremielodi ๊ฐ์ฌํฉ๋๋ค, ์ด๊ฒ์ ๋๋ฅผ
์์ ์ ์ฌํ ์ฝ๋๋ฅผ ์๋ํ์ง๋ง ์๋ง๋ ๋ด๊ฐ ๋ฐ๋ ์ค๋ฅ๋ ๊ทธ๋ค์ง ๊ด๋ จ์ด ์์ ๊ฒ์ ๋๋ค: Uncaught (in promise) undefined... Promise denied (async)
๋๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋ฒํผ ํด๋ฆญ ์ HTML2Canvas๋ฅผ ํธ์ถํ๊ณ ์ถ์์ต๋๋ค. HTML2Canvas ํธ์ถ์ ์ด๋ป๊ฒ ์์ฑํ๋์ง์ ๊ด๊ณ์์ด "Uncaught(in promise) undefined...Promise denied(async)" ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ๋ง์ง๋ง์ผ๋ก, ๋๋ ์ฝ์์ด ์ด๋ป๊ฒ ์๋ํ๋์ง์ ๋ํด ์กฐ๊ธ ๋ฐฐ์ ๊ณ ํด๊ฒฐ์ฑ ์ด ์บ์น๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ๋ฐ๊ฒฌํ์ต๋๋ค.
function myFunction() {
html2canvas(document.querySelector("#capture")).then(canvas => {
var base64encodedstring = canvas.toDataURL("image/jpeg", 1).replace("data:image/jpeg;base64,", "");
j$("[id$='inputHidden']").val(base64encodedstring);
console.log('Saving...');
mySaveFunction();
})
.catch(function (error) {
/* This is fired when the promise executes without the DOM */
});
}
๊ฒฝ๊ณ ์ ๋ง์, ์ ๋ ์์ ํ ์๋ฐ ์คํฌ๋ฆฝํธ ์ด๋ณด์์ด๋ฉฐ ์ฝ์์ด ์ด๋ป๊ฒ ์๋ํ๋์ง ๊ฑฐ์ ์ดํดํ์ง ๋ชปํฉ๋๋ค. ๋ด ์ฝ๋๋ ํจ์ ์ธ๋ถ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ .catch() ์์ด๋ ์ ์๋ํ์ต๋๋ค. ์ด๋ป๊ฒ๋ ์บก์ํํ๋ฉด ๋ ์ด์ ์ฌ๋ฐ๋ฅธ DOM ์ก์ธ์ค ๊ถํ์ด ์๊ณ ์ฝ์์ด ์คํจํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
@ikemike ์ด๊ฒ์ ๋น์ ์ ๋์ธ ์ ์์ต๋๋ค
```html
@niklasvh ์ต์ ๋ฒ์ ์ html2canvas๋ฅผ ์ฌ์ฉ ์ค์ ๋๋ค. Uncaught(์ฝ์): ์ ์๋์ง ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ ๋ฐ ๋ ์ข ๋์ ์ค ์ ์๋.
์ด, ์๋ํฉ๋๋ค
์ด๊ฒ์ ๋๋ฅผ ์ํด ์ ์๋ํฉ๋๋ค.
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
clearDynamicLink(link);
}
function DownloadAsImage() {
var element = $("#table-card")[0];
html2canvas(element).then(function (canvas) {
var myImage = canvas.toDataURL();
downloadURI(myImage, "cartao-virtual.png");
});
}
ํฌ๋กฌ์์๋ ์ ๋๋๋ฐ IE11์์๋ ์๋๋ค์... T_T;
์กํ์ง ์์(์ฝ์ ์ค): ์ ์๋์ง ์์ ์ค๋ฅ
@bandacs ์ด์ ๋ํ ํด๊ฒฐ์ฑ ์ ์ฐพ์์ต๋๊น? ๊ทํ์ ์คํฌ๋ฆฐ ์ท๊ณผ ๊ฐ์ ์ ํํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
1.0 alpha 12 ๋ฆด๋ฆฌ์ค๋ฅผ ์ฌ์ฉํ๊ณ ์๋๋ฐ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ด ๋ชจ๋ ์๋ฃจ์ ์ ์๋ํ์ต๋๋ค.
@smartbepl์์ ์ด ์๋ฃจ์
์ ์๋ํ์ต๋๋ค.
html2canvas($("#์์")[0]).then(ํจ์(์บ๋ฒ์ค) {
$("#์์ ์ถ๋ ฅ").append(์บ๋ฒ์ค);
});
๋๋ ์ด๊ฒ์ @jeremielodi๊ฐ ์๋ํ๋ค.
html2canvas($('#div').get(0)).then( ํจ์(์บ๋ฒ์ค) {
console.log(์บ๋ฒ์ค);
});
๋๋ ๋ํ leandrocgsi ์๋ฃจ์ ์ ์๋ํ๊ณ @ikemike๊ฐ ์ ์ํ ๋๋ก ์บ์น๋ฅผ ์ถ๊ฐํ๋ ค๊ณ ์๋ํ์ต๋๋ค.
์ด๋ฅผ ๋จ์ํํ๊ณ ๋ชจ๋ ์์๊ฐ DOM์ ๋ก๋๋๋๋ก ํ๊ธฐ ์ํด take_screenshot()์ด๋ผ๋ ํจ์๋ฅผ ๋ฐฐ์นํฉ๋๋ค.
ํจ์ take_screenshot()
{
html2canvas($(".image__container")[0]).then(์บ๋ฒ์ค => {
console.log('์ ๋ฐ ์ ์ ์ ์ฐจ๋ฆฌ๊ณ ์์ต๋๋ค');
});
}
๊ทธ๋ฐ ๋ค์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ์์์ ์ง์ take_screenshot()์ ํธ์ถํ์ฌ ๋ชจ๋ ๊ฒ์ด ๋ก๋๋์๋์ง ํ์ธํฉ๋๋ค.
Uncaught(์ฝ์์์) undefined ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
https://github.com/niklasvh/html2canvas/issues/1313#issuecomment -377132089์์ ์ง๋ฉดํ ๊ฒ๊ณผ ๋๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๋๊ตฌ๋ ์ง ์ด ๋ฌธ์ ์ ๋ํ ํด๊ฒฐ์ฑ
์ ์ฐพ์ ์ ์์ต๋๊น?
๋ด๊ฐ ์น์ด๋ฆฌ๋์ ์คํจํ ์ด์ ๋?
.get(url)
.end((err, res) => {
cheerio.load(res.text)
html2Canvas($('#statuses').get(0), {
allowTaint: true
}).then(function(canvas) {})
})
Uncaught (in promise) Error: Element is not attached to a Document
์ด์ ๋ ๋ค๋ฅธ ํธ์ถ ๊ท์น์ด ์์ต๋๋ค.
๋ฒ์ 0.5๋ ๋ฐฐ์ด์ ๋
ธ๋๋ฅผ ์์ํ์ง๋ง ์ง๊ธ์ ์ง์ ์ ๊ณตํฉ๋๋ค.
๋ฒ์ 0.5์๋ "onrendered" ์ต์
์ด ์์์ง๋ง ์ด์ ๋ "then" ๊ตฌ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
jQuery๋ฅผ ์ฌ์ฉํ์ฌ DIV ์ฌ์ฉ์ ์ฐพ๋ ๊ฒฝ์ฐ
html2canvas($("#์์")[0]).then(ํจ์(์บ๋ฒ์ค) {
$("#์์ ์ถ๋ ฅ").append(์บ๋ฒ์ค);
});