forked from resbaz/OpenRes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoronoi_play.html
188 lines (134 loc) · 4.02 KB
/
voronoi_play.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
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="1000"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-color.v1.min.js"></script>
<script>
var col1 = '#00779c';
var col2 = '#ff6700';
var site_r = 3;
var col_test = d3.hsl(col1)
col_test.opacity = 0.6;
col_test.s = 0.5;
col_test.l = 0.6;
var cols = [col1, col2];
console.log(col_test)
var svg = d3.select("svg")
// .on("touchmove mousemove", moved),
width = +svg.attr("width"),
height = +svg.attr("height");
var sites_proto = d3.range(300).map(d => []);
var simulation = d3.forceSimulation(sites_proto)
.force('x', d3.forceX(width).strength(0.7))
.force('y', d3.forceY(height).strength(0.2))
.force('collide', d3.forceCollide()
.radius(function(n){
return 40 + ((Math.random()*40) - (Math.random()*30))
})
.strength(1)
)
.stop()
for (var i=0; i<300; ++i) {
simulation.tick();
simulation.nodes().forEach(function(n){
n.x = Math.max((0+site_r), Math.min((width - site_r), n.x));
n.y = Math.max((0+site_r), Math.min((height - site_r), n.y))
})
}
console.log('calc sites', sites_proto)
// var sites =[
// [0.1, 0.1],
// [0.1, 0.2],
// [0.2, 0.1],
// [0.3, 0.2],
// [0.25, 0.3],
// [0.5, 0.3],
// [0.4, 0.2],
// [0.6, 0.4],
// [0.5, 0.6],
// [0.8, 0.5],
// [0.5, 0.8],
// [0.9, 0.4],
// [0.9, 0.9]];
// sites = sites.map(function(d){
// return [d[0]*width, d[1]*height];
// })
var sites = sites_proto.map(d => [d.x, d.y])
sites
.sort(function(a, b){
return Math.sqrt(Math.pow(b[0], 2) + Math.pow(b[1], 2)) - Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2))
})
console.log('sites', sites)
var voronoi = d3.voronoi();
console.log('triangles', voronoi.triangles(sites))
var triangle = svg.append("g")
.attr("class", "triangles")
.selectAll("path")
.data(voronoi.triangles(sites))
.enter().append("path")
.attr("d", function(d) { return "M" + d.join("L") + "Z"; })
.attr('stroke', '#BBB')
.attr('fill', function(d, i){
// console.log('idxs', i, size)
// col_test.s = i/size;
// col_test.s = ((Math.round(Math.random())*2)-1) * 0.1;
// col_test.s = Math.random();
// col_test.h = (Math.random() * 360)
// var new_col = d3.hsl(col_test.toString())
// new_col.s += ((Math.round(Math.random())*2)-1) * (Math.random()*0.4);
var col = d3.hsl(cols[Math.round(Math.random())])
col.s = 0.95;
col.l = 0.92;
// col.opacity = 0.2;
// col.s += ((Math.round(Math.random())*2)-1) * (Math.random()*0.2);
col.s -= (Math.random()*0.9);
// col.l -= (Math.random()*0.1);
// if (col_test.s < 0) {
// col_test.s = 0
// }
// if (col_test.s > 1) {
// col_test.s = 1
// }
// console.log('col', col_test)
return col;
})
.attr('fill-opacity', 0)
// .attr('stroke-opacity', 0)
.attr('stroke-dasharray', function(){
console.log('path length', d3.select(this).node().getTotalLength())
return d3.select(this).node().getTotalLength();
})
.attr('stroke-dashoffset', function(){
return d3.select(this).node().getTotalLength();
})
.transition()
.delay(function(d, i){return i * 40;})
.duration(1500)
// .attr('stroke-opacity', 1)
.attr('stroke-dashoffset', 0)
.transition()
.duration(800)
// .delay(function(d, i){return i * 200;})
.attr('fill-opacity', 1)
.on('end', function(){
console.log('end trans', this)
d3.select(this)
.attr('stroke-dasharray', 0)
})
var site = svg.append("g")
.attr("class", "sites")
.selectAll("circle")
.data(sites)
.enter().append("circle")
.attr('fill', '#DDD')
.attr('stroke', '#BBB')
.attr('stroke-width', 1)
.attr("r", site_r)
.attr("cx", function(d) { return d[0]; })
.attr("cy", function(d) { return d[1]; })
.attr('opacity', 0)
.transition()
.duration(300)
.delay(function(d, i){return i * 50;})
.attr('opacity', 1);
</script>