Utilizing Vue-cli and Three.js to Load STL Files

I'm facing an issue where I can't seem to load an STL file into a three.js scene that is being created via vue-cli.

The project setup involves using vue-cli 'vue init webpack ProjectName', 'cd ProjectName', 'npm install three --save' and replacing the 'HelloWorld' component with this code snippet.

The STL file is located in the same folder as this vue component.

<template>
  <div id="container"></div>
</template>
<script>
import * as THREE from 'three'
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js';

export default {
  name: 'ThreeTest',
  data() {
    return {
      cube: null,
      renderer: null,
      scene: null,
      camera: null
    }
  },
  methods: {
    init: function() {
      this.scene = new THREE.Scene()
      this.camera = new THREE.PerspectiveCamera(
        75,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      )

      this.renderer = new THREE.WebGLRenderer()
      this.renderer.setSize(window.innerWidth, window.innerHeight)
      document.body.appendChild(this.renderer.domElement)

      const geometry = new THREE.BoxGeometry(1, 1, 1)
      const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
      this.cube = new THREE.Mesh(geometry, material)
      var loader = new STLLoader();
            loader.load( 'test.stl', function ( geometry ) {
                var material = new THREE.MeshPhongMaterial( {
                      ambient: 0xff5533, 
                      color: 0xff5533, 
                      specular: 0x111111,
                      shininess: 200 }                                         
        );
        mesh = new THREE.Mesh( geometry, material );
        mesh.position.set(0, 100, 0);
        this.scene.add(mesh)
      }, undefined, function ( error ) {console.error( error );} );
      // this.scene.add(this.cube)

      this.camera.position.z = 5

      const animate = function() {}
    },
    animate: function() {
      requestAnimationFrame(this.animate)

      this.cube.rotation.x += 0.01
      this.cube.rotation.y += 0.01

      this.renderer.render(this.scene, this.camera)
    }
  },
  mounted() {
    this.init()
    this.animate()
  }
}
</script>

Encountering a black screen with the following error message:

RangeError: Invalid typed array length: 5202511335
    at new Float32Array (<anonymous>)
    at parseBinary (STLLoader.js?e2c6:199)
    at STLLoader.parse (STLLoader.js?e2c6:396)
    at Object.eval [as onLoad] (STLLoader.js?e2c6:87)
    at XMLHttpRequest.eval

If anyone has any guidance or solutions for this problem, I would greatly appreciate it!

Thank you! :)

Best regards

Answer №1

Solution

After updating to the latest version of vue-cli, I added a "public" folder which resolved the issue. Additionally, installing "vue-3d-model" by hujiulong fixed the problem. Everything is now working smoothly!

It seems that Vue may have restrictions on permissions if a public folder is not present.

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 there a way to prevent the window.on('beforeUnload') event from triggering when using the <a> tag?

For my project, I require a user confirmation alert to appear when the user attempts to close the window or tab using the X button. However, the window.on('beforeUnload') function also triggers for hyperlinks. How can I prevent the leave page al ...

Trigger AJAX request upon selecting a value from the dropdown menu

I am in the process of creating a basic dropdown menu that is only visible to users on mobile devices when they visit our website. The main objective is to allow users to select a value and then have that value sent via AJAX. For desktop devices, I have s ...

The webpage hosted on Heroku displays a blank background, requiring users to refresh the page with an F5 key

I developed a group chat program using Python microframework flask, vanilla JavaScript, and Flask-SocketIO. The program is deployed on heroku and can be accessed here: . After deployment, I encountered the following issue: While the program worked fine o ...

Invalid extra parameters in the $.ajax function

I am attempting to access a web service in order to retrieve some data. I must include this URL using the GET method: http://localhost/ecosat/ws/api.php?t=vw_motorista However, when I check in Chrome Developer Tools, the link is shown as: http://localho ...

Building a webRTC peer using only a JavaScript interpreter, no browser required

