Optimized code (significantly)

This commit is contained in:
ItzMineyy committed 2024-12-28 20:50:18 +01:00
1 parent 5fbb41c477
commit a4b69c5b9a
31 files changed
+343 -441

No files matched your search

+12 -1
View File
@@ -1,5 +1,16 @@
@keyframes fadeIn {
0% {
opacity: 0;
visibility: hidden;
}
100% {
opacity: 1;
visibility: visible;
}
}
#vanta-blog-bg {
transition: opacity 0.3s ease-in-out;
animation: fadeIn 1s ease-out;
position: fixed;
top: 0;
left: 0;
+17 -71
View File
@@ -1,10 +1,7 @@
import {Component, OnInit, OnDestroy, AfterViewInit, 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 {DeviceDetectorService} from '../services/device-detector.service'
import {isPlatformBrowser, NgIf} from '@angular/common';
import {VantaBackgroundService} from '../services/vanta-background.service';
@Component({
selector: 'app-blog',
@@ -15,87 +12,36 @@ import { PLATFORM_ID } from '@angular/core';
styleUrl: './blog.component.css'
})
export class BlogComponent implements OnInit, OnDestroy, AfterViewInit {
private readonly elementId = 'vanta-blog-bg';
isMobile: boolean = false;
constructor(
private deviceService: DeviceDetectorService,
private meta: Meta,
private title: Title,
private route: ActivatedRoute,
private vantaService: VantaBackgroundService,
@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-blog-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-blog-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);
}
}