-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsbin-div-to-png-example.html
More file actions
184 lines (160 loc) · 5.01 KB
/
jsbin-div-to-png-example.html
File metadata and controls
184 lines (160 loc) · 5.01 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>layout2vector JSBin Demo</title>
<style>
:root {
--bg: #f3f6fb;
--card: #ffffff;
--ink: #1b2333;
--accent: #0f7cda;
--muted: #6a7487;
}
body {
margin: 0;
font-family: "Segoe UI", "Helvetica Neue", sans-serif;
color: var(--ink);
background: radial-gradient(circle at top right, #d9ebff 0%, var(--bg) 55%);
}
.page {
max-width: 920px;
margin: 24px auto;
padding: 0 16px;
display: grid;
gap: 16px;
}
.panel {
background: var(--card);
border: 1px solid #dbe2ef;
border-radius: 14px;
padding: 16px;
box-shadow: 0 8px 28px rgba(20, 34, 66, 0.08);
}
h1 {
margin: 0 0 10px;
font-size: 22px;
}
p {
margin: 0;
color: var(--muted);
}
#capture {
margin-top: 16px;
width: min(620px, 100%);
min-height: 180px;
border-radius: 20px;
padding: 18px;
color: #0a1b2e;
background:
linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(221, 236, 255, 0.95)),
repeating-linear-gradient(-45deg, rgba(15, 124, 218, 0.08), rgba(15, 124, 218, 0.08) 12px, rgba(15, 124, 218, 0.02) 12px, rgba(15, 124, 218, 0.02) 24px);
border: 2px solid rgba(15, 124, 218, 0.22);
}
#capture h2 {
margin: 0;
font-size: 26px;
}
#capture .badge {
display: inline-block;
margin-top: 10px;
padding: 6px 10px;
border-radius: 999px;
background: #0f7cda;
color: white;
font-size: 12px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#capture .note {
margin-top: 10px;
font-size: 15px;
}
.controls {
margin-top: 14px;
display: flex;
flex-wrap: wrap;
gap: 10px;
}
button {
border: 0;
border-radius: 10px;
background: var(--accent);
color: white;
padding: 10px 14px;
font-weight: 600;
cursor: pointer;
}
button:hover {
filter: brightness(1.08);
}
#status {
margin-top: 10px;
min-height: 1.3em;
font-size: 14px;
color: #274060;
}
#result {
margin-top: 16px;
max-width: 100%;
border-radius: 12px;
border: 1px solid #cfd9ea;
}
</style>
</head>
<body>
<div class="page">
<div class="panel">
<h1>Div to PNG using @node-projects/layout2vector</h1>
<p>
This demo initializes getBoxQuads polyfill first (for Chromium), then extracts
the target div and renders it to a PNG data URL.
</p>
<div id="capture">
<h2>Hello JSBin</h2>
<span class="badge">layout2vector</span>
<p class="note">Export this card as PNG with gradients, text, and rounded corners.</p>
</div>
<div class="controls">
<button id="convertBtn" type="button">Convert #capture to PNG</button>
</div>
<div id="status"></div>
<img id="result" alt="Generated PNG preview" />
</div>
</div>
<script type="module">
import { addPolyfill } from "https://esm.sh/get-box-quads-polyfill@4";
import { extractIR, renderIR, ImageWriter } from "https://esm.sh/@node-projects/layout2vector@5";
// Required in Chromium where getBoxQuads is missing.
addPolyfill(window);
const convertBtn = document.getElementById("convertBtn");
const capture = document.getElementById("capture");
const status = document.getElementById("status");
const result = document.getElementById("result");
convertBtn.addEventListener("click", async () => {
status.textContent = "Converting...";
try {
const bounds = capture.getBoundingClientRect();
const ir = await extractIR(capture, {
includeText: true,
includeImages: true,
});
const writer = new ImageWriter({
width: Math.max(1, Math.ceil(bounds.width)),
height: Math.max(1, Math.ceil(bounds.height)),
scale: Math.min(window.devicePixelRatio || 1, 2),
});
const pngResult = await renderIR(ir, writer);
await pngResult.finalize();
const dataUrl = pngResult.toDataURL("image/png");
result.src = dataUrl;
status.textContent = "Done. PNG generated below.";
} catch (error) {
console.error(error);
status.textContent = `Conversion failed: ${error?.message || error}`;
}
});
</script>
</body>
</html>