Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Threejs Renderer canvas does not fit assigned div

I am trying to display a threejs scene inside nested div. Even changing renderer size, I keep having the canvas occupy too much space.

Initializing renderer:

// Init renderer
renderer = new THREE.WebGLRenderer({
antialias: true,
// canvas: document.getElementById("buildingDisplay")
})
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(document.getElementById("buildingDisplay").getBoundingClientRect().width, 
document.getElementById("buildingDisplay").getBoundingClientRect().height)
document.getElementById("buildingDisplay").appendChild(renderer.domElement)

The canvas containing the scene (in the 'buildingDispplay' div) has a size that fills the whole parent div.

HTML div structure:

<div id="parent" style="display:flex; width: 100%; height: 100%;">
    <div id="blastMapID" style="height: 100%;">
        <button type="button" id="expTA" style="margin-top: 100px;"><strong>LoadTargetArea</strong></button>
    </div>
    <div id="buildingDetails" style="display:none;  width: 100%; height: 100%; border:1px solid black;">      
        <button id="closeDiv">Close</button>
        <div id="buildingDisplay" style="width: 100%; height: 60%; background-color: rgb(187, 187, 187);">
            <!-- <canvas id="renderCanvas" style="width: 100%; height: 100%;"></canvas> -->
        </div>
        <div id="statsDisplay" style="width: 100%; height: 40%; background-color: rgb(255, 255, 255);"></div>
    </div>
</div>
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

maybe try to limit the size of your canvas container with fixed pixels/value.

about 4 years ago · Santiago Gelvez Report

0

In the end I managed to make it work like this:

Renderer in JS file:

renderer = new THREE.WebGLRenderer({
   antialias: true,
   canvas: document.getElementById("renderCanvas")
})

HTML:

 <div id="parent" style="display:flex; width: 100%; height: 100%;">
    <div id="blastMapID" style="height: 100%;">
        <button type="button" id="expTA" style="margin-top: 100px;"><strong>LoadTargetArea</strong></button>
    </div>
    <div id="buildingDetails" style="display:none;  width: 100%; height: 100%; border:1px solid black;">      
        
        <div id="buildingDisplay" style="max-width: 100%; max-height: 60%; background-color: rgb(255, 255, 255);">
            <canvas id="renderCanvas" style="max-width: 100%; max-height:600px;"></canvas>
            <button id="closeDiv">Close</button>
        </div>
        <div id="statsDisplay" style="width: 100%; height: 200px; background-color: rgb(255, 255, 255);"></div>
    </div>
</div>
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!