Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function prepareBalls(images){
  2.             reader = new FileReader();
  3.  
  4.             for(var i = 0; i<images.length; i++){
  5.  
  6.                 reader.onload = function(e){
  7.                     var placeIsGood = false;
  8.                     var image = document.createElement('img');
  9.  
  10.                     image.src = reader.result;
  11.  
  12.                     var startAngle = Math.floor(Math.random()*360) * Math.PI/180;
  13.  
  14.                     var speed = 1 + Math.floor(Math.random()*5);
  15.  
  16.                     var velX = Math.cos(startAngle) * speed;
  17.                     var velY = Math.sin(startAngle) * speed;
  18.  
  19.                     while(!placeIsGood){
  20.                         var r = 20;
  21.                         var X = r + Math.floor(Math.random()*width) - r;
  22.                         var Y = r + Math.floor(Math.random()*height) - r;
  23.  
  24.                         var ball = makeBall(X,Y,r,image,velX,velY);
  25.                         if(canDrawHere(ball)){
  26.                             placeIsGood = true;
  27.                         }
  28.                     }
  29.  
  30.                     balls.push(ball);
  31.                 };
  32.  
  33.                 reader.readAsDataURL(images[i]);
  34.             }
  35.              
  36.         }
  37.  
  38.         function canDrawHere(ball){
  39.             var result = true;
  40.             for(var i=0; i<balls.length; i++){
  41.                 if(detectCollision(ball, balls[i])){
  42.                     result = false;
  43.                 }
  44.             }
  45.             return result;
  46.         }
  47.  
  48.         function makeBall(x,y,r,data,Vx,Vy){
  49.             var ball = {
  50.             x:x,
  51.             y:y,
  52.             nextX:x,
  53.             nextY:y,
  54.  
  55.             radius:r,
  56.             img:data,
  57.             mass: countVolume(r),
  58.            
  59.             Vx:Vx,
  60.             Vy:Vy,
  61.            
  62.             draw:function(){
  63.                 var size = {width:40,height:40};
  64.  
  65.                 var imgX = this.x - size.width/2;
  66.                 var imgY = this.y - size.height/2;
  67.  
  68.                 context.save();
  69.                 context.beginPath();
  70.                 context.arc(this.x,this.y,this.radius,Math.PI*2,false);
  71.                 context.closePath();
  72.                 context.clip();
  73.  
  74.                 context.drawImage(this.img, imgX, imgY, size.width, size.height);
  75.                 context.restore();
  76.  
  77.                 context.beginPath();
  78.                 context.arc(this.x,this.y,this.radius,Math.PI*2,false);
  79.                 context.closePath();
  80.                 }
  81.             };
  82.  
  83.             return ball;
  84.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement