diff --git a/docs/docs/demos/morph.mdx b/docs/docs/demos/morph.mdx new file mode 100644 index 0000000..46a89b3 --- /dev/null +++ b/docs/docs/demos/morph.mdx @@ -0,0 +1,37 @@ +--- +sidebar_position: 10 +--- + +import { SourceTabs } from "@site/src/components/source-tabs/source-tabs" +import { MorphGallery } from "@site/src/components/demos/morph-gallery/morph-gallery" +import { OpenInCodePen } from "@site/src/components/codepen/open-in-codepen" + +import morphSource from "!!raw-loader!@site/src/components/demos/morph-gallery/morph-gallery.tsx" +import morphRendererSource from "!!raw-loader!@site/src/components/demos/morph-gallery/morph-renderer.ts" +import morphStyles from "!!raw-loader!@site/src/components/demos/morph-gallery/morph-gallery.module.scss" +import morphJs from "!!ts-to-js!@site/src/components/demos/morph-gallery/morph-gallery.tsx" +import morphPen from "!!ts-to-js?codepen!@site/src/components/demos/morph-gallery/morph-gallery.tsx" + +# Morph Gallery + +Morph gallery using a smooth, eased WebGL image morphing transition. + +Under the hood, it cross-blends and warps between two images on a full-bleed canvas using custom shaders, quintic easing (easeInOutQuint), aspect-ratio scaling, and directional movement. + + + + + +## Source + + + +## Renderer (WebGL & Shaders) + + + +## Styles + +The demos on this site are styled with [SCSS modules](https://github.com/css-modules/css-modules). + + diff --git a/docs/src/components/demos/morph-gallery/morph-gallery.module.scss b/docs/src/components/demos/morph-gallery/morph-gallery.module.scss new file mode 100644 index 0000000..9514e92 --- /dev/null +++ b/docs/src/components/demos/morph-gallery/morph-gallery.module.scss @@ -0,0 +1,170 @@ +.root { + width: 100%; + height: auto; + aspect-ratio: 16/9; + overflow: hidden; + border-radius: 10px; + margin-block-end: 16px; +} + +.root > div { + width: 100%; + height: 100%; +} + +.stage { + position: relative; + width: 100%; + height: 100%; +} + +.stage::after { + content: ""; + position: absolute; + inset: auto 0 0; + height: 220px; + background: linear-gradient(to bottom, transparent, rgb(0 0 0 / 65%)); + pointer-events: none; + z-index: 5; +} + +.canvas { + position: absolute; + inset: 0; + display: block; + width: 100%; + height: 100%; +} + +.navPrev, +.navNext { + backdrop-filter: contrast(2) brightness(0.7) blur(10px); + position: absolute; + top: 50%; + transform: translateY(-50%); + width: 44px; + height: 44px; + border-radius: 50%; + background: rgb(255 255 255 / 12%); + border: 1px solid rgb(255 255 255 / 18%); + color: #fff; + display: flex; + align-items: center; + justify-content: center; + cursor: pointer; + z-index: 10; + transition: + background 0.2s ease, + opacity 0.2s ease; +} + +.navPrev:hover, +.navNext:hover { + background: rgb(255 255 255 / 22%); +} + +.navPrev:disabled, +.navNext:disabled { + opacity: 0.25; + pointer-events: none; +} + +.navPrev { + left: 2rem; +} + +.navNext { + right: 2rem; +} + +.thumbs { + position: absolute; + bottom: 2rem; + left: 0; + right: 0; + margin: 0 auto; + + width: max-content; + max-width: calc(100% - 2rem); + + display: flex; + gap: 8px; + list-style: none; + padding: 0 0 10px 0; + z-index: 10; + + overflow-x: auto; + scroll-behavior: smooth; +} +.thumbs::-webkit-scrollbar { + height: 6px; +} +.thumbs::-webkit-scrollbar-track { + background: rgb(255 255 255 / 10%); + border-radius: 4px; +} +.thumbs::-webkit-scrollbar-thumb { + background: rgb(255 255 255 / 30%); + border-radius: 4px; +} +.thumbItem { + list-style: none; + flex-shrink: 0; + margin-block-start: 0; +} + +.thumbButton { + display: block; + padding: 0; + border: 2px solid transparent; + border-radius: 4px; + cursor: pointer; + overflow: hidden; + background: none; + transition: + border-color 0.2s ease, + opacity 0.2s ease; + opacity: 0.55; +} + +.thumbButton[aria-current="true"] { + border-color: #fff; + opacity: 1; +} + +.thumbButton:hover { + opacity: 0.85; +} + +.thumbButton img { + display: block; + width: 80px; + height: 50px; + object-fit: cover; +} + +.srOnly { + position: absolute; + width: 1px; + height: 1px; + padding: 0; + margin: -1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); + white-space: nowrap; + border-width: 0; +} + +@media (max-width: 600px) { + .thumbs { + display: none; + } + + .navPrev { + left: 1rem; + } + + .navNext { + right: 1rem; + } +} diff --git a/docs/src/components/demos/morph-gallery/morph-gallery.tsx b/docs/src/components/demos/morph-gallery/morph-gallery.tsx new file mode 100644 index 0000000..53d6e84 --- /dev/null +++ b/docs/src/components/demos/morph-gallery/morph-gallery.tsx @@ -0,0 +1,99 @@ +import { useEffect, useRef, PropsWithChildren } from "react" +import { + Gallery, + GalleryPagination, + GalleryNav, + GalleryPaginationItem, + useGallery, +} from "@wethegit/react-gallery" +import "@wethegit/react-gallery/style.css" + +import styles from "./morph-gallery.module.scss" + +import { MorphRenderer } from "./morph-renderer" + +import { SLIDES, type Slide } from "../../../data/shared-data" + +export function MorphGallery() { + return ( +
+ +
+ + + + + + + Previous + + + + + + + Next + + + + className={styles.thumbs} + renderPaginationItem={({ item, index, active }) => ( + + {item.alt} + + )} + /> +
+
+
+ ) +} + +function MorphCanvas() { + const canvasRef = useRef(null) + const rendererRef = useRef(null) + const { activeIndex, galleryItems } = useGallery() + const prevIndexRef = useRef(activeIndex) + + useEffect(() => { + if (canvasRef.current === null) return + const renderer = new MorphRenderer(canvasRef.current) + rendererRef.current = renderer + renderer.init(galleryItems.map((i) => i.image)) + return () => renderer.destroy() + }, [galleryItems]) + + useEffect(() => { + const prev = prevIndexRef.current + if (prev !== activeIndex) { + rendererRef.current?.transition(prev, activeIndex) + prevIndexRef.current = activeIndex + } + }, [activeIndex]) + + return