import {AfterViewInit, Component, ElementRef, Inject, OnDestroy, OnInit, PLATFORM_ID, ViewChild, Input, ChangeDetectionStrategy} from '@angular/core'; import {ViewportService} from '../services/viewport.service'; import {isPlatformBrowser} from '@angular/common'; import Parallax from 'parallax-js'; @Component({ selector: 'app-parallax', imports: [], templateUrl: './parallax.component.html', changeDetection: ChangeDetectionStrategy.Eager, styleUrl: './parallax.component.css', }) export class ParallaxComponent implements OnInit, OnDestroy, AfterViewInit { @ViewChild('parallax') sceneRef!: ElementRef; @Input() baseColor: string = '#0b0b0f'; @Input() bgGradient: string = 'radial-gradient(circle 120vw at center, rgba(90,78,130,0.32), transparent 40%), linear-gradient(180deg,#1b172c 0%, #0b0b0f 100%)'; isNarrow: boolean = false; private parallaxInstance: Parallax | null = null; constructor( private deviceService: ViewportService, @Inject(PLATFORM_ID) private platformId: Object ) { } ngOnInit() { this.isNarrow = this.deviceService.isNarrow; } ngAfterViewInit() { if (!isPlatformBrowser(this.platformId)) return; if (!this.sceneRef?.nativeElement) return; this.parallaxInstance = new Parallax(this.sceneRef.nativeElement, { relativeInput: true, hoverOnly: false, clipRelativeInput: true, calibrateX: true, calibrateY: true, scalarX: this.isNarrow ? 6 : 12, scalarY: this.isNarrow ? 6 : 12, frictionX: 0.1, frictionY: 0.1, }); } ngOnDestroy() { this.parallaxInstance?.destroy(); this.parallaxInstance = null; } }