How to Recreate the Iconic Pegman from Google Maps
This is a little snippet I’ve wanted to try for quite some time. I finally got around to recreating this iconic feature from Google Maps: the ability to drag and drop this little guy, known as Pegman, to switch to Street View.
TL;DR
Click and drag the little icon in the bottom-right corner over the map:
HTML
The structure is straightforward. I have:
- A #map to serve as the container (with a background-image applied).
- A #pegman-container positioned in the bottom-right corner.
- The #pegman element itself for the draggable character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
The rotate value changes based on user interaction, which we’ll dive into next.
Javascript
Everything here revolves around user interaction. JavaScript handles:
- Listening for mouse events.
- Updating Pegman’s position and rotation dynamically.
- Adding smooth animations for better UX.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Let’s break down each function:
onMouseDown
This starts the drag by recording the initial mouse position and toggling the isDragging flag.
1 2 3 4 5 |
|
onMouseMove
This handles Pegman’s movement and rotation during the drag. It ensures:
- Rotation stays within a range (to avoid extreme angles like Superman flying).
- The rotate value updates dynamically using --r.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
onMouseUp
This resets Pegman’s state once the user stops dragging:
- Rotation resets to 0.
- Pegman smoothly returns to its original position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Final Thoughts
This snippet highlights how simple animations and interactivity can recreate an iconic user experience. Key takeaways:
- Using CSS custom properties (like --r) keeps styling dynamic and manageable.
- Rotation limits ensure a polished, natural feel for user interactions.
- Timeouts and easing animations add realism to the motion.
Feel free to fork the CodePen, make changes and let me know what you think! ??️
By the way...
I'm using the original sprites from google, which you can see here and make experiments:
- runway state
- dropping state
- dangling state
Did you know? ✨ The Google Maps location I used is the real Santa Claus Village!
Fun fact: Santa’s Finnish name is Joulupukki, and he originally from Finland ???
The above is the detailed content of How to Recreate the Iconic Pegman from Google Maps. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

In this week's roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used
