[javascript] html2canvas
in JavaScript
html2canvas
Screenshots with JavaScript
- ์ฌ์ฉ๋ฒ์ ๋ฌธ์์๋ ์์ง๋ง ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ์บก์ณ ํ๊ณ ์ถ์ DOM์
html2canvas()
ํจ์์ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌํด์ ํธ์ถํ๋ฉดPromise
๊ฐ์ฒด๋ฅผ ๋ฆฌํด๋ฐ์์ ์๊ณ ๊ทธ๊ฒ์ ํตํด ํน์ ์์ญ์ ํฌํจํcanvas
๊ฐ์ฒด๋ฅผ ๋ฐ์์๊ฐ ์์ต๋๋ค.
##### html
<div id="capture" style="padding: 10px; background: #f5da55">
<h4 style="color: #000; ">Hello world!</h4>
</div>
JavaScript
html2canvas(document.querySelector("#capture")).then(canvas => {
document.body.appendChild(canvas)
});
Sample code
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Template</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Style the header */
header {
background-color: #666;
padding: 30px;
text-align: center;
font-size: 35px;
color: white;
}
/* Create two columns/boxes that floats next to each other */
nav {
float: left;
width: 30%;
height: 300px; /* only for demonstration, should be removed */
background: #ccc;
padding: 20px;
}
/* Style the list inside the menu */
nav ul {
list-style-type: none;
padding: 0;
}
article {
float: left;
padding: 20px;
width: 70%;
background-color: #f1f1f1;
height: 300px; /* only for demonstration, should be removed */
}
/* Clear floats after the columns */
section:after {
content: "";
display: table;
clear: both;
}
/* Style the footer */
footer {
background-color: #777;
padding: 10px;
text-align: center;
color: white;
}
/* Responsive layout - makes the two columns/boxes stack on top of each other instead of next to each other, on small screens */
@media (max-width: 600px) {
nav, article {
width: 100%;
height: auto;
}
}
</style>
</head>
<body>
<input type="button" value="์บก์ณ" />
<h2>CSS Layout Float</h2>
<p>In this example, we have created a header, two columns/boxes and a footer. On smaller screens, the columns will stack on top of each other.</p>
<p>Resize the browser window to see the responsive effect (you will learn more about this in our next chapter - HTML Responsive.)</p>
<header>
<h2>Cities</h2>
</header>
<section>
<nav>
<input type="button" value="์บก์ณ" />
<ul>
<li><a href="#">London</a></li>
<li><a href="#">Paris</a></li>
<li><a href="#">Tokyo</a></li>
</ul>
</nav>
<article>
<h1>London</h1>
<p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
<p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
<input type="button" value="์บก์ณ" />
</article>
</section>
<footer>
<p>Footer</p>
</footer>
</body>
</html>
canvas -> image converter
์ฝ๋ฐฑํจ์๋ก ์ ๋ฌ๋ ๋ณ์
canvas
์๋ ์ค์ ๋ก canvas ์์๊ฐ ๋ค์ด์์ต๋๋ค. ํ๋ฉด์ ๊ทธ๋ ค์ง ์บ๋ฒ์ค ์์๋ ์ด๋ฏธ์ง๋ก ์ ์ฅ๋ ๋์ง๋ง ์ผ๋ฐ์ ์ธ<img>
ํ๊ทธ๊ฐ ๋ ์ต์ํ์ค ๊ฒ์ ๋๋ค. ๊ทธ๋ ๋ค๋ฉด canvas์์ ์ ๊ณตํ๋ api๋ฅผ ์ด์ฉํ์ฌ ์ด๋ฏธ์ง ํ๊ทธ์ ์ฌ์ฉํ ์ ์๋ Data URL ๋ฌธ์์ด๋ก ๋ณํํ๋ฉด ๋ฉ๋๋ค.canvas.toDataURL(โimage/jpegโ)
์ ํจ์๋ฅผ ํธ์ถํ๋ฉด Data URL ๋ฌธ์์ด์ ๋ฆฌํดํฉ๋๋ค.(โimage/jpegโ ์ธ ๋ค๋ฅธ ํฌ๋งท๋ ๊ฐ๋ฅ)
add polyfill
html2canvas
๋promise
๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ํ๋ก๋ฏธ์ค๋ฅผ ์ง์ํ์ง ์๋ ์ธํฐ๋ท ์ต์คํ๋ก๋ฌ์์๋ ์ฌ์ฉํ ์๊ฐ ์์ต๋๋ค. ๊ทธ๋ฌ๋promise
๋ฅผpolyfill
์ฒ๋ฆฌ๋ฅผ ํ๋ค๋ฉด IE9 ์ด์์์ ๋์ ๊ฐ๋ฅํ๋ค๊ณ ๋์์์ต๋๋ค. ํ๋ฒ ํด๋ฆฌํ๊น์ง ์ ์ฉํด๋ณด๊ฒ ์ต๋๋ค.##### Browser compatibility
The library should work fine on the following browsers (with promise
polyfill )
- Firefox 3.5+
- Google Chrome
- Opera 12+
- IE9+
- Edge
- Safari 6+
๋ค์ํ ํด๋ฆฌํ ๋ค์ด ์์ง๋ง (์๋ง๋) ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ es6-promise
๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๋งํฌ์ Data URL์ ๋ฃ๊ณ ๋ค์ด๋ก๋ ๋ฐ๋ ๋ฐฉ๋ฒ์ ํฌ๋กฌ์์ ์ด์ ์์ง๋ง ์ต์คํ๋ก๋ฌ์์ ์ ์์ ์ผ๋ก ๋์ํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
if (navigator.msSaveBlob) {
var blob = canvas.msToBlob();
return navigator.msSaveBlob(blob, 'fileName.jpg');
}
์ ๋ฆฌ
์ด์์ผ๋ก ์น ํ๋ฉด์ ์บก์ณํด์ ์ด๋ฏธ์ง๋ก ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ ์์ ๋ณด์์ต๋๋ค. ์ฐ๋ฆฌ๊ฐ ํํ ์ฌ์ฉํ๋ ์บก์ณ ํ๋ก๊ทธ๋จ๊ณผ ๋ฌ๋ฆฌ ํน์ ์์ญ์ ๊ฒน์ณ์ ์บก์ณํ ์ ์๋๊ฒ์ ์๋์ง๋ง ์ธ๋ถ ํ๋ฌ๊ทธ์ธ ์ฌ์ฉ์์ด ์น ํ๋ฉด์ ๋ณด์ด๋ ๊ทธ๋๋ก ์ด๋ฏธ์ง๋ก ๋ด๋ ค๋ฐ์ ์ ์์์ต๋๋ค.
๊ทธ๋ฌ๋ ์ด๊ฒ๋ 100% ๋์ผํ ์ด๋ฏธ์ง๋ก ์บก์ณ๋์ง ์์ต๋๋ค. ์บก์ณ ๋ฐฉ๋ฒ์ด HTML์ DOM๊ณผ CSS ์์ฑ์ ์บ๋ฒ์ค์ ์ฎ๊ฒจ ์ด๋ฏธ์งํ ์ํค๋ ๊ฒ์ธ๋ฐ ์ผ๋ถ CSS๋ ์ง์ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
[์ฐธ๊ณ ] https://sub0709.tistory.com/48