|
| 1 | +# Exercise 15: Mouse Move Shadow |
| 2 | +Nitish Dayal, Software & Applications Developer - [Contact](http://nitishdayal.me) |
| 3 | +Last Commit Date: Dec 23, 2016 |
| 4 | + |
| 5 | +We're given an HTML document with a `div` element containing an `h1` element. |
| 6 | + The `h1` element has a `textShadow` _style property_ which we want to manipulate |
| 7 | + depending on the user's current mouse location. Write the necessary JavaScript |
| 8 | + code to bring functionality to this page. |
| 9 | + |
| 10 | +## Guide |
| 11 | + |
| 12 | +In the `<script>` tag, we're provided with three `const` (constant) variables: one |
| 13 | + which references the `div` element with class `hero`, one which references the `h1` element |
| 14 | + that is a _child_ of the `hero div` element, and one which is defined as a _number literal_ |
| 15 | + value. We'll use the latter when we're calculating the `textShadow` distance. |
| 16 | + |
| 17 | +Begin by attaching an _event listener_ to the `hero` div element that will listen for |
| 18 | + 'mousemove' events and call upon an _event handler_ function (for brevity's sake, let's |
| 19 | + name that function 'shadow'). Declare a `const` variable 'shadow' and define it as |
| 20 | + an _arrow function_ that accepts an event as a parameter; this is our _event handler_ function. |
| 21 | + In the function body, set two `const` variables as the `offsetWidth` and `offsetHeight` |
| 22 | + properties of the `hero` div element. Then declare two `let` variables and define them |
| 23 | + as the _event's `offsetX` and `offsetY` properties_. If the event's current target |
| 24 | + is **different** from the event's originating target (the `hero` div element), increment |
| 25 | + the x & y coordinates by the distance between the current target element and the event's |
| 26 | + **originating target** using the `offsetLeft` and `offsetTop` properties of the original |
| 27 | + target. |
| 28 | + |
| 29 | +Declare two `const` variables and define them as the stretch distance for the `textShadow` property |
| 30 | + on the x & y axis, respectively. Finally, update the `textShadow` style property of the `h1` |
| 31 | + element, using the two `const` variables we just defined as the _horizontal and vertical_ |
| 32 | + positions of the shadow. |
| 33 | + |
| 34 | +**Steps:** |
| 35 | + |
| 36 | +1. Attach an _event listener_ to the `hero` div element that will call upon a provided _event handler_ |
| 37 | + on the 'mousemove' event. |
| 38 | + |
| 39 | + ```JavaScript |
| 40 | + hero.addEventListener('mousemove', shadow) |
| 41 | + ``` |
| 42 | + |
| 43 | +2. Declare a `const` with a name that matches the event listener's _event handler_ function, |
| 44 | + and define it as an _arrow function_ which accepts an event as a parameter. |
| 45 | + |
| 46 | + ```JavaScript |
| 47 | + const shadow = (e) = > { /* ... */ } |
| 48 | + ``` |
| 49 | + |
| 50 | + - Declare two `const` variables and define them as the `offsetWidth` |
| 51 | + and `offSetHeight` properties of the `hero` div HTML element. |
| 52 | + |
| 53 | + ```JavaScript |
| 54 | + // Get the width & height properties of the 'hero' div in relation |
| 55 | + // to the window object |
| 56 | + const { offsetWidth: width, offsetHeight: height } = hero; |
| 57 | + ``` |
| 58 | + |
| 59 | + - Declare two `let` variables and define them as the distance of the |
| 60 | + mouse from the event's _target element_ on the x & y axis. |
| 61 | + |
| 62 | + ```JavaScript |
| 63 | + // Using ES6 object deconstruction |
| 64 | + let { offsetX: x, offsetY: y } = e; |
| 65 | + |
| 66 | + // Without ES6 object deconstruction |
| 67 | + let x = e.offsetX, y = e.offsetY; |
| 68 | + ``` |
| 69 | + |
| 70 | + - If the element that is the _current target_ of the event differs |
| 71 | + from the event's _originating target_, increment the values of the two previously declared |
| 72 | + variables by the distance between the originating target and the current target on the |
| 73 | + x & y axis. |
| 74 | + |
| 75 | + ```JavaScript |
| 76 | + if (e.currentTarget !== e.target) { |
| 77 | + x += e.target.offsetLeft |
| 78 | + y += e.target.offsetTop |
| 79 | + } |
| 80 | + |
| 81 | + ``` |
| 82 | + |
| 83 | + - Calculate the stretch distance for the element's shadow on the x & y axis |
| 84 | + |
| 85 | + ```JavaScript |
| 86 | + const xWalk = Math.round((x / width * walk) - (walk / 2)), |
| 87 | + yWalk = Math.round((y / height * walk) - (walk / 2)); |
| 88 | + ``` |
| 89 | + |
| 90 | + - Update the `textShadow` _style property_ of the `h1` element. |
| 91 | + |
| 92 | + ```JavaScript |
| 93 | + text.style.textShadow = ` |
| 94 | + ${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7), |
| 95 | + ${-xWalk}px ${yWalk}px 0 rgba(0,255,255,0.7), |
| 96 | + ${yWalk}px ${-xWalk}px 0 rgba(0,255,0,0.7), |
| 97 | + ${-yWalk}px ${-xWalk}px 0 rgba(0,0,255,0.7) |
| 98 | + `; |
| 99 | + ``` |
| 100 | + |
| 101 | +3. Last step is best step! Wrap up **all** of the JavaScript code inside an IIFE |
| 102 | + (_immediately invoked function expression_). |
| 103 | + |
| 104 | +# Nothing like the sweet smell of success in the morning! 😎 |
0 commit comments