We want Pacman to follow the mouse. To do this, we need an event that triggers whenever the mouse moves. Here, we can use the mousemove event.
document.addEventListener('mousemove', event => { // ...})We can find the mouseโs position with clientX and clientY.
document.addEventListener('mousemove', event => { const mouseX = event.clientX const mouseY = event.clientY console.log(mouseX, mouseY)})
Itโs quite difficult to follow the mouse position via the console. Letโs write these positions into the DOM. Youโll find a <div> with a debug class in the HTML. Weโll display the mouse movement on this .debug element.
<div class="debug"></div>const debug = document.querySelector('.debug')
document.addEventListener('mousemove', event => { // ... debug.innerHTML = ` <div> mouseX: ${mouseX} </div> <div> mouseY: ${mouseY} </div> `})
Calculating the Angle
We can calculate the angle we need with Trigonometry (via Tangent).
We need to know the opposite and adjacent values to calculate the angle.
opposite: Difference betweenmouseYand Pacmanโs vertical centeradjacent: Difference betweenmouseXand Pacmanโs horizontal center
We can calculate Pacmanโs vertical and horizontal center with values from getBoundingClientRect.
const debug = document.querySelector('.debug')
document.addEventListener('mousemove', event => { // ... const pacmanBox = pacman.getBoundingClientRect() const pacmanXCenter = (pacmanBox.left + pacmanBox.right) / 2 const pacmanYCenter = (pacmanBox.top + pacmanBox.bottom) / 2
debug.innerHTML = ` <div> mouseX: ${mouseX} </div> <div> mouseY: ${mouseY} </div> <div> pacmanXCenter: ${pacmanXCenter} </div> <div> pacmanYCenter: ${pacmanYCenter} </div> `})
opposite is the difference between the mouseY and pacmanYCenter. adjacent is the difference between mouseX and pacmanXCenter.
document.addEventListener('mousemove', event => { // ... const deltaX = mouseX - pacmanXCenter const deltaY = mouseY - pacmanYCenter
debug.innerHTML = ` <div> deltaX: ${deltaX} </div> <div> deltaY: ${deltaY} </div> `})To calculate the angle, we can either use Math.atan or Math.atan2. They both give the angle (in radians), but their output is slightly different:
document.addEventListener('mousemove', event => { // ... const atan1 = Math.atan(deltaY / deltaX) const atan2 = Math.atan2(deltaY, deltaX)
debug.innerHTML = ` <div> deltaX: ${deltaX} </div> <div> deltaY: ${deltaY} </div> <div> atan1: ${atan1} </div> <div> atan2: ${atan2} </div> `})
I donโt know about you, but Iโm much more comfortable with degrees (compared to radians). We can convert radians back to degrees with this formula: radian * 180 / Math.PIย .
(In case you were wondering, I did not remember the formula by heart. I found it on MDN).
document.addEventListener('mousemove', event => { // ... const atan1 = (Math.atan(deltaY / deltaX) * 180) / Math.PI const atan2 = (Math.atan2(deltaY, deltaX) * 180) / Math.PI
debug.innerHTML = ` <div> deltaX: ${deltaX} </div> <div> deltaY: ${deltaY} </div> <div> atan1: ${atan1} </div> <div> atan2: ${atan2} </div> `})
Pay attention to atan1 and atan2 values when you move your mouse around the Pacman. Youโll notice this:
atan1gives you values between0degandยฑ90degatan2gives you values between0degandยฑ180deg
We can use either value to rotate the Pacman.
Rotating the Pacman
Letโs use the angle from atan2. We can rotate the Pacman by setting a rotate transform.
document.addEventListener('mousemove', event => { // ... const angle = (Math.atan2(deltaY, deltaX) * 180) / Math.PI pacman.style.transform = `rotate(${angle}deg)`})
We positioned five directions (North, North-east, East, South-east, and South) correctly with this code. Whatโs next is to flip Pacman so it has the correct orientation in the other three directions.
Pay attention to the angle values. Youโll notice we have 135deg when the Pacman faces South-west.
This means we need to use the fourth formula in our transform chart.
document.addEventListener('mousemove', event => { // ... const angle = (Math.atan2(deltaY, deltaX) * 180) / Math.PI let transform = `rotate(${angle}deg)` if (Math.abs(angle) > 90) transform += 'scaleY(-1)' pacman.style.transform = transform})
More Pacman!
Once we get a single Pacman to rotate properly, adding more Pacman is a simple feat. Go ahead and add more Pacman SVGs into the HTML. Use 8 for the best results.
<main> <svg class="pacman"><use href="images/pacman.svg#pacman"></use></svg> <svg class="pacman"><use href="images/pacman.svg#pacman"></use></svg> <svg class="pacman"><use href="images/pacman.svg#pacman"></use></svg> <!-- ...5 more pacman SVG... --></main>In JavaScript, make sure you do two things:
- Use
querySelectorAllto select all the SVGs. - Make each Pacman follow the mouse with
forEach.
const pacmen = [...document.querySelectorAll('.pacman')]
document.addEventListener('mousemove', event => { const mouseX = event.clientX const mouseY = event.clientY
pacmen.forEach(pacman => { // ... })})
Finally, letโs spice up the page by adding a nice-looking header font! Weโll also remove .debug since we donโt need it anymore.
<body> <header> <h1> <div data-spinning>Spinning</div> <div data-pacman>Pacman</div> </h1> </header>
<main><!-- ... --></main> <script src="js/main.js"></script></body>I played with the Spinning Pacman quite a bit after I made it. Hereโs a video if you want to see what I made! (Turn the sound on for the music).
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson