12 coloured gems scattered randomly, each reacting to the cursor: proximity halo, repulsion
and 3-D cast shadow — driven entirely by native CSS trigonometry
with a single mousemove listener.
CSS Custom PropertiesCSS Trig FunctionsVanilla JSZero dependenciesStandalone HTML
↑ move cursor over the gems
What is it
Overview
NearMouse is a pure CSS + 3-line JS interactive effect.
Diamonds are scattered at random positions. As the cursor enters a gem's
--impact-radius, it glows, slides away, and casts a perspective shadow.
No canvas, no WebGL, no animation loop — the browser re-evaluates CSS custom
properties on every mousemove at native frame-rate.
Capabilities
Features
CSS cos() / sin() / atan2() / sqrt()
Distance factor --dist-factor — clamped 0 → 1
Diamond shape via clip-path polygon
Proximity halo via CSS drop-shadow()
Brightness boost on approach
3-D perspective cast shadow per gem
Random scatter via JS — refreshes on reload
Only 2 variables to customise
Internals
How it works
1
Random scatter — JS generates 12 random --x / --y
pairs (range ±3.8) and sets them as inline CSS custom properties on each gem & shadow pair.
2
Mouse tracking — mousemove writes
--mx / --my (offset from demo-box centre) onto the scene element.
A mouseleave resets them to −9999 so gems go idle.
3
Distance factor — per gem, CSS computes Euclidean distance
sqrt(dx²+dy²) then clamps to [0,1]:
(impact-radius − min(dist, impact-radius)) / impact-radius.
4
Reactivity cascade — --dist-factor drives the repulsion
translation along atan2 direction, the drop-shadow glow radius,
brightness() boost, and the shadow opacity & 3-D tilt simultaneously.
Customisation
Variables
Variable
Default
Description
--diamond-size
38
Gem size in px (unitless, used as × 1px). Also scales orbit & shadow.
--impact-radius
170
Activation radius in px. Gems outside this range stay idle.
--mx / --my
-9999
Cursor offset from scene centre. Set by JS. Defaults keep gems idle on load.
--x / --y
via JS
Random gem position in diamond-size units. Set at scatter time.
Compatibility
Browser support
Requires CSS trigonometric functions —
cos(), sin(), atan2(), sqrt() —
available in all evergreen browsers since mid-2023.
Chrome 111+
Firefox 108+
Safari 15.4+
Edge 111+
No build step, no dependencies, no framework. A single .html file is all you need.