A powerful web component for comparing multiple images with individual opacity controls. Perfect for before/after comparisons, design iterations, or any multi-layer image analysis.
- Multi-layer Support: Compare unlimited number of images
- Individual Controls: Each overlay gets its own opacity slider
- Custom Layer Names: Use meaningful labels like "Sketch", "Final", etc.
- Responsive Design: Automatically scales and maintains aspect ratios
- Grid-Aligned Controls: Perfect slider alignment regardless of label length
- Shadow DOM Encapsulation: No style conflicts with your existing CSS
- Accessibility: Proper ARIA labels and keyboard navigation
- Include the component script in your HTML:
<script src="image-onion-skin.js"></script>- Use the component with your images:
<image-onion-skin>
<img src="image1.jpg" alt="Bottom image">
<img src="image2.jpg" alt="Top image">
</image-onion-skin>Compare two images with a single "Opacity" slider:
<image-onion-skin>
<img src="before.jpg" alt="Before">
<img src="after.jpg" alt="After">
</image-onion-skin>Add multiple images for complex comparisons with individual "Layer X" controls:
<image-onion-skin>
<img src="base-image.jpg" alt="Base layer">
<img src="overlay1.jpg" alt="Second layer">
<img src="overlay2.jpg" alt="Third layer">
</image-onion-skin>Use the data-layer-name attribute for meaningful slider labels:
<image-onion-skin>
<img src="background.jpg" alt="Background">
<img src="sketch.jpg" data-layer-name="Sketch">
<img src="colors.jpg" data-layer-name="Colors">
<img src="final.jpg" data-layer-name="Final">
</image-onion-skin>No limit on the number of images you can compare:
<image-onion-skin>
<img src="layer1.jpg" alt="Base">
<img src="layer2.jpg" alt="Layer 2">
<img src="layer3.jpg" alt="Layer 3">
<img src="layer4.jpg" alt="Layer 4">
<img src="layer5.jpg" alt="Layer 5">
</image-onion-skin>- Base Layer: The first
<img>element becomes the base layer (always visible) - Overlay Layers: Each additional
<img>element becomes an overlay layer - Individual Controls: Each overlay gets its own labeled slider to control opacity (0-100%)
- Custom Naming: Use
data-layer-name="Custom Name"to give layers meaningful names - Automatic Positioning: Images are automatically positioned with proper z-indexing
- Dimension Matching: All images are scaled to match the base image dimensions
- Encapsulation: The component uses Shadow DOM for style encapsulation
The component intelligently labels sliders based on context:
- Two Images: Shows "Opacity:" (simple case)
- Multiple Images (no custom names): Shows "Layer 2:", "Layer 3:", etc.
- Custom Names: Shows your custom name + ":" (e.g., "Sketch:", "Final:")
- Design Workflow: Compare sketches, wireframes, and final designs
- Photo Editing: Before/after comparisons with multiple edit stages
- Art Process: Show progression from concept to finished artwork
- Development: Compare different UI states or implementations
- Medical Imaging: Layer different scan types or time periods
- Architecture: Compare different design iterations or views
The component uses Shadow DOM, so it won't conflict with your existing styles. The built-in styling includes:
- Clean, modern appearance with rounded corners
- Smooth opacity transitions
- Grid-aligned controls for perfect slider alignment
- Responsive design that works on all screen sizes
- Professional color scheme that fits most designs
- Web Components API: Uses custom elements with Shadow DOM
- Slot-based Content: Uses
<slot>to accept your<img>elements - Event Handling: Listens for slider changes and image loading
- CSS Grid Layout: Modern layout for perfect control alignment
- Cross-browser Compatible: Works in all modern browsers
- No Dependencies: Pure vanilla JavaScript, no external libraries
You can mix custom names with default numbering:
<image-onion-skin>
<img src="base.jpg">
<img src="overlay1.jpg" data-layer-name="Sketch"> <!-- Shows "Sketch:" -->
<img src="overlay2.jpg"> <!-- Shows "Layer 3:" -->
<img src="overlay3.jpg" data-layer-name="Final"> <!-- Shows "Final:" -->
</image-onion-skin>The component automatically handles different image sizes and maintains proper aspect ratios.
The component provides helpful error messages:
- If fewer than 2 images are provided
- If images fail to load
image-onion-skin/
├── image-onion-skin.js # Main web component
├── index.html # Demo page with examples
└── README.md # This documentation
Feel free to submit issues, feature requests, or pull requests to improve this component!
This project is open source and available under the MIT License.