What is the solution for addressing a blank canvas after integrating Orbit Controls in Three.js?

Currently, I am working on learning three.js, and my progress has been going well. I successfully programmed a cube on the canvas using JS Fiddle. However, when I attempted to implement Orbit Controls to rotate the cube, the canvas displayed only a black screen.

Despite researching on the three.js website and reviewing examples of working Orbit Controls, I have been unable to resolve the issue.

<script src='http://threejs.org/build/three.min.js'></script>
    <script src='http://threejs.org/examples/js/controls/OrbitControls.js'></script>

    <script>
    let container, renderer, scene, camera, controls;

        function init() {

  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0xccccff);
        document.body.appendChild(renderer.domElement);

  scene = new THREE.Scene();

        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
        camera.position.z = 4;

        controls = new THREE.OrbitControls(camera, renderer.domElement);

        let geometry = new THREE.BoxGeometry(1, 1, 1);
        let material = new THREE.MeshBasicMaterial({color: 0x000000});
        let cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        window.addEventListener('resize', resizeFunction);
        }

        let resizeFunction = function() {
            let width = window.innerWidth;
            let height = window.innerHeight;
            camera.aspect = width / height;
            camera.updateProjectionMatrix();
            renderer.setSize(width, height);
        }
        let animate = function() {
            controls.update();
            renderer.render(scene, camera);
            requestAnimationFrame(animate);
        }
        init();
        animate();
</script>

Despite checking my code repeatedly, I did not encounter any error messages. I was anticipating a rotating black cube on a lighter background as the output, but instead, the screen remained entirely black. Any assistance in identifying the issue would be greatly appreciated!

Answer №1

Looks like it's functioning properly.

    let container, renderer, scene, camera, controls;

        function initialize() {
  
  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0xccccff);
        document.body.appendChild(renderer.domElement);

  scene = new THREE.Scene();

        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
        camera.position.z = 4;

        controls = new THREE.OrbitControls(camera, renderer.domElement);

        let geometry = new THREE.BoxGeometry(1, 1, 1);
        let material = new THREE.MeshBasicMaterial({color: 0x000000});
        let cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        window.addEventListener('resize', reSize);
        }

        let reSize = function() {
            let width = window.innerWidth;
            let height = window.innerHeight;
            camera.aspect = width / height;
            camera.updateProjectionMatrix();
            renderer.setSize(width, height);
        }
        let animate = function() {
            controls.update();
            renderer.render(scene, camera);
            requestAnimationFrame(animate);
        }
        initialize();
        animate();
<script src='http://threejs.org/build/three.min.js'></script>
    <script src='http://threejs.org/examples/js/controls/OrbitControls.js'></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Uncontrolled discord bot flooding with messages despite being set to send messages only once every 60 seconds