My goal is to develop a WebRTC peer that functions solely as a listener/recorder without any graphical presentation like HTML/CSS involved. If achievable using the WebRTC JavaScript APIs, I am curious about which standalone JavaScript engine I should cons ...

Manually update the DOM rather than depending on React's reconciliation process

Creating a React component that wraps a complex rendering API is my current goal. This specific API has the ability to render a plot into HTML+SVG and also offers functionality to update the plot with new data efficiently. I am keen on utilizing this updat ...

Finding the midpoint of the plotted GeoJSON route using Leaflet

I am currently working on a project that involves showcasing a map centered on a specific country and its neighboring countries using GeoJSON. I am curious if there is a way to determine the center point of only the visible portions of a country? One aspe ...

Exporting Data from Angular's ui-grid to Excel

I am currently facing a scenario where I receive significant response data from a REST service to the Angular UI-Grid. Rather than rendering all the data simultaneously, I would like to display it in batches, while still having the option to export all t ...

changing session variables in JavaScript

My index.php page contains all my PHP functions, JavaScript code, and HTML tags. I am trying to figure out how to call a PHP function (addToCart($itemID)) to update session variables when the user clicks on a button (add to cart). Can you provide guidanc ...

Strategies for effectively transferring information from a directive template to a controller

I'm currently working on developing a date-picker that consists of two separate date pickers - one for the start date and another for the end date. Each datepicker element generates a template with two input tags. I want to be able to pass data from t ...

The input element is not assigning an ID or class

My MySQL query generates input rows in a table, allowing users to edit them. However, I want users to be able to add new rows as well. To achieve this, I created a JavaScript function that adds a new row when a button is clicked. The issue I'm facing ...

Calculate the time difference between two dates using provided string inputs

Looking to calculate the duration between two dates. Input: December 2016 to February 2018(string) Expected Output: 1 year 2 months Is there a more efficient method for this calculation? ...

Is it possible to change between universal and spa mode based on the query string?

Currently, I am working on a Nuxt.js application set to universal mode and deployed as a static website using nuxt generate. The app is pulling data from a GraphQL API linked to a CMS, and everything is functioning properly. Whenever content is updated in ...

Difficulty encountered while managing dropdown functionality in Protractor using TypeScript

I'm encountering some difficulties when it comes to selecting a dropdown in Protractor. Here's the structure of my DOM: https://i.stack.imgur.com/qK8sT.png This is the XPath I'm using to select the dropdown with the value "Yes": //label[ ...

Comparing strings with if-else statement

I am having trouble comparing strings in this array. For some reason, the strings are never equal. var person = ["Sam", "John", "Mary", "Liz"]; var searchedName = prompt("Enter name"); var resultMessage = ""; for (index in person) { var currentName = ...

Node.js environment variable returns a value of 'undefined'

One method I use to safeguard my bot tokens is by utilizing dotenv to keep them separate from the main app. However, a problem arises when running the code - the environment variables appear as undefined, resulting in an error message: Error: An invalid to ...

Switch between two distinct menus (Reactjs + Material UI)

Recently, I designed a robust menu system that includes a Switcher feature. The concept is straightforward - if the switch is turned off, display the 1st menu; and if it's on, show the second menu. However, there seems to be an issue. When the switch ...

tips for concealing the shadow of a draggable div during the dragging process

I am facing an issue with a draggable div. When I drag the div, the shadow also moves along with it. I want to find a way to hide this shadow while dragging. <div draggable="true" (dragstart)="mousedown($event)" (drag)="dra ...

The values in the $scope of my Angular app are mysteriously not being displayed, with no error message

I am currently learning the MEAN stack and experimenting with building an app based on a demo from a course I found on thinkster.io. I checked that there is a database using curl, which seems fine, but Angular doesn't bind them together. Here is the ...

Is there a way to efficiently navigate a local JSON file using React JS?

What is the best way to extract data from a JSON file and utilize it within my code? I attempted importing the file and logging it in the console, but all I get is Object {}: import jsonData from "./file.json"; console.log(jsonData); This is the content ...