What is the best way to adjust the canvas size within a list item?

I have successfully created a 3D model with Zdog that dynamically resizes based on the screen size. The model includes a dome and brim, which rotate smoothly as intended. Here is the code snippet:

const TAU = Zdog.TAU;

let hat = new Zdog.Illustration({
  element: '.zdog-canvas', 
  dragRotate: true,
  rotate: {x: TAU/4, z: TAU/-6},
    translate: {y: 20},
  zoom: 4,
    onResize: function( width ){
              this.zoom = width / 300;
          }
});


var dome = new Zdog.Hemisphere({
  addTo: hat,
  diameter: 80,
  stroke: 20,
  color: 'blue',
  fill: true,
});

var brim = new Zdog.Ellipse({
  addTo: dome,
  width: 95,
  height: 73,
  quarters: 2,
  stroke: 20,
  translate: {y: 34},
  rotate: {z: TAU/4},
  fill: true,
  color: 'blue'
});

function animate() {
  hat.rotate.z += 0.03;
  hat.updateRenderGraph();
  // animate next frame
  requestAnimationFrame( animate );
}

animate();
body{
    margin: 0;
}

.myDiv{
    height: 100%;
    min-height: 100%;
    margin: 0;
}

.zdog-canvas {
  display: block;
  position: absolute;
  height: 100%;
  background: #FDB;
  cursor: move;
}
<script src="https://unpkg.com/zdog@1/dist/zdog.dist.min.js"></script>

<html>
    <body>
        <div>
            <div class='myDiv'>
                <canvas class="zdog-canvas" width="800" height="500"/>
            </div>
        </div>
    </body>
</html>

However, when I attempt to place this 3D model inside a list item, it overflows beyond the screen height instead of scaling properly. My objective is to embed the 3D model within a list item alongside text content. How can I adjust my code to ensure that the model scales relative to the height of the list item?

const TAU = Zdog.TAU;

let hat = new Zdog.Illustration({
  element: '.zdog-canvas',
  dragRotate: true,
  rotate: {x: TAU/4, z: TAU/-6},
    translate: {y: 20},
  zoom: 4,
    onResize: function( width ){
              this.zoom = width / 300;
          }
});


var dome = new Zdog.Hemisphere({
  addTo: hat,
  diameter: 80,
  stroke: 20,
  color: 'blue',
  fill: true,
});

var brim = new Zdog.Ellipse({
  addTo: dome,
  width: 95,
  height: 73,
  quarters: 2,
  stroke: 20,
  translate: {y: 34},
  rotate: {z: TAU/4},
  fill: true,
  color: 'blue'
});

function animate() {
  hat.rotate.z += 0.03;
  hat.updateRenderGraph();
  // animate next frame
  requestAnimationFrame( animate );
}

animate();
body{
    margin: 0;
}

.myIcon{
    height: 100%;
    min-height: 100%;
    margin: 0;
}

.zdog-canvas {
  display: block;
  position: absolute;
  height: 100%;
  background: #FDB;
  cursor: move;
}

li{
  width: 100%;
  height: 20vh;
  background: red;
  color: black;
}
<script src="https://unpkg.com/zdog@1/dist/zdog.dist.min.js"></script>

<html>

<body>
  <div>
    <ul>
      <li>
        <div class='flexbox-container'>
          <div class='myIcon'>
            <canvas class="zdog-canvas" width="800" height="500" />
          </div>
          <h1>some text</h1>
        </div>
        <div class='flexbox-container'>
          <div class='myIcon'>
            <canvas class="zdog-canvas" width="800" height="500" />
          </div>
          <h1>some text</h1>
        </div>
      </li>
    </ul>
  </div>
</body>

</html>

Answer №1

Solution:

const PI = Math.PI;

let sphere = new Zdog.Illustration({
  element: '.zdog-canvas', // select canvas
  dragRotate: true,
  rotate: {x: PI/4, z: -PI/6},
    translate: {y: 20},
  zoom: 4,
    onResize: function( width ){
              this.zoom = width / 300;
          }
});


var hemisphere = new Zdog.Hemisphere({
  addTo: sphere,
  diameter: 80,
  stroke: 20,
  color: 'blue',
  fill: true,
});

var brim = new Zdog.Ellipse({
  addTo: hemisphere,
  width: 95,
  height: 73,
  quarters: 2,
  stroke: 20,
  translate: {y: 34},
  rotate: {z: PI/4},
  fill: true,
  color: 'blue'
});

function animate() {
  sphere.rotate.z += 0.03;
  sphere.updateRenderGraph();
  requestAnimationFrame( animate );
}

animate();
body{
    margin: 0;
}

.flexbox-container{
  display: flex;
  flex-direction: row;
}

.myIcon{
  flex: 1;
    height: 100%;
    min-height: 100%;
    margin: 0;
}

.text-div{
  flex: 1;
}

.zdog-canvas {
  display: block;
  position: absolute;
  height: 100%;
  max-height: 20vh;
  background: #FDB;
  cursor: move;
}

.list-item{
  width: 100%;
  height: 20vh;
  background: red;
  color: black;
  margin: 1vw 0;
}
<script src="https://unpkg.com/zdog@1/dist/zdog.dist.min.js"></script>

<html>

<body>
  <div>
    <ul>
      <li class="list-item">
        <div class='flexbox-container'>
          <div class='myIcon'>
            <canvas class="zdog-canvas" width="800" height="500" />
          </div>
          <div class="text-div"> stuff</div>
        </div>
      </li>
      <li class="list-item">
        <div class='flexbox-container'>
          <div class='myIcon'>
            <canvas class="zdog-canvas" width="800" height="500" />
          </div>
          <div class="text-div"> stuff</div>
        </div>
      </li>
    </ul>
  </div>
