/
index.html
183 lines (152 loc) · 4.9 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<html>
<head>
<title>The Infernal Meme Machine</title>
<link rel="stylesheet" href="/css/normalize.css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="/css/skeleton.css" media="screen" title="no title" charset="utf-8">
<style>
.main {
width: 100%;
height: 50vh;
}
#visual {
position: relative;
height: 100%;
}
#visual svg,#visual img {
position: absolute;
top:0;
}
#words svg {
border: 1px solid #CCC;
}
#chat {
list-style: none;
border: 1px solid #CCC;
height: 100%;
}
#chat li {
margin-bottom: 0;
}
</style>
<script src="js/d3.v3.js" charset="utf-8"></script>
</head>
<body>
<div class="main container">
<h1>The Infernal Meme Machine</h1>
<div class="row">
<div id="visual" class="four columns">
<img class="gif-target" src="" alt="">
<div id="words"></div>
</div>
<div class="small four columns">
<div id="colorPicker">
<p>Pick a color :
<svg></svg>
</p>
</div>
<form action="" id="talk">
<input type="text" name="talk" id="talkbox">
<input type="submit" value="Say it!" id="talk-btn">
</form>
</div>
<div class="four columns">
<ul id="chat">
</ul>
</div>
</div>
</div>
<script>
console.log(d3.version); // check if D3 is loaded
// value to store our texts
var texts;
// default color of the text
var color = "green";
// create SVG
var svg = d3.select("#words")
.append("svg")
.attr("width", 200)
.attr("height", 200);
function update() {
texts = svg.selectAll("text")
.data(words)
texts.enter()
.append("text")
.attr("fill", color)
.attr("x", function(d){ return getRandomInt(0,15) + 15})
.attr("y", function(d,i){ return 15+i*15 })
texts.text(function(d) { console.log(d);return d });
texts.exit().remove();
}
// a color selector
var colorPicker = d3.select("#colorPicker svg")
.attr("height", 30)
var colors = ["green","red", "blue", "steelblue", "lime", "olive", "yellow"];
colorPicker
.selectAll("circle")
.data(colors)
.enter()
.append("circle")
.attr("r", 10)
.attr("cx", function(d,i) { return 20+i*25 })
.attr("cy", 20)
.attr("fill", function(d) { return d })
.on("click", function(d){
var c = d3.select(this).attr("fill"); // get color from click
console.log(c); // display color name in the console
color = c; // assign color to the app
d3.select("#talk-btn").style("background-color", c); // change button color
});
// data
var words = ["coucou","salut"];
update()
// click on the button
d3.select("#talk").on("submit", function() {
d3.event.preventDefault() // prevent page refresh
var textValue = d3.select("#talkbox").property("value");
if (textValue != "") { // prevent empty value
words.push(textValue); // add words to our list
update(); // display the whole SVG to show new word
d3.select("#talkbox").property("value", "") // clean box
// log chat
var convertDate = d3.time.format("%B %d - %H:%M");
var now = new Date;
var date = convertDate(now);
console.log(date);
d3.select("#chat")
.append("li")
.style("color", color)
.text( date + ":" +textValue)
}
});
// moving text
var interval = 10; // interval (ms)
setInterval(function(){
texts.attr("x",function(d){
var x = Number(d3.select(this).attr("x"));
if( x > svg.attr("width")) return 0;
else return x+1;
})
}, interval)
// get random Integral Number
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
// get random GIF from Giphy API
var getRandomGif = function(keyword, callback) {
var url = "http://api.giphy.com/v1/gifs/search?q=" + keyword + "&api_key=dc6zaTOxFJmzC&rating=pg";
d3.json(url, function (gifs) {
console.log("loaded gifs:", gifs.data.length);
var randomInt = getRandomInt(1,gifs.data.length-1);
var gifData = gifs.data[randomInt]; // random gif from the array
callback(gifData)
})
}
//fetch a gif
getRandomGif("cat", function(gif){
// append image source to html <img> tag
d3.select(".gif-target")
.attr('src', gif.images.fixed_width.url );
});
</script>
</body>
</html>