Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2014 20:43
Show Gist options
  • Select an option

  • Save anonymous/9162140 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/9162140 to your computer and use it in GitHub Desktop.
var creaParticelle = function() {
var x,y = hc/2;
for(var i=0;i<numParticelle;i++) {
x = Math.random()*wc;
part[i] = new Particella(x,y, Math.random()*5.5+0.5, colori[Math.round(Math.random()*(numColori-1))]);
part[i].setRif(x,y);
}
}
var setup = function() {
creaParticelle();
c.clearRect(0, 0, wc, hc);
c.fillStyle = "#1B2209"
c.fillRect(0, 0, wc, hc);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XL visual</title>
<link href="css/xlvisual.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<!--[if IE]>
<script type="text/javascript" src="js/excanvas_r71.js"></script>
<![endif]-->
<script type="text/javascript" src="js/effect.js"></script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>
</html>
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;
}
}
};
var draw = function() {
c.clearRect(0, 0, wc, hc);
c.fillStyle = "#1B2209"
c.fillRect(0, 0, wc, hc);
for(var i=0;i<numParticelle;i++) {
part[i].update();
part[i].draw();
}
window.setTimeout(draw,0);
}
setup();
var hc,wc;
var resize = function() {
hc = $(window).height()-5;
wc = Math.max($(window).width(),800)-5;
$("#canvas").attr("width",wc).attr("height",hc);
}
$(window).resize(resize);
resize();
var c = $("#canvas")[0].getContext("2d");
var numParticelle = 100;
var colori = ["#00FFFF","#FFFF00","#FF00FF"];
var numColori = colori.length;
var part = new Array(numParticelle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment