Creepy DIY: Eyes that Track You
Table of Contents
- Introduction
- Childhood Memories: Haunted House and Following Eyes
- Reverse Engineering the Proximity Hover Effect
- Understanding the Code: Inspecting the Gopher
- JavaScript and Trigonometry: Calculating the Rotation Angle
- Implementing the Effect: Adding HTML and CSS
- Setting up the Event Listener: Mouse Move
- Getting the Mouse Position and Anchor Element
- Calculating the Angle: Using Trigonometry
- Rotating the Eyes: Applying the Angle with JavaScript
- Making it More Psychedelic: Adding CSS Filters
- Optimizing the Code: Removing Unnecessary Calculations
- Conclusion
Following Eyes: Reverse Engineering a Proximity Hover Effect
Have You ever come across a Website where the eyes of a character seemed to follow your every move? It's a subtle yet impressive effect that adds a touch of magic to the webpage. If you've ever wondered how this proximity hover effect is achieved, you're in luck. In this article, I'll guide you through the process of reverse engineering this effect using JavaScript and basic trigonometry.
Introduction
Imagine visiting a haunted house as a child and being captivated by a painting with eyes that seemed to track your every move. It left a lasting impression that still gives you chills today. Now imagine being able to recreate that eerie sensation on your own webpage. That's exactly what we're going to do.
Childhood Memories: Haunted House and Following Eyes
Before we dive into the technical details, let's take a moment to indulge in nostalgia. Childhood memories often Shape our fascination with certain phenomena. For me, it was the haunted house with the painting that haunted my dreams. The eyes seemed to follow me wherever I went, and that experience sparked my Curiosity about the inner workings of such effects.
Reverse Engineering the Proximity Hover Effect
When reverse engineering a UI element on the web, the browser's Dev tools are our best friends. By inspecting the elements and analyzing the code, we can uncover how the effect is achieved. In this case, we'll be examining a webpage with a gopher whose eyes follow the mouse Cursor.
Understanding the Code: Inspecting the Gopher
Upon inspecting the gopher image, we can see that it consists of three separate images: the gopher itself and two smaller images to fill the eye holes. The rotation of each eye is controlled by a transform: rotate
style that changes dynamically Based on the mouse's position.
JavaScript and Trigonometry: Calculating the Rotation Angle
To understand how the rotation angle is calculated based on the mouse position, we need to brush up on our trigonometry knowledge. The atan2
function, part of the JavaScript Math library, allows us to determine the angle on a Euclidean plane given the coordinates of the origin and a point.
Implementing the Effect: Adding HTML and CSS
Before we can start coding, let's set up our HTML structure. We'll Create a basic HTML page with a main element that contains two images: the base image and a box to cover their eyes. We want the eyes to be stacked on top of the base image, so we'll use CSS positioning to achieve that effect.
Setting up the Event Listener: Mouse Move
To make the eyes follow the mouse cursor, we need to listen for the mousemove
event. This event fires whenever the mouse moves, so we need to handle it efficiently to avoid any performance issues. By registering the event listener globally on the window
object, we can ensure that the effect works anywhere on the page.
Getting the Mouse Position and Anchor Element
Within the mousemove
event listener, we can access the clientX
and clientY
values, which represent the position of the cursor relative to the viewport. We'll store these values in variables for later use. Next, we need to calculate the position of the anchor element, which represents the middle of the circle that contains the eyes.
Calculating the Angle: Using Trigonometry
With the mouse position and anchor element coordinates, we can now calculate the rotation angle. By subtracting the X and Y values from each other and using the Math.atan2
function, we can determine the angle between the two points in radians. We'll convert the angle to degrees, as CSS requires it in that format.
Rotating the Eyes: Applying the Angle with JavaScript
Now that we have the rotation angle, it's time to Apply it to the eyes. We'll loop through each eye element and set its style.transform.rotate
property to the calculated angle. This will create the effect of the eyes following the mouse cursor as it moves around the webpage.
Making it More Psychedelic: Adding CSS Filters
To add an extra touch of psychedelia to our effect, we can use the CSS filter
property. By applying a hue-rotate
filter to the anchor image, we can create a mesmerizing color transformation as the mouse moves. This enhances the overall visual experience and adds an element of surprise.
Optimizing the Code: Removing Unnecessary Calculations
As with any code, optimization is key to ensure smooth performance. In our case, we can improve the code by removing any unnecessary calculations from the event handler function. By caching the DOM elements and calculating the bounding rectangle outside of the event listener, we can minimize the computational load for each mousemove
event.
Conclusion
Congratulations! You've successfully reverse engineered the proximity hover effect by combining JavaScript and trigonometry. By understanding the code and implementing it on your own webpage, you can add a touch of magic and interactivity that captivates your audience. Remember to optimize the code and experiment with additional enhancements to make the effect even more stunning.
Highlights
- Recreate the childhood memories of haunted houses with eyes that follow your every move
- Reverse engineer the proximity hover effect using JavaScript and trigonometry
- Explore the code behind the effect by inspecting the elements and analyzing the transformations
- Set up the HTML structure and CSS positioning for the eyes and base image
- Implement a
mousemove
event listener to track the mouse cursor
- Calculate the rotation angle based on the mouse position and anchor element coordinates
- Apply the rotation angle to the eyes with JavaScript, creating the effect of following eyes
- Enhance the effect by adding CSS filters for a psychedelic visual experience
- Optimize the code by removing unnecessary calculations and caching DOM elements
- Bring a touch of magic and interactivity to your webpages with the proximity hover effect
FAQ
Q: Can this proximity hover effect be applied to other elements on a webpage?
A: Absolutely! The same principles can be applied to any element that you want to follow the mouse cursor. Simply modify the HTML structure and adjust the JavaScript code accordingly.
Q: Can I customize the speed or range of the eye movement?
A: Yes, you can adjust the range and speed of the eye movement by modifying the calculated angle. By scaling the angle values, you can control how much the eyes rotate in response to the mouse movement.
Q: Are there any performance considerations when using this effect?
A: While the effect itself is lightweight, it's important to optimize the code and avoid unnecessary calculations within the mousemove
event handler. Caching DOM elements and reducing the frequency of expensive computations can help maintain smooth performance, especially on pages with a high event frequency.
Q: Can I combine multiple proximity hover effects on one webpage?
A: Absolutely! You can add multiple instances of the proximity hover effect to different elements on the same webpage. Each instance will have its own event listener and independent calculations, allowing for unique and interactive experiences.
Q: Can I use this effect on mobile devices with touch screens?
A: The proximity hover effect relies on mouse movement, so it may not have the same impact on touch screens. However, you can adapt the effect to respond to touch events and create a similar interactive experience for mobile users.