-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
40 lines (40 loc) · 1.46 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
<head>
<title>domcapture</title>
<meta charset="utf-8">
</head>
<body>
<div id="captureTarget" contenteditable="true">DOM capture test</div><br>
<button onclick="captureDOM('#captureTarget')">capture</button><br>
<canvas id="result" hidden></canvas><br>
<img id="img">
<script>
const captureDOM = selector => {
let dom = document.querySelector(selector);
let width = dom.clientWidth;
let height = dom.clientHeight;
console.log("capturing");
let svgData = `
<svg id="captureSVG" width="${width}" height="${height}">
<foreignObject width="${width}" height="${height}" x="0" y="0">
${dom.innerHTML}
</foreignObject>
</svg>
`;
let cvs = document.getElementById("result");
cvs.width = width;
cvs.height = height;
let c2d = cvs.getContext('2d');
let data = new DOMParser().parseFromString(svgData, "text/html");
let img = new Image();
img.src = `data:image/svg+xml;base64,${window.btoa(unescape(encodeURIComponent(new XMLSerializer().serializeToString(data.querySelector("#captureSVG")))))}`;
img.onload = () => {
c2d.clearRect(0, 0, width, height);
c2d.drawImage(img, 0, 0, width, height);
document.querySelector("#img").src = cvs.toDataURL();
};
};
</script>
</body>
</html>