Optimized code (significantly)
This commit is contained in:
1 parent
5fbb41c477
commit
a4b69c5b9a
31 files changed
+343
-441
No files matched your search
@@ -1,5 +1,16 @@
|
||||
@keyframes fadeIn {
|
||||
0% {
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
}
|
||||
100% {
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
}
|
||||
|
||||
#vanta-portfolio-bg {
|
||||
transition: opacity 0.3s ease-in-out;
|
||||
animation: fadeIn 1s ease-out;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import {AfterViewInit, Component, OnDestroy, OnInit, Inject} from '@angular/core';
|
||||
import { DeviceDetectorService } from 'ngx-device-detector';
|
||||
import {NgIf} from '@angular/common';
|
||||
import {Meta, Title} from '@angular/platform-browser';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import { isPlatformBrowser } from '@angular/common';
|
||||
import { PLATFORM_ID } from '@angular/core';
|
||||
import {AfterViewInit, Component, Inject, OnDestroy, OnInit, PLATFORM_ID} from '@angular/core';
|
||||
import {VantaBackgroundService} from '../services/vanta-background.service';
|
||||
import {DeviceDetectorService} from '../services/device-detector.service';
|
||||
import {isPlatformBrowser, NgIf} from '@angular/common';
|
||||
|
||||
@Component({
|
||||
selector: 'app-portfolio',
|
||||
@@ -15,87 +12,37 @@ import { PLATFORM_ID } from '@angular/core';
|
||||
styleUrl: './portfolio.component.css'
|
||||
})
|
||||
export class PortfolioComponent implements OnInit, OnDestroy, AfterViewInit {
|
||||
private readonly elementId = 'vanta-portfolio-bg';
|
||||
isMobile: boolean = false;
|
||||
constructor(
|
||||
private vantaService: VantaBackgroundService,
|
||||
private deviceService: DeviceDetectorService,
|
||||
private meta: Meta,
|
||||
private title: Title,
|
||||
private route: ActivatedRoute,
|
||||
@Inject(PLATFORM_ID) private platformId: Object
|
||||
) {
|
||||
}
|
||||
isMobile: boolean = false;
|
||||
private vantaEffect: any;
|
||||
|
||||
ngOnInit() {
|
||||
this.isMobile = this.deviceService.isMobile();
|
||||
const metaData = this.route.snapshot.data['meta'];
|
||||
if (metaData?.title) {
|
||||
this.title.setTitle(metaData.title);
|
||||
}
|
||||
if (metaData?.description) {
|
||||
this.meta.updateTag({ name: 'description', content: metaData.description });
|
||||
}
|
||||
if (metaData?.image) {
|
||||
this.meta.updateTag({ property: 'og:image', content: metaData.image });
|
||||
}
|
||||
if (metaData?.url) {
|
||||
this.meta.updateTag({ property: 'og:url', content: metaData.url });
|
||||
}
|
||||
window.addEventListener('resize', this.onResize.bind(this)); // Add resize listener for responsiveness
|
||||
|
||||
const bgElement = document.getElementById('vanta-portfolio-bg');
|
||||
if (bgElement) {
|
||||
bgElement.style.position = 'fixed';
|
||||
this.isMobile = this.deviceService.isMobile;
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
window.addEventListener('resize', this.onResize.bind(this));
|
||||
}
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
// Initialize VANTA effect only in the browser
|
||||
this.initVanta();
|
||||
this.resizeVanta();
|
||||
}
|
||||
this.vantaService.initVanta(this.elementId, {
|
||||
color: 0xac1f4e,
|
||||
backgroundColor: 0x191522
|
||||
});
|
||||
}
|
||||
ngOnDestroy() {
|
||||
this.vantaService.destroyVanta(this.elementId);
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
if (this.vantaEffect) {
|
||||
this.vantaEffect.destroy(); // Clean up the effect on destroy
|
||||
}
|
||||
window.removeEventListener('resize', this.onResize); // Remove the listener when the component is destroyed
|
||||
}
|
||||
}
|
||||
|
||||
initVanta() {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
// Initialize the Vanta effect on the element with ID 'vanta-bg'
|
||||
this.vantaEffect = window.VANTA.NET({
|
||||
el: '#vanta-portfolio-bg',
|
||||
mouseControls: false,
|
||||
touchControls: false,
|
||||
gyroControls: false,
|
||||
scale: 1.00,
|
||||
scaleMobile: 1.00,
|
||||
points: 20.00,
|
||||
maxDistance: 25.00,
|
||||
spacing: 18.00,
|
||||
showDots: false,
|
||||
minWidth: window.innerWidth,
|
||||
minHeight: window.innerHeight,
|
||||
color: 0xac1f4e,
|
||||
backgroundColor: 0x191522
|
||||
});
|
||||
window.removeEventListener('resize', this.onResize);
|
||||
}
|
||||
}
|
||||
|
||||
onResize() {
|
||||
// Update the minHeight and minWidth when the window is resized
|
||||
this.resizeVanta();
|
||||
}
|
||||
|
||||
resizeVanta() {
|
||||
if (this.vantaEffect) {
|
||||
// Trigger the resize on the vanta effect
|
||||
this.vantaEffect.resize();
|
||||
}
|
||||
this.vantaService.resizeVanta(this.elementId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user