Can you give me a complete code of HTML, CSS and Javascript to form a 5 tree 3d represeantation arranged in different spaces with its roots connected to each other under the ground. The roots are interconnected with each other and are of color red, blue and pink with buttons on side of red , blue and pink and clicking on respective buttons the respective roots gets highlighted while other becomes grey shaded.
Assistant
User
give me a code of a tree representation with proper branches , leaves and roots below the ground using html, css and javascript in separate files
Assistant
Certainly! Below is a complete example of a 3D scene with 5 trees arranged in space, connected by roots underground. The roots are colored red, blue, and pink, and there are three buttons to highlight the roots of each color while graying out others.
We'll use Three.js (a popular 3D JavaScript library) for rendering the 3D scene, and plain HTML/CSS/JS for the buttons and UI.
Features:
5 Trees with simple trunks and canopies
Roots network underground connecting trees
Roots colored red, blue, pink
Buttons to highlight respective roots while graying out others
How to run:
Save the code into an .html file and open in a modern browser.
You can rotate the scene with mouse drag (orbit controls).
Click the buttons on the right to highlight roots.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>3D Trees with Connected Roots</title>
<style>
body {
margin: 0;
overflow: hidden;
display: flex;
height: 100vh;
background: #eef2f5;
font-family: Arial, sans-serif;
user-select: none;
}
#container {
flex: 1;
position: relative;
}
#buttons {
width: 150px;
background: #fff;
border-left: 2px solid #ddd;
display: flex;
flex-direction: column;
padding: 20px;
box-sizing: border-box;
justify-content: center;
gap: 15px;
box-shadow: -2px 0 5px rgba(0,0,0,0.1);
}
button {
padding: 10px 20px;
border: none;
font-size: 1rem;
cursor: pointer;
border-radius: 5px;
background: #eee;
transition: background-color 0.3s;
}
button.active {
background-color: #4CAF50;
color: white;
font-weight: bold;
}
button:hover:not(.active) {
background-color: #ccc;
}
canvas {
display: block; /* remove inline gap */
}
</style>
</head>
<body>
<div id="container"></div>
<div id="buttons">
<button id="btn-red">Red Roots</button>
<button id="btn-blue">Blue Roots</button>
<button id="btn-pink">Pink Roots</button>
</div>
<!-- Three.js CDN -->
<script src="https://cdn.jsdelivr.net/npm/three@0.153.0/build/three.min.js"></script>
<!-- Orbit Controls -->
<script src="https://cdn.jsdelivr.net/npm/three@0.153.0/examples/js/controls/OrbitControls.min.js"></script>
<script>
// === Basic Setup ===
const container = document.getElementById('container');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeceff1);
const camera = new THREE.PerspectiveCamera(45, container.clientWidth / container.clientHeight, 0.1, 1000);
camera.position.set(0, 10, 20);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
window.addEventListener('resize', () => {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
});
// === Controls ===
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.minDistance = 5;
controls.maxDistance = 50;
controls.target.set(0, 3, 0);
controls.update();
// === Lights ===
const ambient = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambient);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 10);
scene.add(directionalLight);
// === Ground ===
const groundGeo = new THREE.PlaneGeometry(50, 50);
const groundMat = new THREE.MeshStandardMaterial({ color: 0x4a6f2b, side: THREE.DoubleSide });
const ground = new THREE.Mesh(groundGeo, groundMat);
ground.rotation.x = -Math.PI / 2;
ground.position.y = 0;
scene.add(ground);
// === Tree Data ===
// Positions of 5 trees distributed in space
const treePositions = [
new THREE.Vector3(-7, 0, -3),
new THREE.Vector3(-3, 0, 6),
new THREE.Vector3(3, 0, 5),
new THREE.Vector3(7, 0, -2),
new THREE.Vector3(0, 0, -7),
];
// Function to create a simple tree: trunk + canopy
function createTree() {
const tree = new THREE.Group();
// Trunk
const trunkGeo = new THREE.CylinderGeometry(0.2, 0.3, 3, 8);
const trunkMat = new THREE.MeshStandardMaterial({ color: 0x6b4423 });
const trunk = new THREE.Mesh(trunkGeo, trunkMat);
trunk.position.y = 1.5;
tree.add(trunk);
// Canopy (a sphere)
const canopyGeo = new THREE.SphereGeometry(1.5, 16, 12);
const canopyMat = new THREE.MeshStandardMaterial({ color: 0x267326 });
const canopy = new THREE.Mesh(canopyGeo, canopyMat);
canopy.position.y = 4;
tree.add(canopy);
return tree;
}
// Add trees at positions
const trees = [];
for (let pos of treePositions) {
const tree = createTree();
tree.position.copy(pos);
scene.add(tree);
trees.push(tree);
}
// === Roots ===
// Roots will be lines underground (y < 0), connecting tree bases.
// Color scheme: red, blue, pink roots connecting specific trees.
// For simplicity, we'll arrange:
// red roots connect trees 0,1,4
// blue roots connect trees 1,2,3
// pink roots connect trees 3,4,0
/*
Tree indices:
0: (-7,0,-3)
1: (-3,0,6)
2: (3,0,5)
3: (7,0,-2)
4: (0,0,-7)
*/
// Helper to make underground offset of a point, y fixed underground (like -1)
function underground(point) {
return new THREE.Vector3(point.x, -1, point.z);
}
// Create line geometry for root connections
function createRootLine(points, color) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color: color, linewidth: 3 });
const line = new THREE.Line(geometry, material);
return line;
}
// For interconnected roots, we will create smooth curves using CatmullRomCurve3
function createRootCurve(points, color) {
const curve = new THREE.CatmullRomCurve3(points);
const pointsCurve = curve.getPoints(50);
const geometry = new THREE.BufferGeometry().setFromPoints(pointsCurve);
const material = new THREE.LineBasicMaterial({ color: color, linewidth: 5 });
const line = new THREE.Line(geometry, material);
return line;
}
// Roots groups
const rootsGroup = new THREE.Group();
scene.add(rootsGroup);
// Red roots: connect trees 0,1,4 underground
const redPoints = [
underground(treePositions[0]),
new THREE.Vector3(-5, -1, 1), // mid control point for smooth curve
underground(treePositions[1]),
new THREE.Vector3(-1.5, -1, 0),
underground(treePositions[4]),
underground(treePositions[0]) // loop back to 0
];
const redRoots = createRootCurve(redPoints, 0xff0000);
redRoots.name = "redRoots";
rootsGroup.add(redRoots);
// Blue roots: connect trees 1,2,3 underground
const bluePoints = [
underground(treePositions[1]),
new THREE.Vector3(0, -1, 5),
underground(treePositions[2]),
new THREE.Vector3(5, -1, 1),
underground(treePositions[3]),
underground(treePositions[1]) // loop back
];
const blueRoots = createRootCurve(bluePoints, 0x0000ff);
blueRoots.name = "blueRoots";
rootsGroup.add(blueRoots);
// Pink roots: connect trees 3,4,0 underground
const pinkPoints = [
underground(treePositions[3]),
new THREE.Vector3(3, -1, -4),
underground(treePositions[4]),
new THREE.Vector3(-4, -1, -5),
underground(treePositions[0]),
underground(treePositions[3]) // loop back
];
const pinkRoots = createRootCurve(pinkPoints, 0xff69b4); // hot pink color
pinkRoots.name = "pinkRoots";
rootsGroup.add(pinkRoots);
// === Highlighting roots ===
// Buttons and logic to highlight the respective root and grey out others
const btnRed = document.getElementById('btn-red');
const btnBlue = document.getElementById('btn-blue');
const btnPink = document.getElementById('btn-pink');
const roots = {
red: redRoots,
blue: blueRoots,
pink: pinkRoots
};
// Set initial state - no highlight (all normal colors)
// We'll create copies of original colors
const originalColors = {
red: new THREE.Color(0xff0000),
blue: new THREE.Color(0x0000ff),
pink: new THREE.Color(0xff69b4),
grey: new THREE.Color(0x888888)
};
function highlightColor(colorKey) {
for (let key in roots) {
if (key === colorKey) {
roots[key].material.color.copy(originalColors[key]);
roots[key].material.opacity = 1;
roots[key].material.transparent = false;
} else {
roots[key].material.color.copy(originalColors.grey);
roots[key].material.opacity = 0.4;
roots[key].material.transparent = true;
}
}
// Update button styles
btnRed.classList.toggle('active', colorKey === 'red');
btnBlue.classList.toggle('active', colorKey === 'blue');
btnPink.classList.toggle('active', colorKey === 'pink');
}
// Reset highlight (all colored normally)
function resetHighlight() {
for (let key in roots) {
roots[key].material.color.copy(originalColors[key]);
roots[key].material.opacity = 1;
roots[key].material.transparent = false;
}
btnRed.classList.remove('active');
btnBlue.classList.remove('active');
btnPink.classList.remove('active');
}
// Button listeners to toggle highlight
// If already active, reset
btnRed.addEventListener('click', () => {
if (btnRed.classList.contains('active')) {
resetHighlight();
} else {
highlightColor('red');
}
});
btnBlue.addEventListener('click', () => {
if (btnBlue.classList.contains('active')) {
resetHighlight();
} else {
highlightColor('blue');
}
});
btnPink.addEventListener('click', () => {
if (btnPink.classList.contains('active')) {
resetHighlight();
} else {
highlightColor('pink');
}
});
// === Animate ===
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
Explanation:
Trees: simple cylinders + spheres for trunks and canopy.
Roots: 3 groups of CatmullRom curved lines underground (y = -1).
Colors: red, blue, pink roots.
Buttons: toggle highlighting of roots, gray out other colors using opacity and color change.
OrbitControls: allow camera rotation.
Responsive canvas resizing.
Try this out and let me know if you'd like to add more details (like animated leaves, textured trunks, or more complex roots)!
Certainly! Below is a complete example of a tree representation with proper branches, leaves, and roots below ground, using separate HTML, CSS, and JavaScript files.
The tree is drawn using HTML Canvas (2D), which lets us draw branches with recursion, leaves as circles, and roots below a "ground line."
How It Works
Branches: drawn recursively with lines that split and shrink.
Leaves: randomly placed small green circles on branch tips.
Roots: similar structure as branches but inverted under the ground line.
Ground: a brown horizontal line separating above and underground.