当前位置: 代码迷 >> JavaScript >> 设置画布的高度以覆盖整个可滚动页面
  详细解决方案

设置画布的高度以覆盖整个可滚动页面

热度:111   发布时间:2023-06-13 12:30:19.0

我的canvas元素当前具有以下属性:

var c = document.getElementById("c");
    var ctx = c.getContext("2d");

    //making the canvas full screen
    c.height = window.innerHeight;
    c.width = window.innerWidth;

当我将画布放在内容超出屏幕高度的页面上时,当我向下滚动时,画布将被剪切,这是可以预期的。 如何更改height属性以使画布向下扩展到页面上的内容?

用这个:

    var c = document.getElementById("c");
    var ctx = c.getContext("2d");

    //making the canvas full screen
    c.height = document.getElementsByTagName('body')[0].scrollHeight;
    c.width = window.innerWidth;
  相关解决方案