What might be causing me to only view a black background while using ThreeJS in conjunction with VueJS?

Hey there! I recently incorporated a new component into my project that utilizes ThreeJS. My intention is to create a visually appealing scene with a light background and two boxes. However, despite my efforts to render the boxes, all I see is a black background. Is there something I'm missing or doing wrong?

Below is the code snippet of my component:

<template>
<body>
<div id='change'>    
    <router-link class="changepage" to='/OrdreCouches'> >Retour </router-link>    
</div> 
  <div id="container"></div>
</body>
</template>



<script>
import * as THREE from 'three'
import * as dat from 'dat.gui'

export default {
  name: 'Visualisation',
  data() {
    return {
      cube: null,
      cube2: null,
      renderer: null,
      scene: null,
      camera: null,
      group: null
    }
  },
  mounted() {
    this.scene = new THREE.Scene()
    this.scene.background = new THREE.Color( 0xf0f0f0)
    this.camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      1,
      10000
    )

    this.renderer = new THREE.WebGLRenderer({ antialias:true })
    this.renderer.setSize(window.innerWidth, window.innerHeight)
    document.body.appendChild(this.renderer.domElement)

    const geometry2 = new THREE.BoxGeometry(390, 155, 240)
    const material2 = new THREE.MeshBasicMaterial({ color: 0x0246464 })
    const geometry = new THREE.BoxGeometry(390, 155, 240)
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
    this.cube = new THREE.Mesh(geometry, material)
    this.cube2 = new THREE.Mesh(geometry2, material2)
    
    this.cube.position.set(170,0,485)
    this.cube2.position.set(80,0,195)

    this.group = new THREE.Group()
    this.group.add(this.cube)
    this.group.add(this.cube2)

    this.scene.add(this.group)

    const gui = new dat.GUI()
    const camerafolder = gui.addFolder("camera")
    camerafolder.add(this.camera.position, "x", 0, 300, 0.01)
    camerafolder.add(this.camera.position, "y", 0, 300, 0.01)
    camerafolder.add(this.camera.position, "z", 0, 1500, 0.01)
    camerafolder.open()
  }
}
</script>

<style>
* {padding: 0; margin: 0}

.changepage{
  position: absolute;
  color: rgb(0, 0, 0);
  font-size: 30px;
}
.changepage:hover{
  position: absolute;
  color: rgb(53, 20, 199);
  font-size: 30px;
}
</style>

Your insights on this issue would be greatly appreciated. Thank you!

Answer №1

Instead of relying on document.body.appendxxx, consider using Vue ref to directly reference DOM elements. Additionally, make sure to adjust the size of the BoxGeometry for proper display and don't forget to set up the camera and render the scene.

To see an example in action, check out this codesandbox link based on your code:

https://codesandbox.io/s/charming-water-52r64?file=/src/components/HelloWorld.vue:863-874

// Utilize ref to access element references instead of document.getxxx/queryxxxx
this.$refs.container.appendChild(renderer.domElement);

// Render the scene
renderer.setSize(window.innerWidth, window.innerHeight);
camera.position.z = 3;
renderer.render(scene, camera);

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

An HTML form is unable to send data to a JavaScript function via

Creating a web form in HTML for user input and submission, encountering an error when trying to load the submitted data from submitted.html. Error: Cannot POST /api/submitted The code within my index.html file is as shown below: <form action="/a ...

Mongoose encounters an issue: MissingSchemaError is thrown because the Schema has not been registered for the model

Encountering a perplexing error that I can't seem to resolve. I HAVE NAMED THE REF EXACTLY AS THE MODEL: MissingSchemaError: Schema hasn't been registered for model "ParticipantStatus". Here are the models in question: ParticipantStatus model: c ...

Revamp local route variables in Express.js without the need for a page refresh

