Skip to content

Commit 7d04525

Browse files
author
Abdul Bari
committed
Fix sphere always-diamond bug: Proper scroll bounds checking
1 parent b05d3fb commit 7d04525

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

docs/three-sphere-morph.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,30 +315,35 @@ class MorphingSphere {
315315
handleScroll() {
316316
// Apple-style scroll-linked animation: morph as you scroll past the subtitle
317317
const subtitle = document.querySelector('.section-subtitle');
318-
if (!subtitle) return;
318+
if (!subtitle) {
319+
this.targetMorphProgress = 0;
320+
return;
321+
}
319322

320323
const rect = subtitle.getBoundingClientRect();
321324
const windowHeight = window.innerHeight;
322325

323-
// Morph animation zone: from when subtitle is at 60% viewport to when it's -100px past top
324-
const startPosition = windowHeight * 0.6; // Subtitle at 60% down viewport (morph starts)
325-
const endPosition = -100; // Subtitle 100px above viewport top (morph complete)
326-
const morphRange = startPosition - endPosition;
326+
// Morph animation zone: starts when subtitle reaches 70% down viewport
327+
// Completes over ~400px scroll distance
328+
const startTrigger = windowHeight * 0.7; // 70% down viewport
329+
const morphDistance = 400; // Pixels to complete morph
330+
const endTrigger = startTrigger - morphDistance;
327331

328-
// Current subtitle position relative to viewport top
332+
// Current subtitle position
329333
const currentPosition = rect.top;
330334

331-
if (currentPosition <= startPosition && currentPosition >= endPosition) {
332-
// We're in the morph zone - calculate smooth progress
333-
const scrolledDistance = startPosition - currentPosition;
334-
const progress = scrolledDistance / morphRange;
335+
// Calculate morph progress based on scroll position
336+
if (currentPosition > startTrigger) {
337+
// Haven't scrolled to trigger point yet
338+
this.targetMorphProgress = 0;
339+
} else if (currentPosition >= endTrigger && currentPosition <= startTrigger) {
340+
// In the morph zone - progressive transformation
341+
const scrolled = startTrigger - currentPosition;
342+
const progress = scrolled / morphDistance;
335343
this.targetMorphProgress = Math.max(0, Math.min(1, progress));
336-
} else if (currentPosition < endPosition) {
337-
// Scrolled past - fully morphed
338-
this.targetMorphProgress = 1;
339344
} else {
340-
// Haven't reached trigger yet
341-
this.targetMorphProgress = 0;
345+
// Scrolled past the morph zone - fully diamond
346+
this.targetMorphProgress = 1;
342347
}
343348
}
344349

0 commit comments

Comments
 (0)