[javascript] html2canvas


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