Skip to content

Instantly share code, notes, and snippets.

@rformato
Created February 22, 2014 20:53
Show Gist options
  • Select an option

  • Save rformato/9162253 to your computer and use it in GitHub Desktop.

Select an option

Save rformato/9162253 to your computer and use it in GitHub Desktop.
var Particella = function(x, y, radius, col) {
this.dx = 0;
this.dy = 0;
this.dradius = 0;
this.rifx = 0;
this.rify = 0;
this.vel = 30;
this.vibracounter = 0;
this.x = x;
this.y = y;
this.radius = radius;
this.radius2 = radius*2;
this.col = col;
this.state = 0;
};
Particella.prototype = {
setRif: function(x, y) {
this.rifx = x;
this.rify = y;
},
draw: function() {
c.fillStyle = this.col;
c.beginPath();
c.arc(this.x,this.y,this.radius,0,2*Math.PI,true);
c.fill();
},
update: function() {
if(Math.abs(mouseX-this.x)<20 && Math.abs(mouseY-this.y)<20) {
if(this.state==0) {
this.state = 1;
this.dx = this.rifx+Math.random()*200-100;
this.dy = Math.random()*hc;
this.vel = 30;
} else if(this.state==1) {
this.vibracounter = Math.round(Math.random()*20+10);
}
}
switch(this.state) {
case 0:
this.vibraLinea();
break;
case 1:
this.vibraTutto();
break;
}
this.x += (this.dx-this.x)/this.vel;
this.y += (this.dy-this.y)/this.vel;
},
vibraLinea: function() {
if(this.vibracounter==0) {
this.vel = 20;
this.dx = this.rifx+Math.random()*20-10;
this.dy = this.rify+Math.random()*20-10;
this.vibracounter = Math.round(Math.random()*5+5);
}
this.vibracounter--;
},
vibraTutto: function() {
if(Math.abs(this.dx-this.x)<5 && Math.abs(this.dy-this.y)<5){
this.state = 0;
return;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment