JSFiddle - React, Tailwind, and code Playground
by joshmoto
HTML
<div id="zone">
<div id="arrow"></div>
</div>
SCSS
#zone {
background: black;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
cursor: crosshair;
}
#arrow {
width: 2px;
height: 50%;
background: magenta;
position: absolute;
top: 50%;
left: 50%;
transform-origin: center;
transition: all .05s ease;
transform: translate(-1px,-50%);
z-index: 1;
&::before {
content: '';
display: block;
position: absolute;
width: 50px;
height: 50px;
top: -25px;
left: 50%;
background: magenta;
transform: translate(-50%,0);
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
&::after {
content: '';
display: block;
position: absolute;
background: white;
z-index: -100;
width: 2px;
height: 500%;
top: 0;
left: 50%;
transform: translate(-50%,-100%);
opacity: 0.25;
}
}
JavaScript
// on ready
$(function() {
// constants
const $zone = $('#zone');
const $arrow = $('#arrow');
// calculate angle function
function calcAngleDegrees(x, y) {
return Math.atan2(y, x) * 180 / Math.PI;
}
// current mouse position
let currentMousePos = {
x: -1,
y: -1
};
// zone dimensions
let currentWin = {
x: $zone.width(),
y: $zone.height()
};
// zone dimensions split
let currentWinSplit = {
x: currentWin.x / 2,
y: currentWin.y / 2
};
// on zone resize
$zone.on('resize', function(e) {
// update zone dimensions
currentWin = {
x: $zone.width(),
y: $zone.height()
};
// update zone split dimensions
currentWinSplit = {
x: currentWin.x / 2,
y: currentWin.y / 2
};
// on mouse move
}).on('mousemove', function(e) {
// let our quater zone mouse position
let quarterWin = {
x: -1,
y: -1
}
// get our current mouse position in full zone
currentMousePos.x = e.pageX - $zone.scrollLeft();
currentMousePos.y = e.pageY - $zone.scrollTop();
// if mouse is on right side of zone
if (currentMousePos.x > currentWinSplit.x) {
// set right side x quater zone mouse position from center point
quarterWin.x = currentMousePos.x - currentWinSplit.x;
// else if mouse is on left side of zone
} else {
// set left side x quater zone mouse position from center point
quarterWin.x = currentWinSplit.x - currentMousePos.x;
}
// if mouse is on bottom side of zone
if (currentMousePos.y > currentWinSplit.y) {
// set bottom side y quater zone mouse position from center point
quarterWin.y = currentMousePos.y - currentWinSplit.y;
} else {
// set bottom side y quater zone mouse position from center point
quarterWin.y = currentWinSplit.y - currentMousePos.y;
}
// let our tangent and radial angle variables
let tangent_angle =...