Skip to content

Commit 0210fa6

Browse files
committed
Ex16 - Mouse move shadow
1 parent 48e25e7 commit 0210fa6

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

16 - Mouse Move Shadow/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
28+
h1 {
29+
text-shadow: 10px 10px 0 rgba(0,0,0,1);
30+
font-size: 100px;
31+
}
32+
</style>
33+
34+
<script src="script.js" charset="utf-8"></script>
35+
36+
</body>
37+
</html>

16 - Mouse Move Shadow/script.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const hero = document.querySelector('.hero');
4+
const text = hero.querySelector('h1');
5+
const walk = 100; // distance max => max coordinates are going to be -50 et +50 around the element
6+
7+
const {offsetHeight: height, offsetWidth: width} = hero;
8+
9+
function shadow(event) {
10+
let {offsetX: x, offsetY: y} = event;
11+
//this = hero, but event.target = hero or h1 (children of hero)
12+
if (this !== event.target) {
13+
x += event.target.offsetLeft;
14+
y += event.target.offsetTop;
15+
}
16+
17+
const xwalk = Math.round(x / width * walk - walk / 2);
18+
const ywalk = Math.round(y / height * walk - walk / 2);
19+
console.log(xwalk, ywalk);
20+
text.style.textShadow = `
21+
${xwalk}px ${ywalk}px 0 rgb(87, 140, 32),
22+
${xwalk * -1}px ${ywalk * - 1}px 0 rgb(164, 111, 5)
23+
`;
24+
}
25+
26+
27+
hero.addEventListener('mousemove', shadow);

0 commit comments

Comments
 (0)