Skip to content

Commit ca65f5b

Browse files
Update renderer.js
1 parent 3b6aa7f commit ca65f5b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

js/renderer.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1+
import * as THREE from 'three';
12

3+
class Renderer {
4+
constructor() {
5+
this.canvas = document.getElementById('gameCanvas');
6+
this.scene = new THREE.Scene();
7+
8+
this.camera = new THREE.PerspectiveCamera(
9+
75,
10+
window.innerWidth / window.innerHeight,
11+
0.1,
12+
1000
13+
);
14+
this.camera.position.z = 5;
15+
16+
this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas });
17+
this.resize();
18+
window.addEventListener('resize', () => this.resize());
19+
20+
// Add a basic cube to the scene
21+
const geometry = new THREE.BoxGeometry();
22+
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
23+
this.cube = new THREE.Mesh(geometry, material);
24+
this.scene.add(this.cube);
25+
}
26+
27+
resize() {
28+
this.renderer.setSize(window.innerWidth, window.innerHeight);
29+
this.camera.aspect = window.innerWidth / window.innerHeight;
30+
this.camera.updateProjectionMatrix();
31+
}
32+
33+
render() {
34+
this.cube.rotation.x += 0.01;
35+
this.cube.rotation.y += 0.01;
36+
37+
this.renderer.render(this.scene, this.camera);
38+
}
39+
}
40+
41+
export default Renderer;

0 commit comments

Comments
 (0)