
main.js
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const OUTER_RADIUS = 5;
const CIRCLE_COUNT = 100;
const CIRCLE_HEIGHT = 0.12;
const PARTICLE_POOL_SIZE = 2000;
const PARTICLES_PER_CLICK = 40;
const GRAVITY = -9.8;
const PRESS_DEPTH = 0.3;
const SPRING_STIFFNESS = 180;
const SPRING_DAMPING = 12;
const CLICK_MOVE_LIMIT = 5;
const SPIN_SPEED = 0.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc0c0a0);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
200
);
function fitCameraToCircle() {
const halfV = THREE.MathUtils.degToRad(camera.fov) / 2;
const halfH = Math.atan(Math.tan(halfV) * camera.aspect);
const distance = (OUTER_RADIUS * 1.2) / Math.sin(Math.min(halfV, halfH));
const direction = new THREE.Vector3(0, 0.6, 1).normalize();
camera.position.copy(direction.multiplyScalar(distance));
camera.lookAt(0, 0, 0);
}
fitCameraToCircle();
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.enableDamping = true;
controls.dampingFactor = 0.08;
controls.enableRotate = false;
controls.enablePan = false;
controls.enableZoom = true;
const initialDistance = camera.position.length();
controls.minDistance = initialDistance * 0.5;
controls.maxDistance = initialDistance * 1.8;
controls.update();
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5);
directionalLight.position.set(4, 8, 6);
scene.add(directionalLight);
function packCircles(count, outerR, density = 0.65, iterations = 600) {
const circles = [];
const weights = Array.from({ length: count }, () => 0.4 + Math.random() * 0.6);
const sumW2 = weights.reduce((sum, w) => sum + w * w, 0);
const scale = Math.sqrt((density * outerR * outerR) / sumW2);
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const dist = Math.sqrt(Math.random()) * outerR * 0.8;
circles.push({
x: Math.cos(angle) * dist,
z: Math.sin(angle) * dist,
r: weights[i] * scale,
});
}
for (let it = 0; it < iterations; it++) {
for (let i = 0; i < count; i++) {
for (let j = i + 1; j < count; j++) {
const a = circles[i];
const b = circles[j];
let dx = b.x - a.x;
let dz = b.z - a.z;
let dist = Math.hypot(dx, dz);
const minDist = a.r + b.r;
if (dist < minDist) {
if (dist < 1e-9) {
dx = Math.random() - 0.5;
dz = Math.random() - 0.5;
dist = Math.hypot(dx, dz);
}
const push = (minDist - dist) / 2;
const nx = dx / dist;
const nz = dz / dist;
a.x -= nx * push;
a.z -= nz * push;
b.x += nx * push;
b.z += nz * push;
}
}
}
for (const c of circles) {
const dist = Math.hypot(c.x, c.z);
const maxDist = outerR - c.r;
if (dist > maxDist) {
c.x *= maxDist / dist;
c.z *= maxDist / dist;
}
}
}
let k = 1;
for (let i = 0; i < count; i++) {
const a = circles[i];
k = Math.min(k, (outerR - Math.hypot(a.x, a.z)) / a.r);
for (let j = i + 1; j < count; j++) {
const b = circles[j];
const dist = Math.hypot(b.x - a.x, b.z - a.z);
k = Math.min(k, dist / (a.r + b.r));
}
}
const gapFactor = 0.96;
for (const c of circles) {
c.r *= k * gapFactor;
}
return circles;
}
const circleGroup = new THREE.Group();
scene.add(circleGroup);
const circleMeshes = [];
const packed = packCircles(CIRCLE_COUNT, OUTER_RADIUS);
for (const c of packed) {
const geometry = new THREE.CylinderGeometry(c.r, c.r, CIRCLE_HEIGHT, 48);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.4,
metalness: 0,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(c.x, CIRCLE_HEIGHT / 2, c.z);
mesh.userData.radius = c.r;
mesh.userData.spring = { offset: 0, velocity: 0 };
circleGroup.add(mesh);
circleMeshes.push(mesh);
}
const particlePositions = new Float32Array(PARTICLE_POOL_SIZE * 3);
const particleColors = new Float32Array(PARTICLE_POOL_SIZE * 3);
const positionAttr = new THREE.BufferAttribute(particlePositions, 3);
const colorAttr = new THREE.BufferAttribute(particleColors, 3);
positionAttr.setUsage(THREE.DynamicDrawUsage);
colorAttr.setUsage(THREE.DynamicDrawUsage);
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute('position', positionAttr);
particleGeometry.setAttribute('color', colorAttr);
const particleMaterial = new THREE.PointsMaterial({
size: 0.08,
vertexColors: true,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
});
const particlePoints = new THREE.Points(particleGeometry, particleMaterial);
scene.add(particlePoints);
const particlePool = Array.from({ length: PARTICLE_POOL_SIZE }, () => ({
alive: false,
x: 0, y: 0, z: 0,
vx: 0, vy: 0, vz: 0,
life: 0, maxLife: 1,
r: 1, g: 1, b: 1,
}));
let nextParticleIndex = 0;
function spawnParticles(mesh, color) {
const radius = mesh.userData.radius;
const center = mesh.getWorldPosition(new THREE.Vector3());
for (let n = 0; n < PARTICLES_PER_CLICK; n++) {
const p = particlePool[nextParticleIndex];
nextParticleIndex = (nextParticleIndex + 1) % PARTICLE_POOL_SIZE;
const angle = Math.random() * Math.PI * 2;
const dist = Math.sqrt(Math.random()) * radius * 0.9;
p.x = center.x + Math.cos(angle) * dist;
p.y = mesh.position.y + CIRCLE_HEIGHT / 2;
p.z = center.z + Math.sin(angle) * dist;
p.vx = (Math.random() - 0.5) * 0.6;
p.vz = (Math.random() - 0.5) * 0.6;
p.vy = 2.5 + Math.random() * 2;
p.life = 0;
p.maxLife = 0.6 + Math.random() * 0.5;
p.r = color.r;
p.g = color.g;
p.b = color.b;
p.alive = true;
}
}
function updateParticles(dt) {
for (let i = 0; i < PARTICLE_POOL_SIZE; i++) {
const p = particlePool[i];
const i3 = i * 3;
if (p.alive) {
p.life += dt;
if (p.life >= p.maxLife) {
p.alive = false;
} else {
p.vy += GRAVITY * dt;
p.x += p.vx * dt;
p.y += p.vy * dt;
p.z += p.vz * dt;
}
}
if (p.alive) {
const t = p.life / p.maxLife;
const fade = (1 - t) * (1 - t);
particlePositions[i3] = p.x;
particlePositions[i3 + 1] = p.y;
particlePositions[i3 + 2] = p.z;
particleColors[i3] = p.r * fade;
particleColors[i3 + 1] = p.g * fade;
particleColors[i3 + 2] = p.b * fade;
} else {
particleColors[i3] = 0;
particleColors[i3 + 1] = 0;
particleColors[i3 + 2] = 0;
}
}
positionAttr.needsUpdate = true;
colorAttr.needsUpdate = true;
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pressStart = new THREE.Vector2();
const animatingMeshes = new Set();
function pressCircle(mesh) {
const spring = mesh.userData.spring;
spring.offset = -PRESS_DEPTH;
spring.velocity = 0;
animatingMeshes.add(mesh);
}
function updateCirclePress(dt) {
for (const mesh of animatingMeshes) {
const spring = mesh.userData.spring;
const acceleration =
-SPRING_STIFFNESS * spring.offset - SPRING_DAMPING * spring.velocity;
spring.velocity += acceleration * dt;
spring.offset += spring.velocity * dt;
mesh.position.y = CIRCLE_HEIGHT / 2 + spring.offset;
if (Math.abs(spring.offset) < 1e-4 && Math.abs(spring.velocity) < 1e-3) {
spring.offset = 0;
spring.velocity = 0;
mesh.position.y = CIRCLE_HEIGHT / 2;
animatingMeshes.delete(mesh);
}
}
}
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function onPointerDown(event) {
pressStart.set(event.clientX, event.clientY);
}
function onPointerUp(event) {
const dx = event.clientX - pressStart.x;
const dy = event.clientY - pressStart.y;
if (Math.hypot(dx, dy) > CLICK_MOVE_LIMIT) return;
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(circleMeshes, false);
if (hits.length === 0) return;
const mesh = hits[0].object;
const newColor = new THREE.Color().setHSL(Math.random(), 0.9, 0.6);
mesh.material.color.copy(newColor);
pressCircle(mesh);
spawnParticles(mesh, newColor);
}
renderer.domElement.addEventListener('pointerdown', onPointerDown);
renderer.domElement.addEventListener('pointerup', onPointerUp);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize);
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
circleGroup.rotation.y += SPIN_SPEED * dt;
controls.update();
updateCirclePress(dt);
updateParticles(dt);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);