Loading…
← All components

Background · Particles

Point Cloud Field

A slowly rotating cloud of points for a subtle, ambient page background.

<PointCloudScene />Live

Customize

Color 1#ffffff
Color 2#8a8a8a

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add point-cloud-background
01

Install

The 3D packages this component is built on.

$ npm install three @react-three/fiber
02

Add the component

One self-contained file: create components/PointCloudScene.js and paste it in.

components/PointCloudScene.js
1"use client";
2
3import { Suspense, useEffect, useMemo, useRef, useState } from "react";
4import * as THREE from "three";
5import { Canvas, useFrame, useThree } from "@react-three/fiber";
6
7/**
8 * Reusable <Canvas> shell for every scene in /components/3d.
9 *
10 * - Pauses the render loop (frameloop="never") while the canvas is
11 * scrolled out of view, so idle scenes stop burning GPU/battery.
12 * - Caps devicePixelRatio to [1, 2] to avoid over-rendering on retina/4K.
13 * - WebGL resources (geometries, materials, the GL context itself) are
14 * disposed automatically by R3F when the Canvas unmounts.
15 */
16function Canvas3DWrapper({
17 children,
18 className = "",
19 camera = { position: [0, 0, 5], fov: 45 },
20 dpr = [1, 2],
21 fallback = null,
22 ...canvasProps
23}) {
24 const containerRef = useRef(null);
25 // Start "in view" so every scene renders at least one real frame before
26 // a canvas below the fold can be paused — some WebGL backends show an
27 // opaque black canvas if frameloop="never" is set before anything has
28 // ever been drawn, instead of the expected transparent clear color.
29 const [inView, setInView] = useState(true);
30
31 useEffect(() => {
32 const el = containerRef.current;
33 if (!el) return;
34 const observer = new IntersectionObserver(
35 ([entry]) => setInView(entry.isIntersecting),
36 { threshold: 0.1 }
37 );
38 observer.observe(el);
39 return () => observer.disconnect();
40 }, []);
41
42 return (
43 <div ref={containerRef} className={className}>
44 <Canvas
45 dpr={dpr}
46 camera={camera}
47 frameloop={inView ? "always" : "never"}
48 gl={{ antialias: true, alpha: true, powerPreference: "high-performance" }}
49 onCreated={({ gl }) => gl.setClearColor(0x000000, 0)}
50 {...canvasProps}
51 >
52 <Suspense fallback={fallback}>{children}</Suspense>
53 </Canvas>
54 </div>
55 );
56}
57
58/**
59 * Applies the pointer position to `targetRef.rotation` every frame with
60 * spring-like inertia — driven entirely by useFrame, so it never triggers
61 * a React re-render.
62 */
63function usePointerParallax(targetRef, intensity = 0.3, damping = 0.08) {
64 const current = useRef({ x: 0, y: 0 });
65 const { pointer } = useThree();
66
67 useFrame(() => {
68 current.current.x += (pointer.x - current.current.x) * damping;
69 current.current.y += (pointer.y - current.current.y) * damping;
70
71 const obj = targetRef.current;
72 if (!obj) return;
73 obj.rotation.y = current.current.x * intensity;
74 obj.rotation.x = -current.current.y * intensity * 0.6;
75 });
76}
77
78// Fibonacci-sphere point distribution — even coverage without trig-heavy
79// per-frame work, computed once and reused by the BufferGeometry.
80function fibonacciSpherePositions(count, radius) {
81 const positions = new Float32Array(count * 3);
82 const golden = Math.PI * (3 - Math.sqrt(5));
83
84 for (let i = 0; i < count; i++) {
85 const y = 1 - (i / (count - 1)) * 2;
86 const r = Math.sqrt(1 - y * y);
87 const theta = golden * i;
88
89 positions[i * 3] = Math.cos(theta) * r * radius;
90 positions[i * 3 + 1] = y * radius;
91 positions[i * 3 + 2] = Math.sin(theta) * r * radius;
92 }
93
94 return positions;
95}
96
97function Cloud({ count, scale, primaryColor, secondaryColor, parallaxIntensity }) {
98 const groupRef = useRef(null);
99 const pointsRef = useRef(null);
100 usePointerParallax(groupRef, parallaxIntensity);
101
102 const { positions, colors } = useMemo(() => {
103 const radius = 1.6;
104 const positions = fibonacciSpherePositions(count, radius);
105 const colors = new Float32Array(count * 3);
106 const c1 = new THREE.Color(primaryColor);
107 const c2 = new THREE.Color(secondaryColor);
108 for (let i = 0; i < count; i++) {
109 const t = (positions[i * 3 + 1] / radius + 1) / 2;
110 const color = c1.clone().lerp(c2, t);
111 colors[i * 3] = color.r;
112 colors[i * 3 + 1] = color.g;
113 colors[i * 3 + 2] = color.b;
114 }
115 return { positions, colors };
116 }, [count, primaryColor, secondaryColor]);
117
118 useFrame((_, delta) => {
119 if (pointsRef.current) pointsRef.current.rotation.y += delta * 0.08;
120 });
121
122 return (
123 <group ref={groupRef} scale={scale}>
124 <points ref={pointsRef}>
125 <bufferGeometry>
126 <bufferAttribute attach="attributes-position" args={[positions, 3]} />
127 <bufferAttribute attach="attributes-color" args={[colors, 3]} />
128 </bufferGeometry>
129 <pointsMaterial size={0.055} vertexColors sizeAttenuation transparent opacity={0.9} />
130 </points>
131 </group>
132 );
133}
134
135/**
136 * A swirling point cloud on a fibonacci-sphere distribution — cheap to
137 * animate (one group rotation) while reading as a dense, organic field.
138 */
139export default function PointCloudScene({
140 scale = 1.3,
141 primaryColor = "#ffffff",
142 secondaryColor = "#8a8a8a",
143 count = 500,
144 parallaxIntensity = 0.3,
145 className = "",
146}) {
147 return (
148 <Canvas3DWrapper className={className} camera={{ position: [0, 0, 5], fov: 42 }}>
149 <Cloud count={count} scale={scale} primaryColor={primaryColor} secondaryColor={secondaryColor} parallaxIntensity={parallaxIntensity} />
150 </Canvas3DWrapper>
151 );
152}
03

Drop it in a page

Updated live with the settings above.

app/page.jsx
1import PointCloudScene from "@/components/PointCloudScene";
2
3export default function Section() {
4 return (
5 // The component fills this box: give it a position and a height
6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>
7 <PointCloudScene
8 scale={1.3}
9 count={500}
10 parallaxIntensity={0.3}
11 primaryColor="#ffffff"
12 secondaryColor="#8a8a8a"
13 />
14 </div>
15 );
16}
Next componentParticle Field→