@zumer/snapdom 0.9.2 β†’ 0.9.8

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 CHANGED
@@ -1,15 +1,33 @@
1
1
  # snapDOM
2
2
 
3
- **snapDOM** is a high-fidelity DOM capture tool, developed as part of the animation engine I'm building for Zumly β€” a framework for creating smooth zoom-based view transitions.
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
 
28
+ ## Demo
29
+
30
+ https://zumerlab.github.io/snapdom/
13
31
 
14
32
  ## Installation
15
33
 
@@ -47,7 +65,7 @@ import { snapdom } from './snapdom.mjs';
47
65
 
48
66
  ### Script Tag (Type Module)
49
67
 
50
- ```javascript
68
+ ```html
51
69
  <script type="module">
52
70
  import { snapdom } from 'https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs';
53
71
  </script>
@@ -55,53 +73,88 @@ import { snapdom } from './snapdom.mjs';
55
73
 
56
74
  Now you can call `snapdom(el)`, `snapdom.toPng(el)`, etc., directly in your JavaScript.
57
75
 
58
-
59
76
  ## Basic usage
60
77
 
61
78
  ```javascript
62
- // Capture an element as SVG Data URL
63
- const svgDataUrl = await snapdom(document.querySelector("#myElement"));
79
+ // Capture an element as <img> tag
80
+ const el = document.querySelector("#myElement")
81
+ const img = await snapdom.toImg(el);
64
82
 
65
- // Insert the captured image into the page
66
- const img = new Image();
67
- img.src = svgDataUrl;
68
83
  document.body.appendChild(img);
69
84
  ```
70
85
 
71
-
72
86
  ## API
73
87
 
74
88
  ### Note: API may evolve until v1.0.0!
75
89
 
76
90
  The main API is exposed as `snapdom` and offers multiple capture methods:
77
91
 
