1
1
import './style.css' //import the css file
2
2
import * as THREE from 'three' ; //import the three.js library
3
3
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
5
4
6
5
7
6
const scene = new THREE . Scene ( ) ; //create a scene
@@ -12,33 +11,6 @@ const renderer = new THREE.WebGLRenderer({
12
11
canvas : document . querySelector ( '#bg' ) ,
13
12
} ) ;
14
13
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
-
42
14
43
15
function addStar ( ) {
44
16
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() {
51
23
scene . add ( star ) ; //add the star to the scene
52
24
}
53
25
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 ) ;
72
26
73
27
function onWindowResize ( ) {
74
28
75
- camera . aspect = window . innerWidth / window . innerHeight ;
76
- camera . updateProjectionMatrix ( ) ;
29
+ camera . aspect = window . innerWidth / window . innerHeight ;
30
+ camera . updateProjectionMatrix ( ) ;
77
31
78
- renderer . setSize ( window . innerWidth , window . innerHeight ) ;
32
+ renderer . setSize ( window . innerWidth , window . innerHeight ) ;
79
33
80
34
}
81
35
82
36
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
+ } ) ;
94
43
}
95
44
45
+ function moveCamera ( ) {
46
+ const t = document . body . getBoundingClientRect ( ) . top ;
47
+
48
+ camera . position . z = t * - 0.01 ;
96
49
50
+ }
97
51
//we want to set up a recursive function to set up infinite loop to animate automatically
98
52
function animate ( ) {
99
53
requestAnimationFrame ( animate ) ;
100
54
animateStars ( ) ; // call the animateStars function to animate the stars
101
55
renderer . render ( scene , camera ) ;
102
- }
56
+ }
57
+
58
+
59
+
60
+ function main ( ) {
103
61
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 ) ;
104
73
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
105
76
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
+ }
106
88
107
- animate ( ) ; //call the animate function
89
+ main ( ) ; //call the main function
0 commit comments