|
1 | 1 | import './style.css' //import the css file
|
2 | 2 | import * as THREE from 'three'; //import the three.js library
|
3 | 3 |
|
4 |
| -const scene = new THREE.Scene(); |
5 |
| -const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); |
| 4 | +const scene = new THREE.Scene(); //create a scene |
| 5 | +const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); //create a camera with perspective projection |
6 | 6 |
|
| 7 | +//create a renderer, with a canvas element |
7 | 8 | const renderer = new THREE.WebGLRenderer({
|
8 | 9 | canvas: document.querySelector('#bg'),
|
9 | 10 | });
|
10 | 11 |
|
11 | 12 | renderer.setSize(window.innerWidth, window.innerHeight); // set the size of the renderer to the window size
|
12 |
| -document.body.appendChild(renderer.domElement); // append the renderer's DOM element to the body |
13 |
| - |
14 | 13 | renderer.setClearColor(0x000000, 0); // set the renderer's background color to clear color
|
15 |
| - |
16 |
| - |
17 |
| -renderer.render(scene, camera); // render the scene with the camera |
18 |
| -renderer.setSize( window.innerWidth, window.innerHeight ); |
19 |
| -document.body.appendChild( renderer.domElement ) |
20 |
| - |
21 |
| - |
| 14 | +renderer.setSize( window.innerWidth, window.innerHeight ); //set the size of the renderer to the window size |
22 | 15 | renderer.setPixelRatio(window.devicePixelRatio); //set pixel ratio to device pixel ratio
|
23 |
| -renderer.setSize(window.innerWidth, window.innerHeight); //set size of renderer to window size, full screen canvas |
24 |
| - |
25 |
| -camera.position.setZ(30); //set camera position to 30 units back |
| 16 | +document.body.appendChild( renderer.domElement ) //append the renderer's DOM element to the body |
26 | 17 |
|
| 18 | +camera.position.z = 5; //set the camera's z position to 5 |
27 | 19 |
|
28 |
| -renderer.render(scene, camera); //render the scene with the camera |
29 | 20 |
|
30 | 21 | const geometry = new THREE.BoxGeometry( 1, 1, 1 );
|
31 | 22 | const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
|
32 | 23 | const cube = new THREE.Mesh( geometry, material );
|
33 | 24 | scene.add( cube );
|
34 | 25 |
|
35 |
| -camera.position.z = 5; |
| 26 | + |
36 | 27 | //we want to set up a recursive function to set up infinite loop to animate automatically
|
37 | 28 | function animate() {
|
38 | 29 | requestAnimationFrame(animate); //request animation frame to animate
|
|
0 commit comments