Skip to content
Merged
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
18 changes: 6 additions & 12 deletions quartz/components/scripts/tilt.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// and the shadow underneath slides opposite the tilt. Motion runs on springs (after the
// HoloCard component on reactbits.dev) so it eases in and settles instead of snapping.
//
// Touch screens have no hover, so there the card follows a finger while it's pressed
// and lets go on release; a vertical swipe still scrolls the page (touch-action: pan-y).
// Desktop only: it runs where the primary pointer can hover (a mouse or trackpad), and
// ignores touch and pen input there too, so phones and tablets get a still photo.

const PERSPECTIVE = 1100 // px
const TILT_MAX = 12 // deg at the card's edge
Expand Down Expand Up @@ -87,7 +87,7 @@ function setupTilt(root: HTMLElement) {
Math.abs(s.tiltY - targetY) +
Math.abs(s.lift - targetLift) * 40
calm = motion > 0.02 ? 0 : calm + dt
// Sleep once the card has settled and nobody is touching it.
// Sleep once the card has settled and the pointer has left.
if (!pointer.inside && calm > 0.3) {
card.style.transform = ""
return
Expand All @@ -103,41 +103,35 @@ function setupTilt(root: HTMLElement) {
}

const track = (e: PointerEvent) => {
// A finger only steers while it's pressed; otherwise a stray move would stick.
if (e.pointerType === "touch" && e.type === "pointermove" && !e.buttons) return
if (e.pointerType !== "mouse") return
const rect = root.getBoundingClientRect()
pointer.x = clamp((e.clientX - rect.left) / rect.width, 0, 1)
pointer.y = clamp((e.clientY - rect.top) / rect.height, 0, 1)
pointer.inside = true
wake()
}

const leave = (e: PointerEvent) => {
// A mouse button release keeps hovering; only a lifted finger lets go.
if (e.type === "pointerup" && e.pointerType !== "touch") return
const leave = () => {
pointer.inside = false
wake()
}

root.addEventListener("pointerenter", track)
root.addEventListener("pointermove", track)
root.addEventListener("pointerdown", track)
root.addEventListener("pointerleave", leave)
root.addEventListener("pointerup", leave)
root.addEventListener("pointercancel", leave)

window.addCleanup(() => {
cancelAnimationFrame(raf)
root.removeEventListener("pointerenter", track)
root.removeEventListener("pointermove", track)
root.removeEventListener("pointerdown", track)
root.removeEventListener("pointerleave", leave)
root.removeEventListener("pointerup", leave)
root.removeEventListener("pointercancel", leave)
})
}

document.addEventListener("nav", () => {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
if (!window.matchMedia("(hover: hover) and (pointer: fine)").matches) return
document.querySelectorAll<HTMLElement>("[data-tilt]").forEach(setupTilt)
})
3 changes: 0 additions & 3 deletions quartz/components/styles/tilt.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
// Each root sets --tilt-radius to its corner radius.
[data-tilt] {
position: relative;
// Horizontal drags tilt the photo on touch screens; vertical ones still scroll.
touch-action: pan-y;
-webkit-tap-highlight-color: transparent;
}

// Hidden at rest; fades in and slides opposite the tilt while the card is lifted.
Expand Down