What could be causing the renderer error in threejs coding?

Quite strange. Everything was functioning properly before, and I left it running. However, upon my return, the sphere was no longer being displayed on the webpage.

The issue arose after I added three light objects and GUI sliders to adjust their positions and intensities within GUI folders.

The errors that occurred were: THREE.WebGLRenderer: Error creating WebGL context. Uncaught Error: Error creating WebGL context. Uncaught ReferenceError: Cannot access 'renderer' before initialization.

// Loader
const textureLoader = new TextureLoader()
const normalTexture = textureLoader.load('/textures/Sphere-map.jpg')

// Debug
const gui = new dat.GUI()

// Canvas
const canvas = document.querySelector('canvas.webgl')

// Scene
const scene = new THREE.Scene()
const light = new THREE.AmbientLight(0x7d7d7d); // soft white light
scene.add(light);

// Objects
const sphereGeometry = new THREE.SphereGeometry(.5, 64, 64);

// Materials

const material = new THREE.MeshStandardMaterial()
material.color = new THREE.Color(0xFF4FA7)
material.metalness = 0.3
material.roughness = 0.8
material.normalMap = normalTexture

// Mesh
const sphere = new THREE.Mesh(sphereGeometry, material)
scene.add(sphere)


// Lights
const light1 = gui.addFolder('Light 1')
const light2 = gui.addFolder('Light 2')
const light3 = gui.addFolder('Light 3')

// light 1
const pointLight = new THREE.PointLight(0xFF4FA7, 0.1)
pointLight.position.set(0, 0, 0)
pointLight.intensity = 1
scene.add(pointLight)

light1.add(pointLight.position, 'y').min(-3).max(3).step(0.01)
light1.add(pointLight.position, 'x').min(-6).max(6).step(0.01)
light1.add(pointLight.position, 'z').min(-3).max(3).step(0.01)
light1.add(pointLight, 'intensity').min(0).max(10).step(0.01)


// Light 2
const pointLight2 = new THREE.PointLight(0xffffff, 2)
pointLight2.position.set(0.8, 0.67, 0.8)
pointLight2.intensity = 1
scene.add(pointLight2)

light2.add(pointLight2.position, 'y').min(-3).max(3).step(0.01)
light2.add(pointLight2.position, 'x').min(-6).max(6).step(0.01)
light2.add(pointLight2.position, 'z').min(-3).max(3).step(0.01)
light2.add(pointLight2, 'intensity').min(0).max(10).step(0.01)


// Light 3
const pointLight3 = new THREE.PointLight(0xffffff, 2)
pointLight3.position.set(-1.71, -3, 3)
pointLight3.intensity = 1
scene.add(pointLight3)

light3.add(pointLight3.position, 'y').min(-3).max(3).step(0.01)
light3.add(pointLight3.position, 'x').min(-6).max(6).step(0.01)
light3.add(pointLight3.position, 'z').min(-3).max(3).step(0.01)
light3.add(pointLight3, 'intensity').min(0).max(10).step(0.01)

/**
 * Sizes
 */
const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

window.addEventListener('resize', () => {
    // Update sizes
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    // Update camera
    camera.aspect = sizes.width / sizes.height
    camera.updateProjectionMatrix()

    // Update renderer
    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})

/**
 * Camera
 */
// Base camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
camera.position.x = 0
camera.position.y = 0
camera.position.z = 2
scene.add(camera)

// Controls
// const controls = new OrbitControls(camera, canvas)
// controls.enableDamping = true

/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({
    canvas: canvas
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))


/**
 * Animate
 */

const clock = new THREE.Clock()

const tick = () => {

    const elapsedTime = clock.getElapsedTime()

    // Update objects
    sphere.rotation.y = .5 * elapsedTime

    // Update Orbital Controls
    // controls.update()

    // Render
    renderer.render(scene, camera)

    // Call tick again on the next frame
    window.requestAnimationFrame(tick)
}

tick()

Answer №1

After updating Chrome, the code is now functioning properly

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

Loading obj/mtl files in three.js results in a black rendering

I am encountering an issue while attempting to load a complex .obj file into three.js. Below is the code snippet: // Load obj file var loader = new THREE.OBJMTLLoader(); loader.load('MQ-9.obj', 'MQ-9.mtl', function(object) ...

javascript The event handler is not functioning properly for the dynamically loaded AJAX content

