MCP HubMCP Hub
返回技能列表

snapdom

2025Emma
更新于 Today
86 次查看
829
95
829
在 GitHub 上查看
其他general

关于

SnapDOM is a dependency-free library that converts HTML elements into SVG or raster images while preserving styles, fonts, and pseudo-elements. Use it for capturing styled DOM content, exporting to multiple formats (SVG, PNG, JPG, WebP), and handling complex rendering with custom scaling or CORS resources. It's ideal for generating screenshots or image exports directly from the DOM.

快速安装

Claude Code

推荐
插件命令推荐
/plugin add https://github.com/2025Emma/vibe-coding-cn
Git 克隆备选方式
git clone https://github.com/2025Emma/vibe-coding-cn.git ~/.claude/skills/snapdom

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

SnapDOM Skill

Fast, dependency-free DOM-to-image capture library for converting HTML elements into scalable SVG or raster image formats.

When to Use This Skill

Use SnapDOM when you need to:

  • Convert HTML elements to images (SVG, PNG, JPG, WebP)
  • Capture styled DOM with pseudo-elements and shadows
  • Export elements with embedded fonts and icons
  • Create screenshots with custom dimensions or scaling
  • Handle CORS-blocked resources using proxy fallback
  • Implement custom rendering pipelines with plugins
  • Optimize performance on large or complex elements

Key Features

Universal Export Options

  • SVG - Scalable vector format, embeds all styles
  • PNG, JPG, WebP - Raster formats with configurable quality
  • Canvas - Get raw Canvas element for further processing
  • Blob - Raw binary data for custom handling

Performance

  • Ultra-fast capture (1.6ms for small elements, ~171ms for 4000×2000)
  • No dependencies - Uses standard Web APIs only
  • Outperforms html2canvas by 10-40x on complex elements

Style Support

  • Embedded fonts (including icon fonts)
  • CSS pseudo-elements (::before, ::after)
  • CSS counters
  • CSS line-clamp
  • Transform and shadow effects
  • Shadow DOM content

Advanced Capabilities

  • Same-origin iframe support
  • CORS proxy fallback for blocked assets
  • Plugin system for custom transformations
  • Straighten transforms (remove rotate/translate)
  • Selective element exclusion
  • Tight bounding box calculation

Installation

NPM/Yarn

npm install @zumer/snapdom
# or
yarn add @zumer/snapdom

CDN (ES Module)

<script type="module">
  import { snapdom } from "https://unpkg.com/@zumer/snapdom/dist/snapdom.mjs";
</script>

CDN (UMD)

<script src="https://unpkg.com/@zumer/snapdom/dist/snapdom.umd.js"></script>

Quick Start Examples

Basic Reusable Capture

// Create reusable capture object
const result = await snapdom(document.querySelector('#target'));

// Export to different formats
const png = await result.toPng();
const jpg = await result.toJpg();
const svg = await result.toSvg();
const canvas = await result.toCanvas();
const blob = await result.toBlob();

// Use the result
document.body.appendChild(png);

One-Step Export

// Direct export without intermediate object
const png = await snapdom.toPng(document.querySelector('#target'));
const svg = await snapdom.toSvg(element);

Download Element

// Automatically download as file
await snapdom.download(element, 'screenshot.png');
await snapdom.download(element, 'image.svg');

With Options

const result = await snapdom(element, {
  scale: 2,                    // 2x resolution
  width: 800,                  // Custom width
  height: 600,                 // Custom height
  embedFonts: true,            // Include @font-face
  exclude: '.no-capture',      // Hide elements
  useProxy: true,              // Enable CORS proxy
  straighten: true,            // Remove transforms
  noShadows: false             // Keep shadows
});

const png = await result.toPng({ quality: 0.95 });

Essential Options Reference

OptionTypePurpose
scaleNumberScale output (e.g., 2 for 2x resolution)
widthNumberCustom output width in pixels
heightNumberCustom output height in pixels
embedFontsBooleanInclude non-icon @font-face rules
useProxyString|BooleanEnable CORS proxy (URL or true for default)
excludeStringCSS selector for elements to hide
straightenBooleanRemove translate/rotate transforms
noShadowsBooleanStrip shadow effects

Common Patterns

Responsive Screenshots

// Capture at different scales
const mobile = await snapdom.toPng(element, { scale: 1 });
const tablet = await snapdom.toPng(element, { scale: 1.5 });
const desktop = await snapdom.toPng(element, { scale: 2 });