const Discord = require('discord.js'); const { Client, MessageAttachment } = require('discord.js'); const client = new Discord.Client(); client.once('ready', () => { console.log("Ready!") }) client.on(&apos ...

Using CSS and JavaScript to hide a div element

In my one-page website, I have a fixed side navigation bar within a div that is initially hidden using the display:none property. Is there a way to make this side nav appear when the user scrolls to a specific section of the page, like when reaching the # ...

Dynamic Visibility Control of GridPanel Header in ExtJS

I have a GridPanel that needs to display a limited number of resources. If there are more resources available than what is currently shown, I would like the panel's header/title to indicate this by displaying text such as "more items available - displ ...

Unable to retrieve the value of a specific property using _.map based on its property name

I'm currently facing a challenge with my app that utilizes MongoDB as its database. Specifically, I am having trouble extracting property values from array variables. Despite using a _.forEach method to confirm the presence of data, I encountered diff ...

Alter the color of points dynamically within a Three.js PointCloud

I'm working on rendering a matrix of points in Three.js, but I want each particle in the cloud to act as an individual "pixel" that can have its color changed dynamically. While I managed to render the point cloud and set the initial color, I'm s ...

Issues connecting tables to Knockout group

I seem to be encountering some difficulties with the Knockout table bindings in my code. Here is a snippet that I am struggling with: Snippet: $(function () { var FileObject = function(id, name) { this.id = id; this.name = name; ...

Communicating JSON data through AJAX with a WCF web service

Hey everyone, I could really use some assistance with my current coding issue. My goal is to pass the value 2767994111 to my WCF web service using jQuery AJAX. var parameter = { value: "2767994111" }; $('#btSubmit').click(function () { $ ...

The integration of PHP code with an HTML form is causing issues

When I use the insert_teacher('bla bla','bla bla','dqsd') function in a PHP file, everything works fine. However, when I try to implement it with an HTML form, nothing is displayed and no data is inserted into my database. &l ...

Issue encountered when trying to import an image URL as a background in CSS using Webpack

I have been trying to add a background image to my section in my SCSS file. The linear gradient is meant to darken the image, and I'm confident that the URL is correct. background-image: url(../../assets/img/hero-bg.jpg), linear-gradient(r ...

Encountering a parsing failure in the _app.js file of a new Next.js project: Unexpected token

When starting a new Next.js project, I use the following command: npx create-next-app After moving into the project folder and running npm run dev, I encounter the following error: C:/Users/mikke/Documents/Projects/next-project/pages/_app.js 4:9 Module pa ...

variance in efficiency when retrieving a DOM element

While this may not make much of a difference for smaller, simpler DOM elements, the performance impact could be significant when dealing with larger, more complex ones. Is there a noticeable performance gap between storing an element in a variable and cons ...

Jquery's mouseclick event selects an excessive number of elements

Is there a way to retrieve the ID of an element when it is clicked, only if it has one? I currently have this code set up to alert me with the element's ID: $("[id]").click(function(event) { event.preventDefault(); var id_name = $(this).attr ...

Font size determined by both the width and height of the viewport

I'll be brief and direct, so hear me out: When using VW, the font-size changes based on the viewport width. With VH, the font-size adjusts according to the viewport height. Now, I have a question: Is there a way to scale my font-size based on ...

Passing values from a Laravel controller to a Vue component as a prop

At the moment, I have a Laravel route that directs to the index function of my controller with an ID passed, where I then return the ID in a view. public function index($id) { return view('progress') ->with('identifier', ...

Nodejs application crashes due to buffer creation issues

router.post('/image', multipartMiddleware , function(req, res) { var file_name = req.body.name; var data = req.body.data; var stream = fs.createReadStream(data); //issue arises here return s3fsImpl.writeFile(file_name , stream).t ...

What is the best way to transfer the main-video-wrap div into the video-list-Wrapping div?

Thank you @Kathara for your valuable assistance I have successfully set up the video layout with a picture-in-picture mode option. When I click on a video to move it to the background, it works well. However, I am facing difficulty in moving the entire vi ...

The onPlayerReady function in the YouTube API seems to be experiencing a delay and

After following a tutorial on integrating the YouTube API into my website, I encountered difficulties trying to play a YouTube video in fullscreen mode when a button is pressed. Despite following the provided code closely, I am unable to get it to work as ...

Is the webdriver.io waituntil method designed to return a boolean value of true or false

I am working on an automation framework using webdriver.io v5. I need to receive a boolean response from the code snippet below: waitAndCheckForContactToBePresent(contactName) { return browser.waitUntil((value) => { return this.chec ...

I am encountering a problem where I lose the values of a nested array of objects when trying to push it into another array

My goal is to format data for use with the amCharts library. The required structure should look like this: data = [{ name: "First", total: 190, children: [ { name: "A1", value: 100 }, { name: &q ...

Discover the method for measuring the size of a dynamically generated list

I'm currently working on a chat box that displays messages as a list. One issue I'm facing is that when I click on the start chat button, the chat box opens but the length of the list always remains zero. How can I resolve this problem? $(docu ...