TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Writing Shaders in Pure CSS

6 pointsby dvrpover 1 year ago

2 comments

chenglouover 1 year ago
Oh hey =D<p>The ring&#x27;s source code in particular had to be transcribed from an actual shader I made (<a href="https:&#x2F;&#x2F;www.shadertoy.com&#x2F;view&#x2F;msd3R2" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.shadertoy.com&#x2F;view&#x2F;msd3R2</a>). I didn&#x27;t have the patience to write it directly in CSS. It&#x27;s neither the right semantic nor the medium for it (which does make you wonder what CSS&#x27; medium was supposed to be).<p>Out of curiosity, I&#x27;ve transcribed that ring, which runs at &lt;1 fps with pure CSS, into a JS version with requestionAnimationFrame + setting the background color (<a href="https:&#x2F;&#x2F;github.com&#x2F;chenglou&#x2F;pure-css-shaders-art&#x2F;blob&#x2F;master&#x2F;ringjs.html">https:&#x2F;&#x2F;github.com&#x2F;chenglou&#x2F;pure-css-shaders-art&#x2F;blob&#x2F;master...</a>).<p>Perhaps unsurprisingly, it runs at full 120fps on a decent laptop (it&#x27;s just 21*21 JS style setting really). I&#x27;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&#x27;t believe I&#x27;ve hit some particular edge-case. They&#x27;re not used idiomatically here, but still, we shouldn&#x27;t hit this drastic of slowdowns when updating 441 styles. They&#x27;re also pretty hard to read the moment the calculation isn&#x27;t some trivial basic arithmetics; and even then...<p>The true shader version runs at basically infinite fps =)
评论 #38237307 未加载
dvrpover 1 year ago
Here are some demos as shown in the repository:<p>- Heart: <a href="https:&#x2F;&#x2F;chenglou.me&#x2F;pure-css-shaders-art&#x2F;heart.svg" rel="nofollow noreferrer">https:&#x2F;&#x2F;chenglou.me&#x2F;pure-css-shaders-art&#x2F;heart.svg</a><p>- Ring (kinda laggy): <a href="https:&#x2F;&#x2F;chenglou.me&#x2F;pure-css-shaders-art&#x2F;ring.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;chenglou.me&#x2F;pure-css-shaders-art&#x2F;ring.html</a><p>- Squircles (as per Apple patent D670,286): <a href="https:&#x2F;&#x2F;chenglou.me&#x2F;pure-css-shaders-art&#x2F;squircle.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;chenglou.me&#x2F;pure-css-shaders-art&#x2F;squircle.html</a>