-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharsort.html
More file actions
316 lines (270 loc) · 8.59 KB
/
charsort.html
File metadata and controls
316 lines (270 loc) · 8.59 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<style>
*{margin:0;padding:0}
body{background:#0a0a0c;overflow:hidden;touch-action:none;cursor:none}
canvas{display:block}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
const cv=document.getElementById('c');
const ctx=cv.getContext('2d');
const CELL=14;
let W,H,cols,rows;
let mx=0.5,my=0.5,pmx=0.5,pmy=0.5;
let t=0;
let field; // brightness field
let sorted; // sorted output
// combining marks
const ABOVE=['\u0300','\u0301','\u0302','\u0303','\u0307','\u0308','\u030A','\u030B','\u030C','\u0311'];
const BELOW=['\u0323','\u0324','\u0325','\u0330','\u0331','\u0332','\u0347'];
const STRIKE=['\u0334','\u0335','\u0336'];
// characters by brightness
const RAMP=' \u00B7\u2027\u2219\u2022-~:;\u2591=+\u2592x*\u2593#%\u2588@\u25A0\u25CF'.split('');
function resize(){
W=window.innerWidth;H=window.innerHeight;
cv.width=W;cv.height=H;
cols=Math.floor(W/(CELL*0.55));
rows=Math.floor(H/CELL);
field=new Array(rows);
sorted=new Array(rows);
for(let r=0;r<rows;r++){
field[r]=new Float32Array(cols);
sorted[r]=new Array(cols);
}
ctx.textBaseline='top';
}
// simplex-ish noise layers
function fbm(x,y,oct){
let v=0,amp=1,freq=1,total=0;
for(let i=0;i<oct;i++){
v+=amp*(Math.sin(x*freq*2.17+t*0.7+i*31.7)*Math.cos(y*freq*1.93+t*0.5+i*17.3)
+Math.sin((x+y)*freq*1.47+t*0.9+i*7.1)*0.5);
total+=amp;
amp*=0.5;
freq*=2.1;
}
return v/total;
}
// generate procedural field with features
function generateField(){
const time=t;
// slowly moving shapes
const cx1=0.3+Math.sin(time*0.3)*0.2;
const cy1=0.4+Math.cos(time*0.25)*0.2;
const cx2=0.7+Math.cos(time*0.35)*0.15;
const cy2=0.6+Math.sin(time*0.2)*0.2;
const cx3=0.5+Math.sin(time*0.4)*0.25;
const cy3=0.3+Math.cos(time*0.3)*0.25;
for(let r=0;r<rows;r++){
for(let c=0;c<cols;c++){
const fx=c/cols,fy=r/rows;
// noise base
let v=fbm(fx*3,fy*3,4)*0.5+0.5;
// radial shapes that move
const d1=Math.sqrt((fx-cx1)**2+(fy-cy1)**2);
const d2=Math.sqrt((fx-cx2)**2+(fy-cy2)**2);
const d3=Math.sqrt((fx-cx3)**2+(fy-cy3)**2);
v+=Math.max(0,0.3-d1)*2;
v+=Math.max(0,0.25-d2)*1.8;
v+=Math.max(0,0.2-d3)*2.2;
// diagonal bands
v+=Math.sin((fx*4+fy*3+time*0.4)*6)*0.08;
// mouse proximity adds brightness
const dm=Math.sqrt((fx-pmx)**2+(fy-pmy)**2);
v+=Math.max(0,0.12-dm)*4;
field[r][c]=Math.max(0,Math.min(1,v));
}
}
}
// THE SORT - this is the core pixel sort algorithm adapted for characters
function sortField(){
// threshold controlled by mouse Y
const loThresh=Math.max(0.05,pmy*0.5-0.1);
const hiThresh=Math.min(0.98,pmy*0.5+0.4);
// sort direction influenced by mouse X: 0=horizontal, 1=vertical, blend
const hWeight=1-Math.abs(pmx-0.5)*2; // center=vertical, edges=horizontal
// horizontal sort
for(let r=0;r<rows;r++){
// find spans to sort: contiguous runs where brightness is within threshold
let spans=[];
let spanStart=-1;
for(let c=0;c<cols;c++){
const v=field[r][c];
const inRange=v>loThresh&&v<hiThresh;
if(inRange&&spanStart===-1){
spanStart=c;
}else if(!inRange&&spanStart!==-1){
spans.push([spanStart,c-1]);
spanStart=-1;
}
}
if(spanStart!==-1)spans.push([spanStart,cols-1]);
// copy original values
const rowCopy=new Float32Array(field[r]);
// sort each span by brightness
for(const[start,end]of spans){
if(end-start<2)continue;
const slice=[];
for(let c=start;c<=end;c++){
slice.push({val:rowCopy[c],origCol:c});
}
// sort ascending (dark to light) or descending based on position
const descending=r%2===0;
slice.sort((a,b)=>descending?b.val-a.val:a.val-b.val);
for(let i=0;i<slice.length;i++){
field[r][start+i]=slice[i].val;
}
}
}
// vertical sort pass (partial, based on mouse X)
if(pmx>0.3&&pmx<0.7){
const vStrength=1-Math.abs(pmx-0.5)*4;
const vLoThresh=loThresh*0.8;
const vHiThresh=hiThresh*1.1;
// only sort some columns for performance
const step=Math.max(1,Math.floor(3-vStrength*2));
for(let c=0;c<cols;c+=step){
let spans=[];
let spanStart=-1;
for(let r=0;r<rows;r++){
const v=field[r][c];
const inRange=v>vLoThresh&&v<vHiThresh;
if(inRange&&spanStart===-1){
spanStart=r;
}else if(!inRange&&spanStart!==-1){
spans.push([spanStart,r-1]);
spanStart=-1;
}
}
if(spanStart!==-1)spans.push([spanStart,rows-1]);
const colVals=[];
for(let r=0;r<rows;r++)colVals.push(field[r][c]);
for(const[start,end]of spans){
if(end-start<2)continue;
const slice=[];
for(let r=start;r<=end;r++)slice.push(colVals[r]);
slice.sort((a,b)=>a-b);
for(let i=0;i<slice.length;i++){
field[start+i][c]=slice[i];
}
}
}
}
}
// color from brightness - rich warm-to-cold palette
function valToColor(v,origV,isSorted){
// base color from brightness
let r,g,b;
if(v<0.2){
const f=v/0.2;
r=8+f*20;g=6+f*15;b=15+f*40;
}else if(v<0.4){
const f=(v-0.2)/0.2;
r=28+f*30;g=21+f*60;b=55+f*40;
}else if(v<0.6){
const f=(v-0.4)/0.2;
r=58+f*80;g=81+f*40;b=95-f*30;
}else if(v<0.8){
const f=(v-0.6)/0.2;
r=138+f*80;g=121+f*50;b=65-f*20;
}else{
const f=(v-0.8)/0.2;
r=218+f*37;g=171+f*70;b=45+f*130;
}
// sorted streaks get color shift - push toward magenta/cyan
if(isSorted){
const shift=Math.sin(v*12+t*3)*0.3;
if(shift>0){
r=Math.min(255,r+shift*80);
b=Math.min(255,b+shift*60);
}else{
g=Math.min(255,g-shift*60);
b=Math.min(255,b-shift*40);
}
}
return[Math.floor(Math.min(255,Math.max(0,r))),
Math.floor(Math.min(255,Math.max(0,g))),
Math.floor(Math.min(255,Math.max(0,b)))];
}
function frame(){
t+=0.012;
pmx+=(mx-pmx)*0.06;
pmy+=(my-pmy)*0.06;
// generate fresh field
generateField();
// store pre-sort for comparison
const preSorted=new Array(rows);
for(let r=0;r<rows;r++)preSorted[r]=new Float32Array(field[r]);
// SORT
sortField();
// RENDER
ctx.fillStyle='#0a0a0c';
ctx.fillRect(0,0,W,H);
const cw=CELL*0.55;
for(let r=0;r<rows;r++){
for(let c=0;c<cols;c++){
const v=field[r][c];
if(v<0.06)continue;
const origV=preSorted[r][c];
const wasSorted=Math.abs(v-origV)>0.02;
const sortDelta=Math.abs(v-origV);
// character from brightness
const charIdx=Math.floor(v*(RAMP.length-1));
let base=RAMP[Math.min(RAMP.length-1,charIdx)];
// zalgo on sorted streaks proportional to how far the value moved
let glyph=base;
if(wasSorted){
const stack=Math.min(Math.floor(sortDelta*8),4);
if(stack>=1)glyph+=ABOVE[Math.floor((t*2+c*1.1+v*10)%ABOVE.length)];
if(stack>=2)glyph+=BELOW[Math.floor((t*1.7+r*0.9+v*7)%BELOW.length)];
if(stack>=3)glyph+=ABOVE[Math.floor((t*3+c*0.7+r)%ABOVE.length)];
if(stack>=4)glyph+=STRIKE[Math.floor(t*4+c)%STRIKE.length];
}
// color
const[cr,cg,cb]=valToColor(v,origV,wasSorted);
// streak glow: sorted areas slightly brighter
const brightness=wasSorted?1+sortDelta*0.5:1;
const fr=Math.min(255,Math.floor(cr*brightness));
const fg=Math.min(255,Math.floor(cg*brightness));
const fb=Math.min(255,Math.floor(cb*brightness));
ctx.fillStyle=`rgb(${fr},${fg},${fb})`;
ctx.fillText(glyph,c*cw,r*CELL);
}
}
// cursor
const curX=pmx*W,curY=pmy*H;
// threshold indicator lines
const loThresh=Math.max(0.05,pmy*0.5-0.1);
const hiThresh=Math.min(0.98,pmy*0.5+0.4);
ctx.fillStyle='rgba(255,255,255,0.12)';
ctx.fillRect(0,0,4,H);
const loY=loThresh*H,hiY=hiThresh*H;
ctx.fillStyle='rgba(255,100,180,0.25)';
ctx.fillRect(0,loY,6,hiY-loY);
// crosshair
ctx.fillStyle='rgba(220,200,255,0.5)';
ctx.font='14px Courier New';
ctx.fillText('\u253C',curX-4,curY-6);
ctx.font=CELL+'px Courier New';
requestAnimationFrame(frame);
}
document.addEventListener('mousemove',e=>{mx=e.clientX/W;my=e.clientY/H});
document.addEventListener('touchmove',e=>{e.preventDefault();mx=e.touches[0].clientX/W;my=e.touches[0].clientY/H},{passive:false});
// click: drop a bright spot that disrupts sorting
document.addEventListener('click',e=>{
// handled through mouse proximity in field generation
// but also pulse the time forward for visual burst
t+=0.15;
});
window.addEventListener('resize',resize);
resize();
frame();
</script>
</body>
</html>