This repository was archived by the owner on Mar 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonotaxis.html
167 lines (143 loc) · 4.04 KB
/
sonotaxis.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Phyllotaxis</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
/* No style rules here yet */
</style>
</head>
<body>
<script type="text/javascript">
function get_phyllo (num_points, angle) {
c=1;
ret_array = new Array(num_points);
max_r=0;
for (n=1; n<=num_points; n++) {
r=c*Math.sqrt(n);
theta=angle*(n);
ret_array[n-1]=Array(r*Math.cos(theta), r*Math.sin(theta));
max_r=r;
}
return Array(ret_array,max_r);
}
//Width and height
var w = 500;
var h = 500;
var word = "pause";
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.append("a")
.attr("xlink:href", "javascript:stopLoop();")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("height", w)
.attr("width", h)
.style("fill", "white")
.attr("rx", 10)
.attr("ry", 10);
num_points=630;
context = new AudioContext;
var amp = context.createGain();
amp.gain.value = 0.01;
var fundamental = 39;
var num_oscillators = 200;
var oscillators = new Array(num_oscillators)
var gains = new Array(num_oscillators)
for (n=1; n<=num_oscillators; n++) {
oscillators[n-1] = context.createOscillator();
oscillators[n-1].frequency.value = fundamental * n
oscillators[n-1].start(0);
gains[n-1] = context.createGain();
gains[n-1].gain.value = 0.01;
oscillators[n-1].connect(gains[n-1]);
gains[n-1].connect(context.destination);
}
function update_oscillators(num_points, angle) {
c=1;
// max_r = c * Math.sqrt(num_points);
max_r = c * Math.sqrt(num_oscillators);
for (n=1; n<=num_oscillators; n++) {
r = c * Math.sqrt(n);
theta = angle * (n);
theta = theta % (2 * Math.PI);
theta = theta / (2 * Math.PI); //[0,1]
theta = theta + 1; // [1,2]
base_pitch = theta * fundamental;
pitch_multiplier = 1 * ((r / max_r) + 1);
pitch_multiplier = Math.floor(pitch_multiplier);
// pitch_multiplier = 1;
pitch = base_pitch * pitch_multiplier;
// console.log('n:',n,'theta:',theta,'base_pitch:',base_pitch,'multi:',pitch_multiplier,'pitch:',pitch);
oscillators[n-1].frequency.value = pitch;
gains[n-1].gain.value = 0.005 / ( r / max_r);
}
}
var phyllo_set = get_phyllo(num_points,angle);
dataset=phyllo_set[0];
max_r=phyllo_set[1];
var xScale = d3.scale.linear().domain([-1*max_r, 1*max_r]).range([0, w])
var yScale = d3.scale.linear().domain([-1*max_r, 1*max_r]).range([0, w])
var angle = 14.9;
function startLoop() {
return(setInterval(function() {
var phyllo_set = get_phyllo(num_points,angle);
update_oscillators(num_points, angle);
dataset=phyllo_set[0];
max_r=phyllo_set[1];
angle += .0001;
redraw(angle);
}, 50))
}
var timerId = startLoop();
function stopLoop() {
if (timerId != -1) {
clearInterval(timerId);
timerId=-1;
} else {
timerId = startLoop();
}
}
function redraw(angle) {
var textset = [
[5, 20, angle]
];
var circle = svg.selectAll("circle")
.data(dataset)
var circleEnter = circle.enter()
.append("circle")
circle.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return 5;
})
var circleExit = circle.exit().remove();
var label = svg.selectAll("text")
.data(textset);
label.enter()
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "black");
label.text(function(d) {
return d[2].toFixed(3);
})
.attr("x", function(d) {
return d[0];
})
.attr("y", function(d) {
return d[1];
});
}
</script>
</body>
</html>