Giter Site home page Giter Site logo

brianmitchl / svelte-compare-image Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 2.9 MB

๐Ÿ–ผ A Svelte component to compare two images with a slider to reveal one over the other

Home Page: https://projects.brianm.me/svelte-compare-image/

License: MIT License

JavaScript 3.80% CSS 5.59% HTML 3.45% Svelte 46.27% TypeScript 40.89%
svelte compare-images

svelte-compare-image's Introduction

svelte-compare-image npm

A Svelte component to compare two images with a slider to reveal one over the other. Find the package on NPM at svelte-compare-image.

An interactive example can be found at https://projects.brianm.me/svelte-compare-image/

This component requires Svelte 4. Use version 2 to support Svelte 3.

screen recording example

Changes are run inside of requestAnimationFrame to improve performance and reduce lag.

Docs

The component will display the images and fill available width and height the aspect ratios of the first image.

To use it, render the component as seen below, providing image src and alt text for the left and right images. The following CSS custom properties are optional and can be set to customize the appearance of the slider.

Props

Property Default Value
--handle-size 2.5rem
--handle-background-color rgba(0, 0, 0, 0.6)
--handle-background-image see below
--handle-border-width 0.125rem
--slider-color #ffffff
--slider-width 0.125rem

Default thumb background image:

url('data:image/svg+xml;utf8,<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 12H21M3 12L7 8M3 12L7 16M21 12L17 16M21 12L17 8" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>')

It looks like this (shown with darkgray for viewing here, it is white in the component)

slider icon

Slots

CompareImage has one named slot, slider-label, which is used to label the slider control. It is displayed in a visually hidden span. The default value is the following:

Set the visibility of one image over the other. 0 is full visibility of the second image and 100 is full visibility of the first image. Any amount in-between is a left/right cutoff at the percentage of the slider.

Example

<script lang="ts">
  import { CompareImage } from "svelte-compare-image";
  // or
  import CompareImage from "svelte-compare-image/CompareImage.svelte";

  // too many quotes to deal with, storing as a variable is easier
  const handleBackgroundImage = `url('data:image/svg+xml;utf8,<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 12H21M3 12L7 8M3 12L7 16M21 12L17 16M21 12L17 8" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>')`;
</script>

<CompareImage
  imageLeftSrc="https://via.placeholder.com/600x400/ffaa00/ffffff/?text=Example+Text"
  imageLeftAlt="left"
  imageRightSrc="https://via.placeholder.com/600x400/00aaff/ffffff?text=Example+Text"
  imageRightAlt="right"
  --handle-size="2.5rem"
  --handle-background-color="rgba(0, 0, 0, 0.6)"
  --handle-background-image={handleBackgroundImage}
  --handle-border-width="0.125rem"
  --slider-color="#ffffff"
  --slider-width="0.125rem"
>
  <svelte:fragment slot="slider-label">
    Set the visibility of one image over the other. 0 is full visibility of the
    second image and 100 is full visibility of the first image. Any amount
    in-between is a left/right cutoff at the percentage of the slider.
  </svelte:fragment>
</CompareImage>

svelte-compare-image's People

Contributors

brianmitchl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ivandrew05

svelte-compare-image's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.