<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div to PNG</title>
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
<style>
#capture {
width: 300px;
height: 200px;
background-color: lightblue;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div id="capture">
<h1>Hello, World!</h1>
<p>This is a sample div to capture as PNG.</p>
</div>
<button id="downloadBtn">Capture and Download</button>
<script>
document.getElementById('downloadBtn').addEventListener('click', function () {
const captureElement = document.getElementById('capture');
// html2canvas로 캡처
html2canvas(captureElement).then(canvas => {
// 캔버스를 PNG로 변환
const image = canvas.toDataURL('image/png');
// 다운로드 링크 생성
const link = document.createElement('a');
link.href = image;
link.download = 'capture.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
});
</script>
</body>
</html>
728x90
반응형
'Dev_Tip > JavaScript_Tip' 카테고리의 다른 글
XMLHttpRequest 요청하기 (0) | 2021.03.11 |
---|---|
[JS] 현재 페이지 프린트하기 (0) | 2021.02.25 |