tractatus/public/js/scroll-animations.js
TheFlow fa01644c17 feat(phase3): implement scroll animations with Intersection Observer
SUMMARY:
Implemented Phase 3 Task 3.1: Scroll Animations system using Intersection
Observer API for smooth, performant scroll-triggered animations on homepage.

CHANGES:

1. Created scroll-animations.js (new):
   - Intersection Observer-based animation system
   - Supports 7 animation types (fade-in, slide-up/down/left/right, scale-in, rotate-in)
   - Staggered delays via data-stagger attribute
   - Respects prefers-reduced-motion
   - Auto-initializes, can be disabled globally
   - Custom 'scroll-animated' event for other components

2. Enhanced tractatus-theme.css:
   - Added 100+ lines of scroll animation CSS
   - Smooth transitions using CSS transforms (GPU-accelerated)
   - Data-attribute based animation selection
   - Default animation (slide-up) if none specified
   - Accessibility: respects prefers-reduced-motion

3. Updated index.html (homepage):
   - Added scroll-animations.js script
   - Value proposition: slide-up animation
   - Three audience cards: scale-in with 100/200/300ms stagger
   - Capabilities heading: fade-in
   - Six capability cards: slide-up with 100-600ms stagger
   - All animations trigger on scroll (not page load)

ANIMATION TYPES:
- fade-in: Opacity 0 → 1
- slide-up: Translates from bottom (+2rem) → 0
- slide-down: Translates from top (-2rem) → 0
- slide-left: Translates from right (+2rem) → 0
- slide-right: Translates from left (-2rem) → 0
- scale-in: Scales from 0.95 → 1 with opacity
- rotate-in: Rotates from 12deg → 0 with scale

USAGE EXAMPLE:
<div class="animate-on-scroll" data-animation="slide-up" data-stagger="200">
  Content here
</div>

ACCESSIBILITY:
✓ Respects prefers-reduced-motion (disables all animations)
✓ GPU-accelerated transforms (60fps on modern devices)
✓ Progressive enhancement (graceful degradation)
✓ Zero CSP violations maintained

PERFORMANCE:
- Intersection Observer (better than scroll listeners)
- Unobserves elements after animation (memory efficient)
- Supports data-animate-repeat for repeatable animations
- CSS transitions (GPU-accelerated)

UI_TRANSFORMATION_PROJECT_PLAN.md:
✓ Phase 3 Task 3.1.1: Implemented Intersection Observer
✓ Phase 3 Task 3.1.2: Added scroll animations to homepage

NEXT STEPS:
- Phase 3 Task 3.1.3: Apply to all pages site-wide
- Phase 3 Task 3.2: Interactive architecture diagram

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-19 15:09:59 +13:00

135 lines
4.6 KiB
JavaScript

/**
* Scroll Animations using Intersection Observer
* Tractatus Framework - Phase 3: Engagement & Interactive Features
*
* Provides smooth scroll-triggered animations for elements marked with .animate-on-scroll
* Supports multiple animation types via data-animation attribute
*/
class ScrollAnimations {
constructor(options = {}) {
this.observerOptions = {
threshold: options.threshold || 0.1,
rootMargin: options.rootMargin || '0px 0px -100px 0px'
};
this.animations = {
'fade-in': 'opacity-0 animate-fade-in',
'slide-up': 'opacity-0 translate-y-8 animate-slide-up',
'slide-down': 'opacity-0 -translate-y-8 animate-slide-down',
'slide-left': 'opacity-0 translate-x-8 animate-slide-left',
'slide-right': 'opacity-0 -translate-x-8 animate-slide-right',
'scale-in': 'opacity-0 scale-95 animate-scale-in',
'rotate-in': 'opacity-0 rotate-12 animate-rotate-in'
};
this.init();
}
init() {
// Wait for DOM to be ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => this.observe());
} else {
this.observe();
}
console.log('[ScrollAnimations] Initialized');
}
observe() {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
this.animateElement(entry.target);
// Optional: unobserve after animation to improve performance
// Only do this for elements without data-animate-repeat attribute
if (!entry.target.hasAttribute('data-animate-repeat')) {
observer.unobserve(entry.target);
}
} else if (entry.target.hasAttribute('data-animate-repeat')) {
// Reset animation for repeatable elements
this.resetElement(entry.target);
}
});
}, this.observerOptions);
// Find all elements with .animate-on-scroll class
const elements = document.querySelectorAll('.animate-on-scroll');
console.log(`[ScrollAnimations] Observing ${elements.length} elements`);
elements.forEach((el, index) => {
// Add stagger delay if data-stagger attribute is present
if (el.hasAttribute('data-stagger')) {
const delay = parseInt(el.getAttribute('data-stagger')) || (index * 100);
el.style.animationDelay = `${delay}ms`;
}
// Apply initial animation classes based on data-animation attribute
const animationType = el.getAttribute('data-animation') || 'fade-in';
if (this.animations[animationType]) {
// Remove any existing animation classes
Object.values(this.animations).forEach(classes => {
classes.split(' ').forEach(cls => el.classList.remove(cls));
});
// Add initial state classes (will be removed when visible)
const initialClasses = this.getInitialClasses(animationType);
initialClasses.forEach(cls => el.classList.add(cls));
}
observer.observe(el);
});
}
getInitialClasses(animationType) {
// Return classes that represent the "before animation" state
const map = {
'fade-in': ['opacity-0'],
'slide-up': ['opacity-0', 'translate-y-8'],
'slide-down': ['opacity-0', '-translate-y-8'],
'slide-left': ['opacity-0', 'translate-x-8'],
'slide-right': ['opacity-0', '-translate-x-8'],
'scale-in': ['opacity-0', 'scale-95'],
'rotate-in': ['opacity-0', 'rotate-12']
};
return map[animationType] || ['opacity-0'];
}
animateElement(element) {
// Remove initial state classes
element.classList.remove('opacity-0', 'translate-y-8', '-translate-y-8', 'translate-x-8', '-translate-x-8', 'scale-95', 'rotate-12');
// Add visible state
element.classList.add('is-visible');
// Trigger custom event for other components to listen to
element.dispatchEvent(new CustomEvent('scroll-animated', {
bubbles: true,
detail: { element }
}));
}
resetElement(element) {
// Remove visible state
element.classList.remove('is-visible');
// Re-apply initial animation classes
const animationType = element.getAttribute('data-animation') || 'fade-in';
const initialClasses = this.getInitialClasses(animationType);
initialClasses.forEach(cls => element.classList.add(cls));
}
}
// Auto-initialize when script loads
// Can be disabled by setting window.DISABLE_AUTO_SCROLL_ANIMATIONS = true before loading this script
if (typeof window !== 'undefined' && !window.DISABLE_AUTO_SCROLL_ANIMATIONS) {
window.scrollAnimations = new ScrollAnimations();
}
// Export for module usage
if (typeof module !== 'undefined' && module.exports) {
module.exports = ScrollAnimations;
}