This repository has been archived on 2026-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
Files
flair-elements-ui/src/components/fl-gradient-mesh/fl-gradient-mesh.component.ts
Giuliano Silvestro 033cb7e10e fix: improve demo performance and fix broken component animations
- Add IntersectionObserver-based visibility pausing to AnimationLoopService
  (registerWithElement) so canvas components auto-pause when off-screen
- Fix voronoi-mesh: increase step size, add cell cache with invalidation
- Fix particle-field: replace O(n²) connections with spatial grid partitioning
- Fix ambient-light: remove GPU-killing blur filters, use inset -20% instead
- Tune demo: reduce particle/seed counts, remove always-on floating-layers
- Fix celebration components: move effect() inside afterNextRender so canvas
  is ready, pass [active]="true", render as fixed overlays instead of inline
- Fix one-shot text effects (typewriter, scramble-reveal, counter-rollup):
  defer animation until element is visible via IntersectionObserver

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 14:21:13 +10:00

122 lines
3.0 KiB
TypeScript

import {
Component, ChangeDetectionStrategy, input, inject,
ElementRef, afterNextRender, DestroyRef, signal,
} from '@angular/core';
import { ReducedMotionService } from '../../services/reduced-motion.service';
import { AnimationLoopService } from '../../services/animation-loop.service';
interface MeshPoint {
x: number;
y: number;
vx: number;
vy: number;
color: string;
}
@Component({
selector: 'fl-gradient-mesh',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (point of meshPoints(); track $index) {
<div
class="fl-gradient-mesh__blob"
[style.left.%]="point.x"
[style.top.%]="point.y"
[style.background]="point.color"
[style.filter]="'blur(' + blur() + 'px)'"
></div>
}
`,
styleUrl: './fl-gradient-mesh.component.scss',
host: {
'class': 'fl-gradient-mesh',
},
})
export class FlGradientMeshComponent {
private el = inject(ElementRef);
private reducedMotion = inject(ReducedMotionService);
private animationLoop = inject(AnimationLoopService);
private destroyRef = inject(DestroyRef);
// Inputs
readonly points = input(5);
readonly colors = input<string[]>(['#3b82f6', '#8b5cf6', '#ec4899', '#f59e0b']);
readonly speed = input(1);
readonly blur = input(80);
// Internal
private meshPointsData: MeshPoint[] = [];
private loopCleanup: (() => void) | null = null;
// Signals
readonly meshPoints = signal<Array<{ x: number; y: number; color: string }>>([]);
constructor() {
afterNextRender(() => {
this.initializeMesh();
if (this.reducedMotion.reduced()) {
this.updateDisplay();
return;
}
const id = `gradient-mesh-${Math.random().toString(36).slice(2)}`;
this.loopCleanup = this.animationLoop.registerWithElement(id, this.el.nativeElement, (delta) => this.tick(delta));
});
this.destroyRef.onDestroy(() => {
this.loopCleanup?.();
});
}
private initializeMesh(): void {
const count = this.points();
const colors = this.colors();
this.meshPointsData = [];
for (let i = 0; i < count; i++) {
this.meshPointsData.push({
x: Math.random() * 100,
y: Math.random() * 100,
vx: (Math.random() - 0.5) * 10,
vy: (Math.random() - 0.5) * 10,
color: colors[i % colors.length],
});
}
this.updateDisplay();
}
private tick(delta: number): void {
const speed = this.speed() * delta * 10;
for (const point of this.meshPointsData) {
point.x += point.vx * speed;
point.y += point.vy * speed;
// Bounce off edges
if (point.x < 0 || point.x > 100) {
point.vx *= -1;
point.x = Math.max(0, Math.min(100, point.x));
}
if (point.y < 0 || point.y > 100) {
point.vy *= -1;
point.y = Math.max(0, Math.min(100, point.y));
}
}
this.updateDisplay();
}
private updateDisplay(): void {
this.meshPoints.set(
this.meshPointsData.map(p => ({
x: p.x,
y: p.y,
color: p.color,
}))
);
}
}