如何在我的页面的整个窗口上应用canvas标签?

时间:2015-09-18 11:33:33

标签: javascript php html canvas

我想使用canvas标签使用LinearGradient为我的页面添加背景,以便有人可以帮助我吗?

我用过

<script>
(function () {
    var c = document.getElementById('canvas'),
        context = c.getContext('2d');

    window.addEventListener('resize', resizeCanvas, false);

    function resizeCanvas() {
        c.width = window.innerWidth;
        c.height = window.innerHeight;
        drawin();
    }
    resizeCanvas();

    function drawin() {
        var rg = c.createLinearGradient(0, 0, c.width, c.height);
        rg.addColorStop(0, blue);
        rg.addColorStop(0, white);
        c.fillStyle = rg;
        c.fillRect(0, 0, c.width, c.height);
        c.fillStroke(0, 0, c.width, c.height);
    })();

1 个答案:

答案 0 :(得分:0)

HTML

<canvas id="canvas"></canvas>

CSS

* { margin: 0; padding: 0;}

body, html { height:100%; }

canvas {
    position:absolute;
    width:100%;
    height:100%;
}