-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvg-test.html
120 lines (94 loc) · 2.7 KB
/
canvg-test.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<style>
canvas {
border: solid maroon 4px;
}
#svgimage {
margin: 0;
padding: 0;
}
svg, img {
border: solid blue 4px;
}
</style>
<div id="svgimage"></div>
<div id="previewImg">
<canvas />
<img />
</div>
<script type="importmap">
{
"imports": {
"canvg": "https://cdn.jsdelivr.net/npm/canvg@4.0.1/+esm"
}
}
</script>
<script type="module">
/*import { Canvg } from "canvg";
let v = null;
window.onload = async () => {
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
v = await Canvg.from(ctx, "https://raw.githack.com/coder0107git/html2canvas-issue-2881/main/SVG3.svg");
// Start SVG rendering with animations and mouse handling.
await v.render();
//await new Promise(r => setTimeout(r, 2000));
//await document.write(`<img src="${await canvas.toDataURL('image/png')}"/>`);
};
window.onbeforeunload = () => {
v.stop();
};*/
</script>
<script type="module">
import {
Canvg //, presets
} from "canvg";
// the following function is modified from https://stackoverflow.com/a/70166417
async function svgElemToString(svg) {
const newUse = document.createElementNS("http://www.w3.org/2000/svg", "use");
newUse.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
newUse.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "#foo");
await svg.append(newUse);
return await new XMLSerializer().serializeToString(svg);
}
//const preset = presets.offscreen();
let v = null;
async function toPng(data) {
const {
width,
height,
svg
} = data;
//const canvas = new OffscreenCanvas(width, height);
const canvas = document.querySelector("canvas");
if(width && height) {
console.log(width, height);
canvas.style.width = width;
canvas.style.height = height;
}
const ctx = canvas.getContext("2d");
v = await Canvg.fromString(ctx, svg /*, preset*/);
// Render only first frame, ignoring animations and mouse.
await v.start();
await new Promise(r => setTimeout(r, 3000));
await v.stop();
/*return await canvas.toBlob(blob => {
return URL.createObjectURL(blob);
});*/
return await null;
}
// load SVG
fetch("https://raw.githack.com/coder0107git/html2canvas-issue-2881/main/SVG2.svg")
.then(async response => await response.text())
.then(async res => document.querySelector("#svgimage").innerHTML = await res)
.then(async () => {
await toPng({
width: 650,
height: 550,
svg: await svgElemToString(document.querySelector("#svgimage svg")),
}).then((pngUrl) => {
console.log(pngUrl);
//const img = document.querySelector("#previewImg img");
//img.src = pngUrl;
});
}).catch(e => console.error(e.stack));
</script>