https://trekhleb.dev/blog/2021/gyro-web/ In Javascript you may access your device orientation data by listening to the deviceorientation event. It is as easy as the following:
The demo video was compressed by https://clideo.com/ from 4.3MB to 93KB, that’s impressive.
To achieve the effect above, we need to disassemble it into 2 steps:
Show the hidden content of the specified area
Make the “lens” element move with device gyro sensor data
Step 1. Show the hidden content of the specified area
How did this magic happen?
If you were familiar with Photoshop you would know there’s a magic trick property mask, we have that in CSS too. Mask property reference document-MDN
Check caniuse for compatibility and the result is in below. We can see that we have about 97% browser support. For mobile Android device, we need to add the -webkit prefix before the mask attribute to make it effective.
<divclass="example">a quick brown fox jumps over the lazy dog</div>
a quick brown fox jumps over the lazy dog
Then, we add a rounded mask on it
Before adding the mask image, we should know how did the mask work with our background layer. Among the multiple mask properties of CSS, the mask-image property sets the mask layer image, and its property value is very similar to background-image, which can be <url> or <gradient>.
When we set mask-image to <url> or <gradient>, the value of mask-mode attribute is: alpha. This means that the background element and the mask layer element overlap, and the background layer will show through from the non-transparent part of the mask layer.
Through practice, we can see that no matter what color the non-transparent areas of the mask is, it will not make any different mask effect.
On iOS devices, when obtaining gyroscope data authorization for the first time, there will be a system pop-up window asking whether to allow access to gyroscope data, while Android devices are silently authorized without pop-up windows.
Step2. Make The Lens Element Move With Gryo Sensor Data
By using the mask-position property, we can move the position of the mask layer.
It should be noted that when running on a real device, when the iOS device renders the transition of mask-position, it will feel a half-beat slower than the transition of transform of the lens element, because mask-position ` Rendering requires a relatively large overhead. *Tested models are: iPhone 14 Pro (iOS 16.4.0) and Samsung S21+ (Android 13)
“mask-position” | Can I use… Support tables for HTML5, CSS3, etc In this case, to make the Magnifying lens element follow our gyroscope data, we need two layers: one to show the magnifying glass and make it move, and one to hold the hidden information beneath the magnifying glass.
The magnifying lens element follows the movement and uses transform: translate() to update the position. When the magnifying lens moves, the mask-position of the hidden information layer below is updated at the same time, so that the mask position of the layer is also updated together, and you can get Hidden elements are always displayed under the magnifying lens element.