- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
Latest commit
200 lines (176 loc) · 7.1 KB
/
Copy pathbackground.js
File metadata and controls
200 lines (176 loc) · 7.1 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
/* ============================================================
MathEXplained — Shared decorative background
------------------------------------------------------------
Two layers, both purely ornamental and aria-hidden:
1. #proximity-grid — canvas dot grid that glows near the cursor
2. .math-bg-layer — slowly drifting pre-rendered math symbols
Loaded with `defer` on every page, so it runs once the DOM is parsed.
============================================================ */
(function(){
'use strict';
varreduceMotion=window.matchMedia&&
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
/* ----------------------------------------------------------
1. Proximity-glow grid
----------------------------------------------------------
Redraws only when something actually changed (pointer moved,
window resized). The previous version ran a bare
requestAnimationFrame loop that repainted every dot and grid
line ~60x/sec forever, which burned CPU even on an idle page.
Within a frame, the ~2000 background dots are stroked as a
single path with one fill() call; only the handful of cells
inside the glow radius are drawn individually.
---------------------------------------------------------- */
(functioninitGrid(){
varcanvas=document.getElementById('proximity-grid');
if(!canvas)return;
varctx=canvas.getContext('2d');
varTAU=Math.PI*2;
varCELL=36;
varRADIUS=180;
varBASE_ALPHA=0.045;
varGLOW_ALPHA=0.72;
varDOT_R=1.4;
varRGB='84, 145, 209';
varmouse={x: -9999,y: -9999};
varcssW=0,cssH=0;
vardirty=true;
varqueued=false;
functionresize(){
vardpr=Math.min(window.devicePixelRatio||1,2);
cssW=window.innerWidth;
cssH=window.innerHeight;
canvas.width=Math.round(cssW*dpr);
canvas.height=Math.round(cssH*dpr);
ctx.setTransform(dpr,0,0,dpr,0,0);
dirty=true;
schedule();
}
functiondraw(){
varcols=Math.ceil(cssW/CELL)+1;
varrows=Math.ceil(cssH/CELL)+1;
varr,c,x,y;
ctx.clearRect(0,0,cssW,cssH);
/* -- grid lines: all at base alpha in two batched paths -- */
ctx.lineWidth=0.5;
ctx.strokeStyle='rgba('+RGB+','+(BASE_ALPHA*0.7).toFixed(3)+')';
ctx.beginPath();
for(r=0;r<rows;r++){y=r*CELL;ctx.moveTo(0,y);ctx.lineTo(cssW,y);}
for(c=0;c<cols;c++){x=c*CELL;ctx.moveTo(x,0);ctx.lineTo(x,cssH);}
ctx.stroke();
/* -- dots: all at base alpha in one path -- */
ctx.fillStyle='rgba('+RGB+','+BASE_ALPHA.toFixed(3)+')';
ctx.beginPath();
for(r=0;r<rows;r++){
y=r*CELL;
for(c=0;c<cols;c++){
x=c*CELL;
ctx.moveTo(x+DOT_R,y);
ctx.arc(x,y,DOT_R,0,TAU);
}
}
ctx.fill();
/* -- glow: only cells whose centre falls inside the radius -- */
if(mouse.x<-1000)return;
varc0=Math.max(0,Math.floor((mouse.x-RADIUS)/CELL));
varc1=Math.min(cols-1,Math.ceil((mouse.x+RADIUS)/CELL));
varr0=Math.max(0,Math.floor((mouse.y-RADIUS)/CELL));
varr1=Math.min(rows-1,Math.ceil((mouse.y+RADIUS)/CELL));
for(r=r0;r<=r1;r++){
y=r*CELL;
vardy=y-mouse.y;
for(c=c0;c<=c1;c++){
x=c*CELL;
vardx=x-mouse.x;
varprox=1-Math.sqrt(dx*dx+dy*dy)/RADIUS;
if(prox<=0)continue;
ctx.fillStyle='rgba('+RGB+','+
(BASE_ALPHA+prox*prox*(GLOW_ALPHA-BASE_ALPHA)).toFixed(3)+')';
ctx.beginPath();
ctx.arc(x,y,DOT_R,0,TAU);
ctx.fill();
}
}
/* -- glow on the lines nearest the cursor -- */
ctx.lineWidth=0.5;
for(r=r0;r<=r1;r++){
y=r*CELL;
varpyH=1-Math.abs(mouse.y-y)/RADIUS;
if(pyH<=0)continue;
ctx.strokeStyle='rgba('+RGB+','+
(BASE_ALPHA*0.7+pyH*pyH*0.30).toFixed(3)+')';
ctx.beginPath();
ctx.moveTo(0,y);
ctx.lineTo(cssW,y);
ctx.stroke();
}
for(c=c0;c<=c1;c++){
x=c*CELL;
varpxV=1-Math.abs(mouse.x-x)/RADIUS;
if(pxV<=0)continue;
ctx.strokeStyle='rgba('+RGB+','+
(BASE_ALPHA*0.7+pxV*pxV*0.30).toFixed(3)+')';
ctx.beginPath();
ctx.moveTo(x,0);
ctx.lineTo(x,cssH);
ctx.stroke();
}
}
/* Coalesce pointer events into at most one repaint per frame. */
functionschedule(){
if(queued||!dirty)return;
queued=true;
requestAnimationFrame(function(){
queued=false;
dirty=false;
draw();
});
}
functiontrack(x,y){
if(x===mouse.x&&y===mouse.y)return;
mouse.x=x;
mouse.y=y;
dirty=true;
schedule();
}
window.addEventListener('mousemove',function(e){track(e.clientX,e.clientY);},{passive: true});
window.addEventListener('touchmove',function(e){
if(e.touches[0])track(e.touches[0].clientX,e.touches[0].clientY);
},{passive: true});
window.addEventListener('mouseleave',function(){track(-9999,-9999);},{passive: true});
window.addEventListener('resize',resize,{passive: true});
resize();
})();
/* ----------------------------------------------------------
2. Floating math symbols
----------------------------------------------------------
Symbols come from math-symbols.js as pre-rendered SVG, so
there is no MathJax runtime, no CDN webfont fetch, and no
typesetting work on the main thread. Skipped entirely when
the visitor prefers reduced motion.
---------------------------------------------------------- */
(functioninitSymbols(){
varlayer=document.querySelector('.math-bg-layer');
varsvgs=window.MATH_SYMBOL_SVGS;
if(!layer||!svgs||!svgs.length||reduceMotion)return;
// Fewer symbols on small screens — they crowd the text and cost more
// per-pixel to composite on the devices least able to afford it.
varcount=window.innerWidth<700 ? 22 : 58;
varfrag=document.createDocumentFragment();
for(vark=0;k<count;k++){
varidx=(k+Math.floor(Math.random()*svgs.length))%svgs.length;
varel=document.createElement('span');
vardur=22+Math.random()*30;
el.className='math-symbol';
el.innerHTML=svgs[idx];
el.style.cssText=
'left:'+(Math.random()*98).toFixed(2)+'%;'+
'font-size:'+(0.85+Math.random()*1.5).toFixed(2)+'rem;'+
'animation-duration:'+dur.toFixed(1)+'s;'+
'animation-delay:'+(-Math.random()*dur).toFixed(1)+'s;'+
'--rot:'+((Math.random()-0.5)*50).toFixed(1)+'deg;';
frag.appendChild(el);
}
layer.appendChild(frag);
})();
})();