I am facing an issue with adding a JavaScript event listener to a dynamically loaded div via AJAX. Below is my code snippet: var QuantityMiniCart = function() { var infor = document.querySelectorAll( '.mini-cart-product-infor' ); if ( ...

Uncovering the inherent worth of an item pulled from a remote location using Vue.js

Currently, I am retrieving a list of countries by making an API call in VueX. The fetched data is then stored in a state using a mutation. Essentially, the countries are saved in a variable known as this.$state.getters.countryList, which can be accessed f ...

What is the purpose of using *//<![CDATA[* and *//]]>* in a jQuery code snippet?

Check out this JavaScript code snippet that I have: <script type="text/javascript> //<![CDATA[ jQuery(document).ready(function() { jQuery("#page_template option[value='sidebar-page.php']").remove(); }); ...

Avoiding Socket IO from timing out when the browser page is inactive or not the focused tab on Google Chrome, Mozilla Firefox, and Safari mobile browsers

I have developed a basic chat application using socket io. The functionality is quite similar to the socket io chat official demo. However, I am facing an issue where Socket io times out on all mobile browsers when the user minimizes the page, opens anothe ...

Leveraging JavaScript to change the input value by appending the character "#"

I am looking to modify the input value by adding a "#" in front of each word, but I am facing issues with handling spaces properly. function addHash(input) { var text = input.value.replace('#', ''); var words = text.split(" "); ...

Steps for creating a function to validate if two classes are identical

Currently, I am developing a memory game using JavaScript. One of the features I have implemented is toggling between two card faces by changing the class back and forth. Now, I am in the process of creating a function that will check if two game cards are ...

Is it possible to simultaneously stream text and video content?

I currently have a node.js server set up with node-media-server: const NodeMediaServer = require('node-media-server'); const config = { rtmp: { port: 1935, chunk_size: 60000, gop_cache: true, ping: 30, ping_timeout: 60 }, ...

What is the best method for incorporating a data-* attribute to every option while configuring a select2 input field?

Currently, I am utilizing the select2 plugin to enable the selection of an image from a list to display on the screen. In order to achieve this functionality, I must assign a specific attribute to each option: s3_key. My requirement is to set this attribu ...

The Material UI Popover is appearing outside the designated boundaries

In my app, I am using the code below to implement a react-dates picker controller inside a popover element. The functionality works well in most scenarios, but there is an issue when the button triggering the popover is located at the bottom of the screen, ...

Guide on manipulating the position and orientation of external elements in three.js

I'm currently working on a small project that involves creating an interactive 3D scene on the web. To achieve this, I utilized three.js to import objects designed in Blender. Initially, I attempted to export the objects as JSON files, but encountered ...

Tips for utilizing sendToDevice multiple times in a function return value

In my Firebase Cloud Functions script, I have successfully implemented sending push notifications to users using a specified set of token arrays returned by a function: return getTokens(array1).then(tokens => { return (admin.messaging().sendToDevice( ...

Creating a Fixed Header and Footer in HTML When Content Exceeds a Specific Height

Imagine an HTML template with the following structure: ------------- |Header Data| ------------- | | | Main Data | | | ------------- |Footer Data| ------------- The Header Data, Main Data, and Footer Data sections have fixed height va ...

Struggling with PHP variables and AJAX JavaScript

Hey everyone, I've made some edits and have a new question regarding a similar issue. On test.php in my Apache server, I have a PHP script that connects to a database and retrieves data from a table. <?php $con = mysqli_connect("localhost", "user" ...

Steps to display page content only after running Java code in JSP

The webpage index.jsp is responsible for retrieving images and text data from a database using Java code. Within the JavaScript file, I have included: $(document).ready(function(){ //When Document is Ready, Show the Main Page $("#showifjavaenable ...

Error in Displaying Vuetify Child Router View

I am currently working on integrating a child router-view to be displayed alongside surrounding components. Here is an overview of my routing setup: { path: "/login", name: "TheLoginView", component: TheLoginView, }, { path: "/dashboa ...

Issue: You cannot render Objects in React components. Consider using an array if you intended to render a collection of children

I've encountered an error message that has me stumped. My goal is to upload an image into the database and have it displayed on screen. However, every time I attempt to upload an image, instead of rendering it on the screen, I receive this error mess ...

How come my SQL query is functioning properly in pgAdmin, but I am encountering a query error (Error: Connection Terminated) when attempting to execute it from the server?

Currently, I am working on my Capstone project which involves storing telemetry data in a PostgreSQL 9.5 database and using node for the server. The issue I am facing is that every time I attempt to send a query from the server, I encounter a query error ...

identifies a data point from a combined dropdown menu

Is there a way to detect a specific value from a multiplied combo box? For example, when the value in one of the combo boxes changes to "2", a component is displayed. However, if the value in another combo box changes to anything other than "2", the compon ...

Issue: Unidentified supplier following JavaScript compression

Here is the AngularJS controller code that I am working with: (function() { 'use strict'; angular .module('app') .controller('TemplateCtrl', TemplateCtrl); function TemplateCtrl($http, $auth, $rootS ...