# packages/core/src/features/textures/lib/rain-drops.ts
This is the source snapshot used to build these API details. [View this revision on GitHub](https://github.com/benlesh/pibbl/blob/272a94aaf62e0bd6ad8726a4c607a76a9ec44ca1/packages/core/src/features/textures/lib/rain-drops.ts#L401).

[Back to reference](/reference/types/textures/)

<pre class="api-source"><code><span id="L1"><a href="#L1" aria-label="Line 1">1</a> import { defineTexture, type TextureContext, type TextureRecipe } from &quot;@pibbl/core&quot;;</span>
<span id="L2"><a href="#L2" aria-label="Line 2">2</a> import { validateSeed } from &quot;./palette.js&quot;;</span>
<span id="L3"><a href="#L3" aria-label="Line 3">3</a> </span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> /**</span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a>  * Rain rate, gravity, initial drops, condensation, trail, and outline controls for a raindrop texture.</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a>  *</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a>  * @see {@link rainDrops}</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a>  */</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a> export interface RainDropsOptions {</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a>   /**</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a>    * Seed used for deterministic sampling or simulation initialization. See</span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a>    * {@link RainDropsOptions}.</span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a>    */</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a>   readonly seed?: number;</span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a>   /** New beads per second, from 0 through 120. */</span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a>   readonly rainRate?: number;</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a>   /** Downward acceleration multiplier, from 0 through 10. */</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a>   readonly gravity?: number;</span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a>   /** Number of drops seeded before continuous rain starts. See {@link RainDropsOptions}. */</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a>   readonly initialDrops?: number;</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a>   /** Whether simulation or playback advancement is suspended. See {@link RainDropsOptions}. */</span>
<span id="L22"><a href="#L22" aria-label="Line 22">22</a>   readonly paused?: boolean;</span>
<span id="L23"><a href="#L23" aria-label="Line 23">23</a>   /** Fine droplet film coverage, from 0 (clear) through 1 (mist). */</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a>   readonly condensation?: number;</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>   /** Seconds controlling trail drying and condensation recovery, from 0 through 60. */</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a>   readonly trailPersistence?: number;</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a>   /** Residual water left behind a moving drop, from 0 through 1. */</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a>   readonly trailStrength?: number;</span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a>   /** Outline and path variation, from 0 (smooth) through 1. */</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a>   readonly irregularity?: number;</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a> }</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a> </span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a> /**</span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a>  * Pause, reset, finger-wipe, release, and localized nudge commands for a raindrop texture.</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a>  *</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>  * @see {@link rainDrops}</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>  */</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a> export type RainDropsMessage =</span>
<span id="L39"><a href="#L39" aria-label="Line 39">39</a>   | {</span>
<span id="L40"><a href="#L40" aria-label="Line 40">40</a>       /** The literal &quot;pause&quot; identifying this variant. See {@link RainDropsMessage}. */</span>
<span id="L41"><a href="#L41" aria-label="Line 41">41</a>       readonly type: &quot;pause&quot;;</span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a>       /** Whether simulation or playback advancement is suspended. See {@link RainDropsMessage}. */</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a>       readonly paused: boolean;</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a>     }</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a>   | {</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a>       /** The literal &quot;reset&quot; identifying this variant. See {@link RainDropsMessage}. */</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a>       readonly type: &quot;reset&quot;;</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a>     }</span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a>   /** Begin or move one finger contact; successive positions wipe a continuous path. */</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a>   | {</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a>       /** The literal &quot;finger&quot; identifying this variant. See {@link RainDropsMessage}. */</span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a>       readonly type: &quot;finger&quot;;</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a>       /**</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>        * Horizontal coordinate or displacement in the containing coordinate system. See</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a>        * {@link RainDropsMessage}.</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a>        */</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a>       readonly x: number;</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a>       /**</span>
<span id="L59"><a href="#L59" aria-label="Line 59">59</a>        * Vertical coordinate or displacement in the containing coordinate system. See</span>
<span id="L60"><a href="#L60" aria-label="Line 60">60</a>        * {@link RainDropsMessage}.</span>
<span id="L61"><a href="#L61" aria-label="Line 61">61</a>        */</span>
<span id="L62"><a href="#L62" aria-label="Line 62">62</a>       readonly y: number;</span>
<span id="L63"><a href="#L63" aria-label="Line 63">63</a>       /** Radius in the coordinate system of this geometry or effect. See {@link RainDropsMessage}. */</span>
<span id="L64"><a href="#L64" aria-label="Line 64">64</a>       readonly radius?: number;</span>
<span id="L65"><a href="#L65" aria-label="Line 65">65</a>     }</span>
<span id="L66"><a href="#L66" aria-label="Line 66">66</a>   | {</span>
<span id="L67"><a href="#L67" aria-label="Line 67">67</a>       /** The literal &quot;release&quot; identifying this variant. See {@link RainDropsMessage}. */</span>
<span id="L68"><a href="#L68" aria-label="Line 68">68</a>       readonly type: &quot;release&quot;;</span>
<span id="L69"><a href="#L69" aria-label="Line 69">69</a>     }</span>
<span id="L70"><a href="#L70" aria-label="Line 70">70</a>   /** Center and movement use normalized width/height; radius uses the shorter side. */</span>
<span id="L71"><a href="#L71" aria-label="Line 71">71</a>   | {</span>
<span id="L72"><a href="#L72" aria-label="Line 72">72</a>       /** The literal &quot;nudge&quot; identifying this variant. See {@link RainDropsMessage}. */</span>
<span id="L73"><a href="#L73" aria-label="Line 73">73</a>       readonly type: &quot;nudge&quot;;</span>
<span id="L74"><a href="#L74" aria-label="Line 74">74</a>       /**</span>
<span id="L75"><a href="#L75" aria-label="Line 75">75</a>        * Horizontal coordinate or displacement in the containing coordinate system. See</span>
<span id="L76"><a href="#L76" aria-label="Line 76">76</a>        * {@link RainDropsMessage}.</span>
<span id="L77"><a href="#L77" aria-label="Line 77">77</a>        */</span>
<span id="L78"><a href="#L78" aria-label="Line 78">78</a>       readonly x: number;</span>
<span id="L79"><a href="#L79" aria-label="Line 79">79</a>       /**</span>
<span id="L80"><a href="#L80" aria-label="Line 80">80</a>        * Vertical coordinate or displacement in the containing coordinate system. See</span>
<span id="L81"><a href="#L81" aria-label="Line 81">81</a>        * {@link RainDropsMessage}.</span>
<span id="L82"><a href="#L82" aria-label="Line 82">82</a>        */</span>
<span id="L83"><a href="#L83" aria-label="Line 83">83</a>       readonly y: number;</span>
<span id="L84"><a href="#L84" aria-label="Line 84">84</a>       /** Horizontal displacement applied by the interaction. See {@link RainDropsMessage}. */</span>
<span id="L85"><a href="#L85" aria-label="Line 85">85</a>       readonly dx: number;</span>
<span id="L86"><a href="#L86" aria-label="Line 86">86</a>       /** Vertical displacement applied by the interaction. See {@link RainDropsMessage}. */</span>
<span id="L87"><a href="#L87" aria-label="Line 87">87</a>       readonly dy: number;</span>
<span id="L88"><a href="#L88" aria-label="Line 88">88</a>       /** Radius in the coordinate system of this geometry or effect. See {@link RainDropsMessage}. */</span>
<span id="L89"><a href="#L89" aria-label="Line 89">89</a>       readonly radius?: number;</span>
<span id="L90"><a href="#L90" aria-label="Line 90">90</a>     };</span>
<span id="L91"><a href="#L91" aria-label="Line 91">91</a> </span>
<span id="L92"><a href="#L92" aria-label="Line 92">92</a> interface Drop { x: number; y: number; radius: number; vx: number; vy: number; phase: number }</span>
<span id="L93"><a href="#L93" aria-label="Line 93">93</a> interface Config { seed: number; rainRate: number; gravity: number; initialDrops: number; paused: boolean; condensation: number; trailPersistence: number; trailStrength: number; irregularity: number }</span>
<span id="L94"><a href="#L94" aria-label="Line 94">94</a> interface State {</span>
<span id="L95"><a href="#L95" aria-label="Line 95">95</a>   finger?: { x: number; y: number; radius: number; drop?: Drop };</span>
<span id="L96"><a href="#L96" aria-label="Line 96">96</a>   config: Config;</span>
<span id="L97"><a href="#L97" aria-label="Line 97">97</a>   drops: Drop[];</span>
<span id="L98"><a href="#L98" aria-label="Line 98">98</a>   random: number;</span>
<span id="L99"><a href="#L99" aria-label="Line 99">99</a>   births: number;</span>
<span id="L100"><a href="#L100" aria-label="Line 100">100</a>   paused: boolean;</span>
<span id="L101"><a href="#L101" aria-label="Line 101">101</a>   fresh: boolean;</span>
<span id="L102"><a href="#L102" aria-label="Line 102">102</a>   image: ImageData;</span>
<span id="L103"><a href="#L103" aria-label="Line 103">103</a>   film: Float32Array;</span>
<span id="L104"><a href="#L104" aria-label="Line 104">104</a>   trails: Float32Array;</span>
<span id="L105"><a href="#L105" aria-label="Line 105">105</a>   surface: Float32Array;</span>
<span id="L106"><a href="#L106" aria-label="Line 106">106</a>   grain: Float32Array;</span>
<span id="L107"><a href="#L107" aria-label="Line 107">107</a>   fieldActive: boolean;</span>
<span id="L108"><a href="#L108" aria-label="Line 108">108</a> }</span>
<span id="L109"><a href="#L109" aria-label="Line 109">109</a> const MAX_DROPS = 256;</span>
<span id="L110"><a href="#L110" aria-label="Line 110">110</a> </span>
<span id="L111"><a href="#L111" aria-label="Line 111">111</a> function bounded(value: number, name: string, maximum: number): number {</span>
<span id="L112"><a href="#L112" aria-label="Line 112">112</a>   if (!Number.isFinite(value) || value &lt; 0 || value &gt; maximum) throw new RangeError(`Rain ${name} must be finite and within [0, ${maximum}].`);</span>
<span id="L113"><a href="#L113" aria-label="Line 113">113</a>   return value;</span>
<span id="L114"><a href="#L114" aria-label="Line 114">114</a> }</span>
<span id="L115"><a href="#L115" aria-label="Line 115">115</a> function options(input: RainDropsOptions): Config {</span>
<span id="L116"><a href="#L116" aria-label="Line 116">116</a>   const initialDrops = bounded(input.initialDrops ?? 100, &quot;initialDrops&quot;, MAX_DROPS);</span>
<span id="L117"><a href="#L117" aria-label="Line 117">117</a>   if (!Number.isInteger(initialDrops)) throw new RangeError(&quot;Rain initialDrops must be an integer.&quot;);</span>
<span id="L118"><a href="#L118" aria-label="Line 118">118</a>   return { seed: validateSeed(input.seed ?? 7), rainRate: bounded(input.rainRate ?? 24, &quot;rainRate&quot;, 120), gravity: bounded(input.gravity ?? 1, &quot;gravity&quot;, 10), initialDrops, paused: input.paused ?? false,</span>
<span id="L119"><a href="#L119" aria-label="Line 119">119</a>     condensation: bounded(input.condensation ?? 0, &quot;condensation&quot;, 1),</span>
<span id="L120"><a href="#L120" aria-label="Line 120">120</a>     trailPersistence: bounded(input.trailPersistence ?? 8, &quot;trailPersistence&quot;, 60),</span>
<span id="L121"><a href="#L121" aria-label="Line 121">121</a>     trailStrength: bounded(input.trailStrength ?? 0.65, &quot;trailStrength&quot;, 1),</span>
<span id="L122"><a href="#L122" aria-label="Line 122">122</a>     irregularity: bounded(input.irregularity ?? 0.8, &quot;irregularity&quot;, 1) };</span>
<span id="L123"><a href="#L123" aria-label="Line 123">123</a> }</span>
<span id="L124"><a href="#L124" aria-label="Line 124">124</a> function assertMap(context: TextureContext): void {</span>
<span id="L125"><a href="#L125" aria-label="Line 125">125</a>   if (context.palette !== undefined) throw new TypeError(&quot;rainDrops is a numeric displacement map and does not accept a palette.&quot;);</span>
<span id="L126"><a href="#L126" aria-label="Line 126">126</a> }</span>
<span id="L127"><a href="#L127" aria-label="Line 127">127</a> function random(state: State): number {</span>
<span id="L128"><a href="#L128" aria-label="Line 128">128</a>   state.random = (Math.imul(state.random, 1664525) + 1013904223) &gt;&gt;&gt; 0;</span>
<span id="L129"><a href="#L129" aria-label="Line 129">129</a>   return state.random / 4294967296;</span>
<span id="L130"><a href="#L130" aria-label="Line 130">130</a> }</span>
<span id="L131"><a href="#L131" aria-label="Line 131">131</a> function spawn(state: State, context: TextureContext): void {</span>
<span id="L132"><a href="#L132" aria-label="Line 132">132</a>   if (state.drops.length &gt;= MAX_DROPS) return;</span>
<span id="L133"><a href="#L133" aria-label="Line 133">133</a>   const side = Math.min(context.canvas.width, context.canvas.height);</span>
<span id="L134"><a href="#L134" aria-label="Line 134">134</a>   state.drops.push({ x: random(state) * context.canvas.width / side, y: random(state) * context.canvas.height / side, radius: 0.005 + random(state) ** 2 * 0.015, vx: 0, vy: 0, phase: random(state) * Math.PI * 2 });</span>
<span id="L135"><a href="#L135" aria-label="Line 135">135</a> }</span>
<span id="L136"><a href="#L136" aria-label="Line 136">136</a> function reset(state: State, context: TextureContext): void {</span>
<span id="L137"><a href="#L137" aria-label="Line 137">137</a>   state.finger = undefined;</span>
<span id="L138"><a href="#L138" aria-label="Line 138">138</a>   state.random = state.config.seed; state.drops = []; state.births = 0; state.fresh = true;</span>
<span id="L139"><a href="#L139" aria-label="Line 139">139</a>   state.film.fill(1); state.trails.fill(0); state.fieldActive = false;</span>
<span id="L140"><a href="#L140" aria-label="Line 140">140</a>   for (let i = 0; i &lt; state.grain.length; i++) {</span>
<span id="L141"><a href="#L141" aria-label="Line 141">141</a>     let value = Math.imul(i + 1, 0x45d9f3b) ^ state.config.seed;</span>
<span id="L142"><a href="#L142" aria-label="Line 142">142</a>     value = Math.imul(value ^ (value &gt;&gt;&gt; 16), 0x45d9f3b);</span>
<span id="L143"><a href="#L143" aria-label="Line 143">143</a>     state.grain[i] = ((value ^ (value &gt;&gt;&gt; 16)) &gt;&gt;&gt; 0) / 4294967296;</span>
<span id="L144"><a href="#L144" aria-label="Line 144">144</a>   }</span>
<span id="L145"><a href="#L145" aria-label="Line 145">145</a>   for (let i = 0; i &lt; state.config.initialDrops; i++) spawn(state, context);</span>
<span id="L146"><a href="#L146" aria-label="Line 146">146</a> }</span>
<span id="L147"><a href="#L147" aria-label="Line 147">147</a> /** Coalescence conserves volume and volume-weighted position/momentum. */</span>
<span id="L148"><a href="#L148" aria-label="Line 148">148</a> function merge(drops: Drop[]): void {</span>
<span id="L149"><a href="#L149" aria-label="Line 149">149</a>   for (let i = 0; i &lt; drops.length; i++) {</span>
<span id="L150"><a href="#L150" aria-label="Line 150">150</a>     const a = drops[i];</span>
<span id="L151"><a href="#L151" aria-label="Line 151">151</a>     for (let j = i + 1; j &lt; drops.length; j++) {</span>
<span id="L152"><a href="#L152" aria-label="Line 152">152</a>       const b = drops[j];</span>
<span id="L153"><a href="#L153" aria-label="Line 153">153</a>       if (Math.hypot(a.x - b.x, a.y - b.y) &gt;= a.radius + b.radius) continue;</span>
<span id="L154"><a href="#L154" aria-label="Line 154">154</a>       const av = a.radius ** 3, bv = b.radius ** 3, volume = av + bv;</span>
<span id="L155"><a href="#L155" aria-label="Line 155">155</a>       a.x = (a.x * av + b.x * bv) / volume; a.y = (a.y * av + b.y * bv) / volume;</span>
<span id="L156"><a href="#L156" aria-label="Line 156">156</a>       a.vx = (a.vx * av + b.vx * bv) / volume; a.vy = (a.vy * av + b.vy * bv) / volume;</span>
<span id="L157"><a href="#L157" aria-label="Line 157">157</a>       a.radius = Math.cbrt(volume);</span>
<span id="L158"><a href="#L158" aria-label="Line 158">158</a>       drops.splice(j--, 1);</span>
<span id="L159"><a href="#L159" aria-label="Line 159">159</a>     }</span>
<span id="L160"><a href="#L160" aria-label="Line 160">160</a>   }</span>
<span id="L161"><a href="#L161" aria-label="Line 161">161</a> }</span>
<span id="L162"><a href="#L162" aria-label="Line 162">162</a> function active(state: State): boolean {</span>
<span id="L163"><a href="#L163" aria-label="Line 163">163</a>   return state.fieldActive || state.config.rainRate &gt; 0 || state.drops.some(d =&gt; (state.config.gravity &gt; 0 &amp;&amp; d.radius &gt; 0.012) || Math.abs(d.vx) + Math.abs(d.vy) &gt; 0.0001);</span>
<span id="L164"><a href="#L164" aria-label="Line 164">164</a> }</span>
<span id="L165"><a href="#L165" aria-label="Line 165">165</a> function schedule(state: State, context: TextureContext): void {</span>
<span id="L166"><a href="#L166" aria-label="Line 166">166</a>   if (!state.paused &amp;&amp; active(state)) context.requestFrame();</span>
<span id="L167"><a href="#L167" aria-label="Line 167">167</a> }</span>
<span id="L168"><a href="#L168" aria-label="Line 168">168</a> </span>
<span id="L169"><a href="#L169" aria-label="Line 169">169</a> /** Sweep the actual drop footprint, collecting film and leaving a beaded residual trail. */</span>
<span id="L170"><a href="#L170" aria-label="Line 170">170</a> function sweep(state: State, drop: Drop, oldX: number, oldY: number, context: TextureContext): void {</span>
<span id="L171"><a href="#L171" aria-label="Line 171">171</a>   const { width, height } = context.canvas, side = Math.min(width, height);</span>
<span id="L172"><a href="#L172" aria-label="Line 172">172</a>   const ax = oldX * side, ay = oldY * side, bx = drop.x * side, by = drop.y * side;</span>
<span id="L173"><a href="#L173" aria-label="Line 173">173</a>   const dx = bx - ax, dy = by - ay, length2 = dx * dx + dy * dy;</span>
<span id="L174"><a href="#L174" aria-label="Line 174">174</a>   if (length2 &lt; 0.000001) return;</span>
<span id="L175"><a href="#L175" aria-label="Line 175">175</a>   const radius = Math.max(1, drop.radius * side * 1.1);</span>
<span id="L176"><a href="#L176" aria-label="Line 176">176</a>   let collected = 0;</span>
<span id="L177"><a href="#L177" aria-label="Line 177">177</a>   for (let y = Math.max(0, Math.floor(Math.min(ay, by) - radius)); y &lt; Math.min(height, Math.ceil(Math.max(ay, by) + radius)); y++) {</span>
<span id="L178"><a href="#L178" aria-label="Line 178">178</a>     for (let x = Math.max(0, Math.floor(Math.min(ax, bx) - radius)); x &lt; Math.min(width, Math.ceil(Math.max(ax, bx) + radius)); x++) {</span>
<span id="L179"><a href="#L179" aria-label="Line 179">179</a>       const t = Math.max(0, Math.min(1, ((x + 0.5 - ax) * dx + (y + 0.5 - ay) * dy) / length2));</span>
<span id="L180"><a href="#L180" aria-label="Line 180">180</a>       const distance = Math.hypot(x + 0.5 - ax - t * dx, y + 0.5 - ay - t * dy) / radius;</span>
<span id="L181"><a href="#L181" aria-label="Line 181">181</a>       if (distance &gt;= 1) continue;</span>
<span id="L182"><a href="#L182" aria-label="Line 182">182</a>       const i = y * width + x;</span>
<span id="L183"><a href="#L183" aria-label="Line 183">183</a>       const remaining = Math.max(0, Math.min(1, (distance - 0.7) / 0.3));</span>
<span id="L184"><a href="#L184" aria-label="Line 184">184</a>       const removed = Math.max(0, state.film[i] - remaining);</span>
<span id="L185"><a href="#L185" aria-label="Line 185">185</a>       collected += removed;</span>
<span id="L186"><a href="#L186" aria-label="Line 186">186</a>       state.film[i] = Math.min(state.film[i], remaining);</span>
<span id="L187"><a href="#L187" aria-label="Line 187">187</a>       // Spatially stable necks and beads avoid frame-rate-dependent trail stamping.</span>
<span id="L188"><a href="#L188" aria-label="Line 188">188</a>       const neck = 0.18 + 0.12 * Math.sin(y * 0.7 + drop.phase);</span>
<span id="L189"><a href="#L189" aria-label="Line 189">189</a>       const bead = Math.pow(state.grain[i], 7);</span>
<span id="L190"><a href="#L190" aria-label="Line 190">190</a>       const residual = Math.max(0, 1 - distance / (neck + bead * 0.6));</span>
<span id="L191"><a href="#L191" aria-label="Line 191">191</a>       state.trails[i] = Math.max(state.trails[i], residual * (0.00045 + bead * 0.0018) * state.config.trailStrength);</span>
<span id="L192"><a href="#L192" aria-label="Line 192">192</a>     }</span>
<span id="L193"><a href="#L193" aria-label="Line 193">193</a>   }</span>
<span id="L194"><a href="#L194" aria-label="Line 194">194</a>   // Film volume is swept area times a small effective thickness.</span>
<span id="L195"><a href="#L195" aria-label="Line 195">195</a>   drop.radius = Math.cbrt(drop.radius ** 3 + collected / (side * side) * state.config.condensation * 0.0002);</span>
<span id="L196"><a href="#L196" aria-label="Line 196">196</a>   state.fieldActive = true;</span>
<span id="L197"><a href="#L197" aria-label="Line 197">197</a> }</span>
<span id="L198"><a href="#L198" aria-label="Line 198">198</a> </span>
<span id="L199"><a href="#L199" aria-label="Line 199">199</a> /** At capacity, coalesce the closest free beads before releasing newly gathered water. */</span>
<span id="L200"><a href="#L200" aria-label="Line 200">200</a> function makeReleaseRoom(drops: Drop[]): void {</span>
<span id="L201"><a href="#L201" aria-label="Line 201">201</a>   if (drops.length &lt; MAX_DROPS) return;</span>
<span id="L202"><a href="#L202" aria-label="Line 202">202</a>   let first = 0, second = 1, nearest = Infinity;</span>
<span id="L203"><a href="#L203" aria-label="Line 203">203</a>   for (let i = 0; i &lt; drops.length; i++) for (let j = i + 1; j &lt; drops.length; j++) {</span>
<span id="L204"><a href="#L204" aria-label="Line 204">204</a>     const distance = Math.hypot(drops[i].x - drops[j].x, drops[i].y - drops[j].y);</span>
<span id="L205"><a href="#L205" aria-label="Line 205">205</a>     if (distance &lt; nearest) { nearest = distance; first = i; second = j; }</span>
<span id="L206"><a href="#L206" aria-label="Line 206">206</a>   }</span>
<span id="L207"><a href="#L207" aria-label="Line 207">207</a>   const a = drops[first], b = drops[second], av = a.radius ** 3, bv = b.radius ** 3, volume = av + bv;</span>
<span id="L208"><a href="#L208" aria-label="Line 208">208</a>   a.x = (a.x * av + b.x * bv) / volume; a.y = (a.y * av + b.y * bv) / volume;</span>
<span id="L209"><a href="#L209" aria-label="Line 209">209</a>   a.vx = (a.vx * av + b.vx * bv) / volume; a.vy = (a.vy * av + b.vy * bv) / volume;</span>
<span id="L210"><a href="#L210" aria-label="Line 210">210</a>   a.radius = Math.cbrt(volume); drops.splice(second, 1);</span>
<span id="L211"><a href="#L211" aria-label="Line 211">211</a> }</span>
<span id="L212"><a href="#L212" aria-label="Line 212">212</a> </span>
<span id="L213"><a href="#L213" aria-label="Line 213">213</a> /** A finger removes film and residual water, and carries intersected beads without inertia. */</span>
<span id="L214"><a href="#L214" aria-label="Line 214">214</a> function wipeFinger(state: State, oldX: number, oldY: number, context: TextureContext): void {</span>
<span id="L215"><a href="#L215" aria-label="Line 215">215</a>   const finger = state.finger;</span>
<span id="L216"><a href="#L216" aria-label="Line 216">216</a>   if (!finger) return;</span>
<span id="L217"><a href="#L217" aria-label="Line 217">217</a>   const { width, height } = context.canvas, side = Math.min(width, height);</span>
<span id="L218"><a href="#L218" aria-label="Line 218">218</a>   const dx = finger.x - oldX, dy = finger.y - oldY, length2 = dx * dx + dy * dy;</span>
<span id="L219"><a href="#L219" aria-label="Line 219">219</a>   const distance = (x: number, y: number): number =&gt; {</span>
<span id="L220"><a href="#L220" aria-label="Line 220">220</a>     const t = length2 === 0 ? 0 : Math.max(0, Math.min(1, ((x - oldX) * dx + (y - oldY) * dy) / length2));</span>
<span id="L221"><a href="#L221" aria-label="Line 221">221</a>     return Math.hypot(x - oldX - t * dx, y - oldY - t * dy);</span>
<span id="L222"><a href="#L222" aria-label="Line 222">222</a>   };</span>
<span id="L223"><a href="#L223" aria-label="Line 223">223</a>   let volume = finger.drop ? finger.drop.radius ** 3 : 0;</span>
<span id="L224"><a href="#L224" aria-label="Line 224">224</a>   for (let i = state.drops.length - 1; i &gt;= 0; i--) {</span>
<span id="L225"><a href="#L225" aria-label="Line 225">225</a>     const drop = state.drops[i];</span>
<span id="L226"><a href="#L226" aria-label="Line 226">226</a>     if (distance(drop.x, drop.y) &gt; finger.radius + drop.radius * 1.3) continue;</span>
<span id="L227"><a href="#L227" aria-label="Line 227">227</a>     volume += drop.radius ** 3;</span>
<span id="L228"><a href="#L228" aria-label="Line 228">228</a>     state.drops.splice(i, 1);</span>
<span id="L229"><a href="#L229" aria-label="Line 229">229</a>   }</span>
<span id="L230"><a href="#L230" aria-label="Line 230">230</a>   for (let y = Math.max(0, Math.floor((Math.min(oldY, finger.y) - finger.radius) * side)); y &lt; Math.min(height, Math.ceil((Math.max(oldY, finger.y) + finger.radius) * side)); y++) {</span>
<span id="L231"><a href="#L231" aria-label="Line 231">231</a>     for (let x = Math.max(0, Math.floor((Math.min(oldX, finger.x) - finger.radius) * side)); x &lt; Math.min(width, Math.ceil((Math.max(oldX, finger.x) + finger.radius) * side)); x++) {</span>
<span id="L232"><a href="#L232" aria-label="Line 232">232</a>       const coverage = Math.max(0, Math.min(1, (1 - distance((x + 0.5) / side, (y + 0.5) / side) / finger.radius) / 0.15));</span>
<span id="L233"><a href="#L233" aria-label="Line 233">233</a>       if (coverage === 0) continue;</span>
<span id="L234"><a href="#L234" aria-label="Line 234">234</a>       const i = y * width + x, remaining = 1 - coverage;</span>
<span id="L235"><a href="#L235" aria-label="Line 235">235</a>       const removedFilm = Math.max(0, state.film[i] - remaining);</span>
<span id="L236"><a href="#L236" aria-label="Line 236">236</a>       const removedTrail = state.trails[i] * coverage;</span>
<span id="L237"><a href="#L237" aria-label="Line 237">237</a>       volume += (removedFilm * state.config.condensation * 0.0002 + removedTrail) / (side * side);</span>
<span id="L238"><a href="#L238" aria-label="Line 238">238</a>       state.film[i] = Math.min(state.film[i], remaining);</span>
<span id="L239"><a href="#L239" aria-label="Line 239">239</a>       state.trails[i] -= removedTrail;</span>
<span id="L240"><a href="#L240" aria-label="Line 240">240</a>     }</span>
<span id="L241"><a href="#L241" aria-label="Line 241">241</a>   }</span>
<span id="L242"><a href="#L242" aria-label="Line 242">242</a>   if (volume &gt; 0) {</span>
<span id="L243"><a href="#L243" aria-label="Line 243">243</a>     finger.drop ??= { x: finger.x, y: finger.y, radius: 0, vx: 0, vy: 0, phase: 0 };</span>
<span id="L244"><a href="#L244" aria-label="Line 244">244</a>     finger.drop.x = finger.x; finger.drop.y = finger.y; finger.drop.radius = Math.cbrt(volume);</span>
<span id="L245"><a href="#L245" aria-label="Line 245">245</a>   }</span>
<span id="L246"><a href="#L246" aria-label="Line 246">246</a>   state.fieldActive = true;</span>
<span id="L247"><a href="#L247" aria-label="Line 247">247</a> }</span>
<span id="L248"><a href="#L248" aria-label="Line 248">248</a> </span>
<span id="L249"><a href="#L249" aria-label="Line 249">249</a> function recoverFilm(state: State, elapsed: number): void {</span>
<span id="L250"><a href="#L250" aria-label="Line 250">250</a>   if (!state.fieldActive) return;</span>
<span id="L251"><a href="#L251" aria-label="Line 251">251</a>   const retention = state.config.trailPersistence === 0 ? 0 : Math.exp(-elapsed / state.config.trailPersistence);</span>
<span id="L252"><a href="#L252" aria-label="Line 252">252</a>   let active = false;</span>
<span id="L253"><a href="#L253" aria-label="Line 253">253</a>   for (let i = 0; i &lt; state.film.length; i++) {</span>
<span id="L254"><a href="#L254" aria-label="Line 254">254</a>     const missing = (1 - state.film[i]) * retention;</span>
<span id="L255"><a href="#L255" aria-label="Line 255">255</a>     state.film[i] = missing &lt; 0.002 ? 1 : 1 - missing;</span>
<span id="L256"><a href="#L256" aria-label="Line 256">256</a>     const trail = state.trails[i] * retention;</span>
<span id="L257"><a href="#L257" aria-label="Line 257">257</a>     state.trails[i] = trail &lt; 0.000005 ? 0 : trail;</span>
<span id="L258"><a href="#L258" aria-label="Line 258">258</a>     active ||= state.film[i] &lt; 1 || state.trails[i] &gt; 0;</span>
<span id="L259"><a href="#L259" aria-label="Line 259">259</a>   }</span>
<span id="L260"><a href="#L260" aria-label="Line 260">260</a>   state.fieldActive = active;</span>
<span id="L261"><a href="#L261" aria-label="Line 261">261</a> }</span>
<span id="L262"><a href="#L262" aria-label="Line 262">262</a> </span>
<span id="L263"><a href="#L263" aria-label="Line 263">263</a> const recipe = defineTexture&lt;RainDropsOptions, State, RainDropsMessage&gt;({</span>
<span id="L264"><a href="#L264" aria-label="Line 264">264</a>   create(context, input) {</span>
<span id="L265"><a href="#L265" aria-label="Line 265">265</a>     assertMap(context);</span>
<span id="L266"><a href="#L266" aria-label="Line 266">266</a>     const config = options(input);</span>
<span id="L267"><a href="#L267" aria-label="Line 267">267</a>     const cells = context.canvas.width * context.canvas.height;</span>
<span id="L268"><a href="#L268" aria-label="Line 268">268</a>     const state: State = { film: new Float32Array(cells), trails: new Float32Array(cells), surface: new Float32Array(cells), grain: new Float32Array(cells), fieldActive: false, config, drops: [], random: config.seed, births: 0, paused: config.paused, fresh: true, image: context.canvas.getContext(&quot;2d&quot;)!.createImageData(context.canvas.width, context.canvas.height) };</span>
<span id="L269"><a href="#L269" aria-label="Line 269">269</a>     reset(state, context); schedule(state, context); return state;</span>
<span id="L270"><a href="#L270" aria-label="Line 270">270</a>   },</span>
<span id="L271"><a href="#L271" aria-label="Line 271">271</a>   update(state, input, context) {</span>
<span id="L272"><a href="#L272" aria-label="Line 272">272</a>     assertMap(context);</span>
<span id="L273"><a href="#L273" aria-label="Line 273">273</a>     const next = options(input), previous = state.config;</span>
<span id="L274"><a href="#L274" aria-label="Line 274">274</a>     state.config = next;</span>
<span id="L275"><a href="#L275" aria-label="Line 275">275</a>     if (next.condensation !== previous.condensation || next.irregularity !== previous.irregularity || next.trailStrength !== previous.trailStrength) context.invalidate();</span>
<span id="L276"><a href="#L276" aria-label="Line 276">276</a>     if (next.paused !== previous.paused) { state.paused = next.paused; state.fresh = true; }</span>
<span id="L277"><a href="#L277" aria-label="Line 277">277</a>     if (next.rainRate !== previous.rainRate || next.gravity !== previous.gravity) state.fresh = true;</span>
<span id="L278"><a href="#L278" aria-label="Line 278">278</a>     if (next.seed !== previous.seed || next.initialDrops !== previous.initialDrops) { reset(state, context); context.invalidate(); }</span>
<span id="L279"><a href="#L279" aria-label="Line 279">279</a>     schedule(state, context);</span>
<span id="L280"><a href="#L280" aria-label="Line 280">280</a>   },</span>
<span id="L281"><a href="#L281" aria-label="Line 281">281</a>   receive(state, message, context) {</span>
<span id="L282"><a href="#L282" aria-label="Line 282">282</a>     if (message.type === &quot;pause&quot;) { state.paused = message.paused; state.fresh = true; }</span>
<span id="L283"><a href="#L283" aria-label="Line 283">283</a>     else if (message.type === &quot;reset&quot;) { reset(state, context); context.invalidate(); }</span>
<span id="L284"><a href="#L284" aria-label="Line 284">284</a>     else if (message.type === &quot;finger&quot;) {</span>
<span id="L285"><a href="#L285" aria-label="Line 285">285</a>       const x = bounded(message.x, &quot;finger x&quot;, 1), y = bounded(message.y, &quot;finger y&quot;, 1);</span>
<span id="L286"><a href="#L286" aria-label="Line 286">286</a>       const radius = bounded(message.radius ?? 0.045, &quot;finger radius&quot;, 2);</span>
<span id="L287"><a href="#L287" aria-label="Line 287">287</a>       if (radius === 0) throw new RangeError(&quot;Rain finger radius must be positive.&quot;);</span>
<span id="L288"><a href="#L288" aria-label="Line 288">288</a>       const side = Math.min(context.canvas.width, context.canvas.height);</span>
<span id="L289"><a href="#L289" aria-label="Line 289">289</a>       const nextX = x * context.canvas.width / side, nextY = y * context.canvas.height / side;</span>
<span id="L290"><a href="#L290" aria-label="Line 290">290</a>       const oldX = state.finger?.x ?? nextX, oldY = state.finger?.y ?? nextY;</span>
<span id="L291"><a href="#L291" aria-label="Line 291">291</a>       state.finger ??= { x: nextX, y: nextY, radius };</span>
<span id="L292"><a href="#L292" aria-label="Line 292">292</a>       state.finger.x = nextX; state.finger.y = nextY; state.finger.radius = radius;</span>
<span id="L293"><a href="#L293" aria-label="Line 293">293</a>       wipeFinger(state, oldX, oldY, context); context.invalidate();</span>
<span id="L294"><a href="#L294" aria-label="Line 294">294</a>     }</span>
<span id="L295"><a href="#L295" aria-label="Line 295">295</a>     else if (message.type === &quot;release&quot;) {</span>
<span id="L296"><a href="#L296" aria-label="Line 296">296</a>       if (state.finger?.drop) { makeReleaseRoom(state.drops); state.drops.push(state.finger.drop); }</span>
<span id="L297"><a href="#L297" aria-label="Line 297">297</a>       state.finger = undefined; context.invalidate();</span>
<span id="L298"><a href="#L298" aria-label="Line 298">298</a>     }</span>
<span id="L299"><a href="#L299" aria-label="Line 299">299</a>     else if (message.type === &quot;nudge&quot;) {</span>
<span id="L300"><a href="#L300" aria-label="Line 300">300</a>       const x = bounded(message.x, &quot;nudge x&quot;, 1), y = bounded(message.y, &quot;nudge y&quot;, 1);</span>
<span id="L301"><a href="#L301" aria-label="Line 301">301</a>       const radius = bounded(message.radius ?? 0.12, &quot;nudge radius&quot;, 2);</span>
<span id="L302"><a href="#L302" aria-label="Line 302">302</a>       if (radius === 0) throw new RangeError(&quot;Rain nudge radius must be positive.&quot;);</span>
<span id="L303"><a href="#L303" aria-label="Line 303">303</a>       for (const delta of [message.dx, message.dy]) if (!Number.isFinite(delta) || Math.abs(delta) &gt; 1) throw new RangeError(&quot;Rain nudge movement must be finite and within [-1, 1].&quot;);</span>
<span id="L304"><a href="#L304" aria-label="Line 304">304</a>       if (message.dx === 0 &amp;&amp; message.dy === 0) return;</span>
<span id="L305"><a href="#L305" aria-label="Line 305">305</a>       const side = Math.min(context.canvas.width, context.canvas.height);</span>
<span id="L306"><a href="#L306" aria-label="Line 306">306</a>       const width = context.canvas.width / side, height = context.canvas.height / side;</span>
<span id="L307"><a href="#L307" aria-label="Line 307">307</a>       for (const drop of state.drops) {</span>
<span id="L308"><a href="#L308" aria-label="Line 308">308</a>         const distance = Math.hypot(drop.x - x * width, drop.y - y * height);</span>
<span id="L309"><a href="#L309" aria-label="Line 309">309</a>         const weight = Math.max(0, 1 - distance / radius) ** 2;</span>
<span id="L310"><a href="#L310" aria-label="Line 310">310</a>         const oldX = drop.x, oldY = drop.y;</span>
<span id="L311"><a href="#L311" aria-label="Line 311">311</a>         drop.x = Math.max(0, Math.min(width, drop.x + message.dx * width * weight));</span>
<span id="L312"><a href="#L312" aria-label="Line 312">312</a>         drop.y = Math.max(0, Math.min(height, drop.y + message.dy * height * weight));</span>
<span id="L313"><a href="#L313" aria-label="Line 313">313</a>         sweep(state, drop, oldX, oldY, context);</span>
<span id="L314"><a href="#L314" aria-label="Line 314">314</a>         drop.vx += message.dx * width * weight * 3; drop.vy += message.dy * height * weight * 3;</span>
<span id="L315"><a href="#L315" aria-label="Line 315">315</a>       }</span>
<span id="L316"><a href="#L316" aria-label="Line 316">316</a>       merge(state.drops); context.invalidate();</span>
<span id="L317"><a href="#L317" aria-label="Line 317">317</a>     } else throw new TypeError(&quot;Unknown rain drop message.&quot;);</span>
<span id="L318"><a href="#L318" aria-label="Line 318">318</a>     schedule(state, context);</span>
<span id="L319"><a href="#L319" aria-label="Line 319">319</a>   },</span>
<span id="L320"><a href="#L320" aria-label="Line 320">320</a>   advance(state, frame, context) {</span>
<span id="L321"><a href="#L321" aria-label="Line 321">321</a>     if (state.paused) return;</span>
<span id="L322"><a href="#L322" aria-label="Line 322">322</a>     if (state.fresh) state.fresh = false;</span>
<span id="L323"><a href="#L323" aria-label="Line 323">323</a>     else {</span>
<span id="L324"><a href="#L324" aria-label="Line 324">324</a>       // Bounded substeps prevent tab suspension from creating a rainfall burst.</span>
<span id="L325"><a href="#L325" aria-label="Line 325">325</a>       const elapsed = Math.min(frame.delta / 1000, 0.05);</span>
<span id="L326"><a href="#L326" aria-label="Line 326">326</a>       recoverFilm(state, elapsed);</span>
<span id="L327"><a href="#L327" aria-label="Line 327">327</a>       const steps = Math.max(1, Math.ceil(elapsed * 120)), dt = elapsed / steps;</span>
<span id="L328"><a href="#L328" aria-label="Line 328">328</a>       const side = Math.min(context.canvas.width, context.canvas.height);</span>
<span id="L329"><a href="#L329" aria-label="Line 329">329</a>       for (let step = 0; step &lt; steps; step++) {</span>
<span id="L330"><a href="#L330" aria-label="Line 330">330</a>         state.births += state.config.rainRate * dt;</span>
<span id="L331"><a href="#L331" aria-label="Line 331">331</a>         while (state.births &gt;= 1) { spawn(state, context); state.births--; }</span>
<span id="L332"><a href="#L332" aria-label="Line 332">332</a>         for (const drop of state.drops) {</span>
<span id="L333"><a href="#L333" aria-label="Line 333">333</a>           const slide = Math.max(0, drop.radius / 0.012 - 1);</span>
<span id="L334"><a href="#L334" aria-label="Line 334">334</a>           const oldX = drop.x, oldY = drop.y;</span>
<span id="L335"><a href="#L335" aria-label="Line 335">335</a>           drop.vx += Math.sin(drop.y * 85 + drop.phase) * slide * state.config.gravity * state.config.irregularity * 0.09 * dt;</span>
<span id="L336"><a href="#L336" aria-label="Line 336">336</a>           drop.vy += state.config.gravity * slide * 0.3 * dt;</span>
<span id="L337"><a href="#L337" aria-label="Line 337">337</a>           drop.vx *= Math.exp(-7 * dt); drop.vy *= Math.exp(-3 * dt);</span>
<span id="L338"><a href="#L338" aria-label="Line 338">338</a>           if (Math.abs(drop.vx) &lt; 0.00005) drop.vx = 0;</span>
<span id="L339"><a href="#L339" aria-label="Line 339">339</a>           if (Math.abs(drop.vy) &lt; 0.00005) drop.vy = 0;</span>
<span id="L340"><a href="#L340" aria-label="Line 340">340</a>           drop.x += drop.vx * dt; drop.y += drop.vy * dt;</span>
<span id="L341"><a href="#L341" aria-label="Line 341">341</a>           sweep(state, drop, oldX, oldY, context);</span>
<span id="L342"><a href="#L342" aria-label="Line 342">342</a>         }</span>
<span id="L343"><a href="#L343" aria-label="Line 343">343</a>         merge(state.drops);</span>
<span id="L344"><a href="#L344" aria-label="Line 344">344</a>         state.drops = state.drops.filter(d =&gt; d.y - d.radius &lt; context.canvas.height / side &amp;&amp; d.y + d.radius &gt; 0 &amp;&amp; d.x + d.radius &gt; 0 &amp;&amp; d.x - d.radius &lt; context.canvas.width / side);</span>
<span id="L345"><a href="#L345" aria-label="Line 345">345</a>       }</span>
<span id="L346"><a href="#L346" aria-label="Line 346">346</a>       if (state.finger) wipeFinger(state, state.finger.x, state.finger.y, context);</span>
<span id="L347"><a href="#L347" aria-label="Line 347">347</a>       context.invalidate();</span>
<span id="L348"><a href="#L348" aria-label="Line 348">348</a>     }</span>
<span id="L349"><a href="#L349" aria-label="Line 349">349</a>     schedule(state, context);</span>
<span id="L350"><a href="#L350" aria-label="Line 350">350</a>   },</span>
<span id="L351"><a href="#L351" aria-label="Line 351">351</a>   rasterize(state, context) {</span>
<span id="L352"><a href="#L352" aria-label="Line 352">352</a>     const { width, height } = context.canvas, side = Math.min(width, height);</span>
<span id="L353"><a href="#L353" aria-label="Line 353">353</a>     const data = state.image.data, surface = state.surface;</span>
<span id="L354"><a href="#L354" aria-label="Line 354">354</a>     for (let i = 0; i &lt; surface.length; i++) {</span>
<span id="L355"><a href="#L355" aria-label="Line 355">355</a>       surface[i] = state.trails[i] + state.grain[i] * 0.00018 * state.config.condensation * state.film[i];</span>
<span id="L356"><a href="#L356" aria-label="Line 356">356</a>     }</span>
<span id="L357"><a href="#L357" aria-label="Line 357">357</a>     const visibleDrops = state.finger?.drop ? [...state.drops, state.finger.drop] : state.drops;</span>
<span id="L358"><a href="#L358" aria-label="Line 358">358</a>     for (const drop of visibleDrops) {</span>
<span id="L359"><a href="#L359" aria-label="Line 359">359</a>       const stretch = 1 + Math.min(1.3, Math.abs(drop.vy) * 3);</span>
<span id="L360"><a href="#L360" aria-label="Line 360">360</a>       const rx = drop.radius * side / Math.sqrt(stretch), ry = drop.radius * side * stretch;</span>
<span id="L361"><a href="#L361" aria-label="Line 361">361</a>       const cx = drop.x * side, cy = drop.y * side;</span>
<span id="L362"><a href="#L362" aria-label="Line 362">362</a>       const extent = 1.3;</span>
<span id="L363"><a href="#L363" aria-label="Line 363">363</a>       for (let y = Math.max(0, Math.floor(cy - ry * extent)); y &lt; Math.min(height, Math.ceil(cy + ry * extent)); y++) {</span>
<span id="L364"><a href="#L364" aria-label="Line 364">364</a>         for (let x = Math.max(0, Math.floor(cx - rx * extent)); x &lt; Math.min(width, Math.ceil(cx + rx * extent)); x++) {</span>
<span id="L365"><a href="#L365" aria-label="Line 365">365</a>           const ny = (y + 0.5 - cy) / ry;</span>
<span id="L366"><a href="#L366" aria-label="Line 366">366</a>           const nx = (x + 0.5 - cx) / rx - state.config.irregularity * 0.09 * Math.sin(ny * 3 + drop.phase);</span>
<span id="L367"><a href="#L367" aria-label="Line 367">367</a>           const angle = Math.atan2(ny, nx);</span>
<span id="L368"><a href="#L368" aria-label="Line 368">368</a>           const edge = 1 + state.config.irregularity * (0.12 * Math.sin(angle * 3 + drop.phase) + 0.07 * Math.sin(angle * 5 - drop.phase));</span>
<span id="L369"><a href="#L369" aria-label="Line 369">369</a>           const r2 = (nx * nx + ny * ny) / (edge * edge);</span>
<span id="L370"><a href="#L370" aria-label="Line 370">370</a>           if (r2 &gt;= 1) continue;</span>
<span id="L371"><a href="#L371" aria-label="Line 371">371</a>           const i = y * width + x;</span>
<span id="L372"><a href="#L372" aria-label="Line 372">372</a>           surface[i] = Math.max(surface[i], drop.radius * Math.sqrt(1 - r2));</span>
<span id="L373"><a href="#L373" aria-label="Line 373">373</a>         }</span>
<span id="L374"><a href="#L374" aria-label="Line 374">374</a>       }</span>
<span id="L375"><a href="#L375" aria-label="Line 375">375</a>     }</span>
<span id="L376"><a href="#L376" aria-label="Line 376">376</a>     for (let y = 0; y &lt; height; y++) for (let x = 0; x &lt; width; x++) {</span>
<span id="L377"><a href="#L377" aria-label="Line 377">377</a>       const i = y * width + x, index = i * 4;</span>
<span id="L378"><a href="#L378" aria-label="Line 378">378</a>       const dx = (surface[y * width + Math.min(width - 1, x + 1)] - surface[y * width + Math.max(0, x - 1)]) * side * 0.5;</span>
<span id="L379"><a href="#L379" aria-label="Line 379">379</a>       const dy = (surface[Math.min(height - 1, y + 1) * width + x] - surface[Math.max(0, y - 1) * width + x]) * side * 0.5;</span>
<span id="L380"><a href="#L380" aria-label="Line 380">380</a>       data[index] = Math.round(128 + Math.max(-1, Math.min(1, dx)) * 110);</span>
<span id="L381"><a href="#L381" aria-label="Line 381">381</a>       data[index + 1] = Math.round(128 + Math.max(-1, Math.min(1, dy)) * 110);</span>
<span id="L382"><a href="#L382" aria-label="Line 382">382</a>       const droplet = Math.min(1, Math.max(0, (surface[i] - 0.001) * 1500));</span>
<span id="L383"><a href="#L383" aria-label="Line 383">383</a>       data[index + 2] = Math.round(255 * state.config.condensation * state.film[i] * (0.8 + state.grain[i] * 0.2) * (1 - droplet));</span>
<span id="L384"><a href="#L384" aria-label="Line 384">384</a>       data[index + 3] = state.config.condensation &gt; 0 ? 255 : Math.round(Math.min(1, surface[i] * side * 2) * 255);</span>
<span id="L385"><a href="#L385" aria-label="Line 385">385</a>     }</span>
<span id="L386"><a href="#L386" aria-label="Line 386">386</a>     context.canvas.getContext(&quot;2d&quot;)!.putImageData(state.image, 0, 0);</span>
<span id="L387"><a href="#L387" aria-label="Line 387">387</a>   },</span>
<span id="L388"><a href="#L388" aria-label="Line 388">388</a> });</span>
<span id="L389"><a href="#L389" aria-label="Line 389">389</a> </span>
<span id="L390"><a href="#L390" aria-label="Line 390">390</a> /**</span>
<span id="L391"><a href="#L391" aria-label="Line 391">391</a>  * A seeded rain-on-glass displacement map, advanced by Pibbl's scheduler.</span>
<span id="L392"><a href="#L392" aria-label="Line 392">392</a>  *</span>
<span id="L393"><a href="#L393" aria-label="Line 393">393</a>  * @param options - Raindrop simulation and appearance settings. See {@link RainDropsOptions}.</span>
<span id="L394"><a href="#L394" aria-label="Line 394">394</a>  * @returns A raindrop texture recipe accepting raindrop messages. See {@link TextureRecipe} ,</span>
<span id="L395"><a href="#L395" aria-label="Line 395">395</a>  * {@link RainDropsMessage} .</span>
<span id="L396"><a href="#L396" aria-label="Line 396">396</a>  *</span>
<span id="L397"><a href="#L397" aria-label="Line 397">397</a>  * @see {@link RainDropsOptions}</span>
<span id="L398"><a href="#L398" aria-label="Line 398">398</a>  * @see {@link TextureRecipe}</span>
<span id="L399"><a href="#L399" aria-label="Line 399">399</a>  * @see {@link RainDropsMessage}</span>
<span id="L400"><a href="#L400" aria-label="Line 400">400</a>  */</span>
<span id="L401"><a href="#L401" aria-label="Line 401">401</a> export function rainDrops(options: RainDropsOptions = {}): TextureRecipe&lt;RainDropsMessage&gt; { return recipe(options); }</span>
<span id="L402"><a href="#L402" aria-label="Line 402">402</a> </span></code></pre>

## Documentation version

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.
