如何更改Stats.js大小?

时间:2019-03-23 18:24:52

标签: javascript css html5-canvas size

我正在尝试更改Stats.js画布的大小。我尝试使用CSS,但是无法在特定的canvas元素上分配ID,因此请尝试以下操作:

HTML:

<div class="modal_container" id="modal_graph">        
   <img id="moveModalImage" src="images/moveIcon.png" alt="Move modal window."/>
   <span onclick='document.getElementById( "modal_graph" ).style.display= "none";' class="close-button">×</span>
   <p>Performance over Time Graph</p>
   <hr>

   <div id="modal_4-content">
     <!-- Add Stats.GUI here -->

   </div>

</div>

CSS:

#modal_4-content canvas{
    width: 240px; //Doesn't work
    height: 150px; //Doesn't work
    float: left; //Works fine
    padding-right: 60px; //Works fine
}

JS:

function createStatsGUI(){

    var thisParent = document.getElementById("modal_4-content");

    //Create new Graph (FPS, MS, MB)
    stats1 = new Stats();

    //Display different panel
    stats1.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
    stats1.domElement.style.width = '200px';
    stats1.domElement.style.height = '200px';

    //Add Stats to Document - modal 4
    thisParent.appendChild( stats1.domElement );  
}

如果您知道如何更改此设置,请告诉我。感谢您的宝贵时间。

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试

 thisParent.firstChild.width =100;
 thisParent.firstChild.height=100;

代替

thisParent.child.style.height=100

在我看来是html

 <div id="modal_4-content"><canvas></canvas>
 </div>

父母与孩子之间没有空格

  <div id="modal_4-content"><canvas></canvas> 
           //canvas is the first element  here try Ur update 1,2,3
          //If not go with queryselector
   </div>

查询选择器

  var canvas = thisParent.querySelector('canvas');
  canvas.width = 500;
  canvas.height = 500;

绘制画布样式不同不会影响调整大小的画布。您需要直接内联属性来更改调整大小的宽度和高度。

答案 1 :(得分:0)

我认为您可以使用.childnodes

获取所有子节点

答案 2 :(得分:0)

这解决了问题:

function createStatsGUI(){

    //Create new Graphs (FPS, MS, MB)
    statsGUI = new Stats();
    statsGUI.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom

    var thisParent = document.getElementById("modal_4-content");
    thisParent.appendChild( statsGUI.domElement );

    var statsALL = document.getElementById("modal_4-content").querySelectorAll("canvas");

    for(var i=0; i<statsALL.length; i++){
        statsALL[i].style.width = "100%";
        statsALL[i].style.height = "160px";
        //...
    }
}

enter image description here