惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

小小孩

vaultwarden 部署 - 小小孩 前端项目路径别名终极解决方案 - 小小孩 去除宝塔面板一些无用的页面信息 - 小小孩 如何在 Bing 和 百度搜索中去除 CSDN 搜索结果 Nginx 安装 brotli 模块 - 小小孩 JS 深拷贝终结者 structuredClone - 小小孩 http 协议头 Content-Disposition 的作用 - 小小孩 Echarts示例 Demo 合集网站 - 小小孩 LNMP Nginx 安装 brotli 模块
JavaScript 高性能现代网页截图 - 小小孩
小小孩 · 2001-09-12 · via 小小孩

mffltfkw.png

最近发现一款简单好用、功能强大的前端截图开发工具库 SnapDOM,现在可以彻底抛弃 html2canvas 了。

SnapDOM

SnapDOM是一款性能优越的网页截图工具,能够快速准确地将网页元素转换成多种图片格式,包括SVG、PNG、JPG、WebP以及Canvas元素。它支持复杂元素的截图,如CSS样式、伪元素、Shadow DOM等,并且能够高度还原网页的视觉效果。

技术特性

  • 零依赖:轻量化(约8KB),纯原生 JavaScript 实现。
  • 渲染速度超快:全屏截图耗时仅0.8秒(比 html2canvas 快8倍);
  • 像素级还原:可以精准渲染复杂样式(渐变、阴影、SVG)和动态加载内容(比如懒加载图片、异步数据等);

html2canvasdom-to-image相比,SnapDOM在速度和还原度上都有显著优势,尤其在处理大型元素时速度可达到html2canvas的93倍以上。

将复杂DOM结构分解为可复用的矢量层和像素层,通过SVG转换和Canvas渲染分离,将处理时间从全量克隆模式缩短至分层处理模式

对比

对比项SnapDOMhtml2canvas
截图速度✅ 超快❌ 慢
高分辨率支持✅ 高清无模糊,甚至支持矢量❌ 缩放易失真
滚动截图✅ 自动拼接长图❌ 需手动截取
GPU加速✅ 异步渲染❌ 阻塞主线程
CSS 支持✅ 绝大部分都支持❌ 大部分不支持
跨域资源✅ 完美支持⚠️ 部分会失效

使用

安装

npm i @zumer/snapdom

简单截图实现

import { snapdom } from '@zumer/snapdom';
const el = document.querySelector('#target');
const res = await snapdom(el);
// 下载图片为指定格式
await res.download({ format: 'jpg', filename: '截图.jpg' });

注意事项

SnapDOM 各方面都很强,是新一代的网页截图库,完全可以在生产上使用,不过需要注意一下几点:

  • 图片跨域:有外部图片,需要得确保这些资源支持 CORS,否则会截图失败;
  • iframe 限制:浏览器的安全限制,SnapDOM 不能截 iframe 里的内容;
  • 长页面截图:建议分块去截超长页面,否则可能会导致内存溢出。