-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (52 loc) · 1.71 KB
/
script.js
File metadata and controls
58 lines (52 loc) · 1.71 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
const sun=document.getElementById("sun");
const moon=document.getElementById("moon");
const bod=document.body;
let offsetX,offsetY;
moon.addEventListener("mousedown",function(e){
offsetX=e.clientX-moon.offsetLeft;
offsetY=e.clientY-moon.offsetTop;
document.addEventListener("mousemove",moveMoon);
document.addEventListener("mouseup",()=>
{
document.removeEventListener("mousemove",moveMoon);
}
);
});
function moveMoon(e){
let {x,y}=withinBound(e.clientX-offsetX,e.clientY-offsetY);
moon.style.left=`${x}px`;
moon.style.top=`${y}px`;
updateBackGround();
}
function areaOverlap(){
let moonRect=moon.getBoundingClientRect();
let sunRect=sun.getBoundingClientRect();
let x=Math.max(0,Math.min(sunRect.right,moonRect.right)-Math.max(moonRect.left,sunRect.left));
let y=Math.max(0,Math.min(sunRect.bottom,moonRect.bottom)-Math.max(moonRect.top,sunRect.top));
return (x*y)/(sunRect.width*sunRect.height);
}
function updateBackGround(){
let overlap=areaOverlap();
if(overlap>0){
moon.classList.add("overlapping");
}else{
moon.classList.remove("overlapping");
}
//rgb(65, 163, 243);
let r1=65,g1=163,b1=243;
//rgb(112, 0, 67))
let r2=112,g2=0,b2=67;
let newR=Math.round(r1+(r2 - r1)*overlap);
let newG=Math.round(g1+(g2 - g1)*overlap);
let newB=Math.round(b1+(b2 - b1)*overlap);
bod.style.backgroundColor = `rgb(${newR}, ${newG}, ${newB})`;
}
function withinBound(x,y){
let screenWidth=window.innerWidth;
let screenHeight=window.innerHeight;
let moonSize=moon.offsetWidth;
return {
x:Math.max(0,Math.min(x,screenWidth-moonSize)),
y:Math.max(0,Math.min(y,screenHeight-moonSize))
}
}