Having trouble getting three.js to display a sphere on the screen?

I am new to using THREE.js and I'm having trouble rendering a sphere in the center of my canvas. Can anyone help troubleshoot my code? Here is what I have:

<canvas id='canvas' width='960' height='720'></canvas>
<script src="https://ajax.googleapis.com/ajax/libs/threejs/r69/three.min.js"></script>
<script>
var T = THREE;
var canvas = document.getElementById('canvas');
var WIDTH = 960, HEIGHT = 720;
var VIEW_ANGLE = 45, ASPECT = WIDTH / HEIGHT, NEAR = 0.1, FAR = 10000;
var renderer = new T.WebGLRenderer({antialias:true});
var camera = new T.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
var scene = new T.Scene();
scene.add(camera);
camera.position.z = 300;
renderer.setSize(WIDTH, HEIGHT);

var sphMat = new T.MeshLambertMaterial({color: 0x778B71});
var sph = new T.Mesh(new T.SphereGeometry(5, 20, 20), sphMat);
sph.position.x = 0;
sph.position.y = 0;
sph.position.z = 0;
scene.add(sph);

renderer.render(scene, camera);
canvas.appendChild(renderer.domElement);
</script>

Answer №1

Could you clarify which parts are not rendering properly? I am having trouble locating a light source in your code, THREE.MeshLambert material requires a light source.

For testing purposes, consider using MeshBasicMaterial:

or add a light to the scene:

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

Guide to smoothly transition the highlighted <tr> element to the top of the scroll bar

Is there a way to utilize jQuery for animating a selected row to the top of a div with a specific scrollbar style? #sc { border:1px solid black; width:450px; height:80px; overflow:scroll; } <div id="sc"> <table id=&quo ...

Obtaining an Array through a direct input on the command line

I am having trouble incorporating raw command line arguments in my Node.js application. When I try with simple variables, everything works as expected (node example.js variable) However, when I pass an array as an argument, it does not work properly (n ...

Passing specific props to child components based on their type in a React application using TypeScript

Sorry if this question has already been addressed somewhere else, but I couldn't seem to find a solution. I'm looking for a way to pass props conditionally to children components based on their type (i.e. component type). For example, consider ...

My controller in AngularJS is having trouble fetching the latest values of the $scope variables

In my code, I've included the relevant snippet below. The base angularJS seems to be functioning properly as the HTML document doesn't display {{}} variables but instead remains blank. I suspect that this is due to the variables receiving a null ...

Efficient ways to organize JSON objects using JavaScript

I am in need of restructuring the data retrieved from an API call, which currently looks like this: { "Label3": [ { "name": "superman", "power": 8900 }, { "name": "iron man", "power": 3000 }, { "name": "spike spiegal", "power": 200 ...

Apple Automation: Extract a targeted string from text and transfer it to a different spot within the page

Apologies for my lack of expertise in this area, but I hope to convey my question clearly. I am working on a form where I need to input text in order to copy specific items and paste them elsewhere on the same webpage. For example, if the input text is " ...

What is the best method for accessing a specific Data value on the route?

Hello everyone! This is my first time posting on Stack Overflow. I am currently working on developing an attendance management system for a school. In my database tables, I have the following fields: Registration: class_id, section_id, user_id. Attendanc ...

Unlocking the Power of refs in React's this.props.children

Wondering if it's possible to call a function of a child component in React by accessing refs from this.props.children. var ComponentSection = React.createClass({ componentDidMount: function() { // Need to find a way to access refs in this ...

Is there a intentional way to cause my node.js server to crash?

Is there a way to intentionally crash my node.js server? I want to trigger this action when specific fatal errors occur in order to immediately bring them to my attention for fixing. I came across a post about this topic here, but I am not interested in u ...

Javascript ajax async data update has encountered a failure

Utilizing HTML, javascript, and Nodejs coding to create a chrome extension. When the page loads, the function getBalance() is invoked to retrieve data and display it in an HTML span. Below is the code snippet: function getBalance() { var request = new ...

The variable $http request is not defined in this context

When I perform a GET request on my backend to fetch JSON data, I am encountering an issue with storing a portion of the data in a variable for later use. Despite following similar steps in another controller where it worked fine, the variable always ends u ...

Ways to send user input to a function within a React component

I am currently working on implementing a simple feature where users can search posts by their tags. To achieve this, I have created the Feed.jsx component with the following code: "use client"; import { useState, useEffect } from "react&quo ...

Is there a way to make a table row clickable? I tried finding solutions online, but none of them seemed to work for me

Having trouble opening a new page when tapping on a cell (TR) using Javascript. Despite trying various online tutorials, I still can't get it to work properly. Any assistance would be greatly appreciated. Thank you. Below is the code snippet: fun ...

"How can I exclude empty fields from the form.serialize function in jQuery when using php?" is not functioning as expected

I am facing an issue with using How do I use jQuery's form.serialize but exclude empty fields. Despite following a minimal example that directs to a php script: <html><head> <script src="jquery.min.js"></script> <script typ ...

Performing an HTTP POST request in Angular 2

After starting my work with Angular 2 and TypeScript, everything was going great. However, I encountered an issue when working with a REST API (POST) where the console log displayed Response {_body: "", status: 204, statusText: "Ok", headers: Headers, type ...

JavaScript nested filtering functions

In my code, I have an array named restaurant_items. This array consists of objects that represent various items from a restaurant. Each object contains another array called in_menu, which also consists of objects. I am looking to filter out the restaurant ...

Assign the physics settings to a variable within the A-frame

Exploring A-frame () for my scene creation has been exciting. I am curious about how I can dynamically adjust the physics in my virtual world using the A-frame physics component. The goal is to have the physics within my scene be determined by a variable c ...

Display a modal as the first screen appears in a React Native application

Trying to implement a non-animating modal before the main screen loads on my react-native app. The goal is to display a login screen in a modal view if no user is logged in. Encountering an issue where the main screen briefly appears before the modal, ins ...

Getting information in the constructor

Is it considered beneficial to retrieve data in the JavaScript class constructor method? For example, in a React class constructor, most tutorials recommend fetching data in the componendDidMount() method. However, there is little explanation as to why we ...

Discover the absent day in an array of dates using JavaScript

Upon receiving an array of day dates from an API, the following structure is observed: 0:{date: "2016-11-17T00:00:00",…} 1:{date: "2016-11-18T00:00:00",…} 2:{date: "2016-11-19T00:00:00",…} 3:{date: "2016-11-21T00:00:00",…} 4:{date: "2016-11-22T00: ...