Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

364
Visualizações
Three.js Geometry isn't showing in browser, but the scene is

was following a tutorial and got the whole scene working in the body tag, then I tried moving it inside a div, and now only the scene will render. I'm not getting any errors, and I've console logged everything to make sure it is available to access. But I can't see why I'm nothing displayed.

// console.log ("Java Script is working!")

// get div for 3D area
const container = document.querySelector('#container')
// console.log (container)
var scene = new THREE.Scene()

var camera = new THREE.PerspectiveCamera(
  75,
  container.innerWidth / container.innerHeight,
  0.1,
  1000
)
camera.position.z = 3

var renderer = new THREE.WebGLRenderer({
  antialias: true
})
renderer.setClearColor('#0D1033')
renderer.setSize(container.innerWidth, container.innerHeight)

container.appendChild(renderer.domElement)

container.addEventListener('resize', () => {
  renderer.setSize(container.innerWidth, container.innerHeight)
  camera.aspect = container.innerWidth / container.innerHeight
  camera.updateProjectionMatrix()
})


var geometry = new THREE.BoxGeometry(10, 1, 1)
var material = new THREE.MeshLambertMaterial({
  color: 0x90e9f9,
  side: THREE.DoubleSide
})
var mesh = new THREE.Mesh(geometry, material)

scene.add(mesh)


var light = new THREE.PointLight(0xffffff, 5, 500)
light.position.set(10, 0, 25)
scene.add(light)

var render = function() {
  requestAnimationFrame(render)
  renderer.render(scene, camera)
}
console.log(mesh)
console.log(camera)

render()
* {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
  height: 100vh;
}

canvas {
  display: block;
  height: 100%;
  width: 100%;
}

#container {
  background-color: black;
  height: 100vh;
}
<!DOCTYPE html>

<body>
  <div id="container">

  </div>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/102/three.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.2/TweenMax.min.js"></script>
  <script src="main.js"></script>
</body>

</html>

I'm trying to get this working first so I know this can work before I move it into my main project, where I'm trying to have a 3d space inside a div on the website, but not take up the full page

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can only use innerWidth and innerHeight on the window object. Use clientWidth and clientHeight instead:

const container = document.querySelector('#container')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(
  75,
  container.clientWidth / container.clientHeight,
  0.1,
  1000
)
camera.position.z = 3

const renderer = new THREE.WebGLRenderer({
  antialias: true
})
renderer.setClearColor('#0D1033')
renderer.setSize(container.clientWidth, container.clientHeight)

container.appendChild(renderer.domElement)

container.addEventListener('resize', () => {
  renderer.setSize(container.clientWidth, container.clientHeight)
  camera.aspect = container.clientWidth / container.clientHeight
  camera.updateProjectionMatrix()
})


const geometry = new THREE.BoxGeometry(10, 1, 1)
const material = new THREE.MeshLambertMaterial({
  color: 0x90e9f9,
  side: THREE.DoubleSide
})
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)


const light = new THREE.PointLight(0xffffff, 5, 500)
light.position.set(10, 0, 25)
scene.add(light)

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

render()
* {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
  height: 100vh;
}

canvas {
  display: block;
  height: 100%;
  width: 100%;
}

#container {
  background-color: black;
  height: 100vh;
}
<script src="https://cdn.jsdelivr.net/npm/three@0.133.1/build/three.min.js"></script>
<div id="container">

</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda