Changed around line 1
- const cube = document.getElementById('cube');
- let posX = 0, posY = 0;
-
- document.addEventListener('keydown', (event) => {
- const step = 10;
- switch (event.key) {
- case 'ArrowUp':
- posY -= step;
- break;
- case 'ArrowDown':
- posY += step;
- break;
- case 'ArrowLeft':
- posX -= step;
- break;
- case 'ArrowRight':
- posX += step;
- break;
+ class Scene3D {
+ constructor() {
+ this.canvas = document.getElementById('renderCanvas');
+ this.gl = this.canvas.getContext('webgl');
+ this.objects = [];
+ this.activeObject = null;
+ this.init();
- cube.style.transform = `translate3d(${posX}px, ${posY}px, 0) rotateX(20deg) rotateY(30deg)`;
- });
+
+ init() {
+ if (!this.gl) {
+ alert('WebGL not supported');
+ return;
+ }
+
+ this.resizeCanvas();
+ window.addEventListener('resize', () => this.resizeCanvas());
+ this.setupControls();
+ this.render();
+ }
+
+ resizeCanvas() {
+ const container = document.getElementById('canvas-container');
+ this.canvas.width = container.clientWidth;
+ this.canvas.height = container.clientHeight;
+ this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
+ }
+
+ setupControls() {
+ document.addEventListener('keydown', (e) => {
+ if (!this.activeObject) return;
+
+ const speed = 0.1;
+ switch(e.key) {
+ case 'ArrowLeft':
+ this.activeObject.position.x -= speed;
+ break;
+ case 'ArrowRight':
+ this.activeObject.position.x += speed;
+ break;
+ case 'ArrowUp':
+ this.activeObject.position.y += speed;
+ break;
+ case 'ArrowDown':
+ this.activeObject.position.y -= speed;
+ break;
+ }
+ });
+
+ document.getElementById('addCube').addEventListener('click', () => {
+ this.addObject('cube');
+ });
+
+ document.getElementById('addSphere').addEventListener('click', () => {
+ this.addObject('sphere');
+ });
+
+ document.getElementById('addCylinder').addEventListener('click', () => {
+ this.addObject('cylinder');
+ });
+ }
+
+ addObject(type) {
+ // Basic WebGL object creation would go here
+ // This is a simplified version
+ const object = {
+ type,
+ position: { x: 0, y: 0, z: 0 },
+ rotation: { x: 0, y: 0, z: 0 }
+ };
+
+ this.objects.push(object);
+ this.activeObject = object;
+ }
+
+ render() {
+ this.gl.clearColor(0.1, 0.1, 0.1, 1.0);
+ this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
+
+ // Render objects
+ this.objects.forEach(obj => {
+ // WebGL rendering code would go here
+ });
+
+ requestAnimationFrame(() => this.render());
+ }
+ }
+
+ // Initialize when DOM is loaded
+ document.addEventListener('DOMContentLoaded', () => {
+ const scene = new Scene3D();
+ });