-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyteCityNight.html
355 lines (309 loc) · 9.61 KB
/
byteCityNight.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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Byte City Night</title>
<script id="fxhash-snippet">
// FXHASH BOILER PLATE CODE
//---- do not edit the following code (you can indent as you wish)
let alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
var fxhash = "oo" + Array(49).fill(0).map(_=>alphabet[(Math.random()*alphabet.length)|0]).join('')
let b58dec = str=>[...str].reduce((p,c)=>p*alphabet.length+alphabet.indexOf(c)|0, 0)
let fxhashTrunc = fxhash.slice(2)
let regex = new RegExp(".{" + ((fxhash.length/4)|0) + "}", 'g')
let hashes = fxhashTrunc.match(regex).map(h => b58dec(h))
let sfc32 = (a, b, c, d) => {
return () => {
a |= 0; b |= 0; c |= 0; d |= 0
var t = (a + b | 0) + d | 0
d = d + 1 | 0
a = b ^ b >>> 9
b = c + (c << 3) | 0
c = c << 21 | c >>> 11
c = c + t | 0
return (t >>> 0) / 4294967296
}
}
var fxrand = sfc32(...hashes)
//---- /do not edit the following code
</script>
<style>
html { height: 100%; }
body
{
height: 100%;
margin: 0;
background:#111;
display: flex;
justify-content: center;
align-items: center;
}
canvas
{
max-height: 100%;
max-width: 100%;
}
</style>
</head>
<body>
<canvas id=mainCanvas width=4096 height=2048></canvas>
<script>
'use strict';
/*
Byte City Night
An abstract cityscape using bitwise operators.
Right click to download a 4096x2048 image.
By Frank Force @KilledByAPixel
Made with vanilla JavaScript
Based on this dweet by me...
https://www.dwitter.net/d/17507
I made a video that explains how it works...
https://www.youtube.com/watch?v=vnx8kI4EcVc
*/
///////////////////////////////////////////////////////////////////////////////
// FX HASH ARTWORK
let isInverted;
let isGrayscale;
let isRainbow;
let windowOperatorType;
let backgroundOperatorType;
let seedX, seedY, startY;
let brightBuildings;
let windowHueOffset;
let roomsPerSecond;
let extraHueOffset;
let startScale;
let foreSat;
let shiftHue;
let bigMoon;
let earthquake;
let thinBuildings;
let neonHue;
let moonCount;
function init()
{
// init canvas
canvas.width = mainCanvas.width;
canvas.height = mainCanvas.height;
// select operators
const operatorTypes = ['&','|','^','+','*','/','-^+'];
windowOperatorType = random.int(operatorTypes.length);
backgroundOperatorType = random.int(operatorTypes.length);
startScale = random.int(5,9);
// select moon count
moonCount = random.int(1,4);
if (random.bool(.05))
moonCount = random.int(30,60);
if (random.bool(.04))
moonCount = 0;
if (random.bool(.05))
bigMoon = moonCount = 1;
// select features
brightBuildings = random.bool(.05);
isInverted = random.bool(.04);
earthquake = random.bool(.04);
thinBuildings = random.bool(.03);
isGrayscale = random.bool(.1);
isRainbow = !isGrayscale && random.bool(.03);
shiftHue = !isRainbow && !isGrayscale && random.bool(.1);
let effectText = '';
if (earthquake)
effectText += (effectText.length?' ':'')+'Earthquake';
if (isInverted)
effectText += (effectText.length?' ':'')+'Inverted';
if (brightBuildings)
effectText += (effectText.length?' ':'')+'Bright Windows';
if (thinBuildings)
effectText += (effectText.length?' ':'')+'Thin';
if (effectText == '')
effectText = 'None';
let colorText =
isGrayscale ? 'Grayscale' :
isRainbow ? 'isRainbow' :
shiftHue ? 'Shifted' :
'Normal';
let moonText =
bigMoon ? 'Big' :
moonCount>5 ? 'Many' :
moonCount ? moonCount :
'None';
window.$fxhashFeatures =
{
'Window Operator':operatorTypes[windowOperatorType],
'Background Operator':operatorTypes[backgroundOperatorType],
'Moons':moonText,
'Colors':colorText,
'Extras':effectText
}
console.table(window.$fxhashFeatures)
console.log(fxhash);
console.log('Byte City Night\nBy Frank Force @KilledByAPixel');
// setup parameters
extraHueOffset = shiftHue ? .5 : isRainbow ? random.float() : 0;
neonHue = random.float();
seedX = random.int(1e3);
seedY = random.int(1e3);
windowHueOffset = random.float(0,.15);
roomsPerSecond = random.float(2,3);
startY = random.float(99,400);
foreSat = random.float(isRainbow?.4:.1,1);
drawBackground();
update();
}
function drawBackground()
{
const backgroundHueIntensity= random.int(50,100);
const backgroundStartBright = random.float(0,.2);
const backgroundSat = random.float(.2,.9);
const hueOffset = random.float(.4,.75);
const backBrightness = random.float(200,600);
const backBrighHue = random.int(500,2e3);
const scale = random.int(8,33);
const w = Math.ceil(canvas.width/scale);
const h = Math.ceil(canvas.height/scale);
// draw sky
let seedScale = random.int(1e6);
for(let k=w*h; k--;)
{
const i=k%w, j=k/w|0;
const o = applyOperator(backgroundOperatorType,i,j);
const bright = Math.cos(o*seedScale);
const hue = bright*backgroundHueIntensity;
context.fillStyle = hsl(
extraHueOffset+hueOffset+hue/800+j/(isRainbow?100:300),
backgroundSat+hue/1800,
backgroundStartBright+hue/backBrighHue+j/backBrightness);
context.fillRect(i*scale,j*scale,scale,scale);
}
// draw moons
context.save();
context.globalCompositeOperation = 'difference';
context.fillStyle = '#fff';
for(let i=moonCount;i--;)
{
const m = 99;
const r = bigMoon?random.float(500,700):random.float(150,300);
const x = random.float(m*2+r,canvas.width-m*2-r);
const y = random.float(m+r,m+r+300);
context.beginPath();
context.arc(x,y,r,0,9);
context.fill();
}
context.restore();
}
function update()
{
// update time
const FPS = 60;
const t = frame++ / FPS;
// update neon glow
mainCanvas.style.boxShadow=`0px 0px 50px ${10+5*Math.sin(t*PI/2)}px `+
hsl((isRainbow?t/60:0)+neonHue,1,isGrayscale?1:.5);
// prepare to draw buildings
const roomsWide = thinBuildings? random.int(1,4) : random.int(5,12);
const Y = startY+(t*60)*7*roomsWide/roomsPerSecond;
if (Y>canvas.height)
return;
context.save();
if (earthquake)
context.rotate(random.floatSign(.1));
const X = random.float(-200,canvas.width+200);
const w = roomsWide*2+1;
const seedScale = random.int(1e6);
const scale = startScale+t*10|0;
// draw building
for(let k=1e4; k--;)
{
const i=k%w, j=k/w|0;
const o = applyOperator(windowOperatorType,i,j);
const bright = Math.cos(o*seedScale);
let h = windowHueOffset-j*.001
+ bright*(.15-windowHueOffset)
+ (isRainbow ? t : 0);
let s = foreSat + random.floatSign(.1);
let l = bright;
if (i*j%2==0 && !brightBuildings)
l = s = 0;
context.fillStyle = hsl(h+extraHueOffset,s,l);
context.fillRect(
i*scale+X|0, j*scale+Y|0,
scale+earthquake, scale+earthquake);
}
context.restore();
compositeImage();
requestAnimationFrame(update);
}
function applyOperator(operator, x,y)
{
x += seedX;
y += seedY;
switch (operator)
{
case 0: return x&y;
case 1: return x|y;
case 2: return x^y;
case 3: return x+y;
case 4: return x*y;
case 5: return x/y + y/x;
case 6: return (x-y)^(x+y);
default: return 0;
}
}
function compositeImage()
{
mainCanvas.width |= 0;
mainContext.drawImage(canvas,0,0,mainCanvas.width,mainCanvas.height);
// post process
if (isGrayscale)
{
mainContext.save();
mainContext.fillStyle = '#fff';
mainContext.globalCompositeOperation = 'saturation';
mainContext.fillRect(0,0,mainCanvas.width,mainCanvas.height);
mainContext.restore();
}
if (isInverted)
{
mainContext.save();
mainContext.fillStyle = '#fff';
mainContext.globalCompositeOperation = 'difference';
mainContext.fillRect(0,0,mainCanvas.width,mainCanvas.height);
mainContext.restore();
}
}
///////////////////////////////////////////////////////////////////////////////
// SEEDED RANDOM GENERATOR
class Random
{
constructor(seed) { this.setSeed(seed); }
setSeed(seed) { this.seed = seed|0; }
float(a=1, b=0)
{ // xorshift
this.seed ^= this.seed << 13;
this.seed ^= this.seed >>> 17;
this.seed ^= this.seed << 5;
return b + (a-b) * Math.abs(this.seed % 1e9) / 1e9;
}
floatSign(a, b) { return this.float(a,b) * this.sign(); }
int(a=1, b=0) { return this.float(a, b)|0; }
bool(chance = .5) { return this.float() < chance; }
sign() { return this.bool() ? -1 : 1; }
angle(p=1) { return this.float(PI*2*p); }
}
const random = new Random(fxrand()*1e9);
///////////////////////////////////////////////////////////////////////////////
// SIMPLE SHIM
const PI = Math.PI;
const mod = (a, b=1)=> ((a % b) + b) % b;
const clamp = (v, min=0, max=1)=> v < min ? min : v > max ? max : v;
const hsl = (h=0,s=0,l=0,a=1) => `hsla(${mod(h)*360},${clamp(s)*100}%,${clamp(l)*100}%,${clamp(a)})`;
let frame = 0;
const mainContext = mainCanvas.getContext('2d');
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
///////////////////////////////////////////////////////////////////////////////
// STARTUP
init();
</script>
</body>
</html>