Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <title>Minimal.js</title>
  4. <script>
  5.    
  6.  
  7.  
  8.  
  9. //RUNS ON LOAD
  10.     function init(){
  11.         //GLOBAL VARIABLES
  12.         //WINDOW PROPERTIES
  13.             var width=window.innerWidth;
  14.             var height=window.innerHeight;
  15.         //END OF WINDOW PROPERTIES
  16.         //DOM VARIABLES
  17.             var canvas=document.getElementById('canvas');
  18.             var ctx=canvas.getContext('2d');
  19.         //END OF DOM VARIABLES
  20.         //SETTING WIDTH & HEIGHT OF CANVAS = WINDOW
  21.             canvas.width=width;
  22.             canvas.height=height;
  23.         //END OF SETTING WIDTH & HEIGHT OF WINDOW
  24.         //MATH VARIABLES
  25.             var pi=Math.PI;
  26.  
  27.         //END MATH VARIABLES
  28.  
  29.  
  30.     //GLOBAL VARIABLES END
  31.        
  32.                 rectangle=[{x:0,y:0,width:25,height:25,vx:1,vy:1},{x:width-25,y:height-25,width:25,height:25,vx:-1,vy:-1}];
  33.                 //DRAWING MAIN BACKGROUND
  34.                     function drawBG(){
  35.                        
  36.                         for(var i=0;i<=rectangle.length-1;i++){
  37.                             var hexa=[0,0,0,0,0,0]
  38.                             for(var l=0;l<=5;l++){
  39.                             var random=Math.floor(Math.random()*16);
  40.  
  41.                             switch(random){
  42.                                 case 0:
  43.                                  hexa[l]=0;
  44.                                  break;
  45.                                  case 1:
  46.                                  hexa[l]=1;
  47.                                  break;
  48.                                  case 2:
  49.                                  hexa[l]=2;
  50.                                  break;
  51.                                 case 3:
  52.                                  hexa[l]=3;
  53.                                  break;
  54.                                 case 4:
  55.                                  hexa[l]=4;
  56.                                  break;
  57.                                 case 5:
  58.                                  hexa[l]=5;
  59.                                  break;
  60.                                 case 6:
  61.                                  hexa[l]=6;
  62.                                  break;
  63.                                 case 7:
  64.                                  hexa[l]=7;
  65.                                  break;
  66.                                 case 8:
  67.                                  hexa[l]=8;
  68.                                  break;
  69.                                 case 9:
  70.                                  hexa[l]=9;
  71.                                  break;
  72.                                 case 10:
  73.                                  hexa[l]='a';
  74.                                  break;
  75.                                 case 11:
  76.                                  hexa[l]='b';
  77.                                  break;
  78.                                 case 12:
  79.                                  hexa[l]='c';
  80.                                  break;
  81.                                 case 13:
  82.                                  hexa[l]='d';
  83.                                  break;
  84.                                 case 14:
  85.                                  hexa[l]='e';
  86.                                  break;
  87.                                 case 15:
  88.                                  hexa[l]='f';
  89.                                  break;
  90.                            
  91.                            
  92.                            
  93.  
  94.  
  95.                             }
  96.                         }
  97.  
  98.                             ctx.fillStyle='#'+hexa[0]+hexa[1]+hexa[2]+hexa[3]+hexa[4]+hexa[5]+'';
  99.                             ctx.fillRect(rectangle[i].x,rectangle[i].y,rectangle[i].width,rectangle[i].width);
  100.                             rectangle[i].x+=rectangle[i].vx;
  101.                             rectangle[i].y+=rectangle[i].vy;
  102.                             if (rectangle[i].x>=width-rectangle[i].width||rectangle[i].x<=0){
  103.                                 bounceX();
  104.                             }
  105.                             if (rectangle[i].y>=height-rectangle[i].height||rectangle[i].y<=0){
  106.                                 bounceY();
  107.                             }
  108.                             if (rectangle[0].x)
  109.                             function bounceX(){
  110.                                 rectangle[i].vx=-rectangle[i].vx
  111.                             }
  112.                             function bounceY(){
  113.                                 rectangle[i].vy=-rectangle[i].vy
  114.                             }
  115.  
  116.  
  117.                         }
  118.                        
  119.                         ctx.fillStyle='#ff0000';
  120.                         ctx.fillRect(rectangle.x,rectangle.y,rectangle.width,rectangle.height);
  121.                         rectangle.x+=rectangle.vx;
  122.                         rectangle.y+=rectangle.vy;
  123.                         if (rectangle.x==width-rectangle.width||rectangle.x==0){
  124.                             bounceX();
  125.                         }
  126.                         if (rectangle.y==height-rectangle.height||rectangle.y==0){
  127.                             bounceY();
  128.                         }
  129.                         function bounceX(){
  130.                             rectangle.vx=-rectangle.vx
  131.                         }
  132.                         function bounceY(){
  133.                             rectangle.vy=-rectangle.vy
  134.                         }
  135.                        
  136.  
  137.  
  138.  
  139.                        
  140.  
  141.                        
  142.                        
  143.  
  144.                     }
  145.  
  146.                 //END OF MAIN BACKGROUND
  147.                
  148.             //CALLING INITIAL DRAW FUNCTIONS
  149.             var Interval=setInterval(function(){drawBG()},1);
  150.             Interval;
  151.  
  152.             //END OF DRAW FUNCTION     
  153.  
  154. }
  155. //END OF INIT
  156.  
  157.  
  158.  
  159.  
  160. //ONLOAD RUN INIT
  161. window.onload=init;
  162. //
  163.  
  164.    
  165. </script>
  166. <style type="text/css">
  167. body{margin: 0; overflow: hidden;}
  168. </style>
  169. </head>
  170. <body>
  171. <canvas id='canvas' width='10' height='10'></canvas>
  172. </body>
  173. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement