-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·94 lines (74 loc) · 2.2 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
window.onload = function(){
var canvas = document.getElementById("canvas"),
paintcanvas = document.getElementById("paintcanvas"),
context = canvas.getContext("2d"),
paintcontext = paintcanvas.getContext("2d"),
width = canvas.width = paintcanvas.width = window.innerWidth,
height = canvas.height = paintcanvas.height = window.innerHeight,
particles = [],
numParticles = 500;
context.translate(width / 2, height / 2);
paintcontext.translate(width / 2, height / 2);
// initialing particles
for(var i = 0; i < numParticles; i++) {
particles.push(particle.create(0, 0, Math.random()*4 +1, Math.random() * Math.PI *2, 0));
}
//global vars
var mousex = width/2;
var mousey = height/2;
var applyForce = false;
var paint = false;
// events
document.addEventListener("mousedown", function(event){
if(event.button == 0){
applyForce = true;
mousex = event.clientX - width/2;
mousey = event.clientY - height/2;
}
if(event.button == 2){
paint = true;
}
});
document.addEventListener("mouseup", function(event){
if(event.button == 0){
applyForce = false;
}
if(event.button == 2){
paint = false;
}
});
document.body.addEventListener("mousemove", function(event) {
mousex = event.clientX - width/2;
mousey = event.clientY - height/2;
} );
// disables context menu
document.oncontextmenu = function(){
return false;
}
// run once stuff
// paintcontext.fillStyle = "hsla(0%,0%,0%,0)"
paintcontext.clearRect(-width / 2, -height / 2, width, height);
// context.globalCompositeOperation="source-over";
// paintcontext.globalCompositeOperation="destination-over";
update();
function update(){
context.clearRect(-width / 2, -height / 2, width, height);
for (var i = 0; i < numParticles; i++) {
var p = particles[i];
if(applyForce){
var dx = mousex-p.x,
dy = mousey-p.y,
length = Math.sqrt(dx*dx + dy*dy);
dx /= length;
dy /= length;
p.applyForce(0.25*dx, 0.25*dy);
}
p.update(canvas);
if(paint){
p.paint(paintcontext);
}
p.render(context);
};
requestAnimationFrame(update);
}
};