-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·111 lines (88 loc) · 3.56 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
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 = 1000;
context.translate(width / 2, height / 2);
paintcontext.translate(width / 2, height / 2);
var mode = 1;
// 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, 1));
}
var params = InputHandle(canvas, particles, paintcontext).params;
var game = chaosGame.create(0, 0, 1);
//clear paint canvas
paintcontext.clearRect(-width / 2, -height / 2, width, height);
update();
// compute star physics and stuff
function update(){
context.clearRect(-width / 2, -height / 2, width, height);
switch(mode){
case 0:
{
for (var i = 0; i < numParticles; i++) {
var p = particles[i];
if(params.applyForce){
var dx = params.mousex-p.x,
dy = params.mousey-p.y,
length = Math.sqrt(dx*dx + dy*dy);
if(length > 10)
{
dx = dx/length || 0 ;
dy = dy/length || 0 ;
// force of fixed intensity on mouse-position direction
p.applyForce(0.25*dx, 0.25*dy);
}
}
if(params.rotateVelocity)
{
p.rotate(params.turnRate);
}
if(params.delirate)
{
p.delirate(params.turnRate);
}
if(params.increaseVelocity)
{
var per = params.increaseFactor; // linear interpolation
p.xOld = (1-per)*p.x + per*p.xOld;
p.yOld = (1-per)*p.y + per*p.yOld;
}
if(params.decreaseVelocity)
{
var per = params.decreaseFactor; // linear interpolation
p.xOld = (1-per)*p.x + per*p.xOld;
p.yOld = (1-per)*p.y + per*p.yOld;
}
p.update(canvas, params.shouldBounce);
if(params.paint){
p.paint(paintcontext);
}
p.render(context);
}
}
break;
case 1:
{
game.renderVertices(paintcontext);
if(params.mouseLBTriggered()){
game.addVertice(params.mousex, params.mousey);
game.renderVertices(paintcontext);
}
// if(params.mouseRBTriggered()){
// game.iterate(paintcontext, 1);
// }
if(params.mouseRBDown){
game.iterate(paintcontext, 100);
}
}
break;
};
requestAnimationFrame(update);
}
};