-
Notifications
You must be signed in to change notification settings - Fork 0
/
mousey.js
65 lines (55 loc) · 1.94 KB
/
mousey.js
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
document.addEventListener('mousemove', function(e) {
mousePoint.x = e.clientX;
mousePoint.y = resolution.y - e.clientY;
var angle = findAngle(resolutionMidpoint, mousePoint);
var xOnCircle = getXOnCircle(angle, radius);
var yOnCircle = getYOnCircle(angle, radius);
moveInnerCircle(xOnCircle, yOnCircle);
});
document.addEventListener('mousedown', function() {
document.getElementById('background').classList.add('clicked');
});
document.addEventListener('mouseup', function() {
document.getElementById('background').classList.remove('clicked');
});
window.addEventListener('resize', function() {
getResolutionAndMidpoint();
});
var resolution = {};
var radius;
var resolutionMidpoint = {};
var mousePoint = {};
getResolutionAndMidpoint();
function getResolutionAndMidpoint() {
resolution.x = window.innerWidth;
resolution.y = window.innerHeight;
resolutionMidpoint.x = resolution.x / 2;
resolutionMidpoint.y = resolution.y / 2;
}
function moveInnerCircle(xOnCircle, yOnCircle) {
var innerCircle = document.getElementById('inner-circle');
var circle = document.getElementById('circle');
var diameterString = window.getComputedStyle(circle).width;
radius = diameterString.slice(0, diameterString.length - 2) / 2;
innerCircle.style.setProperty('top', (radius - yOnCircle) + 'px');
innerCircle.style.setProperty('right', (radius - xOnCircle) + 'px');
}
function findAngle(center, mouse) {
var x = mouse.x - center.x;
var y = mouse.y - center.y;
return Math.atan(y / x);
}
function getXOnCircle(angle, radius) {
if(mousePoint.x > resolutionMidpoint.x) {
return Math.abs(Math.cos(angle) * radius);
} else {
return -Math.abs(Math.cos(angle) * radius);
}
}
function getYOnCircle(angle, radius) {
if(mousePoint.y > resolutionMidpoint.y) {
return Math.abs(Math.sin(angle) * radius);
} else {
return -Math.abs(Math.sin(angle) * radius);
}
}