欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

纯js实现画一棵树的示例

程序员文章站 2022-09-08 11:51:18
用纯js画一棵树。思路: 1、一棵树的图片,作为页面背景; 2、通过html5中的canvas画布进行遮罩; 3、定时每隔10ms,从下往上清除1px的遮罩;...

用纯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>

画的过程如下:

纯js实现画一棵树的示例

以上这篇纯js实现画一棵树的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。