Having trouble getting the Scene to appear in Three.js

I'm encountering some challenges with three.js as a beginner, specifically in rendering my scene and camera. Although I have successfully converted Adobe Illustrator vectors, I am struggling to render the scene properly. Even when removing the scene, camera & renderer code, there seems to be no impact on what is displayed in the browser.

Let's take a look at my HTML:

<canvas id ="slot">
</canvas>

Now, here is my JavaScript code:

var c = document.getElementById('slot');
c.height = 282;
c.width = 400;
var cx = c.getContext('2d');

// It appears that this part is not functioning correctly...
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// However, this section is working fine
cx.fillStyle = "rgba(255,255,255,0)";
cx.fillRect(0,0,1,1);
cx.fillRect(1,0,1,1);
//....(and so on)

var slot = new THREE.Mesh(cx);

// Setting up GridHelper
var size = 10;
var divisions = 10;
var gridHelper = new THREE.GridHelper(size, divisions);

// Ambient Light
var light = new THREE.AmbientLight(0x404040);

// Fog settings
var fogColor = 0xFFFFFF;
var near = 10;
var far = 100;
var fog = new THREE.Fog(fogColor, near, far);

// Adding elements to the scene
scene.add(slot, gridHelper, light, fog);

camera.position.z = 5;

renderer.render(scene, camera);

Any assistance with troubleshooting these issues would be highly appreciated.

Answer №1

Check out the jsFiddle I created with your updated code.

https://jsfiddle.net/EthanHermsey/qampc5b1/49/

var renderer = new THREE.WebGLRenderer({antialias: true});

var cTexture = new THREE.CanvasTexture( c );

var slot = new THREE.Mesh(
    new THREE.PlaneGeometry(2, 2),
  new THREE.MeshBasicMaterial({     
    map: cTexture,
    transparent: true
  })
);
scene.add(slot);

scene.fog = new THREE.Fog(fogColor, near, far);
  1. The way you are initializing a Mesh is incorrect.
  2. You need to add the canvas as a CanvasTexture to a material.
  3. Do not use .add() for fog in the scene, instead use scene.fog = new THREE.Fog(). This was causing the error message.
  4. I'm unsure if you can add multiple objects to the scene like that.
  5. I included antialias in the render settings to make edges smoother.

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

Is it possible to iterate through various values using the same variable name in Mustache.js?

I have data structured in the following way: hello this is {{replacement_data}} and this {{replacement_data}} is {{replacement_data}}. I'm interested in using Mustache to substitute these placeholders with values from an array: [val1, val2, val3. ...

Finding elements based on their position using Javascript

I'm searching for an optimal method to identify all elements located within a specific range of pixels from the top of the page. Currently, I have implemented a straightforward test called inRange function inRange(el, from, to) { var top = el.offs ...

Unable to configure AngularJS inputs to have a blank default value

I am facing a peculiar issue where I am unable to initialize my input fields as blank/empty. Even after clearing the cache and performing a hard reload, Google Chrome seems to be auto-populating them with older cached values. Here is the current view: ht ...

Encountering an issue with the history module when utilizing the webpack dev server

I am encountering an issue while trying to run webpack dev server. The history functionality was working fine until I started using the webpack module. A warning message appeared in my console: WARNING in ./src/history.js 2:15-35 export 'createBrows ...

Issue with parsing an empty array in a Discord bot built with Node.js

Within this API, there exists an empty array marked with a red underline. Sometimes it's void of content, but other times it contains data. My goal is to display Item Spells: there are no effects on this item. when the array is empty. However, instead ...

Guide to showcasing user input using a Javascript function

I need assistance to show the user input via the submit button. Users will enter tool types and the first five inputs will be displayed in list items (li). Once the limit of 5 tools is reached, a message 'Thanks for your suggestions' should appea ...

What is the method for altering the track color on a range slider?

Struggling to change the track color of a range slider using CSS in Chrome? Look no further, as I found a solution using jQuery (link). However, after implementing the solution, the expected output is not achieved. jQuery: $('.text-size-slider .slid ...

What is the best way to distinguish between an IPFS URL or path (content-addressed) and a regular URL (location-addressed)?

I need to retrieve nft-metadata for the nfts that have been added to my platform. Some of these Nfts have ipfs-urls as their tokenURIs and I am looking for a way to distinguish them from regular urls (location-addressed urls). Previously, I used is-ipfs, ...

Randomizing the JSON loop on every page refresh

Having a JSON file containing various data items (as an example) that I am utilizing with handlebars.js to generate templates. The challenge lies in displaying 15 stories (each within their own div) on page load while shuffling their positions upon each r ...

Trouble with React Native ListView Item Visibility

I'm currently working on integrating the React Native <ListView /> component with the <List /> and <ListItem /> components from React Native Elements. However, I seem to be facing an issue where the <ListItem /> component is no ...

Achieving Dual Actions with a Single onClick in React.js

Within my react application, I have implemented a button(Reset). When this button is clicked, I want multiple actions to be executed simultaneously through the onClick attribute. Is there a way for an onClick attribute to trigger more than one action at t ...

What is the best way to revert my useState back to its original state once the filtered list has been displayed?

I am struggling to reset my projectList to its initial state so that the filterProjects function can search through the entire projectList instead of the already filtered ones. I have a resetProjects function, but I'm unsure where to call it in order ...

No visibility of Mesh Groups in Three.JS ColladaLoader

When using Sketchup, creating components makes it easier to reuse geometry. For example, a car wheel can be designed as a component and then used multiple times for each wheel of the car. The challenge arises when trying to manipulate each reused componen ...

Using setInterval in JavaScript to automatically update a TextField

As someone who is relatively new to Javascript and jQuery, I am trying to make a simple code that updates a text field with random values every 5 seconds. However, my implementation does not seem to be working. I apologize if this question seems too basic ...

Loading a chunked polyfill file in webpack only when needed - a step-by-step guide

In order to prevent unnecessary loading of polyfills, it is recommended to add some logic in the <head> section (https://webpack.js.org/guides/shimming/) <script> var modernBrowser = ( 'fetch' in window && ...

What steps can you take to fix the error message "Cannot read properties of undefined"?

There seems to be a warning that I've encountered: [Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'nestedArray')" How can I resolve this issue? Here is my beforeCreate function: beforeCreat ...

Connect an EventListener in JavaScript to update the currentTime of an HTML5 media element

*update I have made some changes to my code and it is now working. Here's the link: I am trying to pass a JavaScript variable to an HTML link... You can find my original question here: HTML5 video get currentTime not working with media events javscr ...

The document.write function in JavaScript is malfunctioning

I've been attempting to utilize the javascript function document.write to incorporate an external css file in my template. However, I am aiming to achieve this using Twig, like so: document.write('<link href="{{ asset('bundles/activos/cs ...

What is the best way to extract the primary base64 value from reader.result?

After successfully retrieving the base64 value of my file, I noticed that along with the value, I am also getting the type of file and the type of string. However, I only require the actual value in order to send it to the backend. Code for converting fil ...

What are your top choices for customizable dropdowns/menus with enhanced features?

I came across a cool feature on the YUI website, check it out here: http://developer.yahoo.com/yui/examples/button/btn_example07.html Does anyone have recommendations for a library/plugin to enhance native select element dropdowns in response to a client& ...