Below is an example of how to create an interactive 3D heart using Three.js. In this example, we define a heart shape, extrude it into a 3D geometry, and then enable orbit controls so you can rotate and zoom the view.
You can copy the code below into an HTML file and open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>3D Heart in Three.js</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Include Three.js via CDN -->
<script src="https://cdn.jsdelivr.net/npm/three@0.149.0/build/three.min.js"></script>
<!-- Include OrbitControls -->
<script src="https://cdn.jsdelivr.net/npm/three@0.149.0/examples/js/controls/OrbitControls.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<script>
// Set up the scene, camera, and renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff); // white background
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(0, 0, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add OrbitControls so we can pan, rotate, and zoom the scene
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableZoom = true;
controls.enableRotate = true;
// Create a heart shape using bezier curves
const x = 0, y = 0;
const heartShape = new THREE.Shape();
heartShape.moveTo(x + 5, y + 5);
heartShape.bezierCurveTo(x + 5, y + 5, x + 4, y, x, y);
heartShape.bezierCurveTo(x - 6, y, x - 6, y + 7, x - 6, y + 7);
heartShape.bezierCurveTo(x - 6, y + 11, x - 3, y + 15, x + 5, y + 19);
heartShape.bezierCurveTo(x + 12, y + 15, x + 16, y + 11, x + 16, y + 7);
heartShape.bezierCurveTo(x + 16, y + 7, x + 16, y, x + 10, y);
heartShape.bezierCurveTo(x + 7, y, x + 5, y + 5, x + 5, y + 5);
// Extrude the heart shape into 3D geometry.
const extrudeSettings = {
steps: 2,
depth: 8,
bevelEnabled: true,
bevelThickness: 2,
bevelSize: 1,
bevelOffset: 0,
bevelSegments: 1,
};
const geometry = new THREE.ExtrudeGeometry(heartShape, extrudeSettings);
// Center the geometry around (0,0,0)
geometry.center();
// Create a mesh with a material. MeshNormalMaterial is used here for a colorful effect.
const material = new THREE.MeshNormalMaterial();
const heartMesh = new THREE.Mesh(geometry, material);
scene.add(heartMesh);
// Animate and render the scene
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
// Adjust the scene on window resize
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
</script>
</body>
</html>
How It Works
-
Scene, Camera, and Renderer Setup
A scene is created along with a perspective camera. The renderer is set to fill the window, and then appended to the DOM.
-
OrbitControls
The OrbitControls allow you to rotate, zoom, and pan the camera using your mouse or touch gestures.
-
Heart Shape Definition
A heart shape is defined using a series of moveTo and bezierCurveTo commands. This creates a 2D outline of a heart.
-
Extrude Geometry
The heart shape is extruded into 3D with the ExtrudeGeometry class. The extrude settings configure the depth and bevel options.
-
Mesh and Material
The extruded geometry is wrapped in a MeshNormalMaterial, which automatically colors the faces to give a lively appearance.
-
Animation Loop
The render loop constantly updates the scene (including the controls) and renders the view from the camera.
Open the HTML file in your browser, and you should see a 3D heart that you can rotate and zoom using your mouse. Enjoy experimenting with the code!