js画一棵树

  用纯js画一棵树。思路:

1、一棵树的图片,作为页面背景;

2、通过html5中的canvas画布进行遮罩;

3、定时每隔10ms,从下往上清除1px的遮罩;

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My JS tree</title>
<style>
body {
    width: 1000px;
    height: 570px;
    background-image: url(image/tree.png);
    background-size: 1000px, 570px;
    background-repeat: no-repeat;
    margin-top: 0px;
    margin-bottom: 0px;
}
</style>
</head>

<body>
    <canvas id="mycanvas" width="1000px" height="570px"></canvas>

    <script>
        var c = document.getElementById("mycanvas");
        var ctx = c.getContext("2d");

        ctx.fillStyle = "#ffffff";
        ctx.fillRect(0, 0, c.width, c.height);//矩形遮住背景图像

        var y = c.height;

        window.setInterval(function() {
            if (y > 2) {
                ctx.clearRect(0, y - 1, c.width, y);
                y = y - 1;
            } else {
                window.clearInterval(this);//清除定时
                ctx.clearRect(0, 0, c.width, c.height);
            }
        }, 10);//每隔10ms清除1px的遮照
    </script>
</body>
</html>

 

  附图片:

  画的过程如下:

转载于:https://www.cnblogs.com/mistwalker/p/7473478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值