-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphi.html
More file actions
260 lines (224 loc) · 10.2 KB
/
Copy pathphi.html
File metadata and controls
260 lines (224 loc) · 10.2 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<html>
<head>
<meta charset="utf-8">
<title>100 Digits of Phi</title>
<meta name="description" content="A collection of Phi Nguyen's projects">
<meta property="og:title" content="Projects Template">
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script>
<script type="text/javascript" src="js/modernizr.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="../images/favicon-32x32.png">
<link rel="apple-touch-icon" href="images/android-icon-192x192.png">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46752978-1'], ['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" src="d3.v4.min.js"></script>
<style>
body {top:0; right:0; bottom:0; left:0; font-family:Avenir; font-size:14px;}
.header {font-size:24px; margin-top: 10px; margin-bottom: 10px;}
.clearfix {clear:both; padding-bottom: 10px;}
.axis path {stroke-width:0;}
.tick {opacity:0;}
.linepath {stroke-width:2; stroke-linecap: round;}
.linepath2 {stroke-width:5; stroke-linecap: round;}
.button {display:inline-block; width:100px; border-radius: 5px; padding: 7px 5px 7px 5px; margin-left: 10px; text-align: center; border: 2px solid black; cursor: pointer; font-size: 12px; background-color: white;}
.button:hover {font-weight: bold; border-color: #2e6da4;}
.selected {color:#F9F9F9; font-weight:bold; background-color:#337ab7; border-color: #2e6da4;}
/*styling for header*/
.projectpagetextheader {
position: absolute;
top: 28px;
display: inline;
margin-left: 10px;
color: #ef946c;
font-size: 28px;
line-height: 20px;
}
</style>
</head>
<body>
<div class="basicheader">
<a href="http://www.phinguyen.co" style="text-decoration:none"->
<img height="60" src="../images/Orange_White.png">
<div class="projectpagetextheader">phi nguyen</div>
</a>
<div class="header">100 Digits of Phi</div>
<script>
//set margins
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 600 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var body = d3.select("body");
//misc. text
body.append("text").text("My name, Phi, also represents the 'golden ratio' ")
body.append("text").html("Φ");
body.append("text").text(" = 1.618. This visualization graphically depicts the number, with each digit being represented by a color and unit vector in the polar coordinate system. ");
body.append("text").text("Press ");
body.append("text").style("font-weight", "bold").text("ANIMATE ");
body.append("text").text("to start the animation from the beginning. Press ");
body.append("text").style("font-weight", "bold").text("STOP ");
body.append("text").text("to halt the animation at it's current position. Press ");
body.append("text").style("font-weight", "bold").text("RESET ");
body.append("text").text("to show the entire visualization again.")
body.append("div").attr("class", "clearfix");
body.append("text").text("Idea from ");
body.append("text").html("<a href='http://www.visualcinnamon.com/portfolio/the-art-in-pi'>the wonderful Nadieh Bremer.</a>")
body.append("div").attr("class", "clearfix");
//create buttons!
var buttonArray = ["Animate", "Stop", "Reset"];
var animateButton = body.selectAll("button")
.data(buttonArray)
.enter().append("button")
.attr("class", "button")
.attr("id", function(d) {return d;})
.text(function(d) {return d;});
//all the buttons!
d3.select("#Animate").on("click", buttonPress);
d3.select("#Stop").on("click", buttonStop);
d3.select("#Reset").on("click", buttonReset);
body.append("div").attr("class", "clearfix");
//set basic canvas
var svg = body.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("class", "borderstyle")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
//scales
var x = d3.scaleLinear()
.domain([-10,10])
.range([0,width]);
var y = d3.scaleLinear()
.domain([-10,10])
.range([height,0]);
var xAxis = d3.axisTop(x);
var yAxis = d3.axisLeft(y);
svg.append("g")
.call(xAxis)
.attr("class", "axis");
svg.append("g")
.call(yAxis)
.attr("class", "axis");
//1000 digits of phi
var phi = '1618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137484754088075386891752126633862223536931793180060766726354433389086595939582905638322661319928290267880675208766892501711696207032221043216269548626296313614438149758701220340805887954454749246185695364864449241044320771344947049565846788509874339442212544877066478091588460749988712400765217057517978834166256249407589069704000281210427621771117778053153171410117046665991466979873176135600670874807101317952368942752194843530567830022878569978297783478458782289110976250030269615617002504643382437764861028383126833037242926752631165339247316711121158818638513316203840052221657912866752946549068113171599343235973494985090409476213222981017261070596116456299098162905552085247903524060201727997471753427775927786256194320827505131218156285512224809394712341451702237358057727861600868838295230459264787801788992199027077690389532196819861514378031499741106926088674296226757560523172777520353613936';
//initializing some variables
var c10 = d3.schemeCategory10;
var data = d3.range(10);
var lineLength = (1/10)*width;
//doing some unit circle mathematics. coordinates to transform where next segment of line shall go
var angle = [90,54,18,342,306,270,234,198,162,126];
var yfinder = Math.sin;
var xfinder = Math.cos;
var toRadians = function(d) {return d*Math.PI/180};
//Output should be array[10]
var legendArray = [];
for (var i = 0; i < 10; i++) {
legendArray.push({digit:data[i], angle: angle[i], color:c10[i], xcoord:xfinder(toRadians(angle[i])), ycoord:yfinder(toRadians(angle[i]))})
};
//create a quick legend
function createLegend() {
legend = svg.selectAll(".thing")
.data(legendArray)
.enter().append("g")
.attr("class", "legend");
legend.append("line")
.attr("class", "linepath")
.attr("id", function(d,i) {return "legendline"+i;})
.attr("x1", x(3.5))
.attr("y1", y(8.8))
.attr("x2", function(d) {return x(1.3*d.xcoord+3.5);})
.attr("y2", function(d) {return y(1.3*d.ycoord+8.8);})
.style("stroke", function(d) {return d.color;})
legend.append("text")
.text(function(d) {return d.digit})
.attr("id", function(d,i) {return "legendtext"+i;})
//hard coding locations (sorry)
.attr("x", function (d) {return x(1.6*d.xcoord+3.38);})
.attr("y", function(d) {return y(1.6*d.ycoord+8.65);})
.attr("font-size", "12px");
};
createLegend();
//create a dataNest to map digits to other elements
var dataNest = d3.nest()
.key(function(d) {return d.digit;})
.entries(legendArray);
//dataNest[i].values[0].color
//initialize starting point
var xset = 0;
var yset = 0;
//create actual data set for use
var phiData = [];
for (var i = 0; i < 100; i++) {
//initialize starting positions variables
var digitInput = +phi[i];
phiData.push({digit:digitInput, x1:xset, x2:xset+dataNest[digitInput].values[0].xcoord, y1: yset, y2: yset+dataNest[digitInput].values[0].ycoord, color:dataNest[digitInput].values[0].color});
xset = phiData[i].x2;
yset = phiData[i].y2;
};
//then create line
var path = svg.append("g")
.attr("class", "lines");
//create default line
function createLines() {
linesAll = path.selectAll(".newThing")
.data(phiData)
.enter().append("line")
.attr("class", "linepath")
.attr("x1", function(d) {return x(1.3*d.x1);})
.attr("y1", function(d) {return y(1.3*d.y1);})
.attr("x2", function(d) {return x(1.3*d.x2);})
.attr("y2", function(d) {return y(1.3*d.y2);})
.style("stroke", function(d) {return d.color;});
};
createLines();
//create callback function to wrap animation on a click
function buttonPress() {
//first remove existing template
linesAll.remove()
//create new lines, and hide to start animation
createLines()
linesAll.attr("class", "linepath2")
.attr("x2", function(d) {return x(1.3*d.x1);})
.attr("y2", function(d) {return y(1.3*d.y1);})
.style("stroke", "none")
//start the transition
.transition()
.on("start", function(d,i) {
// d3.select("#legendline"+d.digit).attr("class", "linepath2")
// d3.select("#legendtext"+d.digit).attr("font-weight", "bold");
})
.delay(function(d,i) {return i*300;})
.duration(300)
//extend length of x2, y2
.attr("x2", function(d) {return x(1.3*d.x2);})
.attr("y2", function(d) {return y(1.3*d.y2);})
//add color
.style("stroke", function(d) {return d.color;})
//return to normal state at end
.on("end", function(d) {
d3.select(this).attr("class","linepath")
// d3.select("#legendline"+d.digit).remove()
// d3.select("#legendtext"+d.digit).remove();
});
};
function buttonStop() {
linesAll.transition()
.duration();
};
//returns animation to default state
function buttonReset() {
linesAll.remove()
createLines()
};
//NOTES: some enhancements can still be made:
//line should be dynamic based on size of canvas (right now only works on square canvas)
//i really wanna animate the legend lines as well :(
//stop just stops the animation at it's current state. can't continue playing from there.
</script>
</body>
</html>