Currently, I am working on a project using node.js along with the express.js framework and EJS for template handling. Below is my routing code: app.get('/',function(req,res) { res.render('index', { matches: [], status_messag ...

Issue: There is no specified method of transportation established

I'm facing an issue while trying to create an Outlook calendar event from my application using Express.js. The error message I am receiving is [Error:No transport method defined], and eventually, the response returns as 200 success after approximately ...

Angular HttpClient does not support cross-domain POST requests, unlike jQuery which does

I am transitioning to Angular 13 and I want to switch from using jQuery.ajax to HttpClient. The jquery code below is currently functional: function asyncAjax(url: any){ return new Promise(function(resolve, reject) { $.ajax({ type: ...

Tips for utilizing the onClick event:

Currently, I'm utilizing React.createElement in order to integrate React components into an existing HTML document. Despite successfully rendering the button within the returned div, I am encountering an issue where my button fails to respond when cli ...

The peculiar characteristics of the dragLeave event in various web browsers

I observed a peculiar behavior while working with drag events in Internet Explorer 11. It seems that adding a 'width' property to the elements triggers the dragLeave event, whereas without it, the event does not fire. Can anyone shed light on why ...

Array values remain unchanged after forEach method execution

availableButtons.forEach(function(part, index) { console.log(this[index].title) // this[index].title = intl.formatMessage(this[index].title); }, availableButtons) The snippet above demonstrates looping through an array of available buttons to pr ...

Is there a way to determine if contextIsolation is activated in Electron?

In my application, I am managing two separate BrowserWindows where one of them has contextIsolation disabled. This leads to an error when attempting to utilize contextBridge.exposeInMainWorld: Error: contextBridge API can only be used when contextIsolation ...

Is there a way to dynamically refresh the Bing web API search results on a React.js application after entering a search query in the search bar (in the SER

I've been using the Bing Web Search API to access data by typing a query into a search bar I created. However, I've been facing an issue where the data isn't displaying in the search results when I submit the query. I'm specifically try ...

tips for working with vueJs and properties file

Hey, I was wondering if there's a way to store commonly used values and properties in a file like YML or a property file... ...and then reference them in VueJS components? In Java, we have Spring which allows us to use YML and property file properti ...

Disable Jquery toolstrip while the menu is open

Currently, I am utilizing the jQuery toolstrip plugin in my project. I have a requirement to disable it whenever the sidebar menu is opened. Below are the HTML codes for my menu with li tags: <div class="sidebar-wrapper" id="sidebar-wrapper"> <ul ...

NodeJS controller function to generate and send a response

I'm facing an issue with my controller method where I am unable to send a response to the user, even though the value is displaying in the console. Below is my code snippet: const hubHome = (req,res) => { const hubId = req.params.hubId; fetch ...

An error was encountered: object does not possess the function 'tooltip'

I have been attempting to display a tooltip on a textbox using jQuery scripts within a web form that utilizes a Master Page. While everything appears to be working fine when I run my code, I am encountering the above error in the resources folder of the DO ...

Checking the API every 20 seconds and halting upon receiving a certain response

In my project, I need to continuously call an API every 20 seconds starting from when the component is rendered. The API calls should only stop when a specific response is received. Here's a little more detail: The API is a GET method that returns a ...

Stop access to geojson files through targeted media queries

Using a vue.js app, I have the ability to choose locations using either a map or an input field. However, for mobile users, I want to exclude the map and only serve the search engine. The issue is that it's not very user-friendly on mobile devices be ...

Is it possible to pass in two separate variables to an ng directive in Angular?

I've been working on developing a password validation directive, but I've encountered a few challenges. You can find the code in this jsfiddle link One issue is that when I enter information into one input box, it automatically updates the othe ...

Activate the service function within the identical service

Currently, I am facing an issue while trying to call a function within the same AngularJS service. In my controller, I am able to successfully call the 'geocoding' function without any problems. However, within the 'geocoding' functio ...

The advantages of utilizing a "for" loop for appending a method to an object Array

Many JavaScript books recommend using a for loop when dealing with an Array of objects to perform a certain action. function() { var links = document.getElementsByTagName("a"); for (var i = 0, ii = links.length; i < ii; i++) { ...

Custom translations in Vue i18n

I have a VUE application with the vue-i18n plugin installed. I want to load a 'custom path' for translations when the app is initialized. However, I seem to be having trouble loading the translations. What am I missing? File: i18n/index.js /* e ...