forked from qrohlf/trianglify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
284 lines (257 loc) · 11.3 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="low poly style background generator with d3.js">
<meta name="author" content="@qrohlf">
<link rel="shortcut icon" href="/lair/favicon.png">
<meta property="og:image" content="http://qrohlf.com/trianglify/examples/example1.jpg" />
<title>Trianglify by @qrohlf</title>
<link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Open+Sans:400' type='text/css'>
<style type="text/css">
html {
margin: 0;
padding: 0;
width: 100%;
min-height: 100%;
position: relative;
}
body {
margin: 0;
padding: 0;
min-height: 100%;
background-color: #34495e;
background-repeat: no-repeat;
background-position: center;
background-size: 100% 100%;
position: relative;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
.container {
text-align: center;
margin: 0 auto;
max-width: 600px;
color: #2d3a3f;
padding-top: 60px;
}
a, a:visited, a:hover {
color: inherit;
text-decoration: underline;
}
@media screen and (max-width: 600px) {
.container {
padding-top: 60px;
}
}
.box {
display: block;
background: rgba(255, 255, 255, 0.6);
padding: 20px;
border-radius: 3px;
margin: 10px 5px;
box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.4);
}
.btn.box {
padding: 10px 20px;
display: inline-block;
/*color: #eee;
background-color: #3d566e;*/
background-color: #fefefe;
text-decoration: none;
}
.btn.box.small {
padding: 5px 10px;
}
#controls ul {
list-style-type: none;
margin: 0 auto;
padding: 0;
text-align: right;
display: table;
}
#controls li {
display: table-row;
}
p {
margin-bottom: 10px;
}
.hidden {
display: none !important;
}
.download::before {
display: inline-block;
content: '';
height: 16px;
width: 16px;
margin-right: 10px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABQElEQVQ4T92TMU7DQBBFZ7zbUOEbkBtgQQ7gBsmp4hpB2BuQI8ANwgnYWIjarmJEwwEc4SOEG4SKZjfDrKMgO2yiSHS4W83M+zPzxwh//HBffX8wIhevZtnOvP8IOEuuVYD46NsNEUznZabaMe8OfBBfsQN1AOeDqxQpOK3K7L4N2RT3k9EdELxXL1mx6QKji8tISnFDgIppIQF9WGPT+vW5dhAEjF3bLk9IkfP7hL1dIpA2xk7Rec0Kn0zMrTUTV7jvNhqQkGPOSRHheA1gIhAxwD4cBhC3gJi6jhtAW5FpC55zYjEo6lIvXCxKVE/QasgbG7Nqr+PCNqADA8qRIGS1eNdYzRKFlIpVlZvpkH+r2RmCtsboXzYCsRuIQ/8hUQFIej57yn9s9CVGsQqDI6u4/ebqiNVWX0LXb3q5nf8Nymmadx3ne7YAAAAASUVORK5CYII=);
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<p>Trianglify is a javascript library for generating colorful triangle meshes that can be used as SVG images and CSS backgrounds.</p>
<p>Trianglify is open source software. Check it out on <a href="https://github.com/qrohlf/trianglify">Github</a>!</p>
<p>Made by <a href="http://qrohlf.com/">Quinn Rohlf</a></p>
<p>
<a href="#" id="button" class="btn box">Click for a new pattern</a>
<a href="#" id="saveas" target="_blank" class="download btn box" title="right click > save as svg" download="trianglify-background.svg">SVG</a>
<!-- <a href="https://cloudconvert.org/svg-to-png" target="_blank" class="download btn box">PNG</a> -->
</p>
<p>(if you need a PNG, try downloading the image in SVG format and then using <a href="https://cloudconvert.org/svg-to-png">Cloud Convert</a>)</p>
<div id="controls" class="hidden">
<ul>
<li>Noise: <a href="#" id="subtractnoise" class="btn box small">-</a><span id="noise">0</span><a href="#" id="addnoise" class="btn box small">+</a></li>
<li>Cellsize: <a href="#" id="subtractcellsize" class="btn box small">-</a><span id="cellsize">0</span><a href="#" id="addcellsize" class="btn box small">+</a></li>
<li>Cellpadding: <a href="#" id="subtractcellpadding" class="btn box small">-</a><span id="cellpadding">0</span><a href="#" id="addcellpadding" class="btn box small" title="cellpadding must be smaller than cellsize/2">+</a></li>
</ul>
<p>Note: noise effect will cause rendering to take longer</p>
</div>
<p>
<a href="#" id="togglecontrols" class="btn box small">show controls</a>
</p>
<footer>
<iframe src="http://ghbtns.com/github-btn.html?user=qrohlf&repo=trianglify&type=watch&count=true&size=large" allowtransparency="true" frameborder="0" scrolling="0" width="150" height="30"></iframe>
<a href="https://twitter.com/qrohlf" class="twitter-follow-button" data-show-count="false" data-size="large">Follow @qrohlf</a>
</footer>
</div>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="trianglify.js"></script>
<script>
var t = new Trianglify({noiseIntensity: 0});
var button = document.getElementById('button'),
saveas = document.getElementById('saveas'),
subtractnoise = document.getElementById('subtractnoise'),
addnoise = document.getElementById('addnoise'),
noise_display = document.getElementById('noise'),
subtractcellsize = document.getElementById('subtractcellsize'),
addcellsize = document.getElementById('addcellsize'),
cellsize_display = document.getElementById('cellsize'),
subtractcellpadding = document.getElementById('subtractcellpadding'),
addcellpadding = document.getElementById('addcellpadding'),
cellpadding_display = document.getElementById('cellpadding'),
toggle_controls = document.getElementById('togglecontrols'),
controls = document.getElementById('controls'),
prevheight = height();
button.onclick = function() {
recolor();
redraw();
return false;
};
addnoise.onclick = function() {
noise(0.2);
return false;
};
subtractnoise.onclick = function() {
noise(-0.2);
return false;
};
addcellsize.onclick = function() {
cellsize(10);
return false;
};
subtractcellsize.onclick = function() {
cellsize(-10);
return false;
};
addcellpadding.onclick = function() {
cellpadding(5);
return false;
};
subtractcellpadding.onclick = function() {
cellpadding(-5);
return false;
};
toggle_controls.onclick = function() {
toggle_controls.innerHTML = toggleClass(controls, "hidden") ? "show controls":"hide controls";
window.setTimeout(heightChange, 0); //see if the height changed after render and redraw if it did
return false;
};
window.onresize = function() {
redraw();
};
function heightChange() {
if (height() != prevheight) {
console.log("height changed from "+prevheight+" to "+height());
prevheight = height();
redraw();
};
}
redraw();
function redraw() {
console.log("drawing "+document.body.clientWidth+"x"+height())
var pattern = t.generate(document.body.clientWidth, height());
document.body.setAttribute('style', 'background-image: '+pattern.dataUrl);
saveas.setAttribute('href', pattern.dataUri);
noise_display.innerHTML = t.options.noiseIntensity.toFixed(1);
cellsize_display.innerHTML = t.options.cellsize.toFixed(0);
cellpadding_display.innerHTML = t.options.cellpadding.toFixed(0);
};
function recolor() {
t.options.x_gradient = Trianglify.randomColor();
t.options.y_gradient = t.options.x_gradient.map(function(c){return d3.rgb(c).brighter(.5)});
}
function noise(i) {
i += t.options.noiseIntensity;
if (i >= 0 && i <= 1) {
t.options.noiseIntensity = i;
redraw();
} else if (i < 0) {
t.options.noiseIntensity = 0;
redraw();
}
}
function cellsize(i) {
i += t.options.cellsize;
if (i >= 0) {
t.options.cellsize = i;
t.options.bleed = i;
if (t.options.cellpadding >= t.options.cellsize/2) {
t.options.cellpadding = 5*Math.floor((t.options.cellsize/2 - 1)/5);
}
redraw();
}
}
function cellpadding(i) {
i += t.options.cellpadding;
if (i >= 0 && i < t.options.cellsize/2) {
t.options.cellpadding = i;
redraw();
}
}
function height() {
return Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
}
function toggleClass(el, className) {
if (el.classList) {
return el.classList.toggle(className);
} else {
var classes = el.className.split(' ');
var existingIndex = classes.indexOf(className);
if (existingIndex >= 0)
classes.splice(existingIndex, 1);
else
classes.push(className);
el.className = classes.join(' ');
return existingIndex >= 0;
}
}
// analytics
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-40241848-1', 'qrohlf.com');
ga('send', 'pageview');
</script>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</body>
</html>