</body>

</html>

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 determine if audio is currently playing in a UIWebView on iOS?

I am using a UIWebView in my iOS app to load an HTML web link: . However, I am struggling to detect the playing state of the audio within the UIWebView. All I need is a way to determine if the audio in the UIWebView is currently playing or not in my iOS a ...

Struggling with implementing responsive mobile buttons that stack in a column? Here's the solution

My content is nearly responsive on all screen sizes, but I'm having trouble getting the buttons to stack neatly in a column. Can anyone provide suggestions on how to fix this issue? You can view the current progress below. Thank you in advance for any ...

Exploring the Height Settings in Bootstrap

After reviewing this code, I stumbled upon an element that has left me puzzled. To gain a better understanding, you can view a live preview of this website. My inquiry pertains to the height setting of the "Download" section (the vibrant yellow one). I me ...

What is the reason why the VIEW SOURCE feature does not display filled dropdown list boxes?

The following code functions well in terms of dynamically populating two dropdown list boxes. Initially, the getBuildings function is called to make a request to the getBuildings.php file. This action fills the buildingID dropdown list box with options. ...

The fixed blocks are covering the Blueimp gallery within the relative container

Check out this DEMO I created to showcase something interesting. In this demo, you will find a basic example of setting up a blueimp gallery, a navigation bar, and a button. <div class="nav">nav</div> <div class="wrapper"> <div id ...

Is there a way to ensure that my JavaScript code remains permanent?

Is there a way to have the website remember the user's theme preference between visits? For example, if they choose the dark theme, can it automatically be loaded the next time they access the site? This is my current javascript code for handling the ...

Is there a way to display the message "no results" when none of the div elements are visible?

I am facing an issue with displaying a message on my webpage. I have multiple divs that are being filtered using filters on a webshop. My goal is to show the message "no results" when all divs have their display set to none. Can anyone provide guidance on ...

What is the best way to assign a background color to each tr element using JavaScript?

I have a list of table rows with different background colors like <tr bgcolor="#OC6110"> <tr bgcolor="#000000"> <tr bgcolor="#FFFFFF"> Is there a way to assign unique background colors to each tr element without specifying its id value ...

Can the font family of <body> be customized in Bootstrap 5?

I am facing an issue where the Font-Family changes for my heading tags, but the body tag keeps getting cancelled out by the error "_reboot.scss:50". When I inspect my webpage and toggle off the default "enter code herefont-family: var(--bs-body-font-family ...

Navigation menu extending all the way to the far right of the webpage

This particular Angular 8 MVC C# application has been giving me a run for my money. I find myself caught in a cycle of fixing one issue only to have another one pop up in its place. Within the css file, I am setting *, html, body { font-size: 12px; font- ...

The progress bar fails to update once it hits 100%

My circle progress bar is not resetting to start over when it reaches 100%. The code seems to work fine in my browser, but for some reason it's not working on JSFiddle. Here is the link: https://jsfiddle.net/BrsJsk/5e9hs69y/5/ window.onload = fu ...

Implementing FAQs on ASP.NET MVC is my latest project

I have stored all the questions and answers in a SQL Server table, and it is fetching them perfectly. The FAQ user interface displays all the questions correctly from the table. You can see the interface in action by clicking on the following links: Questi ...

Can you point out the syntax error in my flex code?

I'm attempting to redesign my layout grid using flex. It seems that the flex code I commented out is incorrect, possibly due to an issue with setting the width in my .container class. The grid auto property adjusts the columns automatically within my ...

What is the process of utilizing PHP to generate and insert HTML tags into a separate HTML file?

I am facing an issue with two files in my project - input.php and output.html. Within the input.php file, the code is as follows: <?php $file="output.html"; $doc=new DOMDocument('1.0'); $doc->loadHTMLFile($file); $elements = ...

I'm unsure of how to eliminate the border outline on this particular text area

The textarea has an unwanted blue outline, and despite trying outline:none; in the CSS, it's still there. See the code below: #floatingTextarea { border: none; outline: none; } <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/ ...

What sets npm install apart from manual installation?

I've been exploring requirejs recently. I'm trying to decide between installing it using npm install requirejs or manually downloading it from the website. Are there any differences between the two methods? Are there any advantages or disadvantag ...

Is there a way to display the JavaScript widget exclusively on the homepage or main page

How can I limit the display of a Twitter widget on my blog page to only show on the main page and hide it when visitors navigate to sub-pages or individual blog entries? ...

The problem of creating a custom pop-up with jQuery

I'm running into an issue while trying to develop a custom CSS and jQuery popup. Despite my efforts, the popup isn't functioning as intended. Take a look at my code below and point out where I may have gone wrong. What I aim to achieve is a popup ...

An effective method for creating responsive layouts for custom-shaped HTML elements in a board game

Can anyone guide me on aligning spaces responsively on a board in HTML using Angular 1.x? I've tried using vw/vh's and %s, but consistency breaks on different view sizes. Any straightforward suggestions to address this issue? Avoiding multiple c ...

Adjusting the appearance of a JavaScript element based on its hierarchy level

Currently, I am utilizing Jqtree to create a drag and drop tree structure. My main goal is to customize the appearance of the tree based on different node levels. Javascript Tree Structure var data = [ { name: 'node1', id: 1, chi ...