Hacker News new | past | comments | ask | show | jobs | submit login
Writing Shaders in Pure CSS (github.com/chenglou)
6 points by dvrp on Nov 12, 2023 | hide | past | favorite | 3 comments



Oh hey =D

The ring's source code in particular had to be transcribed from an actual shader I made (https://www.shadertoy.com/view/msd3R2). I didn't have the patience to write it directly in CSS. It's neither the right semantic nor the medium for it (which does make you wonder what CSS' medium was supposed to be).

Out of curiosity, I've transcribed that ring, which runs at <1 fps with pure CSS, into a JS version with requestionAnimationFrame + setting the background color (https://github.com/chenglou/pure-css-shaders-art/blob/master...).

Perhaps unsurprisingly, it runs at full 120fps on a decent laptop (it's just 21*21 JS style setting really). I've tried to make a half-JS, half-CSS version, and the speed is somewhere in-between. Basically, CSS variables are _extremely_ slow across all browsers, and I don't believe I've hit some particular edge-case. They're not used idiomatically here, but still, we shouldn't hit this drastic of slowdowns when updating 441 styles. They're also pretty hard to read the moment the calculation isn't some trivial basic arithmetics; and even then...

The true shader version runs at basically infinite fps =)


Yo!

Since you did some compiler stuff, what's the reason—no pun intended—behind such a drastic performance gap?

Also, your first link is not working because of the HN link parsing logic I guess.


Here are some demos as shown in the repository:

- Heart: https://chenglou.me/pure-css-shaders-art/heart.svg

- Ring (kinda laggy): https://chenglou.me/pure-css-shaders-art/ring.html

- Squircles (as per Apple patent D670,286): https://chenglou.me/pure-css-shaders-art/squircle.html




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: