-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·137 lines (114 loc) · 3.19 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 = 100;
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;
var shouldBounce = true;
// 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;
} );
document.body.addEventListener("keydown", function(event) {
switch(event.key){
case 'Backspace': //backspace
{
paintcontext.clearRect(-width / 2, -height / 2, width, height);
}
break;
case 'b':
{
shouldBounce ^= 1;
}
break;
case 'x':
{
for(var i = 0; i < numParticles; i++) {
particles[i].reset(mousex, mousey);
}
}
break;
case 'ArrowUp':
{
var p = 1.1; // linear interpolation
for(var i = 0; i < numParticles; i++) {
particles[i].xOld = (1-p)*particles[i].x + p*particles[i].xOld;
particles[i].yOld = (1-p)*particles[i].y + p*particles[i].yOld;
}
}
break;
case 'ArrowDown':
{
var p = 0.9; // linear interpolation
for(var i = 0; i < numParticles; i++) {
particles[i].xOld = (1-p)*particles[i].x + p*particles[i].xOld;
particles[i].yOld = (1-p)*particles[i].y + p*particles[i].yOld;
}
}
break;
}
} );
// 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, shouldBounce);
if(paint){
p.paint(paintcontext);
}
p.render(context);
};
requestAnimationFrame(update);
}
};