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

时间:2015-07-19 05:02:50

标签: javascript css html5 html5-canvas

我的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属性以使画布延伸到页面上的内容?

1 个答案:

答案 0 :(得分:0)

使用此:

    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;