<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tarun Raja</title>
    <description>The latest articles on DEV Community by Tarun Raja (@tarun_raja_5c9da995d22137).</description>
    <link>https://dev.to/tarun_raja_5c9da995d22137</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4103395%2Fa9dfcc9f-9126-41f5-9b19-52cf64e15671.png</url>
      <title>DEV Community: Tarun Raja</title>
      <link>https://dev.to/tarun_raja_5c9da995d22137</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tarun_raja_5c9da995d22137"/>
    <language>en</language>
    <item>
      <title>Simulating Capillary Action in a WebGL Shader for a UI Press Effect</title>
      <dc:creator>Tarun Raja</dc:creator>
      <pubDate>Mon, 31 Aug 2026 22:41:23 +0000</pubDate>
      <link>https://dev.to/tarun_raja_5c9da995d22137/simulating-capillary-action-in-a-webgl-shader-for-a-ui-press-effect-48cm</link>
      <guid>https://dev.to/tarun_raja_5c9da995d22137/simulating-capillary-action-in-a-webgl-shader-for-a-ui-press-effect-48cm</guid>
      <description>&lt;p&gt;Most "ink spreading" effects on the web are the same three ingredients: a&lt;br&gt;
&lt;code&gt;radial-gradient&lt;/code&gt;, a blur filter, and an opacity transition on a timer.&lt;br&gt;
They look fine. None of them are dye, and none of them move like dye.&lt;/p&gt;

&lt;p&gt;I wanted to see what changed if the effect actually followed the physics&lt;br&gt;
of a fluid wicking through cloth, so I built it as a real-time WebGL&lt;br&gt;
fragment shader, driven by the same differential equation that describes&lt;br&gt;
how dye moves through cotton.&lt;/p&gt;
&lt;h2&gt;
  
  
  The equation
&lt;/h2&gt;

&lt;p&gt;In 1918, Lucas described how a wetting front advances through a porous&lt;br&gt;
medium; Washburn formalized and extended it in 1921. The result — now&lt;br&gt;
called the Lucas–Washburn equation — says the distance a capillary front&lt;br&gt;
travels is proportional to the square root of elapsed time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;L ∝ √t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
plaintext&lt;/p&gt;

&lt;p&gt;That's the whole idea. It's a genuinely different shape from the curve&lt;br&gt;
most "ink" effects reach for by default: the exponential saturation curve,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p(t) = 1 − e^(−kt)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
plaintext&lt;/p&gt;

&lt;p&gt;which is &lt;em&gt;correct&lt;/em&gt; for a different quantity — how wet a single point&lt;br&gt;
becomes as dye accumulates there — and &lt;em&gt;wrong&lt;/em&gt; for where the front has&lt;br&gt;
actually reached. Driven as a radius, the exponential curve stalls hard.&lt;br&gt;
Measured against its own peak speed, it's roughly 92% stopped by the time&lt;br&gt;
it's three-quarters of the way through its own duration. The front just…&lt;br&gt;
stops moving, well before the animation is over.&lt;/p&gt;

&lt;p&gt;Washburn doesn't do that. It's still moving at roughly a quarter of its&lt;br&gt;
peak speed at the very end. That's the visible signature of real&lt;br&gt;
capillary flow: a fast initial rush, then a long, still-moving crawl —&lt;br&gt;
never quite motionless until the moment it actually stops.&lt;/p&gt;
&lt;h2&gt;
  
  
  Two adjustments the raw equation needs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Regularizing at t=0.&lt;/strong&gt; Pure Washburn predicts infinite front speed at&lt;br&gt;
