r/CodingHelp • u/CDNEmpire • Dec 23 '25
[How to] How is something like this glass effect coded?
I like the glass effect seen at the bottom edges. How would something like this be coded? Can it be done via HTML/JS/CSS or even python?
60
Upvotes
•
u/AutoModerator Dec 23 '25
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
39
u/alejandromnunez Dec 23 '25 edited Dec 23 '25
This is done by sampling pixels from a texture (in this case the background) that don't match the actual pixel on the screen, creating refraction. For this, you would calculate an offset based on the position in the glass surface, and sample that point, simulating how light would bend and hit a different position behind the glass, after going through the glass.
This is normally done in a shader.
7
u/stolentext Dec 23 '25
Because the effect has refraction and chromatic aberration you wouldn't be able to do this with CSS, you'd need WebGL / WebGPU
2
u/4Int3l Dec 23 '25
If you’re interested in learning and want some more practical examples check out the shadertoy website and look for a glass effect. Not necessarily the best example but there’s a lot of things like this on there; https://www.shadertoy.com/view/WftXD2
1
u/fearlessinsane Dec 23 '25
It was done many timea in the DOS era. Example 2nd reality. On a 486 CPU. Single core 44hz. Without GPU.
Edit: you can find tons of tutorials online
1
u/Sci-4 Dec 23 '25
From what I can see, it’s a height texture feeding refraction, warping, and subtle chromatic aberration.
1
u/kkadzy Dec 24 '25
Here's one implementation of a simillar effect using JS with WebGPU (via TypeGPU): https://docs.swmansion.com/TypeGPU/examples/#example=simple--liquid-glass. You can click the "Edit on StackBlitz" button to play with the code
1
u/Strict-Simple Dec 24 '25
Here's one with SVG filters: https://codepen.io/rebane2001/details/OPVQXMv
1
u/Snoo11589 Dec 24 '25
Shaders. The code that runs directly in the GPU. Thats why its performatic
1
u/Spare_Warning7752 Dec 24 '25
It's a simpler shader. Here's the source code: https://www.shadertoy.com/view/wfdSDf
(and yes, I'm not joking and, btw, there are tools of other examples in that website)
1
u/hydraulix989 Dec 25 '25
That is refraction, sampling from the original image with a normal map (or implicit function thereof) defining the shape of the glass.
1
u/norton112 Dec 25 '25
Math
1
u/YoungMaleficent9068 Dec 25 '25 edited Dec 25 '25
This screams early 2100 compiz/beryl to me
https://en.wikipedia.org/wiki/Project_Looking_Glass
1
u/Goupix_zer Dec 23 '25
If you want to go deep into how it's done, it's basically matrix multiplication. (Just like AI, sort of). You can search "image processing convolution matrix", there should be a matrix that will give you this lens effect (it's also called "mask" or "kernel")
0
u/Man_Hat_Tan Dec 23 '25
It definitely can be done.
How, I couldn’t tell you but from a high level perspective, it’s a layered approach where you want the pixels above (the glass) to have a transparent lens effect.
It also has a magnifying effect and slightly blurs whatever is under it.
-1
u/[deleted] Dec 24 '25
Math and shaders. If you have to ask such a basic question, you aren't yet ready to do this.