-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
365 lines (308 loc) · 13.5 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE html>
<html lang="en">
<head>
<title>bradi.sh</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Homepage for bradi.sh">
<script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
<div id = "svg" ></div>
<script type = "application/javascript">
const margin = {top: 0, right: 0, bottom: 0, left: 0};
const width = 400 - margin.left - margin.right;
const height = 880 - margin.top - margin.bottom;
const TILE_SIZE = 80
const TILES_PER_ROW = Math.ceil(width/TILE_SIZE)
const TILES_PER_COLUMN = Math.ceil(height/TILE_SIZE)
const COLOURS = [
"#FFF4E0",
"#F8B501",
"#06ACB5",
"#17191D",
"#FC3D3C"
]
const PAINT_LOCATIONS = {
TOP_LEFT_CORNER: 0,
TOP_RIGHT_CORNER: 1,
BOTTOM_RIGHT_CORNER: 2,
BOTTOM_LEFT_CORNER: 3,
CENTER: 4,
BASE:5,
TEXT:6
}
const N_PAINT_LOCATIONS = Object.keys(PAINT_LOCATIONS).length
const CORNERS = [
PAINT_LOCATIONS.TOP_LEFT_CORNER,
PAINT_LOCATIONS.TOP_RIGHT_CORNER,
PAINT_LOCATIONS.BOTTOM_RIGHT_CORNER,
PAINT_LOCATIONS.BOTTOM_LEFT_CORNER
]
const CHANCE = {
ofRecolour : 0.1,
ofRecircle : 0.1,
ofRerotate : 0.01,
ofHavingACenter: 0.75,
ofOffCorners : 0.1,
ofIgnoringNeighbours: 0.2,
}
const TEXT = [["b","r"],[,"a","d","i"],[,,"\u00b7","s","h"]]
const URLS = [["https://greenchef.bradi.sh"]]
const findText = (x,y)=>(TEXT[y] || [])[x] || false
const findURL = (x,y)=>(URL[y] || [])[x] || false
class Tile{
constructor(ix,iy){
this._ix = ix
this._iy = iy
this._x = this.ix*TILE_SIZE;
this._y = this.iy*TILE_SIZE;
this._neighbours = [
//up
this.ix + TILES_PER_ROW * ((this.iy-1+TILES_PER_COLUMN)%TILES_PER_COLUMN),
//down
this.ix + TILES_PER_ROW * ((this.iy+1+TILES_PER_COLUMN)%TILES_PER_COLUMN),
//left
(this.ix-1+TILES_PER_ROW)%TILES_PER_ROW + TILES_PER_ROW*this.iy,
//right
(this.ix+1+TILES_PER_ROW)%TILES_PER_ROW + TILES_PER_ROW*this.iy
]
this._cx = this._cy = TILE_SIZE/2
this._index = this.iy * TILES_PER_ROW + this.ix
this.rerotate()
this.recircle(true)
this.recolour(true)
this._text = findText(this.ix,this.iy)
this._url = findURL(this.ix,this.iy)
this._clicked = false
if(this.text){
this._rotation = 0
this.fixTextColour()
}
}
tickle(force=false){
if(force || Math.random()<CHANCE.ofRecolour) this.recolour()
if(force || Math.random()<CHANCE.ofRecircle) this.recircle()
if(force || Math.random()<CHANCE.ofRerotate) this.rerotate()
this.text && this.fixTextColour()
}
recircle(noOffCorners=false){
this._centerCircleRadius = Math.random() < CHANCE.ofHavingACenter ? TILE_SIZE/2*Math.SQRT2*(Math.random() * 0.1 + 0.15) : 0
this._cornerCircleRadius = TILE_SIZE/2
//prefer diagonal pairs, but allow switching
this._corners = Math.random() > 0.5 ? [1,0,1,0] : [0,1,0,1]
//now flick some on at random
if(!noOffCorners) this._corners = this._corners.map(corner=>Math.random()<CHANCE.ofOffCorners ? 1-corner : corner)
}
recolour(ignoreNeighbours=false){
const chosenNeighbour = Math.random()*this.neighbours.length|0
if(!ignoreNeighbours && Math.random()>CHANCE.ofIgnoringNeighbours){
this._colours = this._colours.map((colour,index) => Math.random()>0.5 ? tiles[this.neighbours[chosenNeighbour]].colours[index] : colour)
} else {
this._colours = Array(N_PAINT_LOCATIONS).fill(0).map(_=>COLOURS[Math.random()*COLOURS.length|0])
}
}
rerotate(){
this._rotation = [-180,-90,0,90,180][Math.random()*5|0]
}
fixTextColour(){
if(this.colours[PAINT_LOCATIONS.TEXT] == this.colours[this.centerCircleRadius > 0 ? PAINT_LOCATIONS.CENTER : PAINT_LOCATIONS.BASE]){
this._colours[PAINT_LOCATIONS.TEXT] = COLOURS.find(entry=>this.colours[this.centerCircleRadius>0 ? PAINT_LOCATIONS.CENTER : PAINT_LOCATIONS.BASE]!=entry)
}
}
click(){
if(!this._clicked){
this.tickle(true)
return this._clicked = true
} else return false
}
done(){
this._clicked = false
}
get neighbours(){
return this._neighbours
}
get clicked(){
return this._clicked
}
get text(){
return this._text
}
get url(){
return this._url
}
get ix(){
return this._ix
}
get iy(){
return this._iy
}
get x(){
return this._x
}
get y(){
return this._y
}
get cx(){
return this._cx
}
get cy(){
return this._cy
}
get index(){
return this._index
}
get cornerCircleRadius(){
return this._cornerCircleRadius
}
get centerCircleRadius(){
return this._centerCircleRadius
}
get colours(){
return this._colours
}
get rotation(){
return this._rotation
}
get corners(){
return [
(radius=>`M${radius},0A${radius},${radius},0,0,1,0,${radius}L0,0Z`)(this._corners[0] * this.cornerCircleRadius),
(radius=>`M${TILE_SIZE},${radius}A${radius},${radius},0,0,1,${TILE_SIZE-radius},0L${TILE_SIZE},0Z`)(this._corners[1] * this.cornerCircleRadius),
(radius=>`M${TILE_SIZE-radius},${TILE_SIZE}A${radius},${radius},0,0,1,${TILE_SIZE},${TILE_SIZE-radius}L${TILE_SIZE},${TILE_SIZE}Z`)(this._corners[2] * this.cornerCircleRadius),
(radius=>`M0,${TILE_SIZE-radius}A${radius},${radius},0,0,1,${radius},${TILE_SIZE}L0,${TILE_SIZE}Z`)(this._corners[3] * this.cornerCircleRadius)
]
}
get arcs(){
return [
(radius=>`M${radius},0A${radius},${radius},0,0,1,0,${radius}`)(this._corners[0] * this.cornerCircleRadius),
(radius=>`M${TILE_SIZE},${radius}A${radius},${radius},0,0,1,${TILE_SIZE-radius},0`)(this._corners[1] * this.cornerCircleRadius),
(radius=>`M${TILE_SIZE-radius},${TILE_SIZE}A${radius},${radius},0,0,1,${TILE_SIZE},${TILE_SIZE-radius}`)(this._corners[2] * this.cornerCircleRadius),
(radius=>`M0,${TILE_SIZE-radius}A${radius},${radius},0,0,1,${radius},${TILE_SIZE}`)(this._corners[3] * this.cornerCircleRadius)
]
}
}
const tiles = []
for(let y=0;y<height/TILE_SIZE;y++){
for(let x=0;x<width/TILE_SIZE;x++){
tiles.push(new Tile(x,y))
}
}
const clickHandler = function(d,i){
tiles[i.index].click() && redraw(i.index)
}
const svg = d3
.selectAll("#svg")
.append("svg")
.attr("viewBox", [0, 0, width, height]);
const theTiles = svg
.append("g")
.attr("id","theTiles")
const tileTranslations = theTiles
.selectAll(".tileTranslations")
.data(tiles,d=>d.index)
.enter()
.append("g")
.attr("transform",d=>`translate(${d.x},${d.y})`)
.attr("class","tileTranslations")
.on('click', clickHandler);
const tileOffsets = tileTranslations
.append("g")
.attr("transform",d=>`translate(${TILE_SIZE/2},${TILE_SIZE/2})`)
const tileRotations = tileOffsets
.append("g")
.attr("transform",d=>`rotate(${d.rotation})`)
const tileGroups = tileRotations
.append("g")
.attr("transform",d=>`translate(${TILE_SIZE/-2},${TILE_SIZE/-2})`)
.attr("class","tileTranslations")
const bases = tileGroups
.append("rect")
.attr("width",TILE_SIZE)
.attr("height",TILE_SIZE)
.attr("x",0)
.attr("y",0)
.attr("fill",d=>d.colours[PAINT_LOCATIONS.BASE])
.attr("stroke","none")
const corners = CORNERS.map(corner=>tileGroups
.append("path")
.attr("d",d=>d.corners[corner])
.attr("fill",d=>d.colours[corner])
.attr("stroke","none")
)
const arcs = CORNERS.map(arc=>tileGroups
.append("path")
.attr("d",d=>d.arcs[arc])
.attr("fill","none")
.attr("stroke","white")
)
const centers = tileGroups
.append("circle")
.attr("r",d=>d.centerCircleRadius)
.attr("cx",d=>d.cx)
.attr("cy",d=>d.cy)
.attr("fill",d=>d.colours[PAINT_LOCATIONS.CENTER])
const text = tileGroups
.filter(d=>d.text)
.append("text")
.attr("alignment-baseline","central")
.attr("text-anchor","middle")
.attr("font-family","sans-serif")
.attr("font-weight","bold")
.attr("x",TILE_SIZE/2)
.attr("y",TILE_SIZE/2)
.attr("fill",d=>d.colours[PAINT_LOCATIONS.TEXT])
.attr("font-size",`${TILE_SIZE/4}px`)
.text(d=>d.text)
const url = tileGroups
.filter(d=>d.url)
.append("a")
.attr("xlink:href",d=>d.url)
const redraw = tileIndex =>{
let all = tileIndex ? false : true
tileRotations
.filter(d=>(all&&!d.clicked)||d.index==tileIndex)
.transition()
.duration(2000)
.attr("transform",d=>`rotate(${d.rotation})`)
.on("end",d=>d.done())
bases
.filter(d=>(all&&!d.clicked)||d.index==tileIndex)
.transition()
.duration(2000)
.attr("fill",d=>d.colours[PAINT_LOCATIONS.BASE])
centers
.filter(d=>(all&&!d.clicked)||d.index==tileIndex)
.transition()
.duration(2000)
.attr("r",d=>d.centerCircleRadius)
.attr("cx",d=>d.cx)
.attr("cy",d=>d.cy)
.attr("fill",d=>d.colours[PAINT_LOCATIONS.CENTER])
arcs
.filter(d=>(all&&!d.clicked)||d.index==tileIndex)
.forEach((arc,index)=>{
arc
.transition()
.duration(2000)
.attr("d",d=>d.arcs[index])
})
corners
.filter(d=>(all&&!d.clicked)||d.index==tileIndex)
.forEach((corner,index)=>{
corner
.transition()
.duration(2000)
.attr("d",d=>d.corners[index])
.attr("fill",d=>d.colours[index])
})
text
.transition()
.duration(2000)
.attr("fill",d=>d.colours[PAINT_LOCATIONS.TEXT])
}
setInterval(()=>{
tiles.forEach(tile=>tile.tickle())
redraw()
},2500)
</script>
</body>
</html>