Issue with THREE.js Line Object not displaying in web browser

Having just recently started to dabble in THREE.js, I encountered an issue where the code only displays a black screen instead of the intended triangle shape. Can someone assist me in troubleshooting the problem within the code?

<!DOCTYPE html>
<html>
<head>
    <title>First Line Object</title>
    <style>
        body{ margin: 0; }
        canvas { width: 100%; height: 100%; }
    </style>
</head>
<body>
    <script src="js/three.js"></script>
    <script>
    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);

    var lineGeom = new Geometry();
    lineGeom.vertices = [
        new THREE.Vector3(-2,-2, 0),
        new THREE.Vector3(2, -2, 0),
        new THREE.Vector3(0, 2, 0),
        new THREE.Vector3(-2,-2, 0)
    ];

    var lineMat = new THREE.LineBasicMaterial({
        color: 0xA000A0,
        linewidth: 2
    });

    var line = new THREE.Line(lineGeom, lineMat, THREE.LineStrip);
    scene.add(line);

    camera.position.z = 5;

    var render = function() {
        requestAnimationFrame(render);
        renderer.render(scene, camera);
    }
    render();

    </script>
</body>
</html>

Upon analysis, the code is constructed to render a triangle based on the provided vertices in the array. However, an unexpected outcome occurs where only a black screen is visible without the triangle shape. It's crucial to identify and resolve this issue for the desired output.

Answer №1

If you are facing an issue with the initialization of the Geometry, make sure to initialize it using the constructor in the following way:

new THREE.Geometry()

By following this method, you should be able to resolve the problem you are encountering.

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

Error occurred in AngularJS service due to incorrect data type

Looking to store the URL of a query in an AngularJS service like this: var mortgageloanService = angular.module('loanstreetIpadAppApp', []); mortgageloanService.factory('updateTable', function($http) { return { getParams: fun ...

I have successfully implemented the Context API in my Next.js project and everything is functioning smoothly. However, I encountered an error while using TypeScript

Currently, I am working on a Next.js project that involves using the Context API. The data fetched from the Context API works perfectly fine, but I am encountering errors with TypeScript and I'm not sure how to resolve them. Error: Property openDialo ...

A guide on organizing nested object data in react-native

{ "crocodile":{ "age_in_years":"20", "name":"snapjaw", "country":"australia" }, "jaguar":{ "age_in_years":"8", "name&q ...

Sluggish animation performance in threejs when using requestAnimationFrame within a class

Experiencing choppy animation on the JSFiddle? When trying to move your mouse or interact with the content, you may have come across issues with the classing related to requestAnimationFrame(this.animation). Initially, it didn't work as expected, but ...

Warning: data and salt parameters are necessary, please provide them

Issue: I am encountering an error with registering a new user, specifically when using Postman. I'm not sure why this error is occurring only in Postman. Additionally, I am facing proxy problems where requests cannot be proxied from localhost:3000 to ...

Target the <select> element within a <tr> using jQuery selector and apply an empty css style

When looking at this HTML snippet, I am attempting to target the <select> element with id= "g+anything" inside the <tr id='g2'>. <table> <tr id='g1><td> <select id="gm"> <opt ...

Passing a parameter to an AngularJS directive, which triggers the opening of an ngDialog, and subsequently updating the value to reflect changes in the root scope

Struggling with a persistent issue here; Essentially, I have a directive that triggers an ngDialog to open. This directive should be able to receive a variable from the root scope. The directive contains a click event that opens an ngDialog which then use ...

The conditional statement is failing to execute properly in the JavaScript AJAX response

When checking the value in the alert, it always shows as 'Initial'. However, the condition if(checkoption == 'Initial') always returns false. The actual code is as follows: function changeItem(id,item,option) { var xhttp; xht ...

The raycaster fails to detect collision with the .gltf model

When using the raycaster to detect mouse intersection with a cube, everything works perfectly: raycaster.setFromCamera(mouse, camera) const intersects = raycaster.intersectObject(cubeMesh) if(intersects.length > 0) console.log('intersecting ...

Unable to execute tests on angular example project

I recently cloned the Angular Material-Start project. According to the documentation: When I run npm run tests to start all my Karma unit tests, I encounter an error: npm ERR! Windows_NT 10.0.10586 npm ERR! argv "C:\\DevSoft\\Node ...

Implementing the requiredUnless validator of vuelidate for checkboxes: A step-by-step guide

When utilizing Vuelidate, the 'required' validator now accepts boolean 'false' as a valid value. To enforce required validation for checkboxes, you must use 'sameAs' such as sameAs: sameAs( () => true ). How can 'requi ...

io.emit is unable to send messages to all connected clients

Struggling to implement a feature in a socket.io game where players can leave the game, I'm facing an issue with io.emit only notifying the departing player. Here's the snippet from my socket.js: io.on("connection", sock => { sock.on(&ap ...

Error Encountered with Next.js 13.4.1 when using styled-components Button in React Server-Side Rendering

I am currently working on a React project using Next.js version 13.4.1 and styled-components. One problem I'm facing is with a custom Button component that I've created: import React from 'react'; import styled from 'styled-compone ...

Is it possible to test code that utilizes a different framework from jQuery using QUnit?

Currently, I am in the process of developing a compact library that is capable of utilizing multiple frameworks (specifically jQuery, Prototype, YUI2). To ensure its functionality, I am conducting tests using QUnit. Nevertheless, one setback is that QUnit ...

There was an issue with the routing that prevented access to the student activity information at

I've been working on building my own Note Taking App using Express. Following my instructor's example, I wrote some code but encountered an issue when deploying it. Whenever I try to add a new note, I receive an error that says "cannot get/api/na ...

Looking to cycle through arrays containing objects using Vue

Within my array list, each group contains different objects. My goal is to iterate through each group and verify if any object in a list meets a specific condition. Although I attempted to achieve this, my current code falls short of iterating through eve ...

When executing npm run dev, an UnhandledPromiseRejectionWarning is triggered

I'm encountering an UnhandledPromiseRejectionWarning error when running npm run dev on my Vagrant machine, and I'm struggling to identify the root cause. The console output suggests that a promise is not being properly completed with the catch() ...

Determine the number of items in an array with JavaScript in Node

Looking to create a new variable that counts the names in an array and assigns them accordingly? I'm currently stuck on this task. The array contains names: names = [Jan, Jan, Jana] Counting the names is simple, but I'm facing difficulty in org ...

Steps for exporting a module from a JavaScript file that is linked:

When I include the main.js file in my project, the code below works properly: modules.exports = { property: value } However, if I include it in a web page like this: <!DOCTYPE html> <html> <head> <script src="main.js"& ...

Tips for sending arguments up in Angular2

I need to supply an argument to a function. <select class="chooseWatchlist" (change)="updateWatchlistTable(item.name)"> <option *ngFor="let item of _items">{{ item.name }}</option> </select> It's crucial for me, as I have ...