The error message "domElement" cannot be modified because it is read-only in the THREE.WebGLRenderer

I encountered an issue while attempting to initialize the WebGLRenderer:
(Some unnecessary lines have been omitted)

import * as THREE from "https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c48544e59597c0c120d0e05120c">[email protected]</a>/build/three.module.js";
import { OrbitControls } from "https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91e5f9e3f4f4d1a1bfa0a3a8bfa1">[email protected]</a>/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="add9c5dfc8c8ed9d839c9f94839d">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js";


async function load() {
    // Setup THREE JS
    window.scene = new THREE.Scene();
    window.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

    // Load Model
    window.loader = new GLTFLoader();
    window.loader.load(
        "/assets/shotgun/scene.gltf",
        function (gltf) {
            const model = gltf.scene;
            scene.add(model);
            window.shotgun = model;
        },
        function (xhr) {
            console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
        },
        function (error) {
            console.error(error);
        }
    );

    // Initialize Renderer - Error occurs here
    window.renderer = THREE.WebGLRenderer({
        canvas: document.getElementById("shotgun"),
        antialias: true,
        alpha: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
    camera.position.set(10, 0, 0);

    // Lighting
    var light = new THREE.PointLight(0xffffff);
    light.position.set(-5, 10, 10);
    scene.add(light);

    // Helpers
    window.ctrl = new OrbitControls(camera, renderer.domElement);
    scene.add(new THREE.PointLightHelper(light), new THREE.GridHelper(200, 50));

}

load().then(() => {
    document.getElementById("loading").animate({ opacity: 0 }, { fill: "forwards", duration: 1000 });
    render();
});

DevTools indicate that the error is in this line:

function WebGLRenderer(parameters) {
  parameters = parameters || {};
  const _canvas2 = parameters.canvas !== void 0 ? parameters.canvas : createCanvasElement() // ...
  let currentRenderList = null;
  let currentRenderState = null;
  const renderListStack = [];
  const renderStateStack = [];
  this.domElement = _canvas2; // <----
  this.debug = {
    checkShaderErrors: true
  };
  this.autoClear = true;
  this.autoClearColor = true;
  this.autoClearDepth = true;
  ...
}

https://i.sstatic.net/mUojx6Ds.png

Additonally, how can I pause code execution during model loading? (Without placing it within the loader's function)

"Your post seems to be mostly code; consider providing more context."
Any suggestions on what additional information to include?

Answer №1

The missing piece in your code is the inclusion of the new keyword:

// Initializing renderer object without 'new' keyword
   window.renderer = THREE.WebGLRenderer({
// Correct way with 'new' keyword included
   window.renderer = new THREE.WebGLRenderer({

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

CSS Challenge: How to crop an image without using its parent container directly

I'm currently facing a complex CSS challenge that I can't seem to solve. I want to create an image controller (two-by-two layout on two lines) that can display: The top-left image in full size, The top-right with horizontal scrolling, The botto ...

Use jQuery to dynamically update a text field within a table row based on a selection from

My understanding of JS and jQuery is not very strong. This is the HTML Output I created using a foreach-loop: $('#ProjectOfferPosition0IsystemTypeVariantId').on('change', function () { var prices = []; prices[1] = 500.00; ...

json How to retrieve the first index value in jQuery

As part of my Ajax loop, I am successfully generating JSON and iterating through the results. My goal is to extract only the first index value of JSON which is name. In jQuery, I have the following code: PHP $jsonRows[] = array( "name" => ...

Error encountered: Mongodb db.collection.find() function is not successfully retrieving data, whereas the collection.insert() function

Working with node.js/express and utilizing a Mongodb database to store various data sets. The functionality for adding, editing, and deleting data on a webpage is functioning properly. For instance, the code snippet for adding data is as follows: router. ...

Can you effectively leverage a prop interface in React Typescript by combining it with another prop?

Essentially, I am looking to create a dynamic connection between the line injectComponentProps: object and the prop interface of the injectComponent. For example, it is currently set as injectComponentProps: InjectedComponentProps, but I want this associat ...

Encountering a GET-error while trying to nest jQuery/json calls: Resolution Update

My goal is to utilize services for ip-lookup and geo-lookup to pinpoint a visitor's location on a map. This information will be stored in a database and used to display live user locations on a dashboard. Currently, I am working on a script that, whe ...

Merge requirejs modules using build script

I am attempting to merge and compress require modules into a single file. For instance, if I have a file named article.js with the following content: define(["jquery","flexslider","share_div"],function(){}); I wish for all these dependencies to be merge ...

What is the best way to insert an object at a particular position within an array containing numerous nested objects?

This is the given Object: [ { "id": "1709408412689", "name": "WB1", "children": [ { "id": "1709408412690", "n ...

Tips for passing a variable from one function to another file in Node.js

Struggling to transfer a value from a function in test1.js to a variable in test2.js. Both files, test.js and test2.js, are involved but the communication seems to be failing. ...

The act of coming back with just one array

I'm having trouble grasping the concept of returning a single array from a function that calls another function multiple times. The issue I'm facing is that each time the scrapingfunction runs, the console.log in the code provided outputs an arra ...

Denied the execution of the inline script due to a violation of the CSP by the Chrome extension

We've been working on integrating Google Analytics into our Chrome extension, and here are the steps we've taken: We updated our manifest.json with the following line: "Content-Security-Policy": "default-src 'self'; script-src 'n ...

What sets apart the CSS file directories in a React application compared to those in an Express server?

For instance, there is a public folder that contains all the css files, and a separate view folder for ejs files. When linking the css file in the ejs file, the code usually looks like this: <link rel=”stylesheet” href=”styles.css”> or like ...

Receiving JSON objects from Javascript in Django Views

When I attempt to pass a Json Object value from making an API call to my views.py in Django template, I encounter difficulty retrieving the value after an ajax call. let application = JSON.parse(sessionStorage.getItem("appId")); let kycStatus = a ...

Conditionally assign a class to the body tag in your Jade template

Let's imagine I have a main template file called layout.jade and several files that extend this template - home, about, products, and more. Within the main template, I have structured it like this: body section.main-content b ...

Unable to locate JavaScript files within the Django project

I utilized Python 3.7 on Windows 10 to develop a Django project. settings.py: STATIC_URL = '/static/' LOGIN_URL = '/login' LOGIN_REDIRECT_URL = '/' https://i.sstatic.net/LSAdq.png I employed superuser to create some regu ...

In React, a singular reference cannot establish focus amidst an array of references

Scenario In this scenario, we are restricted to using only keyboard navigation without any mouse clicks. Imagine a situation where we have 10 table rows displayed on the screen. Each row contains a menu button for interaction. When the tab key is pressed ...

How can I use the same popup button to open a different link in a new tab?

I have a situation where I am using a button to trigger an ajax html popup. What I want is for the same button, when clicked, to open another page in a new tab. Any assistance would be greatly appreciated. Below is the HTML code I am currently using: < ...

Converting RowDataPacket to an array in Node.js and MySQL API, learn how to convert a RowDataPacket from the MySQL API into an array

Hello, I need assistance with converting my row data packet into an array of arrays or nested arrays. Please provide code snippet below: router.get('/getPosts/:user_id', (req, res, next) => { connection.query('SELECT * FROM files WHERE ...

Unable to utilize external JavaScript files in Angular 8

I've been working on integrating an HTML template into my Angular project and for the most part, everything is going smoothly. However, I've encountered an issue where my JS plugins are not loading properly. I have double-checked the file paths i ...

What is the optimal 3D object format to use with three.js?

When working on a webgl-three.js context with multiple 3D objects that have textures, slight movement, and mouse listeners, what format is more appropriate for importing them - JSON or OBJMTL? I am capable of making both work, but I want to determine whic ...