Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function prepareBalls(images){
- reader = new FileReader();
- for(var i = 0; i<images.length; i++){
- reader.onload = function(e){
- var placeIsGood = false;
- var image = document.createElement('img');
- image.src = reader.result;
- var startAngle = Math.floor(Math.random()*360) * Math.PI/180;
- var speed = 1 + Math.floor(Math.random()*5);
- var velX = Math.cos(startAngle) * speed;
- var velY = Math.sin(startAngle) * speed;
- while(!placeIsGood){
- var r = 20;
- var X = r + Math.floor(Math.random()*width) - r;
- var Y = r + Math.floor(Math.random()*height) - r;
- var ball = makeBall(X,Y,r,image,velX,velY);
- if(canDrawHere(ball)){
- placeIsGood = true;
- }
- }
- balls.push(ball);
- };
- reader.readAsDataURL(images[i]);
- }
- }
- function canDrawHere(ball){
- var result = true;
- for(var i=0; i<balls.length; i++){
- if(detectCollision(ball, balls[i])){
- result = false;
- }
- }
- return result;
- }
- function makeBall(x,y,r,data,Vx,Vy){
- var ball = {
- x:x,
- y:y,
- nextX:x,
- nextY:y,
- radius:r,
- img:data,
- mass: countVolume(r),
- Vx:Vx,
- Vy:Vy,
- draw:function(){
- var size = {width:40,height:40};
- var imgX = this.x - size.width/2;
- var imgY = this.y - size.height/2;
- context.save();
- context.beginPath();
- context.arc(this.x,this.y,this.radius,Math.PI*2,false);
- context.closePath();
- context.clip();
- context.drawImage(this.img, imgX, imgY, size.width, size.height);
- context.restore();
- context.beginPath();
- context.arc(this.x,this.y,this.radius,Math.PI*2,false);
- context.closePath();
- }
- };
- return ball;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement