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-about-bg {
transition: opacity 0.3s ease-in-out;
animation: fadeIn 1s ease-out;
position: fixed;
top: 0;
left: 0;
+17 -73
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, OnDestroy, OnInit, ChangeDetectorRef, PLATFORM_ID, Inject} from '@angular/core';
import {isPlatformBrowser, NgIf} from '@angular/common';
import {VantaBackgroundService} from '../services/vanta-background.service';
import {DeviceDetectorService} from '../services/device-detector.service';
@Component({
selector: 'app-about',
@@ -15,84 +12,31 @@ import { PLATFORM_ID } from '@angular/core';
styleUrls: ['./about.component.css']
})
export class AboutComponent implements OnInit, OnDestroy, AfterViewInit {
constructor(
private deviceService: DeviceDetectorService,
private meta: Meta,
private title: Title,
private route: ActivatedRoute,
@Inject(PLATFORM_ID) private platformId: Object
) {
}
private readonly elementId = 'vanta-about-bg';
isMobile: boolean = false;
private vantaEffect: any;
constructor(
private vantaService: VantaBackgroundService,
private deviceService: DeviceDetectorService,
@Inject(PLATFORM_ID) private platformId: Object
) {}
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-about-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, {});
}
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-about-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,
});
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);
}
}
+38 -12
View File
@@ -1,8 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ArticleService } from '../article.service';
import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {ArticleService} from '../services/article.service';
import {NgFor, NgIf} from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import {Observer} from 'rxjs';
@Component({
selector: 'app-admin',
@@ -10,8 +10,7 @@ import { HttpClientModule } from '@angular/common/http';
styleUrls: ['./admin.component.css'],
imports: [
NgIf,
NgFor,
HttpClientModule
NgFor
]
})
export class AdminComponent implements OnInit {
@@ -28,19 +27,35 @@ export class AdminComponent implements OnInit {
fetchArticles() {
const token = localStorage.getItem('token');
if (!token) {
this.router.navigate(['/login']); // Redirect to login if not logged in
this.router.navigate(['/login']).then(
(success) => {
if (success) {
console.log('Navigation to login succeeded');
} else {
console.warn('Navigation to login failed');
}
},
(error) => {
console.error('Navigation error:', error);
}
);
return;
}
this.articleService.getArticles(token).subscribe(
(data) => {
const articleObserver: Observer<any> = {
next: (data) => {
this.articles = data;
this.noArticles = this.articles.length === 0;
},
(error) => {
error: (error) => {
console.error('Failed to load articles', error);
},
complete: () => {
console.log('Article loading complete');
}
);
};
this.articleService.getArticles(token).subscribe(articleObserver);
}
createArticle() {
@@ -61,6 +76,17 @@ export class AdminComponent implements OnInit {
logout() {
localStorage.removeItem('token');
this.router.navigate(['/login']); // Redirect to login page
this.router.navigate(['/login']).then(
(success) => {
if (success) {
console.log('Navigation to login succeeded');
} else {
console.warn('Navigation to login failed');
}
},
(error) => {
console.error('Navigation error:', error);
}
);
}
}
+13 -27
View File
@@ -1,48 +1,35 @@
import { Component, OnInit, Inject, PLATFORM_ID} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {NgIf} from '@angular/common';
import { DeviceDetectorService } from 'ngx-device-detector';
import {RouterOutlet, ActivatedRoute, Router, NavigationEnd} from '@angular/router';
import {ActivatedRoute, NavigationEnd, Router, RouterOutlet} from '@angular/router';
import {NavbarComponent} from './navbar/navbar.component';
import {MobileNavbarComponent} from './mobile-navbar/mobile-navbar.component';
import {FooterComponent} from './footer/footer.component';
import { Title, Meta } from '@angular/platform-browser';
import { filter, map, mergeMap } from 'rxjs/operators';
import { isPlatformBrowser } from '@angular/common';
import {subscribe} from 'node:diagnostics_channel';
import {Meta, Title} from '@angular/platform-browser';
import {filter, map, mergeMap} from 'rxjs/operators';
import {DeviceDetectorService} from './services/device-detector.service';
@Component({
selector: 'app-root',
imports: [RouterOutlet, NavbarComponent, FooterComponent, MobileNavbarComponent, NgIf],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
styleUrl: './app.component.css',
})
export class AppComponent implements OnInit {
isMobile: boolean;
isMobile: boolean = false;
showNavbar: boolean = true;
showFooter: boolean = true;
constructor(
private deviceService: DeviceDetectorService,
private router: Router,
private activatedRoute: ActivatedRoute,
private titleService: Title,
private metaService: Meta,
@Inject(PLATFORM_ID) private platformId: Object
) {
if (isPlatformBrowser(this.platformId)) {
// Use client-side logic to determine if it's mobile
this.isMobile = window.innerWidth <= 768; // Adjust breakpoint as needed
} else {
// Default fallback for SSR
this.isMobile = false; // Or any other logic to handle SSR
}
}
private deviceService: DeviceDetectorService,
) {}
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
this.isMobile = window.innerWidth <= 768;
}
this.isMobile = this.deviceService.isMobile;
this.router.events
.pipe(
filter(event => event instanceof NavigationEnd),
@@ -56,7 +43,7 @@ export class AppComponent implements OnInit {
.subscribe(data => {
const meta = data['meta'] || {};
if (this.router.url === '/about') {
// fallback meta
// fallback meta data
meta.title = meta.title || 'itzMiney\'s Home';
meta.description = meta.description || 'Welcome to itzMiney\'s Homepage! Here you can find my portfolio and other cool stuff.';
meta.image = meta.image || 'https://itzminey.dev/assets/ogimg.png';
@@ -66,7 +53,6 @@ export class AppComponent implements OnInit {
this.updateMetaTags(meta);
this.checkRoute();
});
this.checkRoute();
}
updateMetaTags(meta: { title?: string; description?: string; image?: string; url?: string; color?: string }) {
@@ -92,7 +78,7 @@ export class AppComponent implements OnInit {
checkRoute() {
const currentRoute = this.activatedRoute.snapshot.firstChild?.routeConfig?.path;
// Hide navbar and footer on admin route
switch (currentRoute) {
case 'admin': {
this.showNavbar = false;
+5 -5
View File
@@ -1,8 +1,8 @@
import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
import { provideServerRendering } from '@angular/platform-server';
import { provideServerRoutesConfig } from '@angular/ssr';
import { appConfig } from './app.config';
import { serverRoutes } from './app.routes.server';
import {ApplicationConfig, mergeApplicationConfig} from '@angular/core';
import {provideServerRendering} from '@angular/platform-server';
import {provideServerRoutesConfig} from '@angular/ssr';
import {appConfig} from './app.config';
import {serverRoutes} from './app.routes.server';
const serverConfig: ApplicationConfig = {
providers: [
+8 -8
View File
@@ -1,12 +1,12 @@
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import {ApplicationConfig, provideZoneChangeDetection} from '@angular/core';
import {provideRouter} from '@angular/router';
import { routes } from './app.routes';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { provideHttpClient } from '@angular/common/http';
import { AuthService } from './auth.service';
import { AuthInterceptor } from './auth.interceptor';
import {routes} from './app.routes';
import {provideAnimationsAsync} from '@angular/platform-browser/animations/async';
import {provideClientHydration, withEventReplay} from '@angular/platform-browser';
import {provideHttpClient} from '@angular/common/http';
import {AuthService} from './services/auth.service';
import {AuthInterceptor} from './auth.interceptor';
export const appConfig: ApplicationConfig = {
providers: [
+4 -4
View File
@@ -1,7 +1,7 @@
import { Routes } from '@angular/router';
import { AboutComponent } from './about/about.component';
import { PortfolioComponent } from './portfolio/portfolio.component';
import { BlogComponent } from './blog/blog.component';
import {Routes} from '@angular/router';
import {AboutComponent} from './about/about.component';
import {PortfolioComponent} from './portfolio/portfolio.component';
import {BlogComponent} from './blog/blog.component';
import {NotFoundComponent} from './not-found/not-found.component';
import {LoginComponent} from './login/login.component';
import {AdminComponent} from './admin/admin.component';
+3 -3
View File
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
+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);
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import {Component} from '@angular/core';
@Component({
selector: 'app-footer',
+12 -1
View File
@@ -1,5 +1,16 @@
@keyframes fadeIn {
0% {
opacity: 0;
visibility: hidden;
}
100% {
opacity: 1;
visibility: visible;
}
}
#vanta-login-bg {
transition: opacity 0.3s ease-in-out;
animation: fadeIn 1s ease-out;
position: fixed;
top: 0;
left: 0;
+36 -57
View File
@@ -1,11 +1,11 @@
import {Component, OnInit, OnDestroy, AfterViewInit, Inject} from '@angular/core';
import { AuthService } from '../auth.service';
import { Router } from '@angular/router';
import {AfterViewInit, Component, Inject, OnDestroy, OnInit, PLATFORM_ID} from '@angular/core';
import {AuthService} from '../services/auth.service';
import {Router} from '@angular/router';
import {FormsModule} from '@angular/forms';
import {NgIf} from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { isPlatformBrowser } from '@angular/common';
import { PLATFORM_ID } from '@angular/core';
import {VantaBackgroundService} from '../services/vanta-background.service';
import {DeviceDetectorService} from '../services/device-detector.service';
import {isPlatformBrowser} from '@angular/common';
@Component({
selector: 'app-login',
@@ -14,7 +14,6 @@ import { PLATFORM_ID } from '@angular/core';
imports: [
FormsModule,
NgIf,
HttpClientModule
]
})
@@ -22,80 +21,60 @@ export class LoginComponent implements OnInit, OnDestroy, AfterViewInit {
username: string = '';
password: string = '';
errorMessage: string = '';
private vantaEffect: any;
private readonly elementId = 'vanta-login-bg';
isMobile: boolean = false;
constructor(
private authService: AuthService,
private router: Router,
private vantaService: VantaBackgroundService,
private deviceService: DeviceDetectorService,
@Inject(PLATFORM_ID) private platformId: Object
) {}
ngOnInit() {
window.addEventListener('resize', this.onResize.bind(this));
const bgElement = document.getElementById('vanta-login-bg');
if (bgElement) {
bgElement.style.position = 'fixed';
this.isMobile = this.deviceService.isMobile;
if (isPlatformBrowser(this.platformId)) {
window.addEventListener('resize', this.onResize.bind(this));
}
}
onLogin() {
this.authService.login(this.username, this.password).subscribe(
(response: any) => {
const observer = {
'next': (response: any) => {
localStorage.setItem('token', response.token);
this.router.navigate(['/admin']); // Redirect to admin page
this.router.navigate(['/admin']).then(
(success) => {
if (success) {
console.log('Navigation to admin succeeded');
} else {
console.warn('Navigation to admin failed');
}
},
(error) => {
console.error('Navigation error:', error);
}
);
},
() => {
error: () => {
this.errorMessage = 'Invalid credentials';
}
);
},
};
this.authService.login(this.username, this.password).subscribe(observer);
}
ngAfterViewInit() {
if (isPlatformBrowser(this.platformId)) {
// Initialize VANTA effect only in the browser
this.initVanta();
this.resizeVanta();
}
this.vantaService.initVanta(this.elementId, {});
}
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-login-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,
});
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);
}
}
@@ -1,7 +1,7 @@
import {Component, OnInit } from '@angular/core';
import { DeviceDetectorService } from 'ngx-device-detector';
import {Component, OnInit} from '@angular/core';
import {DeviceDetectorService} from 'ngx-device-detector';
import {NgIf} from '@angular/common';
import {Router, NavigationEnd, RouterLink} from '@angular/router';
import {NavigationEnd, Router, RouterLink} from '@angular/router';
@Component({
selector: 'app-mobile-navbar',
+1 -1
View File
@@ -1,4 +1,4 @@
<nav class="navbar" *ngIf="!isMobile">
<nav class="navbar">
<div class="navbar-links">
<a [routerLink]="'/'" [class.active]="activeLink === '/'">About</a>
<a [routerLink]="'/portfolio'" [class.active]="activeLink === '/portfolio'">Portfolio</a>
+4 -5
View File
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import {Router, NavigationEnd, RouterLink} from '@angular/router';
import { DeviceDetectorService } from 'ngx-device-detector';
import {Component, OnInit} from '@angular/core';
import {NavigationEnd, Router, RouterLink} from '@angular/router';
import {DeviceDetectorService} from 'ngx-device-detector';
import {NgIf} from '@angular/common';
@Component({
@@ -8,8 +8,7 @@ import {NgIf} from '@angular/common';
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css'],
imports: [
RouterLink,
NgIf,
RouterLink
]
})
export class NavbarComponent implements OnInit {
+12 -1
View File
@@ -1,5 +1,16 @@
@keyframes fadeIn {
0% {
opacity: 0;
visibility: hidden;
}
100% {
opacity: 1;
visibility: visible;
}
}
#vanta-not-found-bg {
transition: opacity 0.3s ease-in-out;
animation: fadeIn 1s ease-out;
position: fixed;
top: 0;
left: 0;
+17 -71
View File
@@ -1,9 +1,7 @@
import {Component, OnInit, OnDestroy, AfterViewInit, Inject} from '@angular/core';
import {DeviceDetectorService} from 'ngx-device-detector';
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 {VantaBackgroundService} from '../services/vanta-background.service';
import {isPlatformBrowser} from '@angular/common';
@Component({
selector: 'app-not-found',
@@ -13,87 +11,35 @@ import { PLATFORM_ID } from '@angular/core';
})
export class NotFoundComponent implements OnInit, OnDestroy, AfterViewInit {
private readonly elementId = 'vanta-not-found-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-not-found-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-not-found-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);
}
}
+12 -1
View File
@@ -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;
+17 -70
View File
@@ -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);
}
}
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, tap} from 'rxjs';
@Injectable({
@@ -0,0 +1,24 @@
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
@Injectable({
providedIn: 'root',
})
export class DeviceDetectorService {
isMobile: boolean = false;
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
this.updateDeviceType();
if (isPlatformBrowser(this.platformId)) {
window.addEventListener('resize', () => this.updateDeviceType());
}
}
private updateDeviceType() {
if (isPlatformBrowser(this.platformId)) {
this.isMobile = window.innerWidth <= 768;
} else {
this.isMobile = false;
}
}
}
@@ -0,0 +1,52 @@
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
@Injectable({
providedIn: 'root',
})
export class VantaBackgroundService {
private vantaEffect: Map<string, any> = new Map();
constructor(
@Inject(PLATFORM_ID) private platformId: Object,
) {}
initVanta(elementId: string, options?: any) {
if (isPlatformBrowser(this.platformId)) {
const element = document.getElementById(elementId);
if (element && !this.vantaEffect.has(elementId)) {
(window as any).VANTA.NET({
el: element,
mouseControls: false,
touchControls: false,
gyroControls: false,
scale: 1.0,
scaleMobile: 1.0,
points: 20.0,
maxDistance: 25.0,
spacing: 18.0,
showDots: false,
minWidth: window.innerWidth,
minHeight: window.innerHeight,
...(options || {}),
});
element.style.position = 'fixed';
}
}
}
resizeVanta(elementId: string): void {
const effect = this.vantaEffect.get(elementId);
if (effect) {
effect.resize();
}
}
destroyVanta(elementId: string): void {
const effect = this.vantaEffect.get(elementId);
if (effect) {
effect.destroy();
this.vantaEffect.delete(elementId);
}
}
}