-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
449 lines (381 loc) · 14.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html>
<head>
<title>Ethan Chem Project</title>
<style>
body {
margin: 0;
background: #1a1a1a;
font-family: 'Segoe UI', Arial, sans-serif;
overflow: hidden;
}
canvas {
display: block;
}
#info {
position: absolute;
top: 10px;
width: 100%;
padding: 10px;
box-sizing: border-box;
text-align: center;
color: white;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
pointer-events: none;
z-index: 10;
}
.panel {
position: absolute;
color: white;
background: rgba(0, 0, 0, 0.8);
padding: 15px;
border-radius: 12px;
max-width: 300px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(5px);
transition: opacity 0.3s;
}
#controls {
bottom: 20px;
left: 20px;
}
#viewControls {
top: 20px;
right: 20px;
}
button {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
color: white;
padding: 8px 12px;
border-radius: 6px;
cursor: pointer;
transition: all 0.3s;
margin: 4px;
}
button:hover {
background: rgba(255, 255, 255, 0.2);
}
.tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.8);
color: white;
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
pointer-events: none;
z-index: 100;
display: none;
}
</style>
</head>
<body>
<div id="info">
<h1>Baking soda</h1>
<p>3D Visualization</p>
</div>
<div id="controls" class="panel">
<strong>Controls:</strong><br>
• Left-click + drag to rotate<br>
• Right-click + drag to pan<br>
• Scroll to zoom<br>
• Double-click to reset view<br>
• Hover over atoms for details<br>
<div class="button-group">
<button onclick="toggleRotation()">Toggle Rotation</button>
</div>
</div>
<div id="viewControls" class="panel">
<strong>View Options:</strong><br>
<button onclick="setViewMode('ball-stick')">Ball and Stick</button>
<button onclick="setViewMode('wireframe')">Wireframe</button>
</div>
<div class="tooltip"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
let isRotating = true;
let viewMode = 'ball-stick';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x1a1a1a);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 8;
const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(5, 5, 5);
directionalLight.castShadow = true;
scene.add(directionalLight);
const pointLight1 = new THREE.PointLight(0xffffff, 0.6);
pointLight1.position.set(-5, -5, -5);
scene.add(pointLight1);
// Raycaster for interaction
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const tooltip = document.querySelector('.tooltip');
function createAtom(color, radius, metallic = false, name = '', properties = {}) {
const geometry = new THREE.SphereGeometry(radius, 32, 32);
const material = new THREE.MeshPhysicalMaterial({
color: color,
metalness: metallic ? 0.8 : 0.1,
roughness: metallic ? 0.2 : 0.7,
clearcoat: metallic ? 0.8 : 0.3,
clearcoatRoughness: 0.2,
envMapIntensity: 1.0,
wireframe: false
});
const atom = new THREE.Mesh(geometry, material);
atom.castShadow = true;
atom.receiveShadow = true;
atom.userData = {
...properties,
name,
type: 'atom',
originalRadius: radius
};
return atom;
}
function createBond(start, end, isDouble = false) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const bondGroup = new THREE.Group();
// Gradient for bonds
const startColor = new THREE.Color(0x666666);
const endColor = new THREE.Color(0x444444);
if (isDouble) {
const offset = 0.12;
const perpendicular = new THREE.Vector3(direction.y, -direction.x, 0).normalize().multiplyScalar(offset);
const geometry = new THREE.CylinderGeometry(0.04, 0.04, length, 16, 8);
const colors = new Float32Array(geometry.attributes.position.count * 3);
const positions = geometry.attributes.position.array;
for (let i = 0; i < geometry.attributes.position.count; i++) {
const y = positions[i * 3 + 1];
const color = new THREE.Color().lerpColors(startColor, endColor, (y + length / 2) / length);
colors[i * 3] = color.r;
colors[i * 3 + 1] = color.g;
colors[i * 3 + 2] = color.b;
}
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
const material = new THREE.MeshPhysicalMaterial({
vertexColors: true,
metalness: 0.1,
roughness: 0.5,
clearcoat: 0.3,
wireframe: false
});
const bond1 = new THREE.Mesh(geometry, material);
const bond2 = new THREE.Mesh(geometry.clone(), material);
bond1.position.copy(start).add(perpendicular);
bond2.position.copy(start).sub(perpendicular);
bond1.position.lerp(end.clone().add(perpendicular), 0.5);
bond2.position.lerp(end.clone().sub(perpendicular), 0.5);
bond1.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), direction.normalize());
bond2.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), direction.normalize());
bondGroup.add(bond1);
bondGroup.add(bond2);
} else {
const geometry = new THREE.CylinderGeometry(0.06, 0.06, length, 16, 8);
const colors = new Float32Array(geometry.attributes.position.count * 3);
const positions = geometry.attributes.position.array;
for (let i = 0; i < geometry.attributes.position.count; i++) {
const y = positions[i * 3 + 1];
const color = new THREE.Color().lerpColors(startColor, endColor, (y + length / 2) / length);
colors[i * 3] = color.r;
colors[i * 3 + 1] = color.g;
colors[i * 3 + 2] = color.b;
}
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
const material = new THREE.MeshPhysicalMaterial({
vertexColors: true,
metalness: 0.1,
roughness: 0.5,
clearcoat: 0.3,
wireframe: false
});
const bond = new THREE.Mesh(geometry, material);
bond.position.copy(start).lerp(end, 0.5);
bond.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), direction.normalize());
bondGroup.add(bond);
}
bondGroup.castShadow = true;
bondGroup.receiveShadow = true;
return bondGroup;
}
const molecule = new THREE.Group();
const sodium = createAtom(0x9933FF, 1.02, true, 'Na⁺', {
fullName: 'Sodium',
charge: '+1',
atomicNumber: 11,
atomicWeight: '22.99 g/mol'
});
sodium.position.set(-3.0, -0.675, 0);
const carbon = createAtom(0x808080, 0.70, false, 'C', {
fullName: 'Carbon',
charge: '0',
atomicNumber: 6,
atomicWeight: '12.01 g/mol'
});
carbon.position.set(0, 0, 0);
const oxygen1 = createAtom(0xFF0000, 0.60, false, 'O', {
fullName: 'Oxygen',
charge: '0',
atomicNumber: 8,
atomicWeight: '16.00 g/mol'
});
oxygen1.position.set(0, 1.23, 0);
const oxygen2 = createAtom(0xFF0000, 0.60, false, 'O', {
fullName: 'Oxygen',
charge: '-1',
atomicNumber: 8,
atomicWeight: '16.00 g/mol'
});
oxygen2.position.set(-1.17, -0.675, 0);
const oxygen3 = createAtom(0xFF0000, 0.60, false, 'O', {
fullName: 'Oxygen',
charge: '0',
atomicNumber: 8,
atomicWeight: '16.00 g/mol'
});
oxygen3.position.set(1.17, -0.675, 0);
const hydrogen = createAtom(0xFFFFFF, 0.25, false, 'H', {
fullName: 'Hydrogen',
charge: '0',
atomicNumber: 1,
atomicWeight: '1.008 g/mol'
});
hydrogen.position.set(1.8, -1.1, 0);
const atoms = [sodium, carbon, oxygen1, oxygen2, oxygen3, hydrogen];
atoms.forEach(atom => molecule.add(atom));
// Create bonds
const bonds = [
createBond(carbon.position, oxygen1.position, true),
createBond(carbon.position, oxygen2.position),
createBond(carbon.position, oxygen3.position),
createBond(oxygen3.position, hydrogen.position),
createBond(sodium.position, oxygen2.position)
];
bonds.forEach(bond => molecule.add(bond));
scene.add(molecule);
// Interaction handling
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(atoms);
if (intersects.length > 0) {
const atom = intersects[0].object;
const userData = atom.userData;
tooltip.style.display = 'block';
tooltip.style.left = event.clientX + 10 + 'px';
tooltip.style.top = event.clientY + 10 + 'px';
tooltip.innerHTML = `
${userData.fullName} (${userData.name})<br>
Atomic Number: ${userData.atomicNumber}<br>
Atomic Weight: ${userData.atomicWeight}<br>
Charge: ${userData.charge}
`;
// Highlight atom
atom.material.emissive.setHex(0x666666);
} else {
tooltip.style.display = 'none';
atoms.forEach(atom => atom.material.emissive.setHex(0x000000));
}
}
// View mode functions
function setViewMode(mode) {
viewMode = mode;
atoms.forEach(atom => {
switch (mode) {
case 'ball-stick':
atom.scale.setScalar(1);
atom.material.wireframe = false;
break;
case 'wireframe':
atom.scale.setScalar(0.8);
atom.material.wireframe = true;
break;
}
});
bonds.forEach(bondGroup => {
bondGroup.children.forEach(bond => {
bond.material.wireframe = (mode === 'wireframe');
// Adjust bond visibility in wireframe mode
bond.material.opacity = mode === 'wireframe' ? 0.7 : 1;
bond.material.transparent = mode === 'wireframe';
});
});
}
// Animation controls
function toggleRotation() {
isRotating = !isRotating;
}
// Event listeners
document.addEventListener('mousemove', onMouseMove);
let mouseDown = false;
let rightMouseDown = false;
let mouseX = 0;
let mouseY = 0;
document.addEventListener('mousedown', (e) => {
if (e.button === 0) {
mouseDown = true;
} else if (e.button === 2) {
rightMouseDown = true;
}
mouseX = e.clientX;
mouseY = e.clientY;
});
document.addEventListener('mouseup', (e) => {
if (e.button === 0) {
mouseDown = false;
} else if (e.button === 2) {
rightMouseDown = false;
}
});
document.addEventListener('contextmenu', (e) => e.preventDefault());
document.addEventListener('mousemove', (e) => {
if (mouseDown) {
const deltaX = e.clientX - mouseX;
const deltaY = e.clientY - mouseY;
molecule.rotation.y += deltaX * 0.01;
molecule.rotation.x += deltaY * 0.01;
} else if (rightMouseDown) {
const deltaX = e.clientX - mouseX;
const deltaY = e.clientY - mouseY;
camera.position.x -= deltaX * 0.01;
camera.position.y += deltaY * 0.01;
}
mouseX = e.clientX;
mouseY = e.clientY;
});
document.addEventListener('dblclick', () => {
molecule.rotation.set(0, 0, 0);
camera.position.set(0, 0, 8);
});
document.addEventListener('wheel', (e) => {
camera.position.z += e.deltaY * 0.01;
camera.position.z = Math.max(4, Math.min(camera.position.z, 20));
});
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (isRotating && !mouseDown && !rightMouseDown) {
molecule.rotation.y += 0.005;
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>