@slithy/math-particles 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +341 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthew Campagna
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @slithy/math-particles
|
|
2
|
+
|
|
3
|
+
Animated math equations particle overlay for React. Equations drift outward from the center of the container, scaling and fading over time. No animation library — driven entirely by a `requestAnimationFrame` loop with hand-rolled easing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @slithy/math-particles
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependencies:** `react@^17 || ^18 || ^19`, `react-dom@^17 || ^18 || ^19`
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## `MathParticlesOverlay`
|
|
16
|
+
|
|
17
|
+
Fills its nearest positioned ancestor. Give the parent `position: relative` and defined dimensions.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { MathParticlesOverlay } from '@slithy/math-particles'
|
|
21
|
+
|
|
22
|
+
<div style={{ position: 'relative', width: '100%', height: 480 }}>
|
|
23
|
+
<MathParticlesOverlay />
|
|
24
|
+
</div>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Props:**
|
|
28
|
+
|
|
29
|
+
| Prop | Type | Default | Description |
|
|
30
|
+
|---|---|---|---|
|
|
31
|
+
| `backgroundColor` | `string` | `"#07070f"` | Background fill color of the container |
|
|
32
|
+
| `colors` | `string[]` | ColorBrewer RdYlBu | Array of colors to pick from randomly per particle |
|
|
33
|
+
| `density` | `number` | `1` | Multiplier on the auto-calculated particle count. `0.5` = half, `2` = double. Fixed at mount — use a `key` prop to reinitialize. |
|
|
34
|
+
| `reverse` | `boolean` | `false` | Reverses the animation: equations start large and drift inward rather than starting small and drifting outward |
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Examples
|
|
39
|
+
|
|
40
|
+
### Custom colors
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
<MathParticlesOverlay colors={["#e63946", "#457b9d", "#f1faee"]} />
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Reduced density
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
<MathParticlesOverlay density={0.5} />
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Reversed direction
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
<MathParticlesOverlay reverse />
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Transparent background
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
<div style={{ position: 'relative', width: '100%', height: 480, background: 'your-bg' }}>
|
|
62
|
+
<MathParticlesOverlay backgroundColor="transparent" />
|
|
63
|
+
</div>
|
|
64
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
|
|
3
|
+
type Props = {
|
|
4
|
+
backgroundColor?: string;
|
|
5
|
+
colors?: string[];
|
|
6
|
+
density?: number;
|
|
7
|
+
reverse?: boolean;
|
|
8
|
+
};
|
|
9
|
+
declare const MathParticlesOverlay: ({ backgroundColor, colors, density, reverse, }: Props) => react_jsx_runtime.JSX.Element;
|
|
10
|
+
|
|
11
|
+
export { MathParticlesOverlay };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// src/MathParticlesOverlay.tsx
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var EQUATIONS = [
|
|
5
|
+
{
|
|
6
|
+
label: "euler",
|
|
7
|
+
w: 220,
|
|
8
|
+
h: 60,
|
|
9
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 220 60" width="220" height="60">
|
|
10
|
+
<text x="10" y="44" font-family="Georgia,serif" font-size="38" fill="currentColor">
|
|
11
|
+
<tspan font-style="italic">e</tspan>
|
|
12
|
+
<tspan font-size="22" baseline-shift="super">iπ</tspan>
|
|
13
|
+
<tspan> + 1 = 0</tspan>
|
|
14
|
+
</text>
|
|
15
|
+
</svg>`
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
label: "pythagoras",
|
|
19
|
+
w: 230,
|
|
20
|
+
h: 60,
|
|
21
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 230 60" width="230" height="60">
|
|
22
|
+
<text x="10" y="44" font-family="Georgia,serif" font-size="38" fill="currentColor">
|
|
23
|
+
<tspan font-style="italic">a</tspan><tspan font-size="22" baseline-shift="super">2</tspan>
|
|
24
|
+
<tspan> + </tspan>
|
|
25
|
+
<tspan font-style="italic">b</tspan><tspan font-size="22" baseline-shift="super">2</tspan>
|
|
26
|
+
<tspan> = </tspan>
|
|
27
|
+
<tspan font-style="italic">c</tspan><tspan font-size="22" baseline-shift="super">2</tspan>
|
|
28
|
+
</text>
|
|
29
|
+
</svg>`
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
label: "quadratic",
|
|
33
|
+
w: 310,
|
|
34
|
+
h: 80,
|
|
35
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 310 80" width="310" height="80">
|
|
36
|
+
<text x="10" y="32" font-family="Georgia,serif" font-size="22" fill="currentColor">
|
|
37
|
+
<tspan font-style="italic">x</tspan><tspan> = </tspan>
|
|
38
|
+
<tspan>−</tspan><tspan font-style="italic">b</tspan>
|
|
39
|
+
<tspan> ± </tspan>
|
|
40
|
+
<tspan>√(</tspan><tspan font-style="italic">b</tspan>
|
|
41
|
+
<tspan font-size="14" baseline-shift="super">2</tspan>
|
|
42
|
+
<tspan> − 4</tspan><tspan font-style="italic">ac</tspan><tspan>)</tspan>
|
|
43
|
+
</text>
|
|
44
|
+
<line x1="127" y1="40" x2="295" y2="40" stroke="currentColor" stroke-width="1.5"/>
|
|
45
|
+
<text x="195" y="65" font-family="Georgia,serif" font-size="22" fill="currentColor">2<tspan font-style="italic">a</tspan></text>
|
|
46
|
+
</svg>`
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
label: "einstein",
|
|
50
|
+
w: 180,
|
|
51
|
+
h: 60,
|
|
52
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 60" width="180" height="60">
|
|
53
|
+
<text x="10" y="44" font-family="Georgia,serif" font-size="38" fill="currentColor">
|
|
54
|
+
<tspan font-style="italic">E</tspan>
|
|
55
|
+
<tspan> = </tspan>
|
|
56
|
+
<tspan font-style="italic">mc</tspan>
|
|
57
|
+
<tspan font-size="22" baseline-shift="super">2</tspan>
|
|
58
|
+
</text>
|
|
59
|
+
</svg>`
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
label: "fourier",
|
|
63
|
+
w: 330,
|
|
64
|
+
h: 80,
|
|
65
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 330 80" width="330" height="80">
|
|
66
|
+
<text x="10" y="32" font-family="Georgia,serif" font-size="20" fill="currentColor">
|
|
67
|
+
<tspan font-style="italic">F</tspan><tspan>(ξ) = </tspan>
|
|
68
|
+
</text>
|
|
69
|
+
<text x="90" y="22" font-family="Georgia,serif" font-size="30" fill="currentColor">∫</text>
|
|
70
|
+
<text x="67" y="22" font-family="Georgia,serif" font-size="13" fill="currentColor">+∞</text>
|
|
71
|
+
<text x="67" y="38" font-family="Georgia,serif" font-size="13" fill="currentColor">−∞</text>
|
|
72
|
+
<text x="115" y="32" font-family="Georgia,serif" font-size="20" fill="currentColor">
|
|
73
|
+
<tspan font-style="italic">f</tspan><tspan>(</tspan><tspan font-style="italic">x</tspan><tspan>)</tspan>
|
|
74
|
+
<tspan font-style="italic"> e</tspan>
|
|
75
|
+
<tspan font-size="13" baseline-shift="super">−2πiξ</tspan>
|
|
76
|
+
<tspan font-style="italic" font-size="13" baseline-shift="super">x</tspan>
|
|
77
|
+
<tspan> d</tspan><tspan font-style="italic">x</tspan>
|
|
78
|
+
</text>
|
|
79
|
+
</svg>`
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
label: "schrodinger",
|
|
83
|
+
w: 220,
|
|
84
|
+
h: 80,
|
|
85
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 220 80" width="220" height="80">
|
|
86
|
+
<text x="10" y="38" font-family="Georgia,serif" font-size="24" fill="currentColor">
|
|
87
|
+
<tspan font-style="italic">i</tspan><tspan>ℏ</tspan>
|
|
88
|
+
</text>
|
|
89
|
+
<text x="44" y="26" font-family="Georgia,serif" font-size="20" fill="currentColor">∂Ψ</text>
|
|
90
|
+
<line x1="42" y1="32" x2="80" y2="32" stroke="currentColor" stroke-width="1.4"/>
|
|
91
|
+
<text x="46" y="52" font-family="Georgia,serif" font-size="20" fill="currentColor">∂<tspan font-style="italic">t</tspan></text>
|
|
92
|
+
<text x="86" y="38" font-family="Georgia,serif" font-size="24" fill="currentColor"> = ĤΨ</text>
|
|
93
|
+
</svg>`
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
label: "gaussian",
|
|
97
|
+
w: 240,
|
|
98
|
+
h: 80,
|
|
99
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 80" width="240" height="80">
|
|
100
|
+
<text x="22" y="22" font-family="Georgia,serif" font-size="30" fill="currentColor">∫</text>
|
|
101
|
+
<text x="10" y="20" font-family="Georgia,serif" font-size="13" fill="currentColor">+∞</text>
|
|
102
|
+
<text x="10" y="48" font-family="Georgia,serif" font-size="13" fill="currentColor">−∞</text>
|
|
103
|
+
<text x="50" y="38" font-family="Georgia,serif" font-size="24" fill="currentColor">
|
|
104
|
+
<tspan font-style="italic">e</tspan>
|
|
105
|
+
<tspan font-size="15" baseline-shift="super">−<tspan font-style="italic">x</tspan><tspan font-size="10">2</tspan></tspan>
|
|
106
|
+
<tspan> d</tspan><tspan font-style="italic">x</tspan><tspan> = √π</tspan>
|
|
107
|
+
</text>
|
|
108
|
+
</svg>`
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
label: "taylor",
|
|
112
|
+
w: 320,
|
|
113
|
+
h: 80,
|
|
114
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 80" width="320" height="80">
|
|
115
|
+
<text x="10" y="38" font-family="Georgia,serif" font-size="22" fill="currentColor">
|
|
116
|
+
<tspan font-style="italic">f</tspan><tspan>(</tspan><tspan font-style="italic">x</tspan><tspan>) = </tspan>
|
|
117
|
+
</text>
|
|
118
|
+
<text x="90" y="18" font-family="Georgia,serif" font-size="15" fill="currentColor">∞</text>
|
|
119
|
+
<text x="88" y="36" font-family="Georgia,serif" font-size="28" fill="currentColor">∑</text>
|
|
120
|
+
<text x="83" y="56" font-family="Georgia,serif" font-size="14" fill="currentColor"><tspan font-style="italic">n</tspan>=0</text>
|
|
121
|
+
<text x="126" y="26" font-family="Georgia,serif" font-size="17" fill="currentColor">
|
|
122
|
+
<tspan font-style="italic">f</tspan><tspan font-size="12" baseline-shift="super">(<tspan font-style="italic">n</tspan>)</tspan>(<tspan font-style="italic">a</tspan>)
|
|
123
|
+
</text>
|
|
124
|
+
<line x1="124" y1="32" x2="178" y2="32" stroke="currentColor" stroke-width="1.2"/>
|
|
125
|
+
<text x="138" y="52" font-family="Georgia,serif" font-size="17" fill="currentColor">
|
|
126
|
+
<tspan font-style="italic">n</tspan><tspan>!</tspan>
|
|
127
|
+
</text>
|
|
128
|
+
<text x="184" y="38" font-family="Georgia,serif" font-size="22" fill="currentColor">
|
|
129
|
+
(<tspan font-style="italic">x−a</tspan>)<tspan font-size="14" baseline-shift="super"><tspan font-style="italic">n</tspan></tspan>
|
|
130
|
+
</text>
|
|
131
|
+
</svg>`
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
label: "navier-stokes",
|
|
135
|
+
w: 370,
|
|
136
|
+
h: 60,
|
|
137
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 370 60" width="370" height="60">
|
|
138
|
+
<text x="10" y="40" font-family="Georgia,serif" font-size="22" fill="currentColor">
|
|
139
|
+
ρ(∂<tspan font-style="italic">t</tspan><tspan font-weight="bold" font-style="italic">u</tspan> + <tspan font-style="italic">u</tspan>⋅∇<tspan font-style="italic">u</tspan>) = −∇<tspan font-style="italic">p</tspan> + μ∇<tspan font-size="16" baseline-shift="super">2</tspan><tspan font-weight="bold" font-style="italic">u</tspan>
|
|
140
|
+
</text>
|
|
141
|
+
</svg>`
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
label: "maxwell",
|
|
145
|
+
w: 210,
|
|
146
|
+
h: 70,
|
|
147
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 210 70" width="210" height="70">
|
|
148
|
+
<text x="10" y="44" font-family="Georgia,serif" font-size="30" fill="currentColor">
|
|
149
|
+
∇ ⋅ <tspan font-weight="bold" font-style="italic">E</tspan> =
|
|
150
|
+
</text>
|
|
151
|
+
<text x="136" y="32" font-family="Georgia,serif" font-size="22" fill="currentColor">ρ</text>
|
|
152
|
+
<line x1="134" y1="38" x2="165" y2="38" stroke="currentColor" stroke-width="1.4"/>
|
|
153
|
+
<text x="136" y="58" font-family="Georgia,serif" font-size="22" fill="currentColor">ε<tspan font-size="14" baseline-shift="sub">0</tspan></text>
|
|
154
|
+
</svg>`
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
label: "riemann",
|
|
158
|
+
w: 200,
|
|
159
|
+
h: 80,
|
|
160
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 80" width="200" height="80">
|
|
161
|
+
<text x="10" y="38" font-family="Georgia,serif" font-size="22" fill="currentColor">
|
|
162
|
+
ζ(<tspan font-style="italic">s</tspan>) =
|
|
163
|
+
</text>
|
|
164
|
+
<text x="85" y="18" font-family="Georgia,serif" font-size="15" fill="currentColor">∞</text>
|
|
165
|
+
<text x="82" y="36" font-family="Georgia,serif" font-size="28" fill="currentColor">∑</text>
|
|
166
|
+
<text x="78" y="56" font-family="Georgia,serif" font-size="14" fill="currentColor"><tspan font-style="italic">n</tspan>=1</text>
|
|
167
|
+
<text x="120" y="26" font-family="Georgia,serif" font-size="18" fill="currentColor">1</text>
|
|
168
|
+
<line x1="118" y1="32" x2="152" y2="32" stroke="currentColor" stroke-width="1.2"/>
|
|
169
|
+
<text x="120" y="52" font-family="Georgia,serif" font-size="18" fill="currentColor">
|
|
170
|
+
<tspan font-style="italic">n</tspan><tspan font-size="12" baseline-shift="super"><tspan font-style="italic">s</tspan></tspan>
|
|
171
|
+
</text>
|
|
172
|
+
</svg>`
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
label: "bayes",
|
|
176
|
+
w: 300,
|
|
177
|
+
h: 80,
|
|
178
|
+
svg: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 80" width="300" height="80">
|
|
179
|
+
<text x="10" y="36" font-family="Georgia,serif" font-size="22" fill="currentColor">
|
|
180
|
+
<tspan font-style="italic">P</tspan>(<tspan font-style="italic">A</tspan>|<tspan font-style="italic">B</tspan>) =
|
|
181
|
+
</text>
|
|
182
|
+
<text x="118" y="24" font-family="Georgia,serif" font-size="20" fill="currentColor">
|
|
183
|
+
<tspan font-style="italic">P</tspan>(<tspan font-style="italic">B</tspan>|<tspan font-style="italic">A</tspan>)<tspan font-style="italic">P</tspan>(<tspan font-style="italic">A</tspan>)
|
|
184
|
+
</text>
|
|
185
|
+
<line x1="116" y1="32" x2="258" y2="32" stroke="currentColor" stroke-width="1.3"/>
|
|
186
|
+
<text x="162" y="56" font-family="Georgia,serif" font-size="20" fill="currentColor">
|
|
187
|
+
<tspan font-style="italic">P</tspan>(<tspan font-style="italic">B</tspan>)
|
|
188
|
+
</text>
|
|
189
|
+
</svg>`
|
|
190
|
+
}
|
|
191
|
+
];
|
|
192
|
+
var DEFAULT_COLORS = [
|
|
193
|
+
"#313695",
|
|
194
|
+
"#4575b4",
|
|
195
|
+
"#74add1",
|
|
196
|
+
"#abd9e9",
|
|
197
|
+
"#fee090",
|
|
198
|
+
"#fdae61",
|
|
199
|
+
"#f46d43",
|
|
200
|
+
"#d73027",
|
|
201
|
+
"#a50026",
|
|
202
|
+
"#ffffff"
|
|
203
|
+
];
|
|
204
|
+
function rand(a, b) {
|
|
205
|
+
return a + Math.random() * (b - a);
|
|
206
|
+
}
|
|
207
|
+
function easeIn(t) {
|
|
208
|
+
return t * t * t;
|
|
209
|
+
}
|
|
210
|
+
function fadeEnvelope(t, fi = 0.18, fo = 0.78) {
|
|
211
|
+
if (t < fi) return t / fi;
|
|
212
|
+
if (t < fo) return 1;
|
|
213
|
+
return 1 - (t - fo) / (1 - fo);
|
|
214
|
+
}
|
|
215
|
+
function applyEquation(el, eq, color) {
|
|
216
|
+
el.innerHTML = eq.svg;
|
|
217
|
+
const svg = el.querySelector("svg");
|
|
218
|
+
if (svg) svg.style.color = color;
|
|
219
|
+
el.style.width = `${eq.w}px`;
|
|
220
|
+
el.style.height = `${eq.h}px`;
|
|
221
|
+
el.style.filter = `drop-shadow(0 0 7px ${color}aa)`;
|
|
222
|
+
}
|
|
223
|
+
function createParticle(el, w, h, staggerT, colors) {
|
|
224
|
+
const eq = EQUATIONS[Math.floor(Math.random() * EQUATIONS.length)];
|
|
225
|
+
const color = colors[Math.floor(Math.random() * colors.length)];
|
|
226
|
+
applyEquation(el, eq, color);
|
|
227
|
+
return {
|
|
228
|
+
el,
|
|
229
|
+
eq,
|
|
230
|
+
color,
|
|
231
|
+
vx: w * rand(0.35, 0.65),
|
|
232
|
+
vy: h * rand(0.35, 0.65),
|
|
233
|
+
angle: rand(0, Math.PI * 2),
|
|
234
|
+
maxDist: rand(Math.min(w, h) * 0.25, Math.min(w, h) * 0.7),
|
|
235
|
+
scaleStart: rand(0.04, 0.1),
|
|
236
|
+
scaleEnd: rand(1.4, 2.8),
|
|
237
|
+
peakOpacity: rand(0.5, 0.85),
|
|
238
|
+
duration: rand(5e3, 11e3),
|
|
239
|
+
startTime: null,
|
|
240
|
+
t: staggerT
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function resetParticle(p, w, h, colors) {
|
|
244
|
+
const eq = EQUATIONS[Math.floor(Math.random() * EQUATIONS.length)];
|
|
245
|
+
const color = colors[Math.floor(Math.random() * colors.length)];
|
|
246
|
+
p.eq = eq;
|
|
247
|
+
p.color = color;
|
|
248
|
+
p.vx = w * rand(0.35, 0.65);
|
|
249
|
+
p.vy = h * rand(0.35, 0.65);
|
|
250
|
+
p.angle = rand(0, Math.PI * 2);
|
|
251
|
+
p.maxDist = rand(Math.min(w, h) * 0.25, Math.min(w, h) * 0.7);
|
|
252
|
+
p.scaleStart = rand(0.04, 0.1);
|
|
253
|
+
p.scaleEnd = rand(1.4, 2.8);
|
|
254
|
+
p.peakOpacity = rand(0.5, 0.85);
|
|
255
|
+
p.duration = rand(5e3, 11e3);
|
|
256
|
+
p.startTime = null;
|
|
257
|
+
p.t = 0;
|
|
258
|
+
applyEquation(p.el, eq, color);
|
|
259
|
+
}
|
|
260
|
+
function updateParticle(p, now, w, h, reverse, colors) {
|
|
261
|
+
if (p.startTime === null) {
|
|
262
|
+
p.startTime = now - p.t * p.duration;
|
|
263
|
+
}
|
|
264
|
+
let t = (now - p.startTime) / p.duration;
|
|
265
|
+
if (t >= 1) {
|
|
266
|
+
resetParticle(p, w, h, colors);
|
|
267
|
+
p.startTime = now;
|
|
268
|
+
t = 0;
|
|
269
|
+
}
|
|
270
|
+
const tz = easeIn(reverse ? 1 - t : t);
|
|
271
|
+
const scale = p.scaleStart + (p.scaleEnd - p.scaleStart) * tz;
|
|
272
|
+
const dist = p.maxDist * tz;
|
|
273
|
+
const cx = p.vx + Math.cos(p.angle) * dist;
|
|
274
|
+
const cy = p.vy + Math.sin(p.angle) * dist;
|
|
275
|
+
p.el.style.left = `${cx - p.eq.w / 2}px`;
|
|
276
|
+
p.el.style.top = `${cy - p.eq.h / 2}px`;
|
|
277
|
+
p.el.style.opacity = String(p.peakOpacity * fadeEnvelope(t, 0.12, 0.75));
|
|
278
|
+
p.el.style.transform = `scale(${scale})`;
|
|
279
|
+
const blur = t < 0.15 ? (0.15 - t) / 0.15 * 5 : t > 0.8 ? (t - 0.8) / 0.2 * 3 : 0;
|
|
280
|
+
p.el.style.filter = `drop-shadow(0 0 ${(6 * scale).toFixed(1)}px ${p.color}99) blur(${blur.toFixed(2)}px)`;
|
|
281
|
+
}
|
|
282
|
+
var MathParticlesOverlay = ({
|
|
283
|
+
backgroundColor = "#07070f",
|
|
284
|
+
colors = DEFAULT_COLORS,
|
|
285
|
+
density = 1,
|
|
286
|
+
reverse = false
|
|
287
|
+
}) => {
|
|
288
|
+
const containerRef = useRef(null);
|
|
289
|
+
const colorsRef = useRef(colors);
|
|
290
|
+
colorsRef.current = colors;
|
|
291
|
+
const reverseRef = useRef(reverse);
|
|
292
|
+
reverseRef.current = reverse;
|
|
293
|
+
useEffect(() => {
|
|
294
|
+
const container = containerRef.current;
|
|
295
|
+
if (!container) return;
|
|
296
|
+
let w = container.clientWidth;
|
|
297
|
+
let h = container.clientHeight;
|
|
298
|
+
const count = Math.max(1, Math.floor(w * h / 55e3 * density));
|
|
299
|
+
const particles = [];
|
|
300
|
+
for (let i = 0; i < count; i++) {
|
|
301
|
+
const el = document.createElement("div");
|
|
302
|
+
el.style.cssText = "position:absolute;pointer-events:none;will-change:transform,opacity,filter;transform-origin:center center;opacity:0;";
|
|
303
|
+
container.appendChild(el);
|
|
304
|
+
particles.push(
|
|
305
|
+
createParticle(el, w, h, Math.random(), colorsRef.current)
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
let rafId;
|
|
309
|
+
const loop = (now) => {
|
|
310
|
+
for (const p of particles)
|
|
311
|
+
updateParticle(p, now, w, h, reverseRef.current, colorsRef.current);
|
|
312
|
+
rafId = requestAnimationFrame(loop);
|
|
313
|
+
};
|
|
314
|
+
rafId = requestAnimationFrame(loop);
|
|
315
|
+
const onResize = () => {
|
|
316
|
+
w = container.clientWidth;
|
|
317
|
+
h = container.clientHeight;
|
|
318
|
+
};
|
|
319
|
+
window.addEventListener("resize", onResize);
|
|
320
|
+
return () => {
|
|
321
|
+
cancelAnimationFrame(rafId);
|
|
322
|
+
window.removeEventListener("resize", onResize);
|
|
323
|
+
container.innerHTML = "";
|
|
324
|
+
};
|
|
325
|
+
}, []);
|
|
326
|
+
return /* @__PURE__ */ jsx(
|
|
327
|
+
"div",
|
|
328
|
+
{
|
|
329
|
+
ref: containerRef,
|
|
330
|
+
style: {
|
|
331
|
+
position: "absolute",
|
|
332
|
+
inset: 0,
|
|
333
|
+
overflow: "hidden",
|
|
334
|
+
backgroundColor
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
);
|
|
338
|
+
};
|
|
339
|
+
export {
|
|
340
|
+
MathParticlesOverlay
|
|
341
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slithy/math-particles",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Animated math equations particle overlay for React.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"react": "^17 || ^18 || ^19",
|
|
18
|
+
"react-dom": "^17 || ^18 || ^19"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@testing-library/jest-dom": "^6",
|
|
22
|
+
"@testing-library/react": "^16",
|
|
23
|
+
"@types/react": "^19",
|
|
24
|
+
"@types/react-dom": "^19",
|
|
25
|
+
"@vitejs/plugin-react": "^6",
|
|
26
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
27
|
+
"jsdom": "^29.0.1",
|
|
28
|
+
"react": "^19",
|
|
29
|
+
"react-dom": "^19",
|
|
30
|
+
"tsup": "^8",
|
|
31
|
+
"typescript": "^5",
|
|
32
|
+
"vitest": "^4.1.2",
|
|
33
|
+
"@slithy/eslint-config": "0.0.0",
|
|
34
|
+
"@slithy/tsconfig": "0.0.0"
|
|
35
|
+
},
|
|
36
|
+
"author": {
|
|
37
|
+
"name": "Matthew Campagna",
|
|
38
|
+
"url": "https://github.com/mjcampagna"
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"keywords": [
|
|
42
|
+
"react",
|
|
43
|
+
"math",
|
|
44
|
+
"equations",
|
|
45
|
+
"particles",
|
|
46
|
+
"animation",
|
|
47
|
+
"overlay"
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "https://github.com/mjcampagna/slithy.git",
|
|
52
|
+
"directory": "packages/math-particles"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"clean": "rm -rf dist",
|
|
59
|
+
"build": "rm -rf dist && tsup src/index.ts --format esm --dts",
|
|
60
|
+
"dev": "tsup src/index.ts --format esm --watch",
|
|
61
|
+
"typecheck": "tsc --noEmit",
|
|
62
|
+
"lint": "eslint .",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"test:watch": "vitest"
|
|
65
|
+
}
|
|
66
|
+
}
|