打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JavaScript图形实例:H分形

      H分形是由一个字母H演化出迷宫一样场景的分形图案,其构造过程是:取一个中心点(x,y),以此中心点绘制一条长为L的水平直线和两条长为H的竖直直线,构成一个字母“H”的形状;再以两条竖直直线的上下共4个端点为中心点,分别绘制一条长为L/2的水平直线和两条长为H/2的竖直直线;重复以上操作直至达到要求的层数,可以绘制出H分形图案,如图1所示。

图1  H分形图案的生成

      H分形采用递归过程易于实现,编写如下的HTML代码。

<!DOCTYPE html>

<head>

<title>H分形</title>

</head>

<body>

<canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;">

</canvas>

<script type="text/javascript">

   var canvas = document.getElementById('myCanvas');

   var ctx = canvas.getContext('2d');

   ctx.strokeStyle = "red";

   ctx.lineWidth = 3;

   var maxdepth =4;

   var curdepth = 0;

   function drawH(x,y,length,hight)

   {

        curdepth++;

        ctx.beginPath();

        ctx.moveTo(x-length/2,y);

        ctx.lineTo(x+length/2,y);

        ctx.moveTo(x-length/2,y-hight/2);

        ctx.lineTo(x-length/2,y+hight/2);

        ctx.moveTo(x+length/2,y-hight/2);

        ctx.lineTo(x+length/2,y+hight/2);

        ctx.stroke();

        if(curdepth <= maxdepth)

        {

            drawH(x-length/2,y-hight/2,length*0.5,hight*0.5);

            drawH(x-length/2,y+hight/2,length*0.5,hight*0.5);

            drawH(x+length/2,y-hight/2,length*0.5,hight*0.5);

            drawH(x+length/2,y+hight/2,length*0.5,hight*0.5);

        }

        curdepth--;

   }

   drawH(250,250,240,180);

</script>

</body>

</html>

      在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出的H分形图案,如图2所示。

图2  递归深度maxdepth =4的H分形 

      将H分形的生成过程进行动态展示,编写如下的HTML文件。

<!DOCTYPE html>

<head>

<title>H分形</title>

</head>

<body>

<canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;">

</canvas>

<script type="text/javascript">

   var canvas = document.getElementById('myCanvas');

   var ctx = canvas.getContext('2d');

   ctx.strokeStyle = "red";

   ctx.lineWidth = 3;

   var maxdepth =0;

   var curdepth = 0;

   function drawH(x,y,length,hight)

   {

        curdepth++;

        ctx.beginPath();

        ctx.moveTo(x-length/2,y);

        ctx.lineTo(x+length/2,y);

        ctx.moveTo(x-length/2,y-hight/2);

        ctx.lineTo(x-length/2,y+hight/2);

        ctx.moveTo(x+length/2,y-hight/2);

        ctx.lineTo(x+length/2,y+hight/2);

        ctx.stroke();

        if(curdepth <= maxdepth)

        {

            drawH(x-length/2,y-hight/2,length*0.5,hight*0.5);

            drawH(x-length/2,y+hight/2,length*0.5,hight*0.5);

            drawH(x+length/2,y-hight/2,length*0.5,hight*0.5);

            drawH(x+length/2,y+hight/2,length*0.5,hight*0.5);

        }

        curdepth--;

   }

   function go()

   {

        ctx.clearRect(0,0,canvas.width,canvas.height);  

        drawH(250,250,240,180);

        maxdepth++;

        curdepth=0;     

        if (maxdepth>4)

        {

           maxdepth=0;

       }

   }

   window.setInterval('go()', 1500);

</script>

</body>

</html>

      在浏览器中打开包含这段HTML代码的html文件,在浏览器窗口中呈现出如图3所示的H分形动态生成效果。 

 

图3  H分形图案动态生成 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
清除Canvas
canvas
JavaScript模拟实现12306图片验证码 | 逐梦博客
[HTML5]模仿Flash的帧式动画2[实例]笔触轨迹
canvas实现手机的手势解锁(步骤详细)
[代码艺术]17行代码的贪吃蛇小游戏
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服