Changed around line 1
+ class Scene3D {
+ constructor() {
+ this.canvas = document.getElementById('renderCanvas');
+ this.gl = this.canvas.getContext('webgl');
+ this.objects = [];
+ this.selectedObject = null;
+ this.isDragging = false;
+ this.lastMouseX = 0;
+ this.lastMouseY = 0;
+
+ if (!this.gl) {
+ alert('WebGL not supported');
+ return;
+ }
+
+ this.initGL();
+ this.setupEventListeners();
+ this.animate();
+ }
+
+ initGL() {
+ this.gl.clearColor(1.0, 1.0, 1.0, 1.0);
+ this.gl.enable(this.gl.DEPTH_TEST);
+ this.resizeCanvas();
+ }
+
+ resizeCanvas() {
+ const container = this.canvas.parentElement;
+ this.canvas.width = container.clientWidth;
+ this.canvas.height = container.clientHeight;
+ this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
+ }
+
+ setupEventListeners() {
+ window.addEventListener('resize', () => this.resizeCanvas());
+
+ document.getElementById('addCube').addEventListener('click', () => this.addObject('cube'));
+ document.getElementById('addSphere').addEventListener('click', () => this.addObject('sphere'));
+ document.getElementById('addCylinder').addEventListener('click', () => this.addObject('cylinder'));
+
+ this.canvas.addEventListener('mousedown', (e) => this.onMouseDown(e));
+ this.canvas.addEventListener('mousemove', (e) => this.onMouseMove(e));
+ this.canvas.addEventListener('mouseup', () => this.onMouseUp());
+
+ document.addEventListener('keydown', (e) => this.handleKeyPress(e));
+ }
+
+ addObject(type) {
+ const object = {
+ type: type,
+ position: [0, 0, 0],
+ rotation: [0, 0, 0],
+ scale: [1, 1, 1]
+ };
+
+ this.objects.push(object);
+ this.selectedObject = object;
+ }
+
+ handleKeyPress(e) {
+ if (!this.selectedObject) return;
+
+ const speed = 0.1;
+ switch(e.key) {
+ case 'ArrowUp':
+ this.selectedObject.position[2] -= speed;
+ break;
+ case 'ArrowDown':
+ this.selectedObject.position[2] += speed;
+ break;
+ case 'ArrowLeft':
+ this.selectedObject.position[0] -= speed;
+ break;
+ case 'ArrowRight':
+ this.selectedObject.position[0] += speed;
+ break;
+ }
+ }
+
+ onMouseDown(e) {
+ this.isDragging = true;
+ this.lastMouseX = e.clientX;
+ this.lastMouseY = e.clientY;
+ }
+
+ onMouseMove(e) {
+ if (!this.isDragging || !this.selectedObject) return;
+
+ const deltaX = e.clientX - this.lastMouseX;
+ const deltaY = e.clientY - this.lastMouseY;
+
+ this.selectedObject.rotation[1] += deltaX * 0.01;
+ this.selectedObject.rotation[0] += deltaY * 0.01;
+
+ this.lastMouseX = e.clientX;
+ this.lastMouseY = e.clientY;
+ }
+
+ onMouseUp() {
+ this.isDragging = false;
+ }
+
+ animate() {
+ this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
+
+ // Render objects here
+ this.objects.forEach(obj => {
+ // Implement WebGL rendering for each object type
+ });
+
+ requestAnimationFrame(() => this.animate());
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new Scene3D();
+ });