|
1 |
| -# svelte-compare-image |
| 1 | +# svelte-compare-image [](https://www.npmjs.com/package/svelte-compare-image) |
2 | 2 |
|
3 |
| -A Svelte component to compare two images. |
| 3 | +A Svelte component to compare two images with a slider to reveal one over the other. |
4 | 4 | Find the package on NPM at [svelte-compare-image](https://npmjs.com/package/svelte-compare-image).
|
5 | 5 |
|
6 | 6 | An interactive example can be found at https://projects.brianm.me/svelte-compare-image/
|
7 | 7 |
|
8 |
| - |
| 8 | +This component is CI tested against Svelte 4, but should work with Svelte 3 as well. |
9 | 9 |
|
10 |
| -The markup, state logic, and styling were originally adapted from [react-compare-image](https://github.com/junkboy0315/react-compare-image). |
| 10 | + |
| 11 | + |
| 12 | +Changes are run inside of `requestAnimationFrame` to improve performance and reduce lag. |
11 | 13 |
|
12 | 14 | ## Docs
|
13 | 15 |
|
14 |
| -The component will display the images and fill available width and height using a ResizeObserver according to the aspect ratios of the images. |
| 16 | +The component will display the images and fill available width and height the aspect ratios of the first image. |
15 | 17 |
|
16 | 18 | To use it, render the component as seen below, providing image src and alt text for the left and right images.
|
17 | 19 | The following CSS custom properties are optional and can be set to customize the appearance of the slider.
|
18 | 20 |
|
19 |
| -| Property | Default Value | |
20 |
| -| ---------------- | ------------- | |
21 |
| -| `--handle-size` | `2.5rem` | |
22 |
| -| `--slider-color` | `#ffffff` | |
23 |
| -| `--slider-width` | `0.125rem` | |
| 21 | +### Props |
| 22 | + |
| 23 | +| Property | Default Value | |
| 24 | +| --------------------------- | -------------------- | |
| 25 | +| `--handle-size` | `2.5rem` | |
| 26 | +| `--handle-background-color` | `rgba(0, 0, 0, 0.6)` | |
| 27 | +| `--handle-background-image` | _see below_ | |
| 28 | +| `--handle-border-width` | `0.125rem` | |
| 29 | +| `--slider-color` | `#ffffff` | |
| 30 | +| `--slider-width` | `0.125rem` | |
| 31 | + |
| 32 | +Default thumb background image: |
| 33 | + |
| 34 | +```css |
| 35 | +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>') |
| 36 | +``` |
| 37 | + |
| 38 | +It looks like this (shown with `darkgray` for viewing here, it is white in the component) |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +### Slots |
| 43 | + |
| 44 | +`CompareImage` has one named slot, `slider-label`, which is used to label the |
| 45 | +slider control. It is displayed in a visually hidden `span`. The default value |
| 46 | +is the following: |
| 47 | + |
| 48 | +> Set the visibility of one image over the other. 0 is full visibility of |
| 49 | +> the second image and 100 is full visibility of the first image. Any |
| 50 | +> amount in-between is a left/right cutoff at the percentage of the |
| 51 | +> slider. |
| 52 | +
|
| 53 | +## Example |
24 | 54 |
|
25 | 55 | ```svelte
|
26 | 56 | <script lang="ts">
|
27 | 57 | import { CompareImage } from "svelte-compare-image";
|
28 | 58 | // or
|
29 | 59 | import CompareImage from "svelte-compare-image/CompareImage.svelte";
|
| 60 | +
|
| 61 | + // too many quotes to deal with, storing as a variable is easier |
| 62 | + 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>')`; |
30 | 63 | </script>
|
31 | 64 |
|
32 | 65 | <CompareImage
|
33 |
| - imageLeftSrc="https://via.placeholder.com/600x400/ffaa00/ffffff/?text=Left" |
| 66 | + imageLeftSrc="https://via.placeholder.com/600x400/ffaa00/ffffff/?text=Example+Text" |
34 | 67 | imageLeftAlt="left"
|
35 |
| - imageRightSrc="https://via.placeholder.com/600x400/00aaff/ffffff?text=Right" |
| 68 | + imageRightSrc="https://via.placeholder.com/600x400/00aaff/ffffff?text=Example+Text" |
36 | 69 | imageRightAlt="right"
|
37 | 70 | --handle-size="2.5rem"
|
| 71 | + --handle-background-color="rgba(0, 0, 0, 0.6)" |
| 72 | + --handle-background-image={handleBackgroundImage} |
| 73 | + --handle-border-width="0.125rem" |
38 | 74 | --slider-color="#ffffff"
|
39 | 75 | --slider-width="0.125rem"
|
40 |
| -/> |
| 76 | +> |
| 77 | + <svelte:fragment slot="slider-label"> |
| 78 | + Set the visibility of one image over the other. 0 is full visibility of the |
| 79 | + second image and 100 is full visibility of the first image. Any amount |
| 80 | + in-between is a left/right cutoff at the percentage of the slider. |
| 81 | + </svelte:fragment> |
| 82 | +</CompareImage> |
41 | 83 | ```
|
0 commit comments