-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·167 lines (133 loc) · 5.36 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;
context.translate(width / 2, height / 2);
paintcontext.translate(width / 2, height / 2);
var mode = 0;
var params = InputHandle(canvas, paintcontext).params;
/*
*MODES SETUP
*/
// mode 0
var spt = starPaint.create(1000);
// mode 1
var game = chaosGame.create(0, 0, 1);
// mode 2
rangeX = [3.610952469428775, 3.6109524701258087];
// rangeX = [3,4];
rangeY = [.3,.5];
iterationWindowX = [rangeX[0],rangeX[1]];
canvasLimit = [width, height];
var bd = bifurcationDiagram.create(
rangeX, // x interval visible in screen width
rangeY, // y interval visible in screen height
canvasLimit, // canvas limits
100); // max iterations per row.
//clear paint canvas
paintcontext.clearRect(-width / 2, -height / 2, width, height);
setup();
update();
// runs once
function setup(){
switch(mode){
case 2:
{
// translantes canvases to left bottom to facilitate
context.translate(- width/2, height/2);
paintcontext.translate(- width/2, height/2);
context.scale(1, -1);
paintcontext.scale(1, -1);
bd.drawAxis(paintcontext);
}
}
}
// runs every frame
function update(){
context.clearRect(-width / 2, -height / 2, width, height);
// general commands
if(params.keyboard.keyPressed('Backspace'))
paintcontext.clearRect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height);
switch(mode){
// particle simulation
case 0:
{
spt.shouldAttract(params.mouse.lb, params.mouse.x, params.mouse.y);
spt.rotateVelocity = params.keyboard.keyPressed('r');
spt.delirate = params.keyboard.keyPressed('d');
spt.increaseVelocity = params.keyboard.keyPressed('ArrowUp');
spt.decreaseVelocity = params.keyboard.keyPressed('ArrowDown');
if(params.keyboard.keyTriggered('b')){
spt.shouldBounce ^= 1;
}
spt.paint = params.mouse.rb;
if(params.keyboard.keyTriggered('x')){
spt.reset(params.mouse.x, params.mouse.y);
}
spt.run(canvas,context, paintcontext);
}
break;
// chaos game
case 1:
{
game.renderVertices(paintcontext);
if(params.mouse.lbTriggered()){
game.addVertice(params.mouse.x, params.mouse.y);
game.renderVertices(paintcontext);
}
// if(params.mouse.rbTriggered()){
// game.iterate(paintcontext, 1);
// }
if(params.mouse.rb){
game.iterate(paintcontext, 100);
}
}
break;
// bifurcation diagram
case 2:
{
paintcontext.clearRect(-width / 2, -height / 2, width*2, height*2);
bd.drawAxis(paintcontext);
if(params.mouse.lb)
{
var delta = (rangeX[1]-rangeX[0])/10;
if(iterationWindowX[1] + delta <= rangeX[1]){
iterationWindowX = [iterationWindowX[0], iterationWindowX[1] + delta];
}
}
var wheelDY = params.mouse.wheelTriggered();
if(wheelDY)
{
var currentWidth = rangeX[1] - rangeX[0];
var currentWidth = rangeX[1] - rangeX[0];
var normalizedX = (params.mouse.x + canvas.width/2)/canvas.width
var currentX = normalizedX*currentWidth + rangeX[0];
var zoomPercentage = 1.5;
if(wheelDY < 0){
var targetWidth = currentWidth/zoomPercentage;
}
else if(wheelDY > 0){
var targetWidth = currentWidth*zoomPercentage;
}
iterationWindowX = [currentX - targetWidth*normalizedX,
currentX - targetWidth*normalizedX + targetWidth];
rangeX[0] = iterationWindowX[0];
rangeX[1] = iterationWindowX[1];
console.log(rangeX)
}
bd.runDiagram(paintcontext, iterationWindowX);
}
break;
case 3:
{
p = particle.create(0, 0, Math.random()*4 +1, Math.random() * Math.PI *2, 0, 1);
p.render(context);
}
break;
};
requestAnimationFrame(update);
}
};