Having trouble utilizing a JavaScript file within TypeScript

I am currently exploring the integration of Three.js into an Angular application.

Following the documentation, I imported Three.js using the following line: import * as THREE from 'three';

In addition, I installed the types for Three.js with the command npm install @types/three --save.

The issue arises when attempting to load models using the OBJLoader2.

According to the official example, the correct way to instantiate the OBJLoader2 is via

var loader = new THREE.OBJLoader2();
. However, this class is not defined directly in the 'three' module but rather in 'three/examples/js/loaders/OBJLoader2.js'.

After many unsuccessful attempts to import the file, including a naive approach like

import '../../node_modules/three/examples/js/loaders/OBJLoader2.js';
, the browser console returned an error:

OBJLoader2.js:8 Uncaught ReferenceError: THREE is not defined at eval (OBJLoader2.js:8) at Object.../../../../three/examples/js/loaders/OBJLoader2.js (vendor.bundle.js:430)

The content of the file 'OBJLoader2.js' makes it clear that it expects THREE to be globally defined, which leads to the runtime error due to scoping issues.

Prior to importing the file, I attempted to declare THREE globally in various ways:

declare var THREE;

or

declare global{
  interface Window{THREE:any;}
}
import * as THREE from 'three';
window.THREE = THREE;

While these declarations resolved the initial error, a subsequent error occurred when attempting to use the loader:

var loader = THREE.OBJLoader2();
loader.load('assets/untitled1.obj', callbackOnLoad, null, null, null, false);

This resulted in the error:

ERROR TypeError: Cannot read property 'load' of undefined at AppComponent.initCube (app.component.ts:52)

Despite extensive online research, I have been unable to find a solution or similar experiences documented. Any assistance on resolving this challenge would be greatly appreciated. Thank you in advance.

Answer №1

I have not personally tested this method, but it appears that there is a potential solution available here.

From what I gather, the key steps include:

  1. Running npm install three --save
  2. Running npm install @types/three --save-dev
  3. Importing * as THREE from 'three'

It seems like you may have already completed steps #1 and #2.

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

Press the text in a React Native TypeScript component to trigger a render

I am a newcomer to React Native and TypeScript, and I am struggling to figure out how to display something on the page of my app after a button is pressed. Below is the function I'm using: const getRandomNumber = () ={ const number = Math.fl ...

"Threlte three.js Brings a Fresh Perspective with Subtle Hue Shifts

Struggling to create a skybox similar to the one shown in this video using Threlte, but the colors keep appearing washed out. View of image in vscode: https://i.sstatic.net/N89Qi.jpg Image as a texture: imgur link +page.svelte: <script> import ...

Is there a way to execute a code snippet just once when focusing on a specific field?

<form id="myForm"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname"><br> <label for="mname">Middle name:</label> ...

Unable to transfer information between two components using a service if the recipient component has not been initialized

I am facing an issue with passing data from an admin component to an edit component in my project. I have set up an Edit Service for this purpose, but I'm struggling to retrieve the data when the Edit component is loaded. In the admincomponent.ts fil ...

Transforming a Shadertoy experiment into a customized Three.js playground on my local machine

I am currently working on a local shader sandbox project inspired by a Shadertoy created by lennyjpg. I have been referencing two Stack Overflow questions and answers (one, two) for help. The goal is to convert the Shadertoy code to use Three.js for a larg ...

What is the procedure for cancelling a file upload in the FileUpload component of PrimeNG?

1. Issue Overview Looking to terminate file upload in PrimeNG's FileUpload component when certain filename patterns are detected. Using Angular 6.0.7 and PrimeNG 6.0.2. 2. Initial Strategy 2.1. HTML Code <p-fileUpload #fileUploader name="file" ...

Issue with Syncfusion Grid: Unable to Display Column Data as Hyperlinks

I'm currently working on a Webapp that utilizes the Syncfusion grid to display tabular data. One of my requirements is to showcase a column's data as a clickable link and trigger a function upon user interaction. After consulting the tutorial av ...

AngularJS 2: Modifications to templates or components do not automatically reflect in the user interface

My background is in Angular 1, where everything worked seamlessly. However, I am encountering serious issues trying to create a basic application using Angular 2 in Visual Studio. After carefully following the "5 minute tutorial" and getting it to work, I ...

Incorporate additional fields into the info.plist file for a Cordova iOS application

I am in the process of developing a custom plugin that will automatically add entries to the info.plist file for an iOS application built with Cordova and Angular 4. One specific entry I need to include triggers the application to exit when the home button ...

Assigning variables in Angular ngFor loopFresh way to

Here is what I have: <div *ngFor="let item of order.items"> I want to show the result in this format: {{item.product.category.availability.selected.name.value}} {{item.product.category.availability.selected.id.value}} My goal is to assign all of ...

Determine whether or not there are any duplicate elements within an array object

Is there a way to determine true or false if there are any duplicates within an object array? arr = [ { nr:10, name: 'aba' }, { nr:11, name: 'cba' }, { nr:10, name: 'aba' } ] arr2 = [ { year:2020, cit ...

Encountering an issue while compiling with TypeScript, where a typescript class that involves Meteor and Mongo is throwing an error stating "Property 'Collection' does not exist on type 'typeof Mongo'."

During the compilation of Meteor, an error in the console states "Property 'Collection' does not exist on type 'typeof Mongo'." I'm curious if anyone has encountered this issue before and how they resolved it. I am working with me ...

Issue: The Observable type does not contain a timer property

Displayed below is the code snippet: import {Component} from 'angular2/core'; import {Observable} from 'rxjs/Rx'; @Component({ selector: 'my-app', template: 'Ticks (every second) : {{ticks}}' }) export class AppCom ...

Navigate back two levels (../../) using Angular2's relative navigation

One issue I am currently experiencing with my application is related to the functionality of a back button that navigates back through multiple levels. When navigating back one level, the code snippet below works perfectly, resulting in a route URL of http ...

.observe({ action: (response) => { this.updateData = response.Items; }. what comes after this

I need some guidance on what comes next within the callback function .subscribe({ next: (data) => { this.newData = data.Items; } ...

Tips for dynamically updating the included scripts in index.html using Angular 2

As I work on incorporating an Angular website into a Cordova app, one challenge I face is getting the Cordova app to load the Angular remote URL. The index.html file for the Angular site includes the cordova.js file, which is specific to each platform - ...

JavaScript Looping through multiple files for upload will return the last file in the series

I'm currently working on implementing a multiple file upload feature using JavaScript. Within my HTML, I have the following input: <input type="file" (change)="fileChange($event,showFileNames)" multiple /> When the onChange event is triggere ...

Concerns have been raised regarding the lack of light and shadows being detected by THREE.BufferGeometry in JavaScript/th

I've been trying to generate terrain using noise functions, but I'm running into an issue where the BufferGeometry mesh isn't receiving any light from PointLight. When I switch to AmbientLight, the BufferGeometry is visible, but not with Poi ...

Issue with Ion-Searchbar visibility on Android device

My Angular (Ionic) app runs smoothly on the browser with no issues, using Angular v17.0.2 and Ionic v8.2.2. However, when I use npx cap sync and test it on a device with Android Studio, the layout looks distorted as shown in the second image I attached. I ...

Tips for utilizing programmatic object keys as TypeScript type?

Consider the object shown below: const obj = { foo: "bar", hello: "world", } and this function for processing objects: const process = (obj) => { const processedObj = {} for (const key in obj) { processedObj[`--${key}`] ...