Skip to content
Browse files

Added support for mouse events

  • Loading branch information...
1 parent 9800d46 commit 9a8300322d9652063d9905e294d4efa1f760ead0 @cham committed Jun 10, 2012
Showing with 32 additions and 16 deletions.
  1. +32 −16 src/Emitter.js
View
48 src/Emitter.js
@@ -3,17 +3,24 @@ define([],function(){
function fuzzy(range, base){
return (base||0) + (Math.random()-0.5)*range*2;
}
+ function isTouchDevice() {
+ var el = document.createElement('div');
+ el.setAttribute('ongesturestart', 'return;');
+ return typeof el.ongesturestart == "function";
+ }
return {
+ touchdevice: false,
+
cW: 480,
cH: 280,
ctx: null,
$canvas: null,
particles: [],
particleOffset: 0, // required for infinite emitter
- particleAttrs: 6,
+ particleAttrs: 4,
numParticles: 400, // total particles when not infinite or max particles when infinite
particleSize: 1,
@@ -36,34 +43,32 @@ define([],function(){
radius = Math.random() * this.emitForce;
this.particles = this.particles.concat([
- this.emitCoords[0],this.emitCoords[1],0,
- Math.cos(rad)*radius,Math.sin(rad)*radius,0
+ this.emitCoords[0],this.emitCoords[1],
+ Math.cos(rad)*radius,Math.sin(rad)*radius
]);
},
move: function(){
var self = this,
i;
for(i=(this.particleOffset*this.particleAttrs);i<this.particles.length;i+=this.particleAttrs){
- this.particles[i] += this.particles[i+3];
- this.particles[i+1] += this.particles[i+4];
- this.particles[i+2] += this.particles[i+5];
+ this.particles[i] += this.particles[i+2];
+ this.particles[i+1] += this.particles[i+3];
_(this.gravityWells).each(function(well){
var xd = self.particles[i]-well[0],
yd = self.particles[i+1]-well[1],
dist = Math.pow(xd*xd + yd*yd,1/8);
- self.particles[i+3] += (well[2] / dist) * ((xd>0) ? -1 : 1);
- self.particles[i+4] += (well[2] / dist) * ((yd>0) ? -1 : 1);
+ self.particles[i+2] += (well[2] / dist) * ((xd>0) ? -1 : 1);
+ self.particles[i+3] += (well[2] / dist) * ((yd>0) ? -1 : 1);
});
+ this.particles[i+2] *= this.frict;
this.particles[i+3] *= this.frict;
- this.particles[i+4] *= this.frict;
- this.particles[i+5] *= this.frict;
if(this.particles[i]>this.cW || this.particles[i]<0){
- this.particles[i+3] = -this.particles[i+3];
+ this.particles[i+2] = -this.particles[i+2];
}
if(this.particles[i+1]>this.cH || this.particles[i+1]<0){
- this.particles[i+4] = -this.particles[i+4];
+ this.particles[i+3] = -this.particles[i+3];
}
}
},
@@ -80,6 +85,11 @@ define([],function(){
this.addWell([e.changedTouches[0].pageX,e.changedTouches[0].pageY,1]);
},
+ onMousedown: function(e){
+ this.emitting = true;
+ this.addWell([e.pageX,e.pageY,1]);
+ },
+
setupCanvas: function(){
var canvas, self = this;
@@ -91,9 +101,15 @@ define([],function(){
this.ctx = canvas.getContext('2d');
$('body').append(this.$canvas);
- canvas.addEventListener( 'touchstart', function(e){
- self.onTouchStart(e);
- }, false );
+ if(this.touchdevice){
+ canvas.addEventListener( 'touchstart', function(e){
+ self.onTouchStart(e);
+ }, false );
+ }else{
+ canvas.addEventListener( 'mousedown', function(e){
+ self.onMousedown(e);
+ }, false );
+ }
},
draw: function(){
@@ -112,7 +128,7 @@ define([],function(){
this.ctx.fillRect(this.particles[i],this.particles[i+1],this.particleSize,this.particleSize);
}
- this.ctx.strokeStyle = "rgba(100,0,0,0.5)";
+ this.ctx.strokeStyle = "rgba(100,0,0,1)";
this.ctx.lineWidth = 3;
_(this.gravityWells).each(function(well){
self.ctx.beginPath();

0 comments on commit 9a83003

Please sign in to comment.
Something went wrong with that request. Please try again.