HTML5_树(HTML5 高级程序设计)

 

使用html5的canvas实现这么一个图ExpandedBlockStart.gif

复制代码
  1  < html >
  2      < meta  charset ="utf-8"   />
  3      < title >Canvas tree </ title >
  4      < canvas  id ="tree"  width ="500"  height ="500" ></ canvas >
  5      < script >
  6       function  createCanopyPath(context)
  7      {
  8          context.beginPath();
  9          
 10          context.moveTo( - 25 , - 50 );
 11          context.lineTo( - 10 , - 80 );
 12          context.lineTo( - 20 , - 80 );
 13          context.lineTo( - 5 - 110 );
 14          context.lineTo( - 15 , - 110 );
 15          
 16          context.lineTo( 0 - 140 );
 17          
 18          context.lineTo( 15 , - 110 );
 19          context.lineTo( 5 - 110 );
 20          context.lineTo( 20 , - 80 );
 21          context.lineTo( 10 , - 80 );
 22          context.lineTo( 25 , - 50 );
 23          
 24          context.closePath();    
 25      }
 26      
 27       function  drawTree(context)
 28      {
 29           var  trunkGradient  =  context.createLinearGradient( - 5 - 50 5 - 50 );
 30          
 31          trunkGradient.addColorStop( 0 ' #663300 ' );
 32          trunkGradient.addColorStop( 0.4 ' #996600 ' );
 33          trunkGradient.addColorStop( 1 ' #552200 ' );
 34          
 35          context.fillStyle  =  trunkGradient;
 36          context.fillRect( - 5 , - 50 , 10 , 50 );
 37          
 38           var  canopyShadow  =  context.createLinearGradient( 0 - 50 0 0 );
 39          
 40          canopyShadow.addColorStop( 0 ' rgba(0,0,0,0.5) ' );
 41          canopyShadow.addColorStop( 0.2 ' rgba(0,0,0,0.0) ' );
 42          
 43          context.fillStyle  =  canopyShadow;
 44          context.fillRect( - 5 , - 50 , 10 , 50 );
 45          
 46          createCanopyPath(context);
 47          context.lineWidth  =   4 ;
 48          context.lineJoin  =   ' round ' ;
 49          context.strokeStyle  =   " #663300 " ;
 50          context.stroke();
 51          
 52          context.fillStyle  =   " #339900 " ;
 53          context.fill();
 54          
 55          context.save();
 56          context.transform( 1 , 0 , - 0.5 , 1 , 0 , 0 );
 57          context.scale( 1 , 0.6 );
 58          context.fillStyle  =   ' rgba(0,0,0,0.2) ' ;
 59          context.fillRect( - 5 , - 50 , 10 , 50 );
 60          
 61          createCanopyPath(context);
 62          context.fill();
 63          context.restore();
 64      }
 65      
 66       function  drawRoad(context)
 67      {
 68          context.save();
 69          context.translate( - 10 , 350 );
 70          context.beginPath();
 71          
 72          context.moveTo( 0 , 0 );
 73          context.quadraticCurveTo( 170 - 50 260 - 190 );
 74          
 75          context.quadraticCurveTo( 310 , - 250 , 410 , - 250 );
 76          
 77          context.strokeStyle  =   " #663300 " ;
 78          context.lineWidth  =   20 ;
 79          context.stroke();
 80          
 81          context.restore();
 82      }
 83      
 84      
 85       function  drawTrails()
 86      {
 87           var  canvas  =  document.getElementById( " tree " );
 88           var  context  =  canvas.getContext( " 2d " );
 89          
 90          context.save();
 91          
 92          context.translate( 130 , 250 );
 93          drawTree(context);
 94          context.restore();
 95          
 96          context.save();
 97          context.translate( 260 , 500 );
 98          
 99          context.scale( 2 , 2 );
100          drawTree(context);
101          context.restore();
102          
103          drawRoad(context);
104          
105          context.save();
106          context.font  =   " 60px impact " ;
107          context. fillStyle  =   " #996600 " ;
108          context.textAlign  =   " center " ;
109          
110          context.shadowColor  =   " rgba(0,0,0,0.2) " ;
111          context.shadowOffsetX  =   15 ;
112          context.shadowOffsetY  =   10 ;
113          context.shadowBlue  =   2 ;
114          
115          context.fillText( " Happy! " 200 60 400 );
116          context.restore();
117      }
118      
119      drawTrails();
120       </ script >
121  </ html >
复制代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值