Skip to content

Commit c6371a0

Browse files
committedOct 2, 2024
working black background
1 parent 4473ea3 commit c6371a0

File tree

1 file changed

+44
-62
lines changed

1 file changed

+44
-62
lines changed
 

‎main.js

+44-62
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import './style.css' //import the css file
22
import * as THREE from 'three'; //import the three.js library
33
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; //import the orbit controls from three.js
4-
import imgUrl from './space.jpg' //have to import the path for the image, since once we build it will be a different url
54

65

76
const scene = new THREE.Scene(); //create a scene
@@ -12,33 +11,6 @@ const renderer = new THREE.WebGLRenderer({
1211
canvas: document.querySelector('#bg'),
1312
});
1413

15-
renderer.setSize(window.innerWidth, window.innerHeight); // set the size of the renderer to the window size
16-
renderer.setSize( window.innerWidth, window.innerHeight ); //set the size of the renderer to the window size
17-
renderer.setPixelRatio(window.devicePixelRatio); //set pixel ratio to device pixel ratio
18-
document.body.appendChild( renderer.domElement ) //append the renderer's DOM element to the body
19-
20-
camera.position.z = 10; //set the camera's z position to 5
21-
camera.position.x = 0; //set the camera's z position to 5
22-
23-
24-
25-
26-
//create an ambient light
27-
const ambientLight = new THREE.AmbientLight(0xffffff, 10); //ambient light, with color white and intensity 0.5
28-
scene.add(ambientLight);
29-
30-
/*
31-
//create a directional light helper shows where the light is in the scene
32-
const lighthelper = new THREE.DirectionalLightHelper(light, 1); //create a directional light helper
33-
scene.add(lighthelper); //add the light helper to the scene
34-
35-
//create a grid helper, to show the grid in the scene
36-
const gridHelper = new THREE.GridHelper(200, 50); //create a grid helper
37-
scene.add(gridHelper); //add the grid helper to the scene
38-
*/
39-
//create an orbit controls, allows us to move around the scene
40-
const controls = new OrbitControls(camera, renderer.domElement); //create orbit controls with camera and renderer's DOM element
41-
4214

4315
function addStar() {
4416
const geometry = new THREE.OctahedronGeometry(Math.random() * 0.5 + 0.1, 0); //create a sphere geometry with random size
@@ -51,57 +23,67 @@ function addStar() {
5123
scene.add(star); //add the star to the scene
5224
}
5325

54-
Array(500).fill().forEach(addStar); //create array of 200, call to create 200 stars in the scene
55-
56-
//create a background texture
57-
var bgTexture = new THREE.TextureLoader().load(imgUrl);
58-
bgTexture.minFilter = THREE.LinearFilter;
59-
scene.background = bgTexture;
60-
61-
function moveCamera() {
62-
const t = document.body.getBoundingClientRect().top;
63-
64-
camera.position.z = t * -0.01;
65-
66-
}
67-
68-
document.body.onscroll = moveCamera;
69-
moveCamera();
70-
71-
window.addEventListener( 'resize', onWindowResize, false );
7226

7327
function onWindowResize(){
7428

75-
camera.aspect = window.innerWidth / window.innerHeight;
76-
camera.updateProjectionMatrix();
29+
camera.aspect = window.innerWidth / window.innerHeight;
30+
camera.updateProjectionMatrix();
7731

78-
renderer.setSize( window.innerWidth, window.innerHeight );
32+
renderer.setSize( window.innerWidth, window.innerHeight );
7933

8034
}
8135

8236
function animateStars() {
83-
scene.traverse((object) => {
84-
if (object.isMesh ) {
85-
object.rotation.y += 0.01; // rotate the star around the y-axis
86-
object.rotation.x += 0.02; // rotate the star around the y-axis
87-
/*
88-
object.position.y += Math.random() * 0.01 - 0.005; // float the star in a random direction
89-
object.position.x += Math.random() * 0.01 - 0.005; // float the star in a random direction
90-
object.position.z += Math.random() * 0.01 - 0.005; // float the star in a random direction
91-
*/
92-
}
93-
});
37+
scene.traverse((object) => {
38+
if (object.isMesh ) {
39+
object.rotation.y += 0.01; // rotate the star around the y-axis
40+
object.rotation.x += 0.02; // rotate the star around the y-axis
41+
}
42+
});
9443
}
9544

45+
function moveCamera() {
46+
const t = document.body.getBoundingClientRect().top;
47+
48+
camera.position.z = t * -0.01;
9649

50+
}
9751
//we want to set up a recursive function to set up infinite loop to animate automatically
9852
function animate() {
9953
requestAnimationFrame(animate);
10054
animateStars(); // call the animateStars function to animate the stars
10155
renderer.render(scene, camera);
102-
}
56+
}
57+
58+
59+
60+
function main() {
10361

62+
renderer.setSize(window.innerWidth, window.innerHeight); // set the size of the renderer to the window size
63+
renderer.setSize( window.innerWidth, window.innerHeight ); //set the size of the renderer to the window size
64+
renderer.setPixelRatio(window.devicePixelRatio); //set pixel ratio to device pixel ratio
65+
document.body.appendChild( renderer.domElement ) //append the renderer's DOM element to the body
66+
67+
camera.position.z = 10; //set the camera's z position to 5
68+
camera.position.x = 0; //set the camera's z position to 5
69+
70+
//create an ambient light
71+
const ambientLight = new THREE.AmbientLight(0xffffff, 10); //ambient light, with color white and intensity 0.5
72+
scene.add(ambientLight);
10473

74+
//create an orbit controls, allows us to move around the scene
75+
const controls = new OrbitControls(camera, renderer.domElement); //create orbit controls with camera and renderer's DOM element
10576

77+
Array(500).fill().forEach(addStar); //create array of 200, call to create 200 stars in the scene
78+
79+
scene.background = new THREE.Color(0x000000); // Set background to solid black
80+
81+
document.body.onscroll = moveCamera;
82+
moveCamera();
83+
84+
window.addEventListener( 'resize', onWindowResize, false );
85+
86+
animate(); //call the animate function
87+
}
10688

107-
animate(); //call the animate function
89+
main(); //call the main function

0 commit comments

Comments
 (0)
Please sign in to comment.