broadfield-dev commited on
Commit
5a59d0c
·
verified ·
1 Parent(s): 32d34db

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +125 -98
index.html CHANGED
@@ -3,122 +3,149 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>3D Solar System with NEOs</title>
7
  <style>
8
- body { margin: 0; overflow: hidden; }
9
- canvas { width: 100%; height: 100%; }
 
 
 
10
  </style>
11
  </head>
12
  <body>
13
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
14
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
15
  <script>
16
- // Scene setup
17
- const scene = new THREE.Scene();
18
- const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
19
- const renderer = new THREE.WebGLRenderer();
20
- renderer.setSize(window.innerWidth, window.innerHeight);
21
- document.body.appendChild(renderer.domElement);
22
-
23
- // Orbit controls for camera movement
24
- const controls = new THREE.OrbitControls(camera, renderer.domElement);
25
- camera.position.set(0, 20, 100);
26
- controls.update();
27
-
28
- // Lights
29
- const ambientLight = new THREE.AmbientLight(0x404040);
30
- scene.add(ambientLight);
31
- const pointLight = new THREE.PointLight(0xffffff);
32
- pointLight.position.set(0, 100, 100);
33
- scene.add(pointLight);
34
-
35
- // Sun
36
- const sunGeometry = new THREE.SphereGeometry(5, 32, 32);
37
- const sunMaterial = new THREE.MeshBasicMaterial({ color: 0xffcc00 });
38
- const sun = new THREE.Mesh(sunGeometry, sunMaterial);
39
- scene.add(sun);
40
-
41
- // Planets with more realistic orbital speeds
42
- const planets = [
43
- { name: 'Mercury', distance: 10, scale: 0.2, speed: 0.00582 }, // Fastest orbit
44
- { name: 'Venus', distance: 15, scale: 0.4, speed: 0.00224 },
45
- { name: 'Earth', distance: 20, scale: 0.5, speed: 0.00199 },
46
- { name: 'Mars', distance: 25, scale: 0.3, speed: 0.00106 },
47
- { name: 'Jupiter', distance: 35, scale: 2, speed: 0.00028 },
48
- { name: 'Saturn', distance: 45, scale: 1.5, speed: 0.00013 },
49
- { name: 'Uranus', distance: 55, scale: 1, speed: 0.00006 },
50
- { name: 'Neptune', distance: 65, scale: 1, speed: 0.00004 } // Slowest orbit
51
- ];
52
-
53
- planets.forEach(planet => {
54
- const geometry = new THREE.SphereGeometry(planet.scale, 32, 32);
55
- const material = new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff });
56
- const mesh = new THREE.Mesh(geometry, material);
57
- mesh.position.x = planet.distance;
58
- scene.add(mesh);
59
- });
60
-
61
- // NEOs (fictional for now, will update with real data)
62
- let neos = [];
63
- for (let i = 0; i < 10; i++) {
64
- const geometry = new THREE.SphereGeometry(0.1, 32, 32);
65
- const material = new THREE.MeshBasicMaterial({ color: 0xcccccc });
66
- const neo = new THREE.Mesh(geometry, material);
67
- neo.position.set(Math.random() * 30 - 15, Math.random() * 10 - 5, Math.random() * 30 - 15);
68
- scene.add(neo);
69
- neos.push(neo);
70
- }
71
 