the instant of contact — the square root of zero elapsed time, divided&lt;br&gt;
into a distance, blows up. Real cloth doesn't behave that way: there's a&lt;br&gt;
brief inertial regime immediately after contact, before viscous drag&lt;br&gt;
takes over and the sqrt(t) relationship holds. A small regularizing&lt;br&gt;
constant, &lt;code&gt;t0&lt;/code&gt;, keeps the front's initial speed fast but finite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wickProgress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;t0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.04&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;s0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;s0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normalized so &lt;code&gt;wickProgress(1) === 1&lt;/code&gt; — the front reaches full radius&lt;br&gt;
exactly at the end of the effect's duration, regardless of how &lt;code&gt;t0&lt;/code&gt; is&lt;br&gt;
tuned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anisotropy.&lt;/strong&gt; Real fabric doesn't wet as a circle. It conducts faster&lt;br&gt;
along the thread direction — warp and weft — than across the bias, so a&lt;br&gt;
real wicking front is stretched along the fibers. And that stretch isn't&lt;br&gt;
present from the first frame; it ramps up as the front travels further&lt;br&gt;
and has more distance over which the fiber direction can steer it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wickRadii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coverRadius&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;progress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;coverRadius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;progress&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ry&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.18&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;progress&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;rx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ry&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At &lt;code&gt;progress = 0&lt;/code&gt; the front is a perfect circle — the instant of contact&lt;br&gt;
has no directionality yet. By &lt;code&gt;progress = 1&lt;/code&gt; it's noticeably elongated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running it on the GPU
&lt;/h2&gt;

&lt;p&gt;The shape function above drives a real-time WebGL fragment shader rather&lt;br&gt;
than a CSS animation, mainly so the wet edge can carry a genuinely&lt;br&gt;
fibrous, torn boundary instead of a clean geometric curve — domain-warped&lt;br&gt;
value noise displaces the front outward unevenly, the way real capillary&lt;br&gt;
action doesn't wet in a perfectly smooth line.&lt;/p&gt;

&lt;p&gt;The two constants that parameterize the curve — the regularization&lt;br&gt;
constant and the anisotropy factor — live in exactly one place and get&lt;br&gt;
interpolated directly into the GLSL source at load, rather than being&lt;br&gt;
retyped as a second, hand-maintained copy inside the shader string. That&lt;br&gt;
sounds like a small detail. It's the kind of small detail that, done&lt;br&gt;
wrong, means two engines that quietly disagree about the same physical&lt;br&gt;
constant a year from now, with nothing to notice when they do — so there's&lt;br&gt;
a test that pulls the literal that actually landed in the compiled shader&lt;br&gt;
text and checks it against the source value, independent of how it got&lt;br&gt;
there.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part that isn't physics: context pooling
&lt;/h2&gt;

&lt;p&gt;Safari hard-caps how many live WebGL contexts a page can hold and&lt;br&gt;
silently drops the oldest one past that cap — which, for a design system&lt;br&gt;
where any card or button might carry this effect, means surfaces going&lt;br&gt;
dark mid-scroll for no visible reason. The fix is a single shared&lt;br&gt;
offscreen canvas: one WebGL context renders every dye surface in turn,&lt;br&gt;
and each surface blits the result onto its own cheap 2D canvas. Page-wide&lt;br&gt;
context count is exactly one, regardless of how many surfaces exist —&lt;br&gt;
verified by instrumenting &lt;code&gt;getContext&lt;/code&gt; in a real browser and asserting&lt;br&gt;
the count in CI, not just asserted in a comment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to see it
&lt;/h2&gt;

&lt;p&gt;This is the dye engine inside &lt;strong&gt;Tantu&lt;/strong&gt;, a React design system modeled on&lt;br&gt;
handloom weaving rather than the usual soft-UI conventions — square&lt;br&gt;
corners because a woven structure has no curves, a 12-thread grid, three&lt;br&gt;
custom typefaces built from stroke skeletons.&lt;/p&gt;

&lt;p&gt;Live, no install: &lt;strong&gt;&lt;a href="https://tantu-playground.netlify.app" rel="noopener noreferrer"&gt;https://tantu-playground.netlify.app&lt;/a&gt;&lt;/strong&gt; — the "Vat"&lt;br&gt;
section has five dye baths and a long cloth; press any of them.&lt;/p&gt;

&lt;p&gt;Source, the shader, and the test suite:&lt;br&gt;
&lt;strong&gt;&lt;a href="https://github.com/rajatarun/aiweave" rel="noopener noreferrer"&gt;https://github.com/rajatarun/aiweave&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>designsystem</category>
      <category>webgl</category>
      <category>uiengineering</category>
      <category>creativecoding</category>
    </item>
  </channel>
</rss>
