1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
"use client"
import React, { useState, useEffect, useRef } from 'react';
import isMobile from '@/app/lib/isMobile';
import styles from './styles/blob.module.css';
function getRandomInt(min: number, max: number): number {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomColor(): string {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function generateGradient(): string {
let gradient = 'conic-gradient(';
let firstTimeInter = true;
const colorCount = Math.floor(Math.random() * 5) + 2;
for (let i = 0; i < colorCount; i++) {
if (firstTimeInter) {
gradient += getRandomColor();
firstTimeInter = false;
} else {
gradient += ', ' + getRandomColor();
}
}
gradient += ')';
return gradient;
}
function RandomBlob() {
const blobRef = useRef<HTMLDivElement>(null);
const [isResetting, setIsResetting] = useState(false);
const mainInterval = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
mainInterval.current = setTimeout(resetBlob, getRandomInt(7500, 30000));
return () => {
if (mainInterval.current) {
clearTimeout(mainInterval.current);
}
};
}, []);
function randomPosition(element: HTMLElement) {
const x = getRandomInt(0, window.innerWidth);
const y = getRandomInt(0, window.innerHeight);
element.style.position = 'absolute';
element.style.left = `${x}px`;
element.style.top = `${y}px`;
};
function setRandomBlob() {
if (blobRef.current) {
blobRef.current.style.width = `${getRandomInt(50, 100)}%`;
blobRef.current.style.height = `${getRandomInt(50, 100)}%`;
blobRef.current.style.transform = `rotate(${getRandomInt(0, 360)}deg)`;
}
};
function rresetblob() {
setRandomBlob();
blobRef.current!.style.background = generateGradient();
randomPosition(blobRef.current!);
const fadeInAnimation = blobRef.current!.animate([
{ opacity: 0 },
{ opacity: 0.4 }
], {
duration: 3000,
iterations: 1,
fill: 'forwards'
});
fadeInAnimation?.addEventListener('finish', () => {
setIsResetting(false);
});
}
function resetBlob() {
if (blobRef.current) {
if (isResetting) {
return;
}
blobRef.current?.animate([
{ opacity: 0.4 },
{ opacity: 0, transform: 'scale(0.8)' }
], {
duration: 3000,
iterations: 1,
fill: 'forwards'
}).addEventListener('finish', rresetblob);
setIsResetting(true);
}
};
useEffect(rresetblob, []);
return (
<div
id="blob"
className={styles.blob}
ref={blobRef}
onClick={() => {
if (isResetting) {
return;
}
setIsResetting(true);
if (mainInterval.current) {
clearTimeout(mainInterval.current);
}
resetBlob();
}}
/>
);
};
function MouseBlob() {
const blobRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handlePointerMove = (event: PointerEvent) => {
if (blobRef.current) {
const { pageX, pageY } = event;
// Get Initial Momentum
const momentumX = pageX + pageX - blobRef.current.offsetLeft;
const momentumY = pageY + pageY - blobRef.current.offsetTop;
// Move to momentum offset position
blobRef.current.animate({
left: `${momentumX}px`,
top: `${momentumY}px`
}, { duration: 1000, fill: "forwards" });
// Move/Bounce back to initial mouse after momentum
blobRef.current.animate({
left: `${pageX}px`,
top: `${pageY}px`
}, { duration: 500, fill: "forwards" });
// Set Default Initial position for blob / fix safari as the animate don't persist (it doesn't care about fill forwards)
blobRef.current.style.left = `${pageX}px`;
blobRef.current.style.top = `${pageY}px`;
}
};
window.addEventListener('pointermove', handlePointerMove);
return () => {
window.removeEventListener('pointermove', handlePointerMove);
};
}, []);
return (
<div
id="blob"
className={styles.sblob}
ref={blobRef}
style={{ position: 'absolute' }}
/>
);
}
export default function Blob({ blobtype: initialBlobType }: { blobtype?: string }) {
const [blobtype, setBlobtype] = useState<string | undefined>(initialBlobType);
if (blobtype === undefined) {
isMobile().then((isMobile) => {
setBlobtype(isMobile ? 'random' : 'mouse');
});
} else if (blobtype !== 'random' && blobtype !== 'mouse') {
throw new Error(`Invalid blob type ${blobtype}`);
}
if (blobtype === undefined) {
return null; // or a loading spinner
}
return blobtype === 'random' ? <RandomBlob /> : <MouseBlob />;
}
|