zoukankan      html  css  js  c++  java
  • html5学习-万花筒

    今天继续放上学习时候写的实例之一:万花筒。(主要联系html5的矩阵变换)

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4 <meta charset="utf-8">
     5 <title>无标题文档</title>
     6 <style type="text/css">
     7 *{ padding:0; margin:0}
     8 #can{ background:#EBEBEB}
     9 
    10 </style>
    11 
    12 </head>
    13 <body>
    14  <canvas id="can" width="900" height="900"></canvas>
    15 
    16 </body>
    17 
    18 <script>
    19 
    20  var canid=document.getElementById("can");
    21  var can=canid.getContext("2d");
    22   var arr=[];
    23  can.translate(400,350);
    24  setTim();
    25  function  setTim(){
    26 
    27      var timer=setInterval(function(){
    28         
    29             can.clearRect(0,0,900,900);
    30         for(var i=0;i<arr.length;i++){
    31 
    32                can.save();
    33                 //can.translate(200,200);
    34                 
    35                 can.rotate(arr[i].x*Math.PI/180);
    36                 
    37                 can.fillStyle=arr[i].c;
    38                 can.fillRect(arr[i].y,arr[i].y,arr[i].z,arr[i].z);
    39                 can.stroke(); 
    40                 can.restore();
    41             
    42         }
    43 
    44     },60);
    45       
    46      
    47 }
    48 
    49 
    50 setInterval(function(){
    51          var color="rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")";
    52          arr.push({x:0,y:150,z:50,c:color}); 
    53          
    54  },50);
    55 
    56 setInterval(function(){
    57 
    58     
    59     for(var i=0;i<arr.length;i++){
    60     
    61         if(arr[i].y<=0){
    62            arr.splice(i,1);    
    63             continue;
    64         }
    65         arr[i].x=arr[i].x+3;
    66        arr[i].y=arr[i].y-0.3
    67        arr[i].z=arr[i].z-0.1;
    68 
    69     
    70     }
    71     
    72  
    73  
    74 },60);
    75 
    76 
    77  
    78 </script>
    79 
    80 
    81 </html>
    一个不敬业的前端攻城狮
  • 相关阅读:
    设计模式之工厂模式-抽象工厂(02)
    1036 跟奥巴马一起编程 (15 分)
    1034 有理数四则运算 (20 分)
    1033 旧键盘打字 (20 分)
    1031 查验身份证 (15 分)
    大学排名定向爬虫
    1030 完美数列 (25 分)二分
    1029 旧键盘 (20 分)
    1028 人口普查 (20 分)
    1026 程序运行时间 (15 分)四舍五入
  • 原文地址:https://www.cnblogs.com/chaoming/p/3414461.html
Copyright © 2011-2022 走看看