JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<script
src="https://cdn.jsdelivr.net/npm/es-module-shims/dist/es-module-shims.js"
defer
></script>
<script type="importmap">
{
"imports": {
"htm": "https://esm.sh/htm",
"three": "https://esm.sh/three@0.164.0",
"react": "https://esm.sh/react@18.2.0",
"client": "https://esm.sh/react-dom@18.2.0/client",
"fiber": "https://esm.sh/@react-three/fiber@8.15.11",
"drei": "https://esm.sh/@react-three/drei@9.80.10",
"rapier": "https://esm.sh/@react-three/rapier@1.0.0",
"postprocessing": "https://esm.sh/@react-three/postprocessing@2.14.10"
}
}
</script>
</head>
<body>
<div id="root"></div>
</body>
</html>
CSS
body {
margin: 0;
overflow: hidden;
cursor: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAACXBIWXMAAHUwAAB1MAHdM3LNAAABC0lEQVRIicXXMWoCQRiG4cfpIkRQrITcIHiLiLmBncdInVOIR/AEEXILyQUkYiMINkmZFJnBsCAZjbvzwlft7rz7bbHzT0seN3jEA+5xh168tsc73vCKF3xmrnuSLp6xxldm1vGZ7qXSKTZnCKvZxDXOYvYPYTWzXOniitKURZNNs5tPapSmTKrSWxwaEB+iS4jiJ3ROfYor0oku0MbuwgaXZId2wAj9OmtW6GMUMG5QmhgHDAuIhwGDAuJBcNxlmqQX/r6nHoKf/bRp9gHbAuJtwKqAeBWwLCBeUvCX+YF57R2PzKMTBbbF3xQZBBJFRp9EkWGvjubZ421iqsBAn6jtCNPKfIGrH9q+AR1lbyolkfH7AAAAAElFTkSuQmCC"),
auto;
}
#root {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
JavaScript
import htm from "htm";
import {
IcosahedronGeometry,
TextureLoader,
MeshStandardMaterial,
Vector3,
MathUtils
} from "three";
import React, { Suspense, useRef, useMemo } from "react";
import { createRoot } from "client";
import { Canvas, useFrame } from "fiber";
import { Environment } from "drei";
import { RigidBody, BallCollider, Physics } from "rapier";
import { EffectComposer, N8AO, SMAA } from "postprocessing";
///////////////////////////////////////////////
const html = htm.bind(React.createElement);
///////////////////////////////////////////////
const geometry = new IcosahedronGeometry(1, 16);
const material = new MeshStandardMaterial({
color: "white",
emissive: "hotpink",
metalness: 0,
roughness: 0.5,
envMapIntensity: 0.1
});
///////////////////////////////////////////////
const spheres = [...Array(69)].map(() => ({
scale: [0.66][Math.floor(Math.random())]
}));
///////////////////////////////////////////////
const Sphere = ({
vector = new Vector3(),
scale,
randomSpread = MathUtils.randFloatSpread
}) => {
const sphere = useRef();
useFrame((state, delta) => {
delta = Math.min(0.1, delta);
sphere.current.applyImpulse(
vector
.copy(sphere.current.translation())
.normalize()
.multiply({
x: -50 * delta * scale,
y: -150 * delta * scale,
z: -50 * delta * scale
})
);
});
return html`
<${RigidBody}
ref=${sphere}
linearDamping=${0.75}
angularDamping=${0.15}
friction=${0.2}
position=${[
randomSpread(20),
randomSpread(20) - 25,
randomSpread(20) - 10
]}
colliders=${false}
dispose=${null}
>
<mesh
castShadow
receiveShadow
scale=${scale}
geometry=${geometry}
material=${material}
/>
<${BallCollider} args=${[scale]} />
</${RigidBody}>`;
};
///////////////////////////////////////////////
const Pointer =...