-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
192 lines (179 loc) · 6.87 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
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
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1" />
<title>Test DOM Snapshot</title>
<link rel="stylesheet" href="resource/font/index.css">
<link rel="stylesheet" href="resource/font/test-redundant-fonts.css">
<link rel="stylesheet" href="resource/reset.css">
<style>
.box, .flex-row, .flex-column { max-width: 100%; max-height: 100%; }
.box { overflow: auto; padding: 16px; }
.flex-row { display: flex; flex-flow: row wrap; }
.flex-column { display: flex; flex-flow: column wrap; }
#input-style, #input-html, #source-html, #output-svg, #output-canvas, #output-img { overflow: auto; width: 400px; height: 400px; border: 0; box-shadow: 0 0 0 1px #ddd; }
#input-style, #input-html { font-size: 10px; }
#output-svg, #output-canvas, #output-img { image-rendering: pixelated; }
</style>
<style id="source-style"></style>
</head>
<body>
<div class="flex-column" style="overflow: auto; width: 100vw; min-height: 100vh; align-items: center;">
<div class="flex-row">
<div class="flex-column box">
<label for="input-html">Input HTML</label>
<textarea id="input-html" class="box"></textarea>
</div>
<div class="flex-column box">
<label for="input-style">Input Style</label>
<textarea id="input-style" class="box"></textarea>
</div>
</div>
<div class="flex-row">
<div class="flex-column box">
<button onclick="window.applyInput()">applyInput</button>
<button onclick="window.snapshot('cors')">snapshot(cors)</button>
<button onclick="window.snapshot('no-cors')">snapshot(no-cors)</button>
<button onclick="window.resetFetchCache()">resetFetchCache</button>
<button id="download-svg" disabled>Download SVG</button>
<button id="download-png" disabled>Download PNG</button>
</div>
<pre id="output-status" class="box"></pre>
</div>
<div class="flex-row">
<div class="flex-column box">
<label for="source-html">Source DOM</label>
<div id="source-html"></div>
</div>
<div class="flex-column box">
<label for="output-svg">Output svg-img</label>
<img id="output-svg" width="400" height="400" src="//:0" />
</div>
<div class="flex-column box">
<label for="output-canvas">Output canvas</label>
<canvas id="output-canvas" width="400" height="400">CANVAS</canvas>
</div>
<div class="flex-column box">
<label for="output-img">Output img</label>
<img id="output-img" src="//:0" />
</div>
</div>
</div>
<pre id="init-input-style" style="display: none;">
/* test inline style */
.image-row {
display: flex;
flex-flow: row wrap;
max-width: 100%;
max-height: 100%;
}
.url-image {
margin: 24px;
width: 48px;
height: 48px;
background-image: url('resource/Test Image.png');
}
.circle {
border-radius: 100%;
box-shadow: 0 0 0 5px hsl(0, 0%, 20%),
0 0 0 10px hsl(0, 50%, 50%),
0 0 15px 15px hsl(0, 0%, 0%);
}
/* test redundant fonts */
@font-face {
font-style: normal;
font-family: 'SFUIText';
font-weight: 400;
src: local('SFUIText-Regular'), local('.SFUIText-Regular'), local('SFProText-Regular'), local('SFProText'), url('sf-ui/Text-Regular.woff2') format('woff2'), url('sf-ui/Text-Regular.woff') format('woff');
}
@font-face {
font-style: normal;
font-family: 'SFUIText';
font-weight: 700;
src: local('SFUIText-Bold'), local('.SFUIText-Bold'), local('SFProText-Bold'), url('sf-ui/Text-Bold.woff2') format('woff2'), url('sf-ui/Text-Bold.woff') format('woff');
}
</pre>
<pre id="init-input-html" style="display: none;">
<b>Font</b>
<hr />
<p style="font: 22px DrCoder, Roboto, Arial">Test Font DrCoder</p>
<p style="font-family: 'Roboto'">Test<b>Font Roboto</b></p>
<p style="font-family: 'Material Icons'; font-size: 24px;">
<span style="color: #d62;">language</span>
<span style="color: #2d6;">bug_report</span>
<span style="color: #62d;">lightbulb_outline</span>
</p>
<hr />
<b>Style (url())</b>
<hr />
<div class="image-row">
<div style="margin: 24px; width: 48px; height: 48px; background-image: url('resource/Test Image.png')"></div>
<div style="margin: 24px; width: 48px; height: 48px; background-image: url('resource/Test Image.png')" class="circle"></div>
<div class="url-image"></div>
<div class="url-image circle"></div>
</div>
<hr />
<b>Img</b>
<hr />
<img src="resource/Test%20Image.png">
<hr />
<b>Img (CORS)</b>
<hr />
<img src="https://mockingbot.github.io/dom-snapshot/example/resource/Test%20Image.png">
<img src="https://unpkg.com/material-design-icons/action/1x_web/ic_account_box_black_48dp.png">
<img src="https://unpkg.com/material-design-icons/action/svg/production/ic_3d_rotation_48px.svg">
</pre>
<script src="index.js"></script>
<script>
const {
Fetch: { resetFetchCache, setFetchOption },
createSnapshotFromElement,
Time, Format, Resource
} = window.DomSnapshot
const qS = (selector) => document.querySelector(selector)
const sizeOfString = (string) => `${Format.binary(new window.Blob([ string ]).size)}B`
qS('#input-style').value = qS('#init-input-style').innerHTML
qS('#input-html').value = qS('#init-input-html').innerHTML
window.applyInput = () => {
qS('#source-style').innerHTML = qS('#input-style').value
qS('#source-html').innerHTML = qS('#input-html').value
}
window.resetFetchCache = resetFetchCache
let prevFetchMode
window.snapshot = (fetchMode) => {
if (prevFetchMode !== fetchMode) {
prevFetchMode = fetchMode
resetFetchCache()
setFetchOption({ mode: fetchMode, method: 'GET', cache: 'default', credentials: 'same-origin' })
}
qS('#output-status').innerHTML = 'creating snapshot...'
qS('#output-svg').src = ''
qS('#output-canvas').width += 0
qS('#output-img').src = ''
const timeStart = Time.clock()
createSnapshotFromElement({ element: qS('#source-html') }).then(({
htmlString, cssString, domString, svgString, svgDataUrl, imageElement, canvasElement, pngDataUrl
}) => {
const timeStop = Time.clock()
qS('#output-status').innerHTML = [
`time: ${Format.time(timeStop - timeStart)}`,
`htmlString: ${sizeOfString(htmlString)}`,
`cssString: ${sizeOfString(cssString)}`,
`domString: ${sizeOfString(domString)}`,
`svgString: ${sizeOfString(svgString)}`,
`svgDataUrl: ${sizeOfString(svgDataUrl)}`,
`pngDataUrl: ${sizeOfString(pngDataUrl)}`
].join('\n')
qS('#output-svg').src = svgDataUrl
canvasElement && qS('#output-canvas').getContext('2d').drawImage(canvasElement, 0, 0)
qS('#output-img').src = pngDataUrl
qS('#download-svg').disabled = !svgString
qS('#download-svg').onclick = () => svgString && Resource.createDownloadWithBlob('output.svg', new window.Blob([ svgString ]))
qS('#download-png').disabled = !pngDataUrl
qS('#download-png').onclick = () => pngDataUrl && Resource.createDownload('output.png', pngDataUrl)
})
}
</script>
</body>
</html>