72
- // Animation with real-time movement
73
- let time = 0;
74
- function animate() {
75
- requestAnimationFrame(animate);
 
 
 
 
 
 
76
 
77
- time += 0.05; // Adjust this value for speed of animation
 
 
 
 
 
 
78
 
79
- planets.forEach((planet, index) => {
80
- const planetMesh = scene.children[3 + index];
81
- const angle = time * planet.speed; // Use speed to set individual movement speeds
82
- planetMesh.position.x = Math.cos(angle) * planet.distance;
83
- planetMesh.position.z = Math.sin(angle) * planet.distance;
84
- });
85
 
86
- neos.forEach(neo => {
87
- neo.position.x += Math.sin(time * 0.001) * 0.1;
88
- neo.position.z += Math.cos(time * 0.001) * 0.1;
89
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- controls.update();
92
- renderer.render(scene, camera);
 
93
  }
94
 
95
- animate();
 
 
 
 
 
 
 
 
96
 
97
- // Fetching NEO data from NASA API
98
- function fetchNEOData() {
99
- fetch('https://api.nasa.gov/neo/rest/v1/feed?start_date=2025-02-18&end_date=2025-02-19&api_key=DEMO_KEY')
100
- .then(response => response.json())
101
- .then(data => {
102
- const neoData = data.near_earth_objects['2025-02-18'];
103
- if (neoData) {
104
- neos.forEach((neo, index) => {
105
- if (index < neoData.length) {
106
- const obj = neoData[index];
107
- // Simplified NEO positioning based on miss distance
108
- neo.position.set(
109
- obj.close_approach_data[0].miss_distance.astronomical * 100 - 50,
110
- obj.close_approach_data[0].miss_distance.astronomical * 50,
111
- obj.close_approach_data[0].miss_distance.astronomical * 100 - 50
112
- );
113
- }
114
  });
115
  }
116
- }).catch(error => console.error('Error fetching NEO data:', error));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  }
118
 
119
- fetchNEOData(); // Initial fetch
120
- setInterval(fetchNEOData, 3600000); // Update every hour
 
 
121
 
 
 
 
 
 
 
 
 
 
 
122
  </script>
123
  </body>
124
  </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Balls in Rotating Hexagon</title>
7
  <style>
8
+ canvas {
9
+ border: 1px solid black;
10
+ display: block;
11
+ margin: auto;
12
+ }
13
  </style>
14
  </head>
15
  <body>
16
+ <canvas id="myCanvas" width="800" height="600"></canvas>
17
+
18
  <script>
19
+ const canvas = document.getElementById('myCanvas');
20
+ const ctx = canvas.getContext('2d');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ // Ball class
23
+ class Ball {
24
+ constructor(x, y, radius, color) {
25
+ this.x = x;
26
+ this.y = y;
27
+ this.radius = radius;
28
+ this.color = color;
29
+ this.vx = Math.random() * 2 - 1;
30
+ this.vy = Math.random() * 2 - 1;
31
+ }
32
 
33
+ draw() {
34
+ ctx.beginPath();
35
+ ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
36
+ ctx.fillStyle = this.color;
37
+ ctx.fill();
38
+ ctx.closePath();
39
+ }
40
 
41
+ move() {
42
+ this.x += this.vx;
43
+ this.y += this.vy;
44
+ this.vy += 0.05; // gravity
45
+ }
 
46
 
47
+ collideWithWall(hexagon) {
48
+ const vertices = hexagon.vertices;
49
+ for (let i = 0; i < vertices.length; i++) {
50
+ const p1 = vertices[i];
51
+ const p2 = vertices[(i + 1) % vertices.length];
52
+ if (this.lineCircle(p1.x, p1.y, p2.x, p2.y, this.x, this.y, this.radius)) {
53
+ const normal = {x: p2.y - p1.y, y: p1.x - p2.x};
54
+ const distance = Math.sqrt(normal.x * normal.x + normal.y * normal.y);
55
+ normal.x /= distance;
56
+ normal.y /= distance;
57
+
58
+ const dotProduct = this.vx * normal.x + this.vy * normal.y;
59
+ this.vx -= 2 * dotProduct * normal.x;
60
+ this.vy -= 2 * dotProduct * normal.y;
61
+ // Move the ball outside the wall to avoid sticking
62
+ this.x += this.vx;
63
+ this.y += this.vy;
64
+ return;
65
+ }
66
+ }
67
+ }
68
+
69
+ lineCircle(x1, y1, x2, y2, cx, cy, r) {
70
+ const lineLength = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
71
+ const dot = (((cx - x1) * (x2 - x1)) + ((cy - y1) * (y2 - y1))) / Math.pow(lineLength, 2);
72
+ const closestX = x1 + (dot * (x2 - x1));
73
+ const closestY = y1 + (dot * (y2 - y1));
74
+ if (this.distance(closestX, closestY, x1, y1) > lineLength || this.distance(closestX, closestY, x2, y2) > lineLength) {
75
+ return false;
76
+ }
77
+ const distance = this.distance(closestX, closestY, cx, cy);
78
+ return distance <= r;
79
+ }
80
 
81
+ distance(x1, y1, x2, y2) {
82
+ return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
83
+ }
84
  }
85
 
86
+ // Hexagon class
87
+ class Hexagon {
88
+ constructor(centerX, centerY, radius) {
89
+ this.centerX = centerX;
90
+ this.centerY = centerY;
91
+ this.radius = radius;
92
+ this.rotation = 0;
93
+ this.vertices = this.calculateVertices();
94
+ }
95
 
96
+ calculateVertices() {
97
+ const vertices = [];
98
+ for (let i = 0; i < 6; i++) {
99
+ const angle = Math.PI / 3 * i + this.rotation;
100
+ vertices.push({
101
+ x: this.centerX + this.radius * Math.cos(angle),
102
+ y: this.centerY + this.radius * Math.sin(angle)
 
 
 
 
 
 
 
 
 
 
103
  });
104
  }
105
+ return vertices;
106
+ }
107
+
108
+ draw() {
109
+ ctx.beginPath();
110
+ for (let vertex of this.vertices) {
111
+ ctx.lineTo(vertex.x, vertex.y);
112
+ }
113
+ ctx.closePath();
114
+ ctx.strokeStyle = "black";
115
+ ctx.stroke();
116
+ }
117
+
118
+ update() {
119
+ this.rotation += 0.01;
120
+ this.vertices = this.calculateVertices();
121
+ }
122
+ }
123
+
124
+ const hexagon = new Hexagon(canvas.width / 2, canvas.height / 2, 200);
125
+ const balls = [];
126
+ const colors = ["red", "blue", "green", "yellow", "purple", "orange", "pink", "brown", "cyan", "magenta"];
127
+
128
+ for (let i = 0; i < 10; i++) {
129
+ const x = canvas.width / 2 + Math.random() * 100 - 50;
130
+ const y = canvas.height / 2 + Math.random() * 100 - 50;
131
+ balls.push(new Ball(x, y, 10, colors[i]));
132
  }
133
 
134
+ function animate() {
135
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
136
+ hexagon.update();
137
+ hexagon.draw();
138
 
139
+ for (let ball of balls) {
140
+ ball.move();
141
+ ball.collideWithWall(hexagon);
142
+ ball.draw();
143
+ }
144
+
145
+ requestAnimationFrame(animate);
146
+ }
147
+
148
+ animate();
149
  </script>
150
  </body>
151
  </html>