An abstract animated nodal background JavaScript canvas library.
-
Different modes of operation
AntiGravity
produces a pleasing effect of nodes pulling away from each other.Gravity
creates a true n-body simulation with emerging chaotic behaviour.Simple
makes the simulation serene and non-distracting.
-
Fully configurable
- Everything that could have been has been exposed as a configuration variable. Values can be changed that greatly affect the behaviour and visuals of the simulation.
- Concrete implementations of abstract classes can be created to achieve custom behaviour.
-
Interactive
- Nodes can be interacted with using the mouse
- Click scrolling changes the mass of the added node
npm install --save nodal-background
import { useNodalBackground } from "nodal-background"
export const NodalBackgroundComponent = (props: NodalBackgroundProps) => {
const container = useRef()
const {setContainer} = useNodalBackground({
container: container.current,
...props
})
useEffect(() => {
if (container.current) {
setContainer(container.current)
}
}, [container.current])
return <div ref={container}/>
}
The wrapper above is also provided as an importable component.
import {NodalBackgroundComponent} from "nodal-background"
const YourComponent = () => <NodalBackgroundComponent>
new NodalBackground({container: document.getElementById("id")})
2021-10-31.22-18-16.mp4
2021-10-31.22-18-54.mp4
All variables can be changed while the simulation is running.
Canvas is by default transparent, changing the background color below the
canvas element combined with nodeColor
and linkColor
is an easy way to
achieve a different theme.
The demonstration page can be used to interactively change the following controls.
Controls the behaviour of the entire simulation.
NodalBackgroundMode.AntiGravity
(default)NodalBackgroundMode.Gravity
NodalBackgroundMode.Simple
Maximum distance between two nodes for the interaction to be calculated. Higher numbers are more computationally expensive and create a more connected graph, whereas lower values produce fewer connection lines between nodes.
Minimum distance between two nodes for the interaction to be calculated. Lower values produce large slingshot speeds when nodes get or spawn close together. Higher values minimize the slingshot potential. Too high values will affect the perceived realism of the simulation.
Multiplier for node mass, affects speed and chaos.
Multiplier for attraction, affects speed and chaos
The target number of nodes, relative to a 1200x1200 pixel area. Simulation complexity rises with O(n*log n), so higher numbers are discouraged.
Whether to delete extraneous nodes, with this disabled, the user created nodes do not disappear.
Color of the nodes.
Node initial velocity is chosen at random from a range, this control affects that range. Higher values significantly affect the behaviour, increasing the chaos.
Initial mass of the nodes.
When greater than 0, initial mass of nodes will be randomized up to the value.
Maximum mass of the nodes, applied when merging. Value 0
sets no maximum mass.
Visual node size offset, does not affect the simulation apart from merging.
How quickly nodes fade in after spawn.
How quickly nodes fade out after leaving the area.
Changes the link color.
Changes the link line between nodes.
Documentation on MDN - setLineDash()
Sets the target FPS. Simulation is generally acceptable down to 20 fps.
Displays the FPS counter, the current implementation of the fps counter has a negative impact on the actual fps, when bottlenecked it takes about 10fps.
Custom simulation behaviour can be achieved by extending the base abstract classes.
For more information read the included implementing classes.
Ticker performs the simulation steps.
Linker draws links between the nodes.