Exclude Elements

// Hide specific elements from capture
const png = await snapdom.toPng(element, {
  exclude: '.controls, .watermark, [data-no-capture]'
});

Fixed Dimensions

// Capture with specific size
const result = await snapdom(element, {
  width: 1200,
  height: 630  // Standard social media size
});

CORS Handling

// Fallback for CORS-blocked resources
const png = await snapdom.toPng(element, {
  useProxy: 'https://cors.example.com/?' // Custom proxy
});

Plugin System (Beta)

// Extend with custom exporters
snapdom.plugins([pluginFactory, { colorOverlay: true }]);

// Hook into lifecycle
defineExports(context) {
  return {
    pdf: async (ctx, opts) => { /* generate PDF */ }
  };
}

// Lifecycle hooks available:
// beforeSnap → beforeClone → afterClone →
// beforeRender → beforeExport → afterExport

Performance Comparison

SnapDOM significantly outperforms html2canvas:

ScenarioSnapDOMhtml2canvasImprovement
Small (200×100)1.6ms68ms42x faster
Medium (800×600)12ms280ms23x faster
Large (4000×2000)171ms1,800ms10x faster

Development

Setup

git clone https://github.com/zumerlab/snapdom.git
cd snapdom
npm install

Build

npm run compile

Testing

npm test

Browser Support

  • Chrome/Edge 90+
  • Firefox 88+
  • Safari 14+
  • Mobile browsers (iOS Safari 14+, Chrome Mobile)

Resources

Documentation

scripts/

Add helper scripts here for automation, e.g.:

  • batch-screenshot.js - Capture multiple elements
  • pdf-export.js - Convert snapshots to PDF
  • compare-outputs.js - Compare SVG vs PNG quality

assets/

Add templates and examples:

  • HTML templates for common capture scenarios
  • CSS frameworks pre-configured with snapdom
  • Boilerplate projects integrating snapdom

Related Tools

Tips & Best Practices

  1. Performance: Use scale instead of width/height for better performance
  2. Fonts: Set embedFonts: true to ensure custom fonts appear correctly
  3. CORS Issues: Use useProxy: true if images fail to load
  4. Large Elements: Break into smaller chunks for complex pages
  5. Quality: For PNG/JPG, use quality: 0.95 for best quality
  6. SVG Vectors: Prefer SVG export for charts and graphics

Troubleshooting

Elements Not Rendering

  • Check if element has sufficient height/width
  • Verify CSS is fully loaded before capture
  • Try straighten: false if transforms are causing issues

Missing Fonts

  • Set embedFonts: true
  • Ensure fonts are loaded before calling snapdom
  • Check browser console for font loading errors

CORS Issues

  • Enable useProxy: true
  • Use custom proxy URL if default fails
  • Check if resources are from same origin

Performance Issues

  • Reduce scale value
  • Use noShadows: true to skip shadow rendering
  • Consider splitting large captures into smaller sections

GitHub 仓库

2025Emma/vibe-coding-cn
路径: i18n/zh/skills/snapdom

相关推荐技能

algorithmic-art

该Skill使用p5.js创建包含种子随机性和交互参数探索的算法艺术,适用于生成艺术、流场或粒子系统等需求。它能自动生成算法哲学文档(.md)和对应的交互式艺术代码(.html/.js),确保作品原创性避免侵权。开发者可通过定义计算美学理念快速获得可交互的艺术实现方案。

查看技能

subagent-driven-development

开发

该Skill用于在当前会话中执行包含独立任务的实施计划,它会为每个任务分派一个全新的子代理并在任务间进行代码审查。这种"全新子代理+任务间审查"的模式既能保障代码质量,又能实现快速迭代。适合需要在当前会话中连续执行独立任务,并希望在每个任务后都有质量把关的开发场景。

查看技能

executing-plans

设计

该Skill用于当开发者提供完整实施计划时,以受控批次方式执行代码实现。它会先审阅计划并提出疑问,然后分批次执行任务(默认每批3个任务),并在批次间暂停等待审查。关键特性包括分批次执行、内置检查点和架构师审查机制,确保复杂系统实现的可控性。

查看技能

cost-optimization

其他

这个Claude Skill帮助开发者优化云成本,通过资源调整、标记策略和预留实例来降低AWS、Azure和GCP的开支。它适用于减少云支出、分析基础设施成本或实施成本治理策略的场景。关键功能包括提供成本可视化、资源规模调整指导和定价模型优化建议。

查看技能