smooth-screenshot 1.0.0
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/LICENSE +21 -0
- package/README.md +104 -0
- package/dist/index.cjs +1294 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +97 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.mjs +1285 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luccas Carvalho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# smooth-screenshot
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/smooth-screenshot)
|
|
4
|
+
[](https://bundlephobia.com/package/smooth-screenshot)
|
|
5
|
+
[](https://www.npmjs.com/package/smooth-screenshot?activeTab=dependencies)
|
|
6
|
+
[](https://www.npmjs.com/package/smooth-screenshot)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
|
|
9
|
+
**Capture any DOM element to PNG, JPEG, WebP, SVG, or PDF — without ever freezing the UI.**
|
|
10
|
+
|
|
11
|
+
Most DOM-to-image libraries do all their work in one synchronous burst: on a large
|
|
12
|
+
element the page locks up, animations stutter, and your loading spinner freezes
|
|
13
|
+
mid-spin. `smooth-screenshot` slices the work across frames with an internal
|
|
14
|
+
cooperative scheduler, so the main thread stays responsive the whole time. Your
|
|
15
|
+
skeletons and progress animations keep running while the capture happens.
|
|
16
|
+
|
|
17
|
+
- **Never blocks the main thread** — work yields to the event loop on a frame budget.
|
|
18
|
+
- **Per-phase progress** — drive real loading states (`clone` → `embed` → `rasterize` → `encode`).
|
|
19
|
+
- **Cancellable** — pass an `AbortSignal`.
|
|
20
|
+
- **Huge content** — PNG and PDF render in memory-bounded tiles, beyond the browser's single-canvas size limit.
|
|
21
|
+
- **Multi-format** — `toPng`, `toJpeg`, `toWebp`, `toSvg`, `toPdf`, `toBlob`, `toCanvas`.
|
|
22
|
+
- **Zero runtime dependencies.** Inline web worker, hand-written PDF, native compression.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install smooth-screenshot
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { toPng, toPdf, toSvg } from 'smooth-screenshot'
|
|
32
|
+
|
|
33
|
+
const node = document.querySelector('#capture')!
|
|
34
|
+
|
|
35
|
+
// PNG data URL
|
|
36
|
+
const url = await toPng(node)
|
|
37
|
+
|
|
38
|
+
// PDF blob
|
|
39
|
+
const pdf = await toPdf(node)
|
|
40
|
+
|
|
41
|
+
// SVG (vector, infinite zoom)
|
|
42
|
+
const svg = await toSvg(node)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Loading state that doesn't freeze
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
await toPng(node, {
|
|
49
|
+
onProgress({ phase, overall }) {
|
|
50
|
+
setLabel(phase) // 'clone' | 'embed' | 'rasterize' | 'encode'
|
|
51
|
+
setBar(overall) // 0..1
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Cancellation
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const controller = new AbortController()
|
|
60
|
+
cancelButton.onclick = () => controller.abort()
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
await toPng(node, { signal: controller.signal })
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if (err instanceof DOMException && err.name === 'AbortError') {
|
|
66
|
+
// user cancelled
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Options
|
|
72
|
+
|
|
73
|
+
| Option | Default | Description |
|
|
74
|
+
| --- | --- | --- |
|
|
75
|
+
| `type` | `'png'` | Image format for `toBlob` (`png` \| `jpeg` \| `webp`). |
|
|
76
|
+
| `quality` | `0.92` | 0–1 for lossy formats. |
|
|
77
|
+
| `scale` | `devicePixelRatio` | Output pixel density (alias: `pixelRatio`). |
|
|
78
|
+
| `backgroundColor` | transparent | Background painted behind the element (JPEG defaults to white). |
|
|
79
|
+
| `width` / `height` | element size | Force output dimensions (CSS px). |
|
|
80
|
+
| `crop` | — | `{ x, y, width, height }` — capture a sub-region. Use for full-res sections of very large content. |
|
|
81
|
+
| `signal` | — | `AbortSignal` to cancel the capture. |
|
|
82
|
+
| `onProgress` | — | `(p: { phase, phaseRatio, overall }) => void`. |
|
|
83
|
+
| `frameBudgetMs` | `5` | Main-thread time per slice before yielding. |
|
|
84
|
+
| `worker` | `true` | Offload resource fetching to an inline worker. `false` or `{ count }`. |
|
|
85
|
+
| `maxTileSize` | `4096` | Tile edge (device px) before tiling kicks in. |
|
|
86
|
+
| `filter` | — | `(node) => boolean` to exclude nodes. |
|
|
87
|
+
| `fonts` | — | `{ skip?, preferWoff2? }`. |
|
|
88
|
+
| `fetch` | — | `{ requestInit?, placeholder?, timeoutMs?, retries? }`. |
|
|
89
|
+
| `onResourceError` | — | `(url, error) => void` — a failed resource doesn't fail the capture. |
|
|
90
|
+
|
|
91
|
+
## Notes & limits
|
|
92
|
+
|
|
93
|
+
- **Very large content:** `toPng` and `toPdf` render at full resolution via tiling.
|
|
94
|
+
`toJpeg` / `toWebp` / `toCanvas` are bound by the browser's single-canvas size
|
|
95
|
+
limit and are downscaled to fit when exceeded. To export huge content crisply in
|
|
96
|
+
those formats, capture sections with `crop`.
|
|
97
|
+
- **Cross-origin resources** must allow CORS, otherwise they can't be embedded and
|
|
98
|
+
are replaced with a transparent placeholder (see `onResourceError`).
|
|
99
|
+
- **Browser support:** requires `CompressionStream` for tiled PNG/PDF output
|
|
100
|
+
(Chrome 80+, Firefox 113+, Safari 16.4+).
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT © Luccas Carvalho
|