cosmograph 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 +282 -0
- package/index.d.ts +176 -0
- package/package.json +30 -0
- package/src/Node.js +13 -0
- package/src/QuadTree.js +65 -0
- package/src/World.js +534 -0
- package/src/adapters/react.js +37 -0
- package/src/forces/attract.js +26 -0
- package/src/forces/dampen.js +8 -0
- package/src/forces/drift.js +11 -0
- package/src/forces/gravity.js +30 -0
- package/src/forces/mouseRepel.js +49 -0
- package/src/forces/nodeRepel.js +33 -0
- package/src/forces/noise.js +64 -0
- package/src/forces/scrollDrift.js +58 -0
- package/src/forces/twinkle.js +7 -0
- package/src/forces/wind.js +27 -0
- package/src/index.js +14 -0
- package/src/shapes.js +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Corvin Burmeister
|
|
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,282 @@
|
|
|
1
|
+
# cosmograph
|
|
2
|
+
|
|
3
|
+
Lightweight, zero-dependency canvas particle library. Composable forces, configurable everything.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install cosmograph
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { World, drift, dampen, twinkle, mouseRepel } from 'cosmograph'
|
|
15
|
+
|
|
16
|
+
const world = new World({
|
|
17
|
+
canvas: document.getElementById('bg'),
|
|
18
|
+
nodeCount: 60,
|
|
19
|
+
colors: ['#ffffff', '#cce8ff'],
|
|
20
|
+
forces: [twinkle(), mouseRepel(), dampen(), drift()],
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
world.start()
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Forces are just functions** — compose them in any order, pass as an array. Each force runs every frame and mutates node velocities.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Full-screen background
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const world = new World({
|
|
34
|
+
canvas,
|
|
35
|
+
autoResize: true, // fits canvas to window, handles resize
|
|
36
|
+
pauseWhenHidden: true, // pauses when tab is not visible
|
|
37
|
+
pauseWhenOffscreen: true,
|
|
38
|
+
nodeCount: 60,
|
|
39
|
+
colors: ['#ffffff', '#cce8ff', '#fff4e0'],
|
|
40
|
+
edgeMaxDist: 180,
|
|
41
|
+
forces: [twinkle(), mouseRepel(), dampen(), drift()],
|
|
42
|
+
})
|
|
43
|
+
world.start()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## React
|
|
49
|
+
|
|
50
|
+
```jsx
|
|
51
|
+
import { useCosmograph } from 'cosmograph/react'
|
|
52
|
+
import { drift, dampen, twinkle, mouseRepel } from 'cosmograph'
|
|
53
|
+
|
|
54
|
+
export default function Background() {
|
|
55
|
+
const ref = useCosmograph({
|
|
56
|
+
nodeCount: 60,
|
|
57
|
+
colors: ['#ffffff', '#cce8ff'],
|
|
58
|
+
forces: [twinkle(), mouseRepel(), dampen(), drift()],
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
return <canvas ref={ref} style={{ position: 'fixed', inset: 0, width: '100%', height: '100%' }} />
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## World options
|
|
68
|
+
|
|
69
|
+
### Nodes
|
|
70
|
+
|
|
71
|
+
| Option | Type | Default | Description |
|
|
72
|
+
|---|---|---|---|
|
|
73
|
+
| `nodeCount` | `number` | `60` | Number of nodes |
|
|
74
|
+
| `nodeSize` | `[min, max]` | `[0.8, 2.8]` | Radius range in px |
|
|
75
|
+
| `colors` | `string[]` | `['#ffffff']` | Node colors |
|
|
76
|
+
| `nodeShape` | `'circle' \| 'diamond' \| 'star' \| 'cross' \| 'ring'` | `'circle'` | Node shape |
|
|
77
|
+
| `nodeRotation` | `boolean` | `false` | Nodes rotate on their axis — visible on diamond/star |
|
|
78
|
+
| `nodeSizeDistribution` | `'uniform' \| 'gaussian' \| 'weighted-small'` | `'uniform'` | Size sampling distribution |
|
|
79
|
+
| `nodeColorMode` | `'random' \| 'by-size' \| 'sequential' \| 'gradient' \| 'by-position'` | `'random'` | How colors are assigned |
|
|
80
|
+
| `nodeSpawnRegion` | `'full' \| 'center' \| 'edges' \| fn` | `'full'` | Where nodes spawn. `fn(width, height) => {x, y}` for custom regions |
|
|
81
|
+
|
|
82
|
+
### Edges
|
|
83
|
+
|
|
84
|
+
| Option | Type | Default | Description |
|
|
85
|
+
|---|---|---|---|
|
|
86
|
+
| `edgeMaxDist` | `number` | `180` | Max distance for edge to appear |
|
|
87
|
+
| `edgeMaxOpacity` | `number` | `0.18` | Max edge opacity |
|
|
88
|
+
| `edgeWidth` | `number` | `0.5` | Edge stroke width in px |
|
|
89
|
+
| `edgeColors` | `string[]` | `null` | Edge colors — falls back to `colors` if not set |
|
|
90
|
+
| `edgeStyle` | `'solid' \| 'dashed' \| 'gradient'` | `'solid'` | `gradient` interpolates between connected node colors |
|
|
91
|
+
| `edgeColorMode` | `'alternate' \| 'source' \| 'target' \| fn` | `'alternate'` | `fn(a, b, i, j) => color` for custom logic |
|
|
92
|
+
| `edgeCurvature` | `number` | `0` | `0` = straight, `1` = strong bezier curve |
|
|
93
|
+
| `maxEdgesPerNode` | `number \| null` | `null` | Cap connections per node — prevents dense clusters |
|
|
94
|
+
| `minEdgesPerNode` | `number \| null` | `null` | Guarantee minimum connections — draws faint long-range edges as fallback |
|
|
95
|
+
|
|
96
|
+
### Glow
|
|
97
|
+
|
|
98
|
+
| Option | Type | Default | Description |
|
|
99
|
+
|---|---|---|---|
|
|
100
|
+
| `glowOnLargeNodes` | `boolean` | `true` | Adds radial halo to nodes above threshold |
|
|
101
|
+
| `glowThreshold` | `number` | `2` | Minimum radius to trigger glow |
|
|
102
|
+
| `glowScale` | `number` | `4` | Halo radius multiplier |
|
|
103
|
+
| `glowOpacity` | `number` | `0.25` | Halo opacity |
|
|
104
|
+
|
|
105
|
+
### Rendering
|
|
106
|
+
|
|
107
|
+
| Option | Type | Default | Description |
|
|
108
|
+
|---|---|---|---|
|
|
109
|
+
| `blendMode` | `string` | `'source-over'` | Canvas `globalCompositeOperation` — `'screen'` makes overlapping nodes glow brighter |
|
|
110
|
+
| `renderOrder` | `'edges-first' \| 'nodes-first'` | `'edges-first'` | Draw order |
|
|
111
|
+
| `background` | `string \| null` | `null` | Background fill color — `null` for transparent |
|
|
112
|
+
| `pixelRatio` | `number \| 'auto'` | `'auto'` | HiDPI/Retina pixel ratio |
|
|
113
|
+
|
|
114
|
+
### Performance
|
|
115
|
+
|
|
116
|
+
| Option | Type | Default | Description |
|
|
117
|
+
|---|---|---|---|
|
|
118
|
+
| `pauseWhenHidden` | `boolean` | `true` | Pause RAF when tab is not visible |
|
|
119
|
+
| `pauseWhenOffscreen` | `boolean` | `false` | Pause RAF when canvas is scrolled out of view |
|
|
120
|
+
| `autoResize` | `boolean` | `true` | Fit canvas to window on resize — set to `false` when managing canvas size yourself |
|
|
121
|
+
| `maxEdgesPerFrame` | `number \| null` | `null` | Hard cap on edges drawn per frame — useful for very high node counts |
|
|
122
|
+
| `spatialIndex` | `boolean` | `false` | Use a QuadTree for edge queries — O(n log n) instead of O(n²), worth enabling above ~200 nodes |
|
|
123
|
+
|
|
124
|
+
### Callbacks
|
|
125
|
+
|
|
126
|
+
| Option | Type | Description |
|
|
127
|
+
|---|---|---|
|
|
128
|
+
| `onFrame` | `(nodes, context) => void` | Called every frame after rendering |
|
|
129
|
+
| `onNodeHover` | `(node) => void` | Called when cursor enters a node's hitbox |
|
|
130
|
+
| `onNodeLeave` | `(node) => void` | Called when cursor leaves a node's hitbox |
|
|
131
|
+
| `onNodeClick` | `(node) => void` | Called when a node is clicked |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Forces
|
|
136
|
+
|
|
137
|
+
Forces are plain functions — `(nodes, context) => void`. The `context` object provides `{ time, mouse, scrollY, width, height }`. Combine freely.
|
|
138
|
+
|
|
139
|
+
### `drift({ maxSpeed })`
|
|
140
|
+
Caps node velocity — prevents runaway acceleration. Use as the last force in your chain.
|
|
141
|
+
|
|
142
|
+
| Param | Default | |
|
|
143
|
+
|---|---|---|
|
|
144
|
+
| `maxSpeed` | `0.08` | Maximum speed in px/frame |
|
|
145
|
+
|
|
146
|
+
### `dampen({ factor })`
|
|
147
|
+
Multiplies all velocities by `factor` each frame — simulates friction.
|
|
148
|
+
|
|
149
|
+
| Param | Default | |
|
|
150
|
+
|---|---|---|
|
|
151
|
+
| `factor` | `0.99` | `0.95` = heavy friction, `0.999` = near frictionless |
|
|
152
|
+
|
|
153
|
+
### `twinkle({ minBrightness, variance })`
|
|
154
|
+
Oscillates node opacity using a per-node sine wave.
|
|
155
|
+
|
|
156
|
+
| Param | Default | |
|
|
157
|
+
|---|---|---|
|
|
158
|
+
| `minBrightness` | `0.5` | Minimum alpha |
|
|
159
|
+
| `variance` | `0.5` | Oscillation amplitude |
|
|
160
|
+
|
|
161
|
+
### `mouseRepel({ mode, radius, strength, fn })`
|
|
162
|
+
Cursor interaction.
|
|
163
|
+
|
|
164
|
+
| Param | Default | |
|
|
165
|
+
|---|---|---|
|
|
166
|
+
| `mode` | `'repel'` | `'repel'` · `'attract'` · `'orbit'` · `'custom'` |
|
|
167
|
+
| `radius` | `120` | Influence radius in px |
|
|
168
|
+
| `strength` | `0.012` | Force multiplier |
|
|
169
|
+
| `fn` | — | `fn(node, mouse, context)` when `mode: 'custom'` |
|
|
170
|
+
|
|
171
|
+
### `gravity({ x, y, strength })`
|
|
172
|
+
Pulls all nodes toward a fixed point. Values `0..1` are relative to canvas size.
|
|
173
|
+
|
|
174
|
+
| Param | Default | |
|
|
175
|
+
|---|---|---|
|
|
176
|
+
| `x` | `0.5` | Target x — `0.5` = center |
|
|
177
|
+
| `y` | `0.5` | Target y |
|
|
178
|
+
| `strength` | `0.0002` | Force magnitude |
|
|
179
|
+
|
|
180
|
+
### `attract({ x, y, radius, strength })`
|
|
181
|
+
Like gravity but only affects nodes within `radius`. Has a dead zone at the center — combine with `nodeRepel` to prevent clustering.
|
|
182
|
+
|
|
183
|
+
| Param | Default | |
|
|
184
|
+
|---|---|---|
|
|
185
|
+
| `x` | `0.5` | Target x |
|
|
186
|
+
| `y` | `0.5` | Target y |
|
|
187
|
+
| `radius` | `200` | Influence radius in px |
|
|
188
|
+
| `strength` | `0.001` | Force magnitude |
|
|
189
|
+
|
|
190
|
+
### `nodeRepel({ radius, strength })`
|
|
191
|
+
Nodes push each other away — simulates charged particles. O(n²), keep `radius` small.
|
|
192
|
+
|
|
193
|
+
| Param | Default | |
|
|
194
|
+
|---|---|---|
|
|
195
|
+
| `radius` | `60` | Repulsion radius |
|
|
196
|
+
| `strength` | `0.003` | Force magnitude |
|
|
197
|
+
|
|
198
|
+
### `wind({ angle, strength, gust })`
|
|
199
|
+
Constant directional force with optional sinusoidal gusting.
|
|
200
|
+
|
|
201
|
+
| Param | Default | |
|
|
202
|
+
|---|---|---|
|
|
203
|
+
| `angle` | `0` | Direction in radians |
|
|
204
|
+
| `strength` | `0.001` | Base force magnitude |
|
|
205
|
+
| `gust` | `0` | Gust amplitude |
|
|
206
|
+
|
|
207
|
+
### `noise({ scale, strength, speed })`
|
|
208
|
+
Moves nodes along a slowly shifting 2D vector field. Nearby nodes flow in the same direction — organic, non-random.
|
|
209
|
+
|
|
210
|
+
| Param | Default | |
|
|
211
|
+
|---|---|---|
|
|
212
|
+
| `scale` | `0.004` | Field scale — small = broad sweeping currents |
|
|
213
|
+
| `strength` | `0.001` | Force magnitude |
|
|
214
|
+
| `speed` | `0.0003` | How fast the field shifts over time |
|
|
215
|
+
|
|
216
|
+
### `scrollDrift({ mode, strength })`
|
|
217
|
+
Reacts to `world.scrollY`. In `autoResize` mode this tracks `window.scrollY` automatically.
|
|
218
|
+
|
|
219
|
+
| Param | Default | |
|
|
220
|
+
|---|---|---|
|
|
221
|
+
| `mode` | `'rotate'` | `'rotate'` · `'wave'` · `'scatter'` · `'custom'` |
|
|
222
|
+
| `strength` | `1.0` | Effect multiplier |
|
|
223
|
+
| `fn` | — | `fn(node, delta, context)` when `mode: 'custom'` |
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Custom forces
|
|
228
|
+
|
|
229
|
+
Any function matching `(nodes, context) => void` works as a force:
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
function pulse() {
|
|
233
|
+
return (nodes, { time }) => {
|
|
234
|
+
const scale = Math.sin(time * 0.001)
|
|
235
|
+
for (const node of nodes) {
|
|
236
|
+
node.vx += (node.x - 400) * scale * 0.0001
|
|
237
|
+
node.vy += (node.y - 300) * scale * 0.0001
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
new World({ canvas, forces: [pulse(), dampen(), drift()] })
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## API
|
|
248
|
+
|
|
249
|
+
### `world.start()`
|
|
250
|
+
Begins the render loop and registers event listeners.
|
|
251
|
+
|
|
252
|
+
### `world.stop()`
|
|
253
|
+
Cancels the render loop and removes all event listeners.
|
|
254
|
+
|
|
255
|
+
### `world.scrollY`
|
|
256
|
+
Set directly when managing scroll manually (e.g. in a bounded canvas):
|
|
257
|
+
```js
|
|
258
|
+
window.addEventListener('scroll', () => { world.scrollY = window.scrollY })
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## Shapes
|
|
264
|
+
|
|
265
|
+
`circle` · `diamond` · `star` · `cross` · `ring`
|
|
266
|
+
|
|
267
|
+
Custom shape function: `(ctx, x, y, r) => void`
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
import { World } from 'cosmograph'
|
|
271
|
+
|
|
272
|
+
function triangle(ctx, x, y, r) {
|
|
273
|
+
ctx.beginPath()
|
|
274
|
+
ctx.moveTo(x, y - r)
|
|
275
|
+
ctx.lineTo(x + r, y + r)
|
|
276
|
+
ctx.lineTo(x - r, y + r)
|
|
277
|
+
ctx.closePath()
|
|
278
|
+
ctx.fill()
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
new World({ canvas, nodeShape: triangle })
|
|
282
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// cosmograph — canvas particle library
|
|
2
|
+
|
|
3
|
+
export interface NodeOptions {
|
|
4
|
+
x: number
|
|
5
|
+
y: number
|
|
6
|
+
r: number
|
|
7
|
+
vx: number
|
|
8
|
+
vy: number
|
|
9
|
+
color: string
|
|
10
|
+
phase: number
|
|
11
|
+
twinkleSpeed: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare class Node {
|
|
15
|
+
x: number
|
|
16
|
+
y: number
|
|
17
|
+
r: number
|
|
18
|
+
vx: number
|
|
19
|
+
vy: number
|
|
20
|
+
color: string
|
|
21
|
+
brightness: number
|
|
22
|
+
phase: number
|
|
23
|
+
twinkleSpeed: number
|
|
24
|
+
angle?: number
|
|
25
|
+
angularVelocity?: number
|
|
26
|
+
shape?: string | ShapeFn
|
|
27
|
+
constructor(opts: NodeOptions)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type ShapeFn = (ctx: CanvasRenderingContext2D, x: number, y: number, r: number) => void
|
|
31
|
+
|
|
32
|
+
export declare function resolveShape(shape: string | ShapeFn): ShapeFn
|
|
33
|
+
|
|
34
|
+
export declare const circle: ShapeFn
|
|
35
|
+
export declare const diamond: ShapeFn
|
|
36
|
+
export declare const star: ShapeFn
|
|
37
|
+
export declare const cross: ShapeFn
|
|
38
|
+
export declare const ring: ShapeFn
|
|
39
|
+
|
|
40
|
+
export interface ForceContext {
|
|
41
|
+
readonly time: number
|
|
42
|
+
readonly mouse: { x: number; y: number } | null
|
|
43
|
+
readonly scrollY: number
|
|
44
|
+
readonly width: number
|
|
45
|
+
readonly height: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type Force = (nodes: Node[], context: ForceContext) => void
|
|
49
|
+
|
|
50
|
+
export interface WorldOptions {
|
|
51
|
+
canvas: HTMLCanvasElement
|
|
52
|
+
forces?: Force[]
|
|
53
|
+
// Nodes
|
|
54
|
+
nodeCount?: number
|
|
55
|
+
nodeSize?: number | [number, number]
|
|
56
|
+
colors?: string[]
|
|
57
|
+
nodeSizeDistribution?: 'uniform' | 'gaussian' | 'weighted-small'
|
|
58
|
+
nodeColorMode?: 'random' | 'by-size' | 'sequential' | 'gradient' | 'by-position'
|
|
59
|
+
nodeSpawnRegion?: 'full' | 'center' | 'edges' | ((width: number, height: number) => { x: number; y: number })
|
|
60
|
+
nodeRotation?: boolean
|
|
61
|
+
// Edges
|
|
62
|
+
edgeMaxDist?: number
|
|
63
|
+
edgeMaxOpacity?: number
|
|
64
|
+
edgeWidth?: number
|
|
65
|
+
edgeColors?: string[] | null
|
|
66
|
+
edgeStyle?: 'solid' | 'dashed' | 'gradient'
|
|
67
|
+
edgeColorMode?: 'alternate' | 'source' | 'target' | ((a: Node, b: Node, i: number, j: number) => string)
|
|
68
|
+
maxEdgesPerNode?: number | null
|
|
69
|
+
minEdgesPerNode?: number | null
|
|
70
|
+
edgeCurvature?: number
|
|
71
|
+
// Shape
|
|
72
|
+
nodeShape?: string | ShapeFn
|
|
73
|
+
// Glow
|
|
74
|
+
glowOnLargeNodes?: boolean
|
|
75
|
+
glowThreshold?: number
|
|
76
|
+
glowScale?: number
|
|
77
|
+
glowOpacity?: number
|
|
78
|
+
// Rendering
|
|
79
|
+
pixelRatio?: 'auto' | number
|
|
80
|
+
blendMode?: GlobalCompositeOperation
|
|
81
|
+
renderOrder?: 'edges-first' | 'nodes-first'
|
|
82
|
+
background?: string | null
|
|
83
|
+
// Callbacks
|
|
84
|
+
onFrame?: ((nodes: Node[], context: ForceContext) => void) | null
|
|
85
|
+
onNodeHover?: ((node: Node) => void) | null
|
|
86
|
+
onNodeLeave?: ((node: Node) => void) | null
|
|
87
|
+
onNodeClick?: ((node: Node) => void) | null
|
|
88
|
+
// Performance
|
|
89
|
+
pauseWhenHidden?: boolean
|
|
90
|
+
maxEdgesPerFrame?: number | null
|
|
91
|
+
spatialIndex?: boolean
|
|
92
|
+
autoResize?: boolean
|
|
93
|
+
pauseWhenOffscreen?: boolean
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export declare class World {
|
|
97
|
+
canvas: HTMLCanvasElement
|
|
98
|
+
nodes: Node[]
|
|
99
|
+
forces: Force[]
|
|
100
|
+
options: WorldOptions
|
|
101
|
+
constructor(opts: WorldOptions)
|
|
102
|
+
start(): void
|
|
103
|
+
stop(): void
|
|
104
|
+
resize(): void
|
|
105
|
+
update(options: Partial<Omit<WorldOptions, 'canvas'>>): void
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Forces
|
|
109
|
+
export interface DriftOptions {
|
|
110
|
+
speed?: number
|
|
111
|
+
chaos?: number
|
|
112
|
+
}
|
|
113
|
+
export declare function drift(opts?: DriftOptions): Force
|
|
114
|
+
|
|
115
|
+
export interface DampenOptions {
|
|
116
|
+
factor?: number
|
|
117
|
+
}
|
|
118
|
+
export declare function dampen(opts?: DampenOptions): Force
|
|
119
|
+
|
|
120
|
+
export interface TwinkleOptions {
|
|
121
|
+
minBrightness?: number
|
|
122
|
+
variance?: number
|
|
123
|
+
}
|
|
124
|
+
export declare function twinkle(opts?: TwinkleOptions): Force
|
|
125
|
+
|
|
126
|
+
export interface MouseRepelOptions {
|
|
127
|
+
radius?: number
|
|
128
|
+
strength?: number
|
|
129
|
+
mode?: 'repel' | 'attract'
|
|
130
|
+
}
|
|
131
|
+
export declare function mouseRepel(opts?: MouseRepelOptions): Force
|
|
132
|
+
|
|
133
|
+
export interface ScrollDriftOptions {
|
|
134
|
+
mode?: 'rotate' | 'wave' | 'scatter' | 'custom'
|
|
135
|
+
strength?: number
|
|
136
|
+
fn?: (node: Node, delta: number, context: ForceContext) => void
|
|
137
|
+
}
|
|
138
|
+
export declare function scrollDrift(opts?: ScrollDriftOptions): Force
|
|
139
|
+
|
|
140
|
+
export interface GravityOptions {
|
|
141
|
+
x?: number | ((context: ForceContext) => number)
|
|
142
|
+
y?: number | ((context: ForceContext) => number)
|
|
143
|
+
strength?: number
|
|
144
|
+
}
|
|
145
|
+
export declare function gravity(opts?: GravityOptions): Force
|
|
146
|
+
|
|
147
|
+
export interface WindOptions {
|
|
148
|
+
angle?: number
|
|
149
|
+
speed?: number
|
|
150
|
+
turbulence?: number
|
|
151
|
+
}
|
|
152
|
+
export declare function wind(opts?: WindOptions): Force
|
|
153
|
+
|
|
154
|
+
export interface NodeRepelOptions {
|
|
155
|
+
radius?: number
|
|
156
|
+
strength?: number
|
|
157
|
+
}
|
|
158
|
+
export declare function nodeRepel(opts?: NodeRepelOptions): Force
|
|
159
|
+
|
|
160
|
+
export interface NoiseOptions {
|
|
161
|
+
scale?: number
|
|
162
|
+
speed?: number
|
|
163
|
+
strength?: number
|
|
164
|
+
}
|
|
165
|
+
export declare function noise(opts?: NoiseOptions): Force
|
|
166
|
+
|
|
167
|
+
export interface AttractOptions {
|
|
168
|
+
x?: number | ((width: number, height: number) => number)
|
|
169
|
+
y?: number | ((width: number, height: number) => number)
|
|
170
|
+
radius?: number
|
|
171
|
+
strength?: number
|
|
172
|
+
}
|
|
173
|
+
export declare function attract(opts?: AttractOptions): Force
|
|
174
|
+
|
|
175
|
+
// React adapter
|
|
176
|
+
export declare function useCosmograph(options?: Omit<WorldOptions, 'canvas'>): React.RefObject<HTMLCanvasElement>
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cosmograph",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Lightweight, zero-dependency canvas particle library. Composable forces, configurable everything.",
|
|
5
|
+
"keywords": ["canvas", "particles", "animation", "constellation", "webgl", "background", "force", "interactive"],
|
|
6
|
+
"author": "Corvin Burmeister",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"types": "index.d.ts",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"module": "./src/index.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.js",
|
|
14
|
+
"./react": "./src/adapters/react.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"README.md",
|
|
19
|
+
"index.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/BurmCo/cosmograph.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/BurmCo/cosmograph#readme",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/BurmCo/cosmograph/issues"
|
|
28
|
+
},
|
|
29
|
+
"sideEffects": false
|
|
30
|
+
}
|
package/src/Node.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class Node {
|
|
2
|
+
constructor({ x, y, r, vx, vy, color, phase, twinkleSpeed }) {
|
|
3
|
+
this.x = x
|
|
4
|
+
this.y = y
|
|
5
|
+
this.r = r
|
|
6
|
+
this.vx = vx
|
|
7
|
+
this.vy = vy
|
|
8
|
+
this.color = color
|
|
9
|
+
this.phase = phase
|
|
10
|
+
this.twinkleSpeed = twinkleSpeed
|
|
11
|
+
this.brightness = 1
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/QuadTree.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export class QuadTree {
|
|
2
|
+
constructor(x, y, w, h, capacity = 8) {
|
|
3
|
+
this.x = x
|
|
4
|
+
this.y = y
|
|
5
|
+
this.w = w
|
|
6
|
+
this.h = h
|
|
7
|
+
this.capacity = capacity
|
|
8
|
+
this.points = []
|
|
9
|
+
this.divided = false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
insert(point) {
|
|
13
|
+
if (!this._contains(point.x, point.y)) return false
|
|
14
|
+
if (this.points.length < this.capacity && !this.divided) {
|
|
15
|
+
this.points.push(point)
|
|
16
|
+
return true
|
|
17
|
+
}
|
|
18
|
+
if (!this.divided) this._subdivide()
|
|
19
|
+
return (
|
|
20
|
+
this._ne.insert(point) ||
|
|
21
|
+
this._nw.insert(point) ||
|
|
22
|
+
this._se.insert(point) ||
|
|
23
|
+
this._sw.insert(point)
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
queryRadius(cx, cy, r, result = []) {
|
|
28
|
+
if (!this._intersectsCircle(cx, cy, r)) return result
|
|
29
|
+
if (this.divided) {
|
|
30
|
+
this._ne.queryRadius(cx, cy, r, result)
|
|
31
|
+
this._nw.queryRadius(cx, cy, r, result)
|
|
32
|
+
this._se.queryRadius(cx, cy, r, result)
|
|
33
|
+
this._sw.queryRadius(cx, cy, r, result)
|
|
34
|
+
} else {
|
|
35
|
+
for (const p of this.points) {
|
|
36
|
+
if (Math.hypot(p.x - cx, p.y - cy) <= r) result.push(p)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return result
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_contains(px, py) {
|
|
43
|
+
return px >= this.x && px < this.x + this.w && py >= this.y && py < this.y + this.h
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
_intersectsCircle(cx, cy, r) {
|
|
47
|
+
const nearX = Math.max(this.x, Math.min(cx, this.x + this.w))
|
|
48
|
+
const nearY = Math.max(this.y, Math.min(cy, this.y + this.h))
|
|
49
|
+
return Math.hypot(cx - nearX, cy - nearY) <= r
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
_subdivide() {
|
|
53
|
+
const hw = this.w / 2
|
|
54
|
+
const hh = this.h / 2
|
|
55
|
+
this._ne = new QuadTree(this.x + hw, this.y, hw, hh, this.capacity)
|
|
56
|
+
this._nw = new QuadTree(this.x, this.y, hw, hh, this.capacity)
|
|
57
|
+
this._se = new QuadTree(this.x + hw, this.y + hh, hw, hh, this.capacity)
|
|
58
|
+
this._sw = new QuadTree(this.x, this.y + hh, hw, hh, this.capacity)
|
|
59
|
+
this.divided = true
|
|
60
|
+
for (const p of this.points) {
|
|
61
|
+
this._ne.insert(p) || this._nw.insert(p) || this._se.insert(p) || this._sw.insert(p)
|
|
62
|
+
}
|
|
63
|
+
this.points = []
|
|
64
|
+
}
|
|
65
|
+
}
|