-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
141 lines (126 loc) · 5.55 KB
/
Copy pathindex.html
File metadata and controls
141 lines (126 loc) · 5.55 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Moiré</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;500&display=swap" rel="stylesheet">
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{background:#0a0a0f;overflow:hidden;font-family:'Inter',sans-serif;cursor:crosshair;-webkit-tap-highlight-color:transparent;user-select:none}
canvas{display:block}
.title{position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);color:rgba(255,255,255,.55);font-size:clamp(1.4rem,4vw,2.4rem);font-weight:300;letter-spacing:.5em;text-transform:uppercase;z-index:10;transition:opacity 2s;pointer-events:none}
.hint{position:fixed;top:calc(50% + 2.5rem);left:50%;transform:translateX(-50%);color:rgba(255,255,255,.2);font-size:.72rem;font-weight:300;letter-spacing:.18em;z-index:10;transition:opacity 2s;pointer-events:none}
.mode-lbl{position:fixed;top:1.4rem;left:50%;transform:translateX(-50%);color:rgba(255,255,255,.18);font-size:.6rem;font-weight:300;letter-spacing:.25em;text-transform:uppercase;z-index:10;pointer-events:none}
.controls{position:fixed;bottom:2rem;left:50%;transform:translateX(-50%);display:flex;gap:.5rem;z-index:10}
.btn{background:rgba(255,255,255,.03);border:1px solid rgba(255,255,255,.07);color:rgba(255,255,255,.35);padding:.4rem .9rem;font-family:'Inter',sans-serif;font-size:.65rem;letter-spacing:.12em;text-transform:uppercase;cursor:pointer;transition:all .3s;border-radius:2px}
.btn:hover{background:rgba(255,255,255,.08);color:rgba(255,255,255,.7)}
.btn.on{background:rgba(255,255,255,.09);color:rgba(255,255,255,.85);border-color:rgba(255,255,255,.18)}
.speed-lbl{position:fixed;bottom:4.5rem;left:50%;transform:translateX(-50%);color:rgba(255,255,255,.15);font-size:.55rem;letter-spacing:.15em;z-index:10;pointer-events:none}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div class="title" id="title">Moiré</div>
<div class="hint" id="hint">move to shift the pattern</div>
<div class="mode-lbl" id="ml">circles</div>
<div class="speed-lbl" id="sl">scroll to change density</div>
<div class="controls">
<button class="btn on" data-m="circles">circles</button>
<button class="btn" data-m="lines">lines</button>
<button class="btn" data-m="radial">radial</button>
<button class="btn" data-m="grid">grid</button>
</div>
<script>
const $=s=>document.querySelector(s);
const C=$('#c'),X=C.getContext('2d');
let w,h,mx,my,tx,ty,time=0,mode='circles',touched=false,spacing=6;
function resize(){
w=C.width=innerWidth;h=C.height=innerHeight;
mx=tx=w/2;my=ty=h/2;
}
addEventListener('resize',resize);resize();
function onInput(x,y){
tx=x;ty=y;
if(!touched){touched=true;$('#title').style.opacity='.06';$('#hint').style.opacity='0';$('#sl').style.opacity='0';}
}
addEventListener('mousemove',e=>onInput(e.clientX,e.clientY));
addEventListener('touchmove',e=>{e.preventDefault();onInput(e.touches[0].clientX,e.touches[0].clientY)},{passive:false});
addEventListener('touchstart',e=>{e.preventDefault();onInput(e.touches[0].clientX,e.touches[0].clientY)},{passive:false});
addEventListener('wheel',e=>{
e.preventDefault();
spacing=Math.max(3,Math.min(20,spacing+Math.sign(e.deltaY)));
},{passive:false});
document.querySelectorAll('.btn').forEach(b=>{
b.addEventListener('click',()=>{
document.querySelectorAll('.btn').forEach(x=>x.classList.remove('on'));
b.classList.add('on');mode=b.dataset.m;$('#ml').textContent=mode;
});
});
function circles(cx,cy,hue){
const mr=Math.sqrt(w*w+h*h);
for(let r=spacing;r<mr;r+=spacing){
X.beginPath();X.arc(cx,cy,r,0,Math.PI*2);
X.strokeStyle='hsla('+(r*.25+hue)%360+',55%,52%,.18)';
X.lineWidth=.7;X.stroke();
}
}
function lines(cx,cy,ang,hue){
X.save();X.translate(cx,cy);X.rotate(ang);
const n=Math.ceil(Math.sqrt(w*w+h*h)/spacing);
for(let i=-n;i<=n;i++){
X.beginPath();X.moveTo(i*spacing,-h);X.lineTo(i*spacing,h);
X.strokeStyle='hsla('+(Math.abs(i)*1.5+hue)%360+',55%,52%,.18)';
X.lineWidth=.7;X.stroke();
}
X.restore();
}
function radial(cx,cy,hue,rot){
X.save();X.translate(cx,cy);X.rotate(rot);
const mr=Math.sqrt(w*w+h*h),n=90;
for(let i=0;i<n;i++){
const a=(i/n)*Math.PI*2;
X.beginPath();X.moveTo(0,0);
X.lineTo(Math.cos(a)*mr,Math.sin(a)*mr);
X.strokeStyle='hsla('+(i*4+hue)%360+',55%,52%,.14)';
X.lineWidth=.7;X.stroke();
}
X.restore();
}
function grid(cx,cy,hue){
const gap=spacing*2.5,sz=Math.max(1.5,gap*.2);
const cols=Math.ceil(w/gap)+4,rows=Math.ceil(h/gap)+4;
const sx=cx-(cols/2)*gap,sy=cy-(rows/2)*gap;
for(let r=0;r<rows;r++){
for(let c=0;c<cols;c++){
const x=sx+c*gap,y=sy+r*gap;
const d=Math.sqrt((c-cols/2)**2+(r-rows/2)**2);
X.fillStyle='hsla('+(d*6+hue)%360+',55%,52%,.45)';
X.fillRect(x-sz/2,y-sz/2,sz,sz);
}
}
}
function frame(){
const cx=w/2,cy=h/2;
// auto-drift before interaction
if(!touched){tx=cx+Math.cos(time*.4)*60;ty=cy+Math.sin(time*.55)*60;}
// smooth follow
mx+=(tx-mx)*.045;my+=(ty-my)*.045;
const ox=cx+(mx-cx)*.55,oy=cy+(my-cy)*.55;
X.fillStyle='#0a0a0f';X.fillRect(0,0,w,h);
X.globalCompositeOperation='lighter';
const h1=(time*12)%360,h2=(h1+180)%360;
switch(mode){
case'circles':circles(cx,cy,h1);circles(ox,oy,h2);break;
case'lines':lines(cx,cy,0,h1);lines(ox,oy,.025+Math.sin(time*.4)*.018,h2);break;
case'radial':radial(cx,cy,h1,0);radial(ox,oy,h2,time*.08);break;
case'grid':grid(cx,cy,h1);grid(ox,oy,h2);break;
}
X.globalCompositeOperation='source-over';
time+=.007;
requestAnimationFrame(frame);
}
frame();
</script>
</body>
</html>