HTML5 Canvas没有在Javascript中调整大小

时间:2018-06-18 14:51:01

标签: javascript html5 canvas

我试图将我的html5画布调整为与页面大小相同但由于某种原因,javascript不会改变其大小。这是我的代码片段:



.fit-thumbnail { object-fit: cover; }

let canvas = document.getElementById('canvas').getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

$(window).resize(function() {
  canvas.width = $(window).width();
  canvas.height = $(window).height();
});

canvas {
  border: 1px dashed black;
}




无论我做什么,画布仍然是500px,请帮忙。

1 个答案:

答案 0 :(得分:0)

试试这个:



  $(document).ready(function(){
        var w = $(window).width();
        var h = $(window).height();
        $('#canvas').width(w);
        $('#canvas').height(h);
   });

   $(window).resize(function(){
        var w = $(window).width();
        var h = $(window).height();
        $('#canvas').width(w);
        $('#canvas').height(h);
   });

#canvas {
  width: 500px;
  height: 500px;
  /* for testing */
  background: #CFF000;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<canvas id='canvas'></canvas>
&#13;
&#13;
&#13;