78
- | Method | Description | Returns |
79
- |:----------------------|:---------------------------------------|:-------------------------|
80
- | `snapdom(el, options?)` | Captures as SVG Data URL | `Promise<string>` |
81
- | `snapdom.toImg(el, options?)` | Captures as `HTMLImageElement` (SVG) | `Promise<HTMLImageElement>` |
82
- | `snapdom.toCanvas(el, options?)` | Captures as `HTMLCanvasElement` | `Promise<HTMLCanvasElement>` |
83
- | `snapdom.toPng(el, options?)` | Captures as PNG image (`Image`) | `Promise<HTMLImageElement>` |
84
- | `snapdom.toJpg(el, options?)` | Captures as JPG image (`Image`) | `Promise<HTMLImageElement>` |
85
- | `snapdom.toWebp(el, options?)` | Captures as WebP image (`Image`) | `Promise<HTMLImageElement>` |
86
- | `snapdom.toBlob(el, options?)` | Captures as SVG `Blob` | `Promise<Blob>` |
87
-
88
- **Options:**
89
- - `scale` *(number)*: Scale factor (default is `1`)
90
- - `quality` *(number)*: Compression quality for JPG/WebP (0–1)
91
- - `backgroundColor` *(string)*: Background fill for JPG/WebP exports
92
+ | Method | Description |
93
+ | :------------------------------- | :------------------------------------------------------------- |
94
+ | `snapdom(el, options?)` | Captures as SVG Data URL |
95
+ | `snapdom.toImg(el, options?)` | Captures as `HTMLImageElement` (SVG) |
96
+ | `snapdom.toCanvas(el, options?)` | Captures as `HTMLCanvasElement` |
97
+ | `snapdom.toPng(el, options?)` | Captures as PNG image (`Image`) |
98
+ | `snapdom.toJpg(el, options?)` | Captures as JPG image (`Image`) |
99
+ | `snapdom.toWebp(el, options?)` | Captures as WebP image (`Image`) |
100
+ | `snapdom.toBlob(el, options?)` | Captures as SVG `Blob` |
101
+ | `preCache(root?, options?)` | Preload resources for faster and more accurate captures |
102
+
103
+ ---
104
+
105
+ ### Options
106
+
107
+ You can pass an `options` object to control the behavior of all capture methods:
108
+
109
+ | Option | Type | Default | Description |
110
+ | ----------------- | --------- | -------- | ----------------------------------------------------------------------------------- |
111
+ | `compress` | `boolean` | `true` | Removes default styles to reduce size and deduplicate CSS where possible. |
112
+ | `fast` | `boolean` | `true` | Skips `requestIdleCallback` delays and executes preparation immediately. |
113
+ | `embedFonts` | `boolean` | `false` | Inlines external fonts as Data URLs (except icon fonts, which are always embedded). |
114
+ | `scale` | `number` | `1` | Multiplies the output resolution without affecting layout size. |
115
+ | `backgroundColor` | `string` | `"#fff"` | Used when exporting to JPG or WebP formats (which don't support transparency). |
116
+ | `quality` | `number` | `1` | Compression quality for JPG/WebP, between `0` and `1`. |
92
117
 
93
118
  ---
94
119
 
120
+ ### `preCache()` – Optional helper
121
+
122
+ The `preCache()` function can be used to load external resources (like images and fonts) in advance.
123
+
124
+ ```js
125
+ import { preCache } from '@zumer/snapdom';
126
+
127
+ await preCache(document.body, {
128
+ embedFonts: true,
129
+ preWarm: true
130
+ });
131
+ ```
132
+
133
+ ```js
134
+ import { snapdom, preCache } from './snapdom.mjs';
135
+ window.addEventListener('load', async () => {
136
+ await preCache();
137
+ console.log('πŸ“¦ Resources preloaded');
138
+ });
139
+ ```
140
+
141
+ **Options for `preCache()`:**
142
+
143
+ * `embedFonts` *(boolean, default: true)* β€” Inlines non-icon fonts during preload.
144
+ * `reset` *(boolean, default: false)* β€” Clears all existing internal caches.
145
+ * `preWarm` *(boolean, default: true)* β€” Runs a fake capture in memory to warm up styles, fonts and layout detection.
146
+
95
147
  ## Special features
96
148
 
97
- - **Shadow DOM**: Captures content inside Web Components and `shadowRoot`.
98
- - **Pseudo-elements**: Captures `::before` and `::after`, including background images.
99
- - **Backgrounds and images**: Inlines external images as Data URLs.
100
- - **Fonts**: Replicates applied font families without requiring external font files.
101
- - **Icon fonts**: Captures icon fonts like **Font Awesome** and **Material Icons**.
102
- - **Placeholder and Exclusion**:
103
- - `data-capture="exclude"`: Skips an element while preserving layout space.
104
- - `data-capture="placeholder"` + `data-placeholder-text="Text"`: Replaces an element with placeholder text.
149
+ * **Shadow DOM**: Captures content inside Web Components and `shadowRoot`.
150
+ * **Pseudo-elements**: Captures `::before` and `::after`, including background images.
151
+ * **Backgrounds and images**: Inlines external images as Data URLs.
152
+ * **Fonts**: Replicates applied font families without requiring external font files.
153
+ * **Icon fonts**: Captures icon fonts like **Font Awesome** and **Material Icons**.
154
+ * **Placeholder and Exclusion**:
155
+
156
+ * `data-capture="exclude"`: Skips an element while preserving layout space.
157
+ * `data-capture="placeholder"` + `data-placeholder-text="Text"`: Replaces an element with placeholder text.
105
158
 
106
159
  **Now with improved fidelity and even faster performance.**
107
160
 
@@ -121,39 +174,42 @@ The main API is exposed as `snapdom` and offers multiple capture methods:
121
174
  const button = document.getElementById('captureBtn');
122
175
  button.addEventListener('click', async () => {
123
176
  const target = document.getElementById('captureMe');
124
- const img = await snapdom.toImg(target);
177
+ const img = await snapdom.toJpg(target, { quality: 0.5});
125
178
  document.body.appendChild(img);
126
179
  });
127
180
  </script>
128
181
  ```
129
182
 
130
-
131
183
  ## Limitations
132
184
 
133
- - External images must be CORS-accessible.
134
- - Fonts must be fully loaded before capturing (`document.fonts.ready` is automatically awaited).
135
- - Iframes are not captured.
185
+ * External images must be CORS-accessible.
186
+ * Iframes are not captured.
136
187
 
188
+ ### Please if you find a bug **open an issue**.
137
189
 
138
190
  ## Benchmarks
139
191
 
140
- `snapDOM` is not only highly accurate β€” it’s **extremely fast**, especially for large elements.
192
+ `snapDOM` is not only highly accurate β€” it’s **extremely fast**.
141
193
 
142
- Latest benchmarks show **major speed improvements** over existing libraries:
143
194
 
144
- | Element Size | vs. `modern-screenshot` | vs. `html2canvas` |
145
- |:------------------------:|:-----------------------:|:-----------------:|
146
- | **Small (200Γ—100)** | 9.84Γ— faster | 33.19Γ— faster |
147
- | **Modal (400Γ—300)** | 11.72Γ— faster | 36.03Γ— faster |
148
- | **Page view (1200Γ—800)** | 24.40Γ— faster | 55.65Γ— faster |
149
- | **Large (2000Γ—1500)** | 49.85Γ— faster | 90.74Γ— faster |
150
- | **Very large (4000Γ—2000)** | 138.93Γ— faster | 148.41Γ— faster |
195
+ Latest benchmarks show significant performance improvements against other libraries:
196
+
197
+ | Scenario | vs. `modern-screenshot` | vs. `html2canvas` |
198
+ | -------------------------------- | :---------------------: | :---------------: |
199
+ | Small element (200Γ—100) | 6.46Γ— faster | 32.27Γ— faster |
200
+ | Modal size (400Γ—300) | 7.28Γ— faster | 32.66Γ— faster |
201
+ | Page view (1200Γ—800) | 13.17Γ— faster | 35.29Γ— faster |
202
+ | Large scroll area (2000Γ—1500) | 38.23Γ— faster | 68.85Γ— faster |
203
+ | Very large element (4000Γ—2000) | 93.31Γ— faster | 133.12Γ— faster |
204
+ | Complex small element (200Γ—100) | 3.97Γ— faster | 15.23Γ— faster |
205
+ | Complex modal (400Γ—300) | 2.32Γ— faster | 5.33Γ— faster |
206
+ | Complex page (1200Γ—800) | 1.62Γ— faster | 1.65Γ— faster |
207
+ | Complex large scroll (2000Γ—1500) | 1.66Γ— faster | 1.24Γ— faster |
208
+ | Complex very large (4000Γ—2000) | 1.52Γ— faster | 1.28Γ— faster |
151
209
 
152
- βœ… **snapDOM** also improved fidelity, even on complex layouts:
153
- - Up to **7.82Γ— faster** for complex modal captures.
154
- - Still faster even at very large sizes.
155
210
 
156
211
  ### Run the benchmarks
212
+
157
213
  To run these benchmarks yourself:
158
214
 
159
215
  ```sh
@@ -165,7 +221,7 @@ npm run test:benchmark
165
221
 
166
222
  They execute in **headless Chromium** using real DOM nodes.
167
223
 
168
-
169
224
  ## License
170
225
 
171
226
  MIT Β© Zumerlab
227
+