@zumer/snapdom 0.9.1 β 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -73
- package/dist/snapdom.js +577 -334
- package/dist/snapdom.min.js +3 -1
- package/dist/snapdom.min.mjs +3 -1
- package/dist/snapdom.mjs +578 -334
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,29 +1,51 @@
|
|
|
1
1
|
# snapDOM
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
<p align="center">
|
|
5
|
+
<a href="http://zumerlab.github.io/orbit-docs">
|
|
6
|
+
<img src="https://raw.githubusercontent.com/zumerlab/snapdom/main/docs/assets/hero.png" width="80%">
|
|
7
|
+
</a>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://www.npmjs.com/package/@zumer/snapdom"><img src="https://img.shields.io/github/package-json/v/zumerlab/snapdom"></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
**snapDOM** is a fast and accurate DOM capture tool to images developed for **Zumly**, a WIP framework that enables zoom-based view transitions.
|
|
4
18
|
|
|
5
19
|
It converts any HTML element into a scalable SVG image, preserving styles, fonts, backgrounds, shadow DOM content, pseudo-elements, and more.
|
|
6
20
|
|
|
21
|
+
|
|
7
22
|
- πΈ Full DOM capture
|
|
8
23
|
- π¨ Embedded styles, pseudo-elements, and fonts
|
|
9
24
|
- πΌοΈ Export to SVG, PNG, JPG, WebP, or `canvas`
|
|
10
|
-
- β‘
|
|
25
|
+
- β‘ Ultra fast, no dependencies
|
|
11
26
|
- π¦ 100% based on standard Web APIs
|
|
12
27
|
|
|
13
|
-
---
|
|
14
28
|
|
|
15
29
|
## Installation
|
|
16
30
|
|
|
17
|
-
You can use **snapDOM**
|
|
31
|
+
You can use **snapDOM** via **NPM**, **CDN**, **script tag**, or by **importing as a module**.
|
|
32
|
+
|
|
33
|
+
### NPM / Yarn
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
npm i @zumer/snapdom
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
yarn add @zumer/snapdom
|
|
41
|
+
```
|
|
18
42
|
|
|
19
43
|
### CDN
|
|
20
44
|
|
|
21
45
|
```html
|
|
22
|
-
<script src="https://unpkg.com/@zumer/snapdom@latest/snapdom.min.js"></script>
|
|
46
|
+
<script src="https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.min.js"></script>
|
|
23
47
|
```
|
|
24
48
|
|
|
25
|
-
---
|
|
26
|
-
|
|
27
49
|
### Script tag (local)
|
|
28
50
|
|
|
29
51
|
```html
|
|
@@ -32,8 +54,6 @@ You can use **snapDOM** by including it via **CDN**, **script tag**, or by **imp
|
|
|
32
54
|
|
|
33
55
|
The global object `snapdom` will be available.
|
|
34
56
|
|
|
35
|
-
---
|
|
36
|
-
|
|
37
57
|
### ES Module
|
|
38
58
|
|
|
39
59
|
```javascript
|
|
@@ -42,63 +62,99 @@ import { snapdom } from './snapdom.mjs';
|
|
|
42
62
|
|
|
43
63
|
### Script Tag (Type Module)
|
|
44
64
|
|
|
45
|
-
```
|
|
65
|
+
```html
|
|
46
66
|
<script type="module">
|
|
47
|
-
import { snapdom } from 'https://unpkg.com/@zumer/snapdom@latest/snapdom.mjs';
|
|
67
|
+
import { snapdom } from 'https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs';
|
|
48
68
|
</script>
|
|
49
69
|
```
|
|
50
70
|
|
|
51
71
|
Now you can call `snapdom(el)`, `snapdom.toPng(el)`, etc., directly in your JavaScript.
|
|
52
72
|
|
|
53
|
-
---
|
|
54
|
-
|
|
55
73
|
## Basic usage
|
|
56
74
|
|
|
57
75
|
```javascript
|
|
58
|
-
// Capture an element as
|
|
59
|
-
const
|
|
76
|
+
// Capture an element as <img> tag
|
|
77
|
+
const el = document.querySelector("#myElement")
|
|
78
|
+
const img = await snapdom.toImg(el);
|
|
60
79
|
|
|
61
|
-
// Insert the captured image into the page
|
|
62
|
-
const img = new Image();
|
|
63
|
-
img.src = svgDataUrl;
|
|
64
80
|
document.body.appendChild(img);
|
|
65
81
|
```
|
|
66
82
|
|
|
67
|
-
---
|
|
68
|
-
|
|
69
83
|
## API
|
|
70
84
|
|
|
85
|
+
### Note: API may evolve until v1.0.0!
|
|
86
|
+
|
|
71
87
|
The main API is exposed as `snapdom` and offers multiple capture methods:
|
|
72
88
|
|
|
73
|
-
| Method
|
|
74
|
-
|
|
75
|
-
| `snapdom(el,
|
|
76
|
-
| `snapdom.toImg(el,
|
|
77
|
-
| `snapdom.toCanvas(el,
|
|
78
|
-
| `snapdom.toPng(el,
|
|
79
|
-
| `snapdom.toJpg(el,
|
|
80
|
-
| `snapdom.toWebp(el,
|
|
81
|
-
| `snapdom.toBlob(el,
|
|
82
|
-
|
|
83
|
-
**Parameters:**
|
|
84
|
-
- `el`: DOM element to capture.
|
|
85
|
-
- `scale`: Scale factor (default is `1`).
|
|
86
|
-
- `quality`: Compression quality for JPG/WebP (range `0`β`1`).
|
|
89
|
+
| Method | Description |
|
|
90
|
+
| :------------------------------- | :------------------------------------------------------------- |
|
|
91
|
+
| `snapdom(el, options?)` | Captures as SVG Data URL |
|
|
92
|
+
| `snapdom.toImg(el, options?)` | Captures as `HTMLImageElement` (SVG) |
|
|
93
|
+
| `snapdom.toCanvas(el, options?)` | Captures as `HTMLCanvasElement` |
|
|
94
|
+
| `snapdom.toPng(el, options?)` | Captures as PNG image (`Image`) |
|
|
95
|
+
| `snapdom.toJpg(el, options?)` | Captures as JPG image (`Image`) |
|
|
96
|
+
| `snapdom.toWebp(el, options?)` | Captures as WebP image (`Image`) |
|
|
97
|
+
| `snapdom.toBlob(el, options?)` | Captures as SVG `Blob` |
|
|
98
|
+
| `preCache(root?, options?)` | Preload resources for faster and more accurate captures |
|
|
87
99
|
|
|
88
100
|
---
|
|
89
101
|
|
|
90
|
-
|
|
102
|
+
### Options
|
|
91
103
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
104
|
+
You can pass an `options` object to control the behavior of all capture methods:
|
|
105
|
+
|
|
106
|
+
| Option | Type | Default | Description |
|
|
107
|
+
| ----------------- | --------- | -------- | ----------------------------------------------------------------------------------- |
|
|
108
|
+
| `compress` | `boolean` | `true` | Removes default styles to reduce size and deduplicate CSS where possible. |
|
|
109
|
+
| `fast` | `boolean` | `true` | Skips `requestIdleCallback` delays and executes preparation immediately. |
|
|
110
|
+
| `embedFonts` | `boolean` | `false` | Inlines external fonts as Data URLs (except icon fonts, which are always embedded). |
|
|
111
|
+
| `scale` | `number` | `1` | Multiplies the output resolution without affecting layout size. |
|
|
112
|
+
| `backgroundColor` | `string` | `"#fff"` | Used when exporting to JPG or WebP formats (which don't support transparency). |
|
|
113
|
+
| `quality` | `number` | `1` | Compression quality for JPG/WebP, between `0` and `1`. |
|
|
99
114
|
|
|
100
115
|
---
|
|
101
116
|
|
|
117
|
+
### `preCache()` β Optional helper
|
|
118
|
+
|
|
119
|
+
The `preCache()` function can be used to load external resources (like images and fonts) in advance.
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
import { preCache } from '@zumer/snapdom';
|
|
123
|
+
|
|
124
|
+
await preCache(document.body, {
|
|
125
|
+
embedFonts: true,
|
|
126
|
+
preWarm: true
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
import { snapdom, preCache } from './snapdom.mjs';
|
|
132
|
+
window.addEventListener('load', async () => {
|
|
133
|
+
await preCache();
|
|
134
|
+
console.log('π¦ Resources preloaded');
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**Options for `preCache()`:**
|
|
139
|
+
|
|
140
|
+
* `embedFonts` *(boolean, default: true)* β Inlines non-icon fonts during preload.
|
|
141
|
+
* `reset` *(boolean, default: false)* β Clears all existing internal caches.
|
|
142
|
+
* `preWarm` *(boolean, default: true)* β Runs a fake capture in memory to warm up styles, fonts and layout detection.
|
|
143
|
+
|
|
144
|
+
## Special features
|
|
145
|
+
|
|
146
|
+
* **Shadow DOM**: Captures content inside Web Components and `shadowRoot`.
|
|
147
|
+
* **Pseudo-elements**: Captures `::before` and `::after`, including background images.
|
|
148
|
+
* **Backgrounds and images**: Inlines external images as Data URLs.
|
|
149
|
+
* **Fonts**: Replicates applied font families without requiring external font files.
|
|
150
|
+
* **Icon fonts**: Captures icon fonts like **Font Awesome** and **Material Icons**.
|
|
151
|
+
* **Placeholder and Exclusion**:
|
|
152
|
+
|
|
153
|
+
* `data-capture="exclude"`: Skips an element while preserving layout space.
|
|
154
|
+
* `data-capture="placeholder"` + `data-placeholder-text="Text"`: Replaces an element with placeholder text.
|
|
155
|
+
|
|
156
|
+
**Now with improved fidelity and even faster performance.**
|
|
157
|
+
|
|
102
158
|
## Full example
|
|
103
159
|
|
|
104
160
|
```html
|
|
@@ -107,53 +163,62 @@ The main API is exposed as `snapdom` and offers multiple capture methods:
|
|
|
107
163
|
<p>This content will be captured.</p>
|
|
108
164
|
</div>
|
|
109
165
|
|
|
166
|
+
<button id="captureBtn">Capture as img</button>
|
|
167
|
+
|
|
110
168
|
<script type="module">
|
|
111
|
-
import { snapdom } from './snapdom.
|
|
169
|
+
import { snapdom } from './snapdom.mjs';
|
|
112
170
|
|
|
113
|
-
const button = document.
|
|
114
|
-
button.
|
|
115
|
-
|
|
116
|
-
const img = await snapdom.
|
|
171
|
+
const button = document.getElementById('captureBtn');
|
|
172
|
+
button.addEventListener('click', async () => {
|
|
173
|
+
const target = document.getElementById('captureMe');
|
|
174
|
+
const img = await snapdom.toJpg(target, { quality: 0.5});
|
|
117
175
|
document.body.appendChild(img);
|
|
118
|
-
};
|
|
119
|
-
document.body.appendChild(button);
|
|
176
|
+
});
|
|
120
177
|
</script>
|
|
121
178
|
```
|
|
122
179
|
|
|
123
|
-
---
|
|
124
|
-
|
|
125
180
|
## Limitations
|
|
126
181
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
- Iframes are not captured.
|
|
182
|
+
* External images must be CORS-accessible.
|
|
183
|
+
* Iframes are not captured.
|
|
130
184
|
|
|
131
|
-
|
|
185
|
+
### Please if you find a bug **open an issue**.
|
|
132
186
|
|
|
133
|
-
##
|
|
187
|
+
## Benchmarks
|
|
134
188
|
|
|
135
|
-
`snapDOM` is not only highly accurate β it
|
|
189
|
+
`snapDOM` is not only highly accurate β itβs **extremely fast**.
|
|
136
190
|
|
|
137
|
-
In benchmark tests against popular libraries:
|
|
138
191
|
|
|
139
|
-
|
|
140
|
-
|:------------:|:------:|:-------------------------------:|:-------------------------:|
|
|
141
|
-
| 200Γ100 (Small) | `modern-screenshot` | 1.18Γ faster | 4.46Γ faster |
|
|
142
|
-
| 400Γ300 (Modal) | `snapDOM` | 1.04Γ faster | 4.07Γ faster |
|
|
143
|
-
| 1200Γ800 (Page view) | `snapDOM` | 2.43Γ faster | 5.74Γ faster |
|
|
144
|
-
| 2000Γ1500 (Large scroll area) | `snapDOM` | 5.02Γ faster | 9.35Γ faster |
|
|
145
|
-
| 4000Γ2000 (Very large) | `snapDOM` | 11.35Γ faster | 15.98Γ faster |
|
|
192
|
+
Latest benchmarks show significant performance improvements against other libraries:
|
|
146
193
|
|
|
147
|
-
|
|
148
|
-
|
|
194
|
+
| Scenario | vs. `modern-screenshot` | vs. `html2canvas` |
|
|
195
|
+
| -------------------------------- | :---------------------: | :---------------: |
|
|
196
|
+
| Small element (200Γ100) | 6.46Γ faster | 32.27Γ faster |
|
|
197
|
+
| Modal size (400Γ300) | 7.28Γ faster | 32.66Γ faster |
|
|
198
|
+
| Page view (1200Γ800) | 13.17Γ faster | 35.29Γ faster |
|
|
199
|
+
| Large scroll area (2000Γ1500) | 38.23Γ faster | 68.85Γ faster |
|
|
200
|
+
| Very large element (4000Γ2000) | 93.31Γ faster | 133.12Γ faster |
|
|
201
|
+
| Complex small element (200Γ100) | 3.97Γ faster | 15.23Γ faster |
|
|
202
|
+
| Complex modal (400Γ300) | 2.32Γ faster | 5.33Γ faster |
|
|
203
|
+
| Complex page (1200Γ800) | 1.62Γ faster | 1.65Γ faster |
|
|
204
|
+
| Complex large scroll (2000Γ1500) | 1.66Γ faster | 1.24Γ faster |
|
|
205
|
+
| Complex very large (4000Γ2000) | 1.52Γ faster | 1.28Γ faster |
|
|
149
206
|
|
|
150
|
-
β
**Perfect for:**
|
|
151
|
-
- Capturing full-page views
|
|
152
|
-
- Capturing modal windows
|
|
153
|
-
- Complex layouts with custom fonts, backgrounds, or shadow DOM
|
|
154
207
|
|
|
155
|
-
|
|
208
|
+
### Run the benchmarks
|
|
209
|
+
|
|
210
|
+
To run these benchmarks yourself:
|
|
211
|
+
|
|
212
|
+
```sh
|
|
213
|
+
git clone https://github.com/zumerlab/snapdom.git
|
|
214
|
+
cd snapdom
|
|
215
|
+
npm install
|
|
216
|
+
npm run test:benchmark
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
They execute in **headless Chromium** using real DOM nodes.
|
|
156
220
|
|
|
157
221
|
## License
|
|
158
222
|
|
|
159
|
-
MIT Β©
|
|
223
|
+
MIT Β© Zumerlab
|
|
224
|
+
|