Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/docs/demos/morph.mdx
Original file line number Diff line number Diff line change
@@ -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.

<MorphGallery />

<OpenInCodePen title="Morph Gallery" source={morphPen} />

## Source

<SourceTabs file="morph-gallery" ts={morphSource} js={morphJs} />

## Renderer (WebGL & Shaders)

<SourceTabs file="morph-renderer.ts" ts={morphRendererSource} />

## Styles

The demos on this site are styled with [SCSS modules](https://github.com/css-modules/css-modules).

<SourceTabs file="morph-gallery" scss={morphStyles} />
170 changes: 170 additions & 0 deletions docs/src/components/demos/morph-gallery/morph-gallery.module.scss
Original file line number Diff line number Diff line change
@@ -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;
}
}
99 changes: 99 additions & 0 deletions docs/src/components/demos/morph-gallery/morph-gallery.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className={styles.root}>
<Gallery items={SLIDES} loop>
<div className={styles.stage}>
<MorphCanvas />

<GalleryNav direction={0} className={styles.navPrev}>
<SvgIcon>
<polyline points="15 18 9 12 15 6" />
</SvgIcon>
<span className={styles.srOnly}>Previous</span>
</GalleryNav>

<GalleryNav direction={1} className={styles.navNext}>
<SvgIcon>
<polyline points="9 18 15 12 9 6" />
</SvgIcon>
<span className={styles.srOnly}>Next</span>
</GalleryNav>

<GalleryPagination<Slide>
className={styles.thumbs}
renderPaginationItem={({ item, index, active }) => (
<GalleryPaginationItem
key={item.id}
index={index}
active={active}
className={styles.thumbItem}
buttonClassName={styles.thumbButton}
>
<img src={item.image} alt={item.alt} />
</GalleryPaginationItem>
)}
/>
</div>
</Gallery>
</div>
)
}

function MorphCanvas() {
const canvasRef = useRef<null | HTMLCanvasElement>(null)
const rendererRef = useRef<null | MorphRenderer>(null)
const { activeIndex, galleryItems } = useGallery<Slide>()
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 <canvas ref={canvasRef} className={styles.canvas} aria-hidden="true" />
}

function SvgIcon({ children }: PropsWithChildren) {
return (
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
{children}
</svg>
)
}
Loading
Loading