npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

image-comparison-component

v0.0.5

Published

Compare two images using a slider, an overlay, or a side by side view

Downloads

3

Readme

<image-comparison>

Compare two images using a slider, an overlay, or a side by side split view.
This webcomponent follows the open-wc recommendation.

https://user-images.githubusercontent.com/1145514/189548478-f2fc7f66-e886-4d61-93e5-7e0281d32b17.mp4

SRT file for captions

Features

  • three variants available: slider, overlay, split
  • Right-to-left language support (document's dir attribute set to ltr/rtl)
  • Keyboard controls:
    • Slider:
      • / move in small steps
      • Shift + ←/→ for increased steps
      • (Home / Pos1) / End, ⌘/Ctrl + ←/→ to jump left or right
    • Overlay: when in focus press and hold Enter / Space
  • Custom events
  • Support for labels and custom prompts
  • Further customization through CSS variables
  • Runs ✨everywhere✨ because it's a webcomponent

Installation

npm i image-comparison-component

Usage

Import

// Script
<script type="module">
  import 'image-comparison/image-comparison.js';
</script>

// Bundler
import 'image-comparison/image-comparison.js';

HTML

The general structure always follows the scheme below. The only thing that always needs to be adjusted is the "variant" attribute. Further customization through attributes.

<image-comparison variant="slider">
  <label slot="label-before">Original</label>
  <label slot="label-after">Vogue Filter</label>

  <img slot="image-before" src="kasimir.jpg" alt="kasimir" />
  <img
    slot="image-after"
    src="kasimir_filter.jpg"
    alt="kasimir with the vogue filter applied"
  />
</image-comparison>

Slider

slider variant

The "slider" variant lets you compare your images by dragging a slider. You can also just click or tap somewhere or use its keyboard controls

  • / move in small steps
  • Shift + ←/→ for increased steps
  • (Home / Pos1) / End, ⌘/Ctrl + ←/→ to jump left or right

Overlay

overlay variant

The "overlay" variant can be used to compare images with a tap/press and hold action or pressing the Enter / Space key when in focus. Its focus-ring offset can be adjusted using the --overlay-focus-offset variable.

Split

side by side split view variant

"Split" is the simplest of the three variants, as it simply displays both images side by side or one below the other, and does not provide any form of interaction. You can set its columns minimum and maximum width (--split-column-min-width, --split-column-max-width) as well as its column spacing (--split-gap). At least one of the column variables must be a fixed length.

Slots

Slots allow you to define placeholders in your template that can be filled with any markup fragment you want. They are identified using the 'slot' attribute. These placeholder are meant to be used to display the images you want to compare and their associated labels. Pleace add an alt-attribute to describe your images.

| Name | Example value | |-----------------|---------------------------------------------------------------------------------| | label-before | <label slot="label-before">Original</label> | | label-after | <label slot="label-after">After</label> | | image-before | <img slot="image-before" src="kasimir.jpg" alt="kasimir" /> | | image-after | <img slot="image-after" src="kasimir_filter.jpg" alt="kasimir with filter" /> |

Attributes

| Name | Default | Description | |-----------------|------------------------------|-------------------------------------------------------------------------------| | variant | slider | Defines the look and behaviour of this component: slider, overlay, split| | overlayPrompt | Tap and hold to compare | - | | sliderPrompt | Move the slider to compare | - | | sliderSteps | 5 | Number of steps used with Shift + ArrowLeft/ArrowRight | | sliderPosition| 50 | Current slider position expressed in percent |

CSS variables

Select and set the following variables to further customize this component

| Variable | Purpose | Default value | |------------------------------|-----------------------------------------------|-------------------------------| | --base-gap | Spacing for paddings, margins & gaps | 16px | | --base-radius | Border radius for different elements | 8px | | variant: slider | | --thumb-size | The size of the button which moves the slider | 40px | | --thumb-border-width | - | 3px | | --thumb-bar-width | The divider width | --thumb-border-width: 3px | | --slider-color | The color for the thumb button and bar | #fff | | --slider-color-active | The color for active states | #fff | | --label-background-color | - | #fff | | --label-color | - | #000 | | --label-radius | - | --base-radius: 8px | | variant: overlay | | --overlay-focus-offset | Offset value for focus ring around the image | --base-gap / 2 | | variant: split | | --split-gap | Gap between images | --base-gap: 16px | | --split-column-min-width | Min width of a split column | 100px | | --split-column-max-width | Max width of a split column | 1fr |

Example usage:

image-comparison {
  --thumb-size: 30px;
}

Custom events

There are two custom events you can listen to:

DragEvent {
  type: 'drag-event',
  targetElement {
    // ...
    sliderPosition: 73,
  }
}

and

PressEvent {
  type: 'press-event',
  targetElement {},
  pressed: true
}

Example usage:

window.addEventListener('press-event', (e) => console.log(e));
// PressEvent {isTrusted: false, pressed: false, targetElement: image-comparison, type: 'press-event', target: Window, …}

Linting and formatting

To scan the project for linting and formatting errors, run

npm run lint

To automatically fix linting and formatting errors, run

npm run format

Tooling configs

For most of the tools, the configuration is in the package.json to reduce the amount of files in your project.

If you customize the configuration a lot, you can consider moving them to individual files.

Local Demo with web-dev-server

npm start

To run a local development server that serves the basic demo located in demo/index.html

Inspiration & other useful resources

Some websites I took inspiration from:

Useful resources: