Last active
October 24, 2019 12:15
-
-
Save nanu146/aa0e4f8428bc65a8c648cf0ddefc84d4 to your computer and use it in GitHub Desktop.
Particle explosion in javascript canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function degreeToRadians(value){ | |
return (value/360)*2*Math.PI; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> | |
Test Demo | |
</title> | |
<style type="text/css"> | |
html, body { | |
margin: 0px; | |
} | |
canvas { | |
display: block; | |
} | |
</style> | |
<script type="text/javascript" src="geom_conversions.js"></script> | |
<script type="text/javascript" src="vector.js"></script> | |
<script type="text/javascript" src="particles.js"></script> | |
<script type="text/javascript"> | |
window.onload=function(){ | |
canvas=document.getElementById("canvas"); | |
context=canvas.getContext("2d"); | |
width=canvas.width=window.innerWidth; | |
height=canvas.height=window.innerHeight; | |
/*position=vector.create(100,500); | |
velocity=vector.create(0,0); | |
velocity.setLength(10); | |
velocity.setAngle(degreeToRadians(135)) | |
console.log(position)*/ | |
particles=[]; | |
numparticles=500; | |
for(i=0;i<numparticles;i++){ | |
particles.push(particle.create(width/2,height/2,(Math.random()*10)+1,Math.random()*Math.PI*2)) | |
} | |
update(); | |
function update(){ | |
context.clearRect(0,0,width,height); | |
/*position.addTo(velocity); | |
context.arc(position.getX(),position.getY(),10,0,2*Math.PI,false); | |
*/ | |
for (var i = 0; i < numparticles; i++) { | |
particles[i].update(); | |
context.beginPath(); | |
context.arc(particles[i].position.getX(),particles[i].position.getY(),3,0,2*Math.PI,false); | |
context.fill(); | |
} | |
requestAnimationFrame(update); | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<canvas id="canvas"> | |
</canvas> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
particle= | |
{ | |
velocity :null, | |
position : null, | |
/// dummy constructor | |
create : function(x,y,speed,angle) | |
{ | |
console.log(x,y,speed,angle) | |
var obj=Object.create(this); | |
obj.velocity=vector.create(0,0); | |
obj.velocity.setLength(speed); | |
obj.velocity.setAngle(angle); | |
obj.position=vector.create(x,y); | |
console.log("object") | |
console.log(obj); | |
return obj; | |
}, | |
update: function(){ | |
this.position.addTo(this.velocity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector= | |
{ | |
_x:0, | |
_y:0, | |
// dummy constructor | |
create : function(x,y){var obj= Object.create(this);obj._y=y; obj._x=x; return obj;}, | |
// member functions | |
getX : function(){ return this._x}, | |
getY : function(){ return this._y}, | |
setX : function(value){ this._x=value;}, | |
setY : function(value){ this._y=value;}, | |
getLength : function(){ return Math.sqrt(this._x*this._x + this._y*this._y)}, | |
getAngle : function(){ return Math.atan2(this._y,this._x) }, | |
setAngle : function(angle){ length=this.getLength(); this._y =Math.cos(angle)*length; this._x= Math.sin(angle)*length; }, | |
setLength: function(length){ angle=this.getAngle(); this._y=Math.cos(angle)*length; this._x=Math.sin(angle)*length; }, | |
add : function(v2){ vect = this.create(this._x+v2._x, this._y+v2._y); return vect; }, | |
subtract : function(v2){ vect = this.create(this._x-v2._x, this._y-v2._y); return vect; }, | |
multiply: function(value){ return vector.create(this._x*value,this._y*value)}, | |
divide: function(value){ return vector.create(this._x/value,this._y/value)}, | |
scale: function(value){ this._x=this._x*value; this._y=this._y*value;}, | |
addTo: function(v2){ this._x=this._x+v2._x; this._y=this._y+v2._y }, | |
subtractFrom: function(v2){ this._x=this._x-v2._x; this._y=this._y-v2._y } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment