|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Mouse Shadow</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | + <div class="hero"> |
| 10 | + <h1 contenteditable>🔥WOAH!</h1> |
| 11 | + </div> |
| 12 | + |
| 13 | + <style> |
| 14 | + html { |
| 15 | + color:black; |
| 16 | + font-family: sans-serif; |
| 17 | + } |
| 18 | + |
| 19 | + .hero { |
| 20 | + min-height: 100vh; |
| 21 | + display:flex; |
| 22 | + justify-content: center; |
| 23 | + align-items: center; |
| 24 | + color:black; |
| 25 | + } |
| 26 | + |
| 27 | + h1 { |
| 28 | + text-shadow: 10px 10px 0 rgba(0,0,0,1); |
| 29 | + font-size: 100px; |
| 30 | + } |
| 31 | + </style> |
| 32 | + |
| 33 | +<script> |
| 34 | +const hero = document.querySelector('.hero'); |
| 35 | +const text = hero.querySelector('h1'); |
| 36 | +const walk = 100; //100px |
| 37 | + |
| 38 | +function shadow(e){ |
| 39 | + // console.log(e); |
| 40 | + const width = hero.offsetWidth; |
| 41 | + const height = hero.offsetHeight; |
| 42 | + let x = e.offsetX; |
| 43 | + let y = e.offsetY; |
| 44 | + // console.log(y); |
| 45 | + |
| 46 | + // console.log(this); |
| 47 | + // console.log(e.target); |
| 48 | + |
| 49 | + if(this !== e.target) { |
| 50 | + x = x + e.target.offsetLeft; |
| 51 | + // console.log(x); |
| 52 | + y = y + e.target.offsetTop; |
| 53 | + // console.log(y); |
| 54 | + } |
| 55 | + // console.log(x); |
| 56 | + // console.log(y); |
| 57 | + const xWalk = Math.round((x / height * walk) - (walk / 2)); |
| 58 | + const yWalk = Math.round((y / height * walk) - (walk / 2)); |
| 59 | + |
| 60 | + text.style.textShadow = ` |
| 61 | + ${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7), |
| 62 | + ${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7), |
| 63 | + ${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7), |
| 64 | + ${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7) |
| 65 | + `; |
| 66 | + |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +hero.addEventListener('mousemove', shadow); |
| 71 | + |
| 72 | +</script> |
| 73 | +</body> |
| 74 | +</html> |
0 commit comments