generated from pabloqb2000/js-ui_on_canvas_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
166 lines (144 loc) · 3.98 KB
/
sketch.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
let N = 600; // number of input samples
let path = [], svg = [], done=false; // Svg variables
let drawings = [{name: 'treble-clef'},
{name: 'musical-note'},
{name: 'fourier'},
{name: 'pi'}]; // Drawing parameters
let ind = 0; // Index of the last calculated circle
let dt = 0.5e-2; // dt to use in the aproximation of the integral
let circlesSld, speedSld, zoomSld, showBtn, followBtn, optBox; // UI
let circles = []; // Circles list
let drawn = []; // list of positions in the path being drawn
async function preload() {
for(let d of drawings) {
let s = await fetch("https://raw.githubusercontent.com/pabloqb2000/js-fourier_visualization/gh-pages/svg/" + d.name + ".svg")
.then(response => response.text())
.then(text => (new DOMParser).parseFromString(text, "image/svg+xml"))
.then(svg => svg.documentElement);
svg.push(s);
let p = s.querySelector("path");
path.push(p);
}
restart();
done=true;
}
function setup() {
textFont("Orbitron");
createCanvas(windowWidth, windowHeight);
background(32);
// Create UI elements
circlesSld = new Slider(1, 250, 13, 0,0, width/12, height/60, 1, "Circles", true, 0, restart)
speedSld = new Slider(0.1, 3, 1, 0,0, width/12, height/60, null, "Speed", false);
zoomSld = new Slider(0.03, 10, 1,0,0, width/12, height/60, null, "Zoom");
showBtn = new ToggleButton(0,0, width/12, height/30, "Circles", null, true);
followBtn = new ToggleButton(0,0, width/12, height/30, "Follow");
optBox = new OptionsBox(drawings.map(e => e.name), height/25, restart);
// Start UI
UI.tableWidth = 1;
UI.tableHeight = 100;
UI.distrubute();
}
function draw() {
// Draw UI elements
background(32);
UI.update();
UI.draw();
// Translate to the center
translate(13/24*width, height/2);
textSize(height/30*0.7);
// Draw fps for debugging
noStroke();
fill(200);
textAlign(CENTER);
text(frameRate().toFixed(2) + " fps", 0, height/2.5);
if(circles.length == circlesSld.value) {
// Zoom
scale(zoomSld.value, zoomSld.value);
// Update the circles
let sum = new Complex();
for(let c of circles) {
sum.add(c.update());
}
// Translate to the end if follow is active
if(followBtn.active)
translate(-sum.r, -sum.i);
// Update the path
drawn.push(sum);
if(drawn.length > N)
drawn.splice(0,1);
// Draw the circles
noFill();
strokeWeight(1/zoomSld.value);
stroke(128);
sum = new Complex();
for(let c of circles) {
sum.add(c.draw(sum));
}
// Draw the path
strokeWeight(2);
strokeCap(SQUARE);
for(let i = 1; i < drawn.length; i++) {
stroke(86, 210, 227, sqrt(i/N) * 255);
let z0 = drawn[i-1];
let z1 = drawn[i];
line(z0.r, z0.i, z1.r, z1.i);
}
} else if (done) {
addCircle();
drawProgressbar();
} else {
drawProgressbar();
}
}
/**
* Reset all the circles and restart drawing the path
*/
function restart() {
circles = [];
drawn = [];
ind = 0;
}
/**
* Calculate 1 more circles and add it to the list
*/
function addCircle() {
let n = 2; // Number of circles to add
if(ind < circlesSld.value) {
for(let i = 0; i < min(n, circlesSld.value - ind); i++) {
let f = ceil(ind/2)*(-1)**ind;
circles.push(new Circle(f));
ind++;
}
}
}
/**
* Returns the coordinates of the path
* at the corresponding length t
*/
function pathPt(t) {
let n = optBox.selectedIndex();
let viewbox = svg[n].viewBox.baseVal;
const {x, y} = path[n].getPointAtLength(t * path[n].getTotalLength());
let w = viewbox.width == 0 ? 1400 : viewbox.width;
let h = viewbox.height == 0 ? 980 : viewbox.height;
return new Complex(x - w/2, y - h/2).mult(height/h/1.5);
}
function drawProgressbar() {
let load = ind / circlesSld.value;
// Text
let txt = "...";
fill(230);
noStroke();
text("Loading " + txt.substr(0, floor((millis() % 2000)/500)), 0, 25);
text((load * 100).toFixed(0) + " %", 0, -50);
// Inner rectangle
fill(227, 103, 86);
rect(-width/10, -40, width/5*load, 30, 5);
// Outside rectangle
rectMode(CENTER);
noFill();
stroke(230);
strokeWeight(2);
rect(0, -25, width/5, 30, 5);
rectMode(CORNER);
}