[{"data":1,"prerenderedAt":2968,"buildId":2969},["ShallowReactive",2],{"post-\u002Fposts\u002Fstable-fluids-in-150-lines":3,"surround-\u002Fposts\u002Fstable-fluids-in-150-lines":2959},{"id":4,"title":5,"body":6,"description":2944,"draft":2945,"extension":2946,"meta":2947,"navigation":189,"path":2948,"publishDate":2949,"seo":2950,"stem":2951,"tags":2952,"updatedDate":2957,"__hash__":2958},"posts\u002Fposts\u002Fstable-fluids-in-150-lines.md","Stable fluids in 150 lines",{"type":7,"value":8,"toc":2927},"minimark",[9,19,22,35,54,57,60,62,67,78,85,305,323,330,333,378,381,386,393,442,457,461,464,468,474,936,949,953,956,959,1307,1329,1340,1344,1347,1350,1356,1377,1626,1647,1653,1663,1667,1670,2019,2022,2033,2037,2044,2161,2164,2167,2423,2437,2441,2444,2497,2508,2512,2515,2518,2669,2679,2686,2783,2794,2798,2804,2871,2874,2878,2881,2884,2888,2891,2923],[10,11,12,13,18],"p",{},"A while back I wrote about building a ",[14,15,17],"a",{"href":16},"\u002Fposts\u002Fcanvas-random-walk","random walk on canvas",". It was a fun couple of nights, but the whole time I kept thinking the little walker would look a lot better if it were dragging something behind it. Something that swirled. So for the 2027 refresh of this site I went back to it, and the walker now drives an ink simulation in the hero on the home page.",[10,20,21],{},"The goals were pretty simple:",[23,24,25,29,32],"ul",{},[26,27,28],"li",{},"It should feel physical. Ink should curl and push back, not just smear.",[26,30,31],{},"It should run on about a laptop fan's worth of CPU.",[26,33,34],{},"No dependencies. No WebGL, no libraries, just a 2D canvas.",[10,36,37,38,44,45,49,50,53],{},"The approach is Jos Stam's ",[14,39,43],{"href":40,"rel":41},"https:\u002F\u002Fwww.dgp.toronto.edu\u002Fpublic_user\u002Fstam\u002Freality\u002FResearch\u002Fpdf\u002Fns.pdf",[42],"nofollow","Stable Fluids"," from SIGGRAPH 1999. Not counting comments and blank lines, the solver is 115 lines, and the per-frame ",[46,47,48],"code",{},"update()"," and ",[46,51,52],{},"render()"," functions add 37 more. That's where the 150 comes from.",[55,56],"hr",{},[58,59],"demo-fluids",{},[55,61],{},[63,64,66],"h2",{"id":65},"the-grid","The Grid",[10,68,69,70,73,74,77],{},"Everything lives on a grid of cells, 128 wide by 40 tall by default. Each cell has a horizontal and vertical velocity, plus a value for each dye: ",[46,71,72],{},"walker"," for the green ink the random walker leaves and ",[46,75,76],{},"pointer"," for the orange ink you stir in with your mouse. The grid also gets a one-cell border on every side, which we use to handle the walls.",[10,79,80,81,84],{},"Rather than nested arrays, every field is a flat ",[46,82,83],{},"Float32Array",":",[86,87,92],"pre",{"className":88,"code":89,"language":90,"meta":91,"style":91},"language-js shiki shiki-themes vitesse-dark","const rowSize = width + 2               \u002F\u002F one border cell on each side\nconst cellCount = rowSize * (height + 2)\nconst createField = () => new Float32Array(cellCount)\n\nconst velocityX = createField(), velocityY = createField()\nconst prevVelocityX = createField(), prevVelocityY = createField()\nconst pressure = createField(), divergence = createField()\n\nconst index = (x, y) => x + rowSize * y\n","js","",[46,93,94,125,153,184,191,216,239,262,267],{"__ignoreMap":91},[95,96,99,103,107,111,114,117,121],"span",{"class":97,"line":98},"line",1,[95,100,102],{"class":101},"s_wWq","const",[95,104,106],{"class":105},"st-jp"," rowSize",[95,108,110],{"class":109},"s_pn2"," =",[95,112,113],{"class":105}," width",[95,115,116],{"class":101}," +",[95,118,120],{"class":119},"sxA9i"," 2",[95,122,124],{"class":123},"sux-A","               \u002F\u002F one border cell on each side\n",[95,126,128,130,133,135,137,140,143,146,148,150],{"class":97,"line":127},2,[95,129,102],{"class":101},[95,131,132],{"class":105}," cellCount",[95,134,110],{"class":109},[95,136,106],{"class":105},[95,138,139],{"class":101}," *",[95,141,142],{"class":109}," (",[95,144,145],{"class":105},"height",[95,147,116],{"class":101},[95,149,120],{"class":119},[95,151,152],{"class":109},")\n",[95,154,156,158,162,164,167,170,173,176,179,182],{"class":97,"line":155},3,[95,157,102],{"class":101},[95,159,161],{"class":160},"sCK9x"," createField",[95,163,110],{"class":109},[95,165,166],{"class":109}," ()",[95,168,169],{"class":109}," =>",[95,171,172],{"class":101}," new",[95,174,175],{"class":160}," Float32Array",[95,177,178],{"class":109},"(",[95,180,181],{"class":105},"cellCount",[95,183,152],{"class":109},[95,185,187],{"class":97,"line":186},4,[95,188,190],{"emptyLinePlaceholder":189},true,"\n",[95,192,194,196,199,201,203,206,209,211,213],{"class":97,"line":193},5,[95,195,102],{"class":101},[95,197,198],{"class":105}," velocityX",[95,200,110],{"class":109},[95,202,161],{"class":160},[95,204,205],{"class":109},"(),",[95,207,208],{"class":105}," velocityY",[95,210,110],{"class":109},[95,212,161],{"class":160},[95,214,215],{"class":109},"()\n",[95,217,219,221,224,226,228,230,233,235,237],{"class":97,"line":218},6,[95,220,102],{"class":101},[95,222,223],{"class":105}," prevVelocityX",[95,225,110],{"class":109},[95,227,161],{"class":160},[95,229,205],{"class":109},[95,231,232],{"class":105}," prevVelocityY",[95,234,110],{"class":109},[95,236,161],{"class":160},[95,238,215],{"class":109},[95,240,242,244,247,249,251,253,256,258,260],{"class":97,"line":241},7,[95,243,102],{"class":101},[95,245,246],{"class":105}," pressure",[95,248,110],{"class":109},[95,250,161],{"class":160},[95,252,205],{"class":109},[95,254,255],{"class":105}," divergence",[95,257,110],{"class":109},[95,259,161],{"class":160},[95,261,215],{"class":109},[95,263,265],{"class":97,"line":264},8,[95,266,190],{"emptyLinePlaceholder":189},[95,268,270,272,275,277,279,282,285,288,291,293,296,298,300,302],{"class":97,"line":269},9,[95,271,102],{"class":101},[95,273,274],{"class":160}," index",[95,276,110],{"class":109},[95,278,142],{"class":109},[95,280,281],{"class":105},"x",[95,283,284],{"class":109},",",[95,286,287],{"class":105}," y",[95,289,290],{"class":109},")",[95,292,169],{"class":109},[95,294,295],{"class":105}," x",[95,297,116],{"class":101},[95,299,106],{"class":105},[95,301,139],{"class":101},[95,303,304],{"class":105}," y\n",[10,306,307,308,311,312,315,316,318,319,322],{},"If this ",[46,309,310],{},"index"," helper looks familiar, it's the same trick from the random walk post where we turned 2D coordinates into an index in a 1D array. Multiply ",[46,313,314],{},"y"," by the row size to get to the start of the row, then add ",[46,317,281],{},". No ",[46,320,321],{},"* 4"," this time since each field holds a single number per cell.",[10,324,325,326,329],{},"There are a couple of nice wins with flat typed arrays. The memory is contiguous, so walking the grid row by row is about as cache friendly as JavaScript gets. And copying a whole field is one call: ",[46,327,328],{},"prevVelocityX.set(velocityX)"," copies all ~5,000 cells without allocating anything. We do that every frame, so it adds up.",[10,331,332],{},"The dyes are created from whatever you pass in, along with how fast each one fades:",[86,334,336],{"className":88,"code":335,"language":90,"meta":91,"style":91},"dyes: dyeDecay = { walker: 0.994, pointer: 0.988 }, \u002F\u002F dye name -> fade per step\n",[46,337,338],{"__ignoreMap":91},[95,339,340,343,345,348,350,353,357,359,362,364,367,369,372,375],{"class":97,"line":98},[95,341,342],{"class":160},"dyes",[95,344,84],{"class":109},[95,346,347],{"class":105}," dyeDecay",[95,349,110],{"class":109},[95,351,352],{"class":109}," {",[95,354,356],{"class":355},"sm68I"," walker",[95,358,84],{"class":109},[95,360,361],{"class":119}," 0.994",[95,363,284],{"class":109},[95,365,366],{"class":355}," pointer",[95,368,84],{"class":109},[95,370,371],{"class":119}," 0.988",[95,373,374],{"class":109}," },",[95,376,377],{"class":123}," \u002F\u002F dye name -> fade per step\n",[10,379,380],{},"So if you wanted a third color, you'd add a name here and a color in the renderer. The solver doesn't care.",[382,383,385],"h3",{"id":384},"the-walls","The walls",[10,387,388,389,392],{},"The border cells get filled in after every pass by ",[46,390,391],{},"setBoundaries()",". For dye and pressure it just copies the neighbor, so nothing leaks out. For velocity it mirrors the value at the wall it's moving into, so the fluid bounces back instead of flowing off the edge:",[86,394,396],{"className":88,"code":395,"language":90,"meta":91,"style":91},"const SCALAR = 0      \u002F\u002F copy the neighbour (dye, pressure)\nconst VELOCITY_X = 1  \u002F\u002F mirror at the left and right walls\nconst VELOCITY_Y = 2  \u002F\u002F mirror at the top and bottom walls\n",[46,397,398,413,428],{"__ignoreMap":91},[95,399,400,402,405,407,410],{"class":97,"line":98},[95,401,102],{"class":101},[95,403,404],{"class":105}," SCALAR",[95,406,110],{"class":109},[95,408,409],{"class":119}," 0",[95,411,412],{"class":123},"      \u002F\u002F copy the neighbour (dye, pressure)\n",[95,414,415,417,420,422,425],{"class":97,"line":127},[95,416,102],{"class":101},[95,418,419],{"class":105}," VELOCITY_X",[95,421,110],{"class":109},[95,423,424],{"class":119}," 1",[95,426,427],{"class":123},"  \u002F\u002F mirror at the left and right walls\n",[95,429,430,432,435,437,439],{"class":97,"line":155},[95,431,102],{"class":101},[95,433,434],{"class":105}," VELOCITY_Y",[95,436,110],{"class":109},[95,438,120],{"class":119},[95,440,441],{"class":123},"  \u002F\u002F mirror at the top and bottom walls\n",[10,443,444,445,448,449,452,453,456],{},"Stam's original code passes a bare ",[46,446,447],{},"0",", ",[46,450,451],{},"1"," or ",[46,454,455],{},"2"," here. Naming them made the rest of the solver a lot easier to read.",[63,458,460],{"id":459},"three-steps-per-frame","Three Steps Per Frame",[10,462,463],{},"The whole simulation boils down to three steps that run every frame.",[382,465,467],{"id":466},"_1-add-forces","1. Add forces",[10,469,470,473],{},[46,471,472],{},"splat()"," drops a soft blob of velocity and dye into the grid at a point. The blob is a small Gaussian, so it's strongest in the middle and fades out toward the edges:",[86,475,477],{"className":88,"code":476,"language":90,"meta":91,"style":91},"function splat(x, y, forceX, forceY, dyeName, amount = 0.5, radius = 3) {\n  const dye = dyes[dyeName]\n  const spread = (radius * radius) \u002F 2.25\n  for (let offsetY = -radius; offsetY \u003C= radius; offsetY++) {\n    for (let offsetX = -radius; offsetX \u003C= radius; offsetX++) {\n      const cellX = Math.round(x) + offsetX, cellY = Math.round(y) + offsetY\n      if (cellX \u003C 1 || cellY \u003C 1 || cellX > width || cellY > height) continue\n      const weight = Math.exp(-(offsetX * offsetX + offsetY * offsetY) \u002F spread)\n      const cell = index(cellX, cellY)\n      velocityX[cell] += forceX * weight\n      velocityY[cell] += forceY * weight\n      dye[cell] = Math.min(1.4, dye[cell] + amount * weight)\n    }\n  }\n}\n",[46,478,479,535,557,583,625,661,714,761,807,828,852,872,918,924,930],{"__ignoreMap":91},[95,480,481,484,487,489,491,493,495,497,500,502,505,507,510,512,515,517,520,522,525,527,530,532],{"class":97,"line":98},[95,482,483],{"class":101},"function",[95,485,486],{"class":160}," splat",[95,488,178],{"class":109},[95,490,281],{"class":105},[95,492,284],{"class":109},[95,494,287],{"class":105},[95,496,284],{"class":109},[95,498,499],{"class":105}," forceX",[95,501,284],{"class":109},[95,503,504],{"class":105}," forceY",[95,506,284],{"class":109},[95,508,509],{"class":105}," dyeName",[95,511,284],{"class":109},[95,513,514],{"class":105}," amount",[95,516,110],{"class":109},[95,518,519],{"class":119}," 0.5",[95,521,284],{"class":109},[95,523,524],{"class":105}," radius",[95,526,110],{"class":109},[95,528,529],{"class":119}," 3",[95,531,290],{"class":109},[95,533,534],{"class":109}," {\n",[95,536,537,540,543,545,548,551,554],{"class":97,"line":127},[95,538,539],{"class":101},"  const",[95,541,542],{"class":105}," dye",[95,544,110],{"class":109},[95,546,547],{"class":105}," dyes",[95,549,550],{"class":109},"[",[95,552,553],{"class":105},"dyeName",[95,555,556],{"class":109},"]\n",[95,558,559,561,564,566,568,571,573,575,577,580],{"class":97,"line":155},[95,560,539],{"class":101},[95,562,563],{"class":105}," spread",[95,565,110],{"class":109},[95,567,142],{"class":109},[95,569,570],{"class":105},"radius",[95,572,139],{"class":101},[95,574,524],{"class":105},[95,576,290],{"class":109},[95,578,579],{"class":101}," \u002F",[95,581,582],{"class":119}," 2.25\n",[95,584,585,589,591,594,597,599,602,604,607,609,612,614,616,618,621,623],{"class":97,"line":186},[95,586,588],{"class":587},"s3QIE","  for",[95,590,142],{"class":109},[95,592,593],{"class":101},"let",[95,595,596],{"class":105}," offsetY",[95,598,110],{"class":109},[95,600,601],{"class":101}," -",[95,603,570],{"class":105},[95,605,606],{"class":109},";",[95,608,596],{"class":105},[95,610,611],{"class":109}," \u003C=",[95,613,524],{"class":105},[95,615,606],{"class":109},[95,617,596],{"class":105},[95,619,620],{"class":101},"++",[95,622,290],{"class":109},[95,624,534],{"class":109},[95,626,627,630,632,634,637,639,641,643,645,647,649,651,653,655,657,659],{"class":97,"line":193},[95,628,629],{"class":587},"    for",[95,631,142],{"class":109},[95,633,593],{"class":101},[95,635,636],{"class":105}," offsetX",[95,638,110],{"class":109},[95,640,601],{"class":101},[95,642,570],{"class":105},[95,644,606],{"class":109},[95,646,636],{"class":105},[95,648,611],{"class":109},[95,650,524],{"class":105},[95,652,606],{"class":109},[95,654,636],{"class":105},[95,656,620],{"class":101},[95,658,290],{"class":109},[95,660,534],{"class":109},[95,662,663,666,669,671,674,677,680,682,684,686,688,690,692,695,697,699,701,703,705,707,709,711],{"class":97,"line":218},[95,664,665],{"class":101},"      const",[95,667,668],{"class":105}," cellX",[95,670,110],{"class":109},[95,672,673],{"class":105}," Math",[95,675,676],{"class":109},".",[95,678,679],{"class":160},"round",[95,681,178],{"class":109},[95,683,281],{"class":105},[95,685,290],{"class":109},[95,687,116],{"class":101},[95,689,636],{"class":105},[95,691,284],{"class":109},[95,693,694],{"class":105}," cellY",[95,696,110],{"class":109},[95,698,673],{"class":105},[95,700,676],{"class":109},[95,702,679],{"class":160},[95,704,178],{"class":109},[95,706,314],{"class":105},[95,708,290],{"class":109},[95,710,116],{"class":101},[95,712,713],{"class":105}," offsetY\n",[95,715,716,719,721,724,727,729,732,734,736,738,740,742,745,747,749,751,753,756,758],{"class":97,"line":241},[95,717,718],{"class":587},"      if",[95,720,142],{"class":109},[95,722,723],{"class":105},"cellX",[95,725,726],{"class":109}," \u003C",[95,728,424],{"class":119},[95,730,731],{"class":101}," ||",[95,733,694],{"class":105},[95,735,726],{"class":109},[95,737,424],{"class":119},[95,739,731],{"class":101},[95,741,668],{"class":105},[95,743,744],{"class":109}," >",[95,746,113],{"class":105},[95,748,731],{"class":101},[95,750,694],{"class":105},[95,752,744],{"class":109},[95,754,755],{"class":105}," height",[95,757,290],{"class":109},[95,759,760],{"class":587}," continue\n",[95,762,763,765,768,770,772,774,777,779,782,784,787,789,791,793,795,797,799,801,803,805],{"class":97,"line":264},[95,764,665],{"class":101},[95,766,767],{"class":105}," weight",[95,769,110],{"class":109},[95,771,673],{"class":105},[95,773,676],{"class":109},[95,775,776],{"class":160},"exp",[95,778,178],{"class":109},[95,780,781],{"class":101},"-",[95,783,178],{"class":109},[95,785,786],{"class":105},"offsetX",[95,788,139],{"class":101},[95,790,636],{"class":105},[95,792,116],{"class":101},[95,794,596],{"class":105},[95,796,139],{"class":101},[95,798,596],{"class":105},[95,800,290],{"class":109},[95,802,579],{"class":101},[95,804,563],{"class":105},[95,806,152],{"class":109},[95,808,809,811,814,816,818,820,822,824,826],{"class":97,"line":269},[95,810,665],{"class":101},[95,812,813],{"class":105}," cell",[95,815,110],{"class":109},[95,817,274],{"class":160},[95,819,178],{"class":109},[95,821,723],{"class":105},[95,823,284],{"class":109},[95,825,694],{"class":105},[95,827,152],{"class":109},[95,829,831,834,836,839,842,845,847,849],{"class":97,"line":830},10,[95,832,833],{"class":105},"      velocityX",[95,835,550],{"class":109},[95,837,838],{"class":105},"cell",[95,840,841],{"class":109},"]",[95,843,844],{"class":101}," +=",[95,846,499],{"class":105},[95,848,139],{"class":101},[95,850,851],{"class":105}," weight\n",[95,853,855,858,860,862,864,866,868,870],{"class":97,"line":854},11,[95,856,857],{"class":105},"      velocityY",[95,859,550],{"class":109},[95,861,838],{"class":105},[95,863,841],{"class":109},[95,865,844],{"class":101},[95,867,504],{"class":105},[95,869,139],{"class":101},[95,871,851],{"class":105},[95,873,875,878,880,882,884,886,888,890,893,895,898,900,902,904,906,908,910,912,914,916],{"class":97,"line":874},12,[95,876,877],{"class":105},"      dye",[95,879,550],{"class":109},[95,881,838],{"class":105},[95,883,841],{"class":109},[95,885,110],{"class":109},[95,887,673],{"class":105},[95,889,676],{"class":109},[95,891,892],{"class":160},"min",[95,894,178],{"class":109},[95,896,897],{"class":119},"1.4",[95,899,284],{"class":109},[95,901,542],{"class":105},[95,903,550],{"class":109},[95,905,838],{"class":105},[95,907,841],{"class":109},[95,909,116],{"class":101},[95,911,514],{"class":105},[95,913,139],{"class":101},[95,915,767],{"class":105},[95,917,152],{"class":109},[95,919,921],{"class":97,"line":920},13,[95,922,923],{"class":109},"    }\n",[95,925,927],{"class":97,"line":926},14,[95,928,929],{"class":109},"  }\n",[95,931,933],{"class":97,"line":932},15,[95,934,935],{"class":109},"}\n",[10,937,938,939,941,942,944,945,948],{},"Dye is capped at ",[46,940,897],{}," rather than ",[46,943,451],{}," so the center of a fresh splat stays fully saturated for a few frames before it starts fading. It gets clamped back to ",[46,946,947],{},"0..1"," when we read it for drawing.",[382,950,952],{"id":951},"_2-advect","2. Advect",[10,954,955],{},"Advection is how the fluid carries stuff around, including its own velocity. The naive approach is to take each cell and push its contents forward along the velocity. The problem is that forward pushing blows up with big time steps, and things land between cells.",[10,957,958],{},"Stam's trick is to go backwards. For every cell, ask \"where did the stuff that's here now come from?\" Trace back along the velocity, land somewhere between four cells, and blend them with bilinear interpolation:",[86,960,962],{"className":88,"code":961,"language":90,"meta":91,"style":91},"const fromX = Math.max(0.5, Math.min(width + 0.5, x - dt * prevVelocityX[cell]))\nconst fromY = Math.max(0.5, Math.min(height + 0.5, y - dt * prevVelocityY[cell]))\nconst left = Math.floor(fromX), top = Math.floor(fromY)\nconst right = fromX - left, bottom = fromY - top\ntarget[cell] =\n  (1 - right) * ((1 - bottom) * source[index(left, top)] + bottom * source[index(left, top + 1)]) +\n  right * ((1 - bottom) * source[index(left + 1, top)] + bottom * source[index(left + 1, top + 1)])\n",[46,963,964,1022,1075,1117,1146,1160,1237],{"__ignoreMap":91},[95,965,966,968,971,973,975,977,980,982,985,987,989,991,993,995,998,1000,1002,1004,1006,1008,1011,1013,1015,1017,1019],{"class":97,"line":98},[95,967,102],{"class":101},[95,969,970],{"class":105}," fromX",[95,972,110],{"class":109},[95,974,673],{"class":105},[95,976,676],{"class":109},[95,978,979],{"class":160},"max",[95,981,178],{"class":109},[95,983,984],{"class":119},"0.5",[95,986,284],{"class":109},[95,988,673],{"class":105},[95,990,676],{"class":109},[95,992,892],{"class":160},[95,994,178],{"class":109},[95,996,997],{"class":105},"width",[95,999,116],{"class":101},[95,1001,519],{"class":119},[95,1003,284],{"class":109},[95,1005,295],{"class":105},[95,1007,601],{"class":101},[95,1009,1010],{"class":105}," dt",[95,1012,139],{"class":101},[95,1014,223],{"class":105},[95,1016,550],{"class":109},[95,1018,838],{"class":105},[95,1020,1021],{"class":109},"]))\n",[95,1023,1024,1026,1029,1031,1033,1035,1037,1039,1041,1043,1045,1047,1049,1051,1053,1055,1057,1059,1061,1063,1065,1067,1069,1071,1073],{"class":97,"line":127},[95,1025,102],{"class":101},[95,1027,1028],{"class":105}," fromY",[95,1030,110],{"class":109},[95,1032,673],{"class":105},[95,1034,676],{"class":109},[95,1036,979],{"class":160},[95,1038,178],{"class":109},[95,1040,984],{"class":119},[95,1042,284],{"class":109},[95,1044,673],{"class":105},[95,1046,676],{"class":109},[95,1048,892],{"class":160},[95,1050,178],{"class":109},[95,1052,145],{"class":105},[95,1054,116],{"class":101},[95,1056,519],{"class":119},[95,1058,284],{"class":109},[95,1060,287],{"class":105},[95,1062,601],{"class":101},[95,1064,1010],{"class":105},[95,1066,139],{"class":101},[95,1068,232],{"class":105},[95,1070,550],{"class":109},[95,1072,838],{"class":105},[95,1074,1021],{"class":109},[95,1076,1077,1079,1082,1084,1086,1088,1091,1093,1096,1099,1102,1104,1106,1108,1110,1112,1115],{"class":97,"line":155},[95,1078,102],{"class":101},[95,1080,1081],{"class":105}," left",[95,1083,110],{"class":109},[95,1085,673],{"class":105},[95,1087,676],{"class":109},[95,1089,1090],{"class":160},"floor",[95,1092,178],{"class":109},[95,1094,1095],{"class":105},"fromX",[95,1097,1098],{"class":109},"),",[95,1100,1101],{"class":105}," top",[95,1103,110],{"class":109},[95,1105,673],{"class":105},[95,1107,676],{"class":109},[95,1109,1090],{"class":160},[95,1111,178],{"class":109},[95,1113,1114],{"class":105},"fromY",[95,1116,152],{"class":109},[95,1118,1119,1121,1124,1126,1128,1130,1132,1134,1137,1139,1141,1143],{"class":97,"line":186},[95,1120,102],{"class":101},[95,1122,1123],{"class":105}," right",[95,1125,110],{"class":109},[95,1127,970],{"class":105},[95,1129,601],{"class":101},[95,1131,1081],{"class":105},[95,1133,284],{"class":109},[95,1135,1136],{"class":105}," bottom",[95,1138,110],{"class":109},[95,1140,1028],{"class":105},[95,1142,601],{"class":101},[95,1144,1145],{"class":105}," top\n",[95,1147,1148,1151,1153,1155,1157],{"class":97,"line":193},[95,1149,1150],{"class":105},"target",[95,1152,550],{"class":109},[95,1154,838],{"class":105},[95,1156,841],{"class":109},[95,1158,1159],{"class":109}," =\n",[95,1161,1162,1165,1167,1169,1171,1173,1175,1178,1180,1182,1184,1186,1188,1191,1193,1195,1197,1200,1202,1204,1207,1209,1211,1213,1215,1217,1219,1221,1223,1225,1227,1229,1231,1234],{"class":97,"line":218},[95,1163,1164],{"class":109},"  (",[95,1166,451],{"class":119},[95,1168,601],{"class":101},[95,1170,1123],{"class":105},[95,1172,290],{"class":109},[95,1174,139],{"class":101},[95,1176,1177],{"class":109}," ((",[95,1179,451],{"class":119},[95,1181,601],{"class":101},[95,1183,1136],{"class":105},[95,1185,290],{"class":109},[95,1187,139],{"class":101},[95,1189,1190],{"class":105}," source",[95,1192,550],{"class":109},[95,1194,310],{"class":160},[95,1196,178],{"class":109},[95,1198,1199],{"class":105},"left",[95,1201,284],{"class":109},[95,1203,1101],{"class":105},[95,1205,1206],{"class":109},")]",[95,1208,116],{"class":101},[95,1210,1136],{"class":105},[95,1212,139],{"class":101},[95,1214,1190],{"class":105},[95,1216,550],{"class":109},[95,1218,310],{"class":160},[95,1220,178],{"class":109},[95,1222,1199],{"class":105},[95,1224,284],{"class":109},[95,1226,1101],{"class":105},[95,1228,116],{"class":101},[95,1230,424],{"class":119},[95,1232,1233],{"class":109},")])",[95,1235,1236],{"class":101}," +\n",[95,1238,1239,1242,1244,1246,1248,1250,1252,1254,1256,1258,1260,1262,1264,1266,1268,1270,1272,1274,1276,1278,1280,1282,1284,1286,1288,1290,1292,1294,1296,1298,1300,1302,1304],{"class":97,"line":241},[95,1240,1241],{"class":105},"  right",[95,1243,139],{"class":101},[95,1245,1177],{"class":109},[95,1247,451],{"class":119},[95,1249,601],{"class":101},[95,1251,1136],{"class":105},[95,1253,290],{"class":109},[95,1255,139],{"class":101},[95,1257,1190],{"class":105},[95,1259,550],{"class":109},[95,1261,310],{"class":160},[95,1263,178],{"class":109},[95,1265,1199],{"class":105},[95,1267,116],{"class":101},[95,1269,424],{"class":119},[95,1271,284],{"class":109},[95,1273,1101],{"class":105},[95,1275,1206],{"class":109},[95,1277,116],{"class":101},[95,1279,1136],{"class":105},[95,1281,139],{"class":101},[95,1283,1190],{"class":105},[95,1285,550],{"class":109},[95,1287,310],{"class":160},[95,1289,178],{"class":109},[95,1291,1199],{"class":105},[95,1293,116],{"class":101},[95,1295,424],{"class":119},[95,1297,284],{"class":109},[95,1299,1101],{"class":105},[95,1301,116],{"class":101},[95,1303,424],{"class":119},[95,1305,1306],{"class":109},")])\n",[10,1308,1309,49,1311,1314,1315,49,1318,1321,1322,1325,1326,1328],{},[46,1310,1199],{},[46,1312,1313],{},"top"," are the cell we landed in, and ",[46,1316,1317],{},"right",[46,1319,1320],{},"bottom"," are how far we are toward the next cell over and the next cell down. So ",[46,1323,1324],{},"(1 - right)"," is how much the left column counts, ",[46,1327,1317],{}," is how much the right column counts, and the same goes for top and bottom.",[10,1330,1331,1332,1336,1337,676],{},"Because we only ever blend values that already exist, the result can never grow beyond what was there. That's what makes it ",[1333,1334,1335],"em",{},"unconditionally stable",", which is the whole point of the paper. You can crank the velocity way up and it'll look chaotic, but it won't explode into ",[46,1338,1339],{},"NaN",[382,1341,1343],{"id":1342},"_3-project","3. Project",[10,1345,1346],{},"This is the step that took me a minute to wrap my head around, and it's also the one that makes the whole thing look like fluid.",[10,1348,1349],{},"After adding forces and advecting, the velocity field is a mess. Some cells have more flowing in than out, which isn't how water works. Real fluid is incompressible. Push on it in one spot and it has to go somewhere, which is exactly where the swirls come from.",[10,1351,1352,1355],{},[46,1353,1354],{},"project()"," fixes this in three passes:",[1357,1358,1359,1367,1374],"ol",{},[26,1360,1361,1362,1366],{},"Measure the ",[1363,1364,1365],"strong",{},"divergence"," of every cell, meaning how much more is flowing out than in.",[26,1368,1369,1370,1373],{},"Solve for a ",[1363,1371,1372],{},"pressure"," field that would cancel that out. This is a Poisson equation, and we solve it with 16 iterations of Gauss-Seidel, where each cell repeatedly becomes the average of its neighbors plus its divergence.",[26,1375,1376],{},"Subtract the pressure gradient from the velocity.",[86,1378,1380],{"className":88,"code":1379,"language":90,"meta":91,"style":91},"for (let n = 0; n \u003C iterations; n++) {\n  for (let y = 1; y \u003C= height; y++) {\n    for (let x = 1; x \u003C= width; x++) {\n      const cell = index(x, y)\n      const neighbours = pressure[cell - 1] + pressure[cell + 1] + pressure[cell - rowSize] + pressure[cell + rowSize]\n      pressure[cell] = (divergence[cell] + neighbours) \u002F 4\n    }\n  }\n  setBoundaries(SCALAR, pressure)\n}\n",[46,1381,1382,1417,1449,1481,1501,1564,1598,1602,1606,1622],{"__ignoreMap":91},[95,1383,1384,1387,1389,1391,1394,1396,1398,1400,1402,1404,1407,1409,1411,1413,1415],{"class":97,"line":98},[95,1385,1386],{"class":587},"for",[95,1388,142],{"class":109},[95,1390,593],{"class":101},[95,1392,1393],{"class":105}," n",[95,1395,110],{"class":109},[95,1397,409],{"class":119},[95,1399,606],{"class":109},[95,1401,1393],{"class":105},[95,1403,726],{"class":109},[95,1405,1406],{"class":105}," iterations",[95,1408,606],{"class":109},[95,1410,1393],{"class":105},[95,1412,620],{"class":101},[95,1414,290],{"class":109},[95,1416,534],{"class":109},[95,1418,1419,1421,1423,1425,1427,1429,1431,1433,1435,1437,1439,1441,1443,1445,1447],{"class":97,"line":127},[95,1420,588],{"class":587},[95,1422,142],{"class":109},[95,1424,593],{"class":101},[95,1426,287],{"class":105},[95,1428,110],{"class":109},[95,1430,424],{"class":119},[95,1432,606],{"class":109},[95,1434,287],{"class":105},[95,1436,611],{"class":109},[95,1438,755],{"class":105},[95,1440,606],{"class":109},[95,1442,287],{"class":105},[95,1444,620],{"class":101},[95,1446,290],{"class":109},[95,1448,534],{"class":109},[95,1450,1451,1453,1455,1457,1459,1461,1463,1465,1467,1469,1471,1473,1475,1477,1479],{"class":97,"line":155},[95,1452,629],{"class":587},[95,1454,142],{"class":109},[95,1456,593],{"class":101},[95,1458,295],{"class":105},[95,1460,110],{"class":109},[95,1462,424],{"class":119},[95,1464,606],{"class":109},[95,1466,295],{"class":105},[95,1468,611],{"class":109},[95,1470,113],{"class":105},[95,1472,606],{"class":109},[95,1474,295],{"class":105},[95,1476,620],{"class":101},[95,1478,290],{"class":109},[95,1480,534],{"class":109},[95,1482,1483,1485,1487,1489,1491,1493,1495,1497,1499],{"class":97,"line":186},[95,1484,665],{"class":101},[95,1486,813],{"class":105},[95,1488,110],{"class":109},[95,1490,274],{"class":160},[95,1492,178],{"class":109},[95,1494,281],{"class":105},[95,1496,284],{"class":109},[95,1498,287],{"class":105},[95,1500,152],{"class":109},[95,1502,1503,1505,1508,1510,1512,1514,1516,1518,1520,1522,1524,1526,1528,1530,1532,1534,1536,1538,1540,1542,1544,1546,1548,1550,1552,1554,1556,1558,1560,1562],{"class":97,"line":193},[95,1504,665],{"class":101},[95,1506,1507],{"class":105}," neighbours",[95,1509,110],{"class":109},[95,1511,246],{"class":105},[95,1513,550],{"class":109},[95,1515,838],{"class":105},[95,1517,601],{"class":101},[95,1519,424],{"class":119},[95,1521,841],{"class":109},[95,1523,116],{"class":101},[95,1525,246],{"class":105},[95,1527,550],{"class":109},[95,1529,838],{"class":105},[95,1531,116],{"class":101},[95,1533,424],{"class":119},[95,1535,841],{"class":109},[95,1537,116],{"class":101},[95,1539,246],{"class":105},[95,1541,550],{"class":109},[95,1543,838],{"class":105},[95,1545,601],{"class":101},[95,1547,106],{"class":105},[95,1549,841],{"class":109},[95,1551,116],{"class":101},[95,1553,246],{"class":105},[95,1555,550],{"class":109},[95,1557,838],{"class":105},[95,1559,116],{"class":101},[95,1561,106],{"class":105},[95,1563,556],{"class":109},[95,1565,1566,1569,1571,1573,1575,1577,1579,1581,1583,1585,1587,1589,1591,1593,1595],{"class":97,"line":218},[95,1567,1568],{"class":105},"      pressure",[95,1570,550],{"class":109},[95,1572,838],{"class":105},[95,1574,841],{"class":109},[95,1576,110],{"class":109},[95,1578,142],{"class":109},[95,1580,1365],{"class":105},[95,1582,550],{"class":109},[95,1584,838],{"class":105},[95,1586,841],{"class":109},[95,1588,116],{"class":101},[95,1590,1507],{"class":105},[95,1592,290],{"class":109},[95,1594,579],{"class":101},[95,1596,1597],{"class":119}," 4\n",[95,1599,1600],{"class":97,"line":241},[95,1601,923],{"class":109},[95,1603,1604],{"class":97,"line":264},[95,1605,929],{"class":109},[95,1607,1608,1611,1613,1616,1618,1620],{"class":97,"line":269},[95,1609,1610],{"class":160},"  setBoundaries",[95,1612,178],{"class":109},[95,1614,1615],{"class":105},"SCALAR",[95,1617,284],{"class":109},[95,1619,246],{"class":105},[95,1621,152],{"class":109},[95,1623,1624],{"class":97,"line":830},[95,1625,935],{"class":109},[10,1627,1628,1629,49,1632,1635,1636,49,1639,1642,1643,1646],{},"A nice side effect of the flat arrays: ",[46,1630,1631],{},"cell - 1",[46,1633,1634],{},"cell + 1"," are the cells to the left and right, and ",[46,1637,1638],{},"cell - rowSize",[46,1640,1641],{},"cell + rowSize"," are the cells above and below. No need to call ",[46,1644,1645],{},"index()"," four times.",[10,1648,1649,1650,1652],{},"Notice that ",[46,1651,1372],{}," is updated in place. Each cell reads neighbors that were already updated earlier in the same pass, and that's what makes it Gauss-Seidel instead of the slower Jacobi method. Sixteen iterations isn't enough to fully converge, but it's plenty for something that only has to look right.",[10,1654,1655,1656,1658,1659,1662],{},"Without this step you get ink that drifts and smears. With it you get curls. If you want to see the difference, comment out both ",[46,1657,1354],{}," calls in ",[46,1660,1661],{},"step()"," and watch the ink lose all of its curls.",[382,1664,1666],{"id":1665},"putting-it-together","Putting it together",[10,1668,1669],{},"Here's the full step:",[86,1671,1673],{"className":88,"code":1672,"language":90,"meta":91,"style":91},"function step(dt = 1 \u002F 60) {\n  project()\n  prevVelocityX.set(velocityX); prevVelocityY.set(velocityY)\n  advect(VELOCITY_X, velocityX, prevVelocityX, dt)\n  advect(VELOCITY_Y, velocityY, prevVelocityY, dt)\n  project()\n  prevVelocityX.set(velocityX); prevVelocityY.set(velocityY)\n  for (const name of dyeNames) {\n    prevDye.set(dyes[name])\n    advect(SCALAR, dyes[name], prevDye, dt)\n    for (let cell = 0; cell \u003C cellCount; cell++) dyes[name][cell] *= dyeDecay[name]\n  }\n  for (let cell = 0; cell \u003C cellCount; cell++) {\n    velocityX[cell] *= velocityDecay\n    velocityY[cell] *= velocityDecay\n  }\n}\n",[46,1674,1675,1700,1707,1738,1762,1785,1791,1817,1838,1859,1888,1942,1946,1978,1994,2009,2014],{"__ignoreMap":91},[95,1676,1677,1679,1682,1684,1687,1689,1691,1693,1696,1698],{"class":97,"line":98},[95,1678,483],{"class":101},[95,1680,1681],{"class":160}," step",[95,1683,178],{"class":109},[95,1685,1686],{"class":105},"dt",[95,1688,110],{"class":109},[95,1690,424],{"class":119},[95,1692,579],{"class":101},[95,1694,1695],{"class":119}," 60",[95,1697,290],{"class":109},[95,1699,534],{"class":109},[95,1701,1702,1705],{"class":97,"line":127},[95,1703,1704],{"class":160},"  project",[95,1706,215],{"class":109},[95,1708,1709,1712,1714,1717,1719,1722,1725,1727,1729,1731,1733,1736],{"class":97,"line":155},[95,1710,1711],{"class":105},"  prevVelocityX",[95,1713,676],{"class":109},[95,1715,1716],{"class":160},"set",[95,1718,178],{"class":109},[95,1720,1721],{"class":105},"velocityX",[95,1723,1724],{"class":109},");",[95,1726,232],{"class":105},[95,1728,676],{"class":109},[95,1730,1716],{"class":160},[95,1732,178],{"class":109},[95,1734,1735],{"class":105},"velocityY",[95,1737,152],{"class":109},[95,1739,1740,1743,1745,1748,1750,1752,1754,1756,1758,1760],{"class":97,"line":186},[95,1741,1742],{"class":160},"  advect",[95,1744,178],{"class":109},[95,1746,1747],{"class":105},"VELOCITY_X",[95,1749,284],{"class":109},[95,1751,198],{"class":105},[95,1753,284],{"class":109},[95,1755,223],{"class":105},[95,1757,284],{"class":109},[95,1759,1010],{"class":105},[95,1761,152],{"class":109},[95,1763,1764,1766,1768,1771,1773,1775,1777,1779,1781,1783],{"class":97,"line":193},[95,1765,1742],{"class":160},[95,1767,178],{"class":109},[95,1769,1770],{"class":105},"VELOCITY_Y",[95,1772,284],{"class":109},[95,1774,208],{"class":105},[95,1776,284],{"class":109},[95,1778,232],{"class":105},[95,1780,284],{"class":109},[95,1782,1010],{"class":105},[95,1784,152],{"class":109},[95,1786,1787,1789],{"class":97,"line":218},[95,1788,1704],{"class":160},[95,1790,215],{"class":109},[95,1792,1793,1795,1797,1799,1801,1803,1805,1807,1809,1811,1813,1815],{"class":97,"line":241},[95,1794,1711],{"class":105},[95,1796,676],{"class":109},[95,1798,1716],{"class":160},[95,1800,178],{"class":109},[95,1802,1721],{"class":105},[95,1804,1724],{"class":109},[95,1806,232],{"class":105},[95,1808,676],{"class":109},[95,1810,1716],{"class":160},[95,1812,178],{"class":109},[95,1814,1735],{"class":105},[95,1816,152],{"class":109},[95,1818,1819,1821,1823,1825,1828,1831,1834,1836],{"class":97,"line":264},[95,1820,588],{"class":587},[95,1822,142],{"class":109},[95,1824,102],{"class":101},[95,1826,1827],{"class":105}," name",[95,1829,1830],{"class":101}," of",[95,1832,1833],{"class":105}," dyeNames",[95,1835,290],{"class":109},[95,1837,534],{"class":109},[95,1839,1840,1843,1845,1847,1849,1851,1853,1856],{"class":97,"line":269},[95,1841,1842],{"class":105},"    prevDye",[95,1844,676],{"class":109},[95,1846,1716],{"class":160},[95,1848,178],{"class":109},[95,1850,342],{"class":105},[95,1852,550],{"class":109},[95,1854,1855],{"class":105},"name",[95,1857,1858],{"class":109},"])\n",[95,1860,1861,1864,1866,1868,1870,1872,1874,1876,1879,1882,1884,1886],{"class":97,"line":830},[95,1862,1863],{"class":160},"    advect",[95,1865,178],{"class":109},[95,1867,1615],{"class":105},[95,1869,284],{"class":109},[95,1871,547],{"class":105},[95,1873,550],{"class":109},[95,1875,1855],{"class":105},[95,1877,1878],{"class":109},"],",[95,1880,1881],{"class":105}," prevDye",[95,1883,284],{"class":109},[95,1885,1010],{"class":105},[95,1887,152],{"class":109},[95,1889,1890,1892,1894,1896,1898,1900,1902,1904,1906,1908,1910,1912,1914,1916,1918,1920,1922,1924,1927,1929,1931,1934,1936,1938,1940],{"class":97,"line":854},[95,1891,629],{"class":587},[95,1893,142],{"class":109},[95,1895,593],{"class":101},[95,1897,813],{"class":105},[95,1899,110],{"class":109},[95,1901,409],{"class":119},[95,1903,606],{"class":109},[95,1905,813],{"class":105},[95,1907,726],{"class":109},[95,1909,132],{"class":105},[95,1911,606],{"class":109},[95,1913,813],{"class":105},[95,1915,620],{"class":101},[95,1917,290],{"class":109},[95,1919,547],{"class":105},[95,1921,550],{"class":109},[95,1923,1855],{"class":105},[95,1925,1926],{"class":109},"][",[95,1928,838],{"class":105},[95,1930,841],{"class":109},[95,1932,1933],{"class":101}," *=",[95,1935,347],{"class":105},[95,1937,550],{"class":109},[95,1939,1855],{"class":105},[95,1941,556],{"class":109},[95,1943,1944],{"class":97,"line":874},[95,1945,929],{"class":109},[95,1947,1948,1950,1952,1954,1956,1958,1960,1962,1964,1966,1968,1970,1972,1974,1976],{"class":97,"line":920},[95,1949,588],{"class":587},[95,1951,142],{"class":109},[95,1953,593],{"class":101},[95,1955,813],{"class":105},[95,1957,110],{"class":109},[95,1959,409],{"class":119},[95,1961,606],{"class":109},[95,1963,813],{"class":105},[95,1965,726],{"class":109},[95,1967,132],{"class":105},[95,1969,606],{"class":109},[95,1971,813],{"class":105},[95,1973,620],{"class":101},[95,1975,290],{"class":109},[95,1977,534],{"class":109},[95,1979,1980,1983,1985,1987,1989,1991],{"class":97,"line":926},[95,1981,1982],{"class":105},"    velocityX",[95,1984,550],{"class":109},[95,1986,838],{"class":105},[95,1988,841],{"class":109},[95,1990,1933],{"class":101},[95,1992,1993],{"class":105}," velocityDecay\n",[95,1995,1996,1999,2001,2003,2005,2007],{"class":97,"line":932},[95,1997,1998],{"class":105},"    velocityY",[95,2000,550],{"class":109},[95,2002,838],{"class":105},[95,2004,841],{"class":109},[95,2006,1933],{"class":101},[95,2008,1993],{"class":105},[95,2010,2012],{"class":97,"line":2011},16,[95,2013,929],{"class":109},[95,2015,2017],{"class":97,"line":2016},17,[95,2018,935],{"class":109},[10,2020,2021],{},"We project once before advecting the velocity and once after, so the field that moves the dye is always divergence free. Then each dye rides along on it and fades a little.",[10,2023,2024,2025,2028,2029,2032],{},"If you've read Stam's paper you'll notice I skipped the diffusion step entirely. That's on purpose. Bilinear sampling already blurs things a little every frame, and the decay multipliers slowly fade everything out. Together they get close enough to viscosity that a separate diffusion solve wasn't worth the extra CPU. The orange decays a bit faster than the green (",[46,2026,2027],{},"0.988"," vs ",[46,2030,2031],{},"0.994",") so your trail fades before the walker's does.",[63,2034,2036],{"id":2035},"rendering-cheaply","Rendering Cheaply",[10,2038,2039,2040,2043],{},"The grid is only 128×40, but the canvas is a lot bigger than that. Instead of drawing big rectangles per cell, we write the dye straight into an ",[46,2041,2042],{},"ImageData"," the same size as the grid, put it on a small offscreen canvas, and then let the browser scale it up:",[86,2045,2047],{"className":88,"code":2046,"language":90,"meta":91,"style":91},"gridContext.putImageData(image, 0, 0)\ncontext.imageSmoothingEnabled = true\ncontext.imageSmoothingQuality = 'high'\ncontext.drawImage(gridCanvas, 0, 0, canvas.value.width, canvas.value.height)\n",[46,2048,2049,2074,2089,2111],{"__ignoreMap":91},[95,2050,2051,2054,2056,2059,2061,2064,2066,2068,2070,2072],{"class":97,"line":98},[95,2052,2053],{"class":105},"gridContext",[95,2055,676],{"class":109},[95,2057,2058],{"class":160},"putImageData",[95,2060,178],{"class":109},[95,2062,2063],{"class":105},"image",[95,2065,284],{"class":109},[95,2067,409],{"class":119},[95,2069,284],{"class":109},[95,2071,409],{"class":119},[95,2073,152],{"class":109},[95,2075,2076,2079,2081,2084,2086],{"class":97,"line":127},[95,2077,2078],{"class":105},"context",[95,2080,676],{"class":109},[95,2082,2083],{"class":105},"imageSmoothingEnabled",[95,2085,110],{"class":109},[95,2087,2088],{"class":587}," true\n",[95,2090,2091,2093,2095,2098,2100,2104,2108],{"class":97,"line":155},[95,2092,2078],{"class":105},[95,2094,676],{"class":109},[95,2096,2097],{"class":105},"imageSmoothingQuality",[95,2099,110],{"class":109},[95,2101,2103],{"class":2102},"sNJcY"," '",[95,2105,2107],{"class":2106},"s7rlk","high",[95,2109,2110],{"class":2102},"'\n",[95,2112,2113,2115,2117,2120,2122,2125,2127,2129,2131,2133,2135,2138,2140,2143,2145,2147,2149,2151,2153,2155,2157,2159],{"class":97,"line":186},[95,2114,2078],{"class":105},[95,2116,676],{"class":109},[95,2118,2119],{"class":160},"drawImage",[95,2121,178],{"class":109},[95,2123,2124],{"class":105},"gridCanvas",[95,2126,284],{"class":109},[95,2128,409],{"class":119},[95,2130,284],{"class":109},[95,2132,409],{"class":119},[95,2134,284],{"class":109},[95,2136,2137],{"class":105}," canvas",[95,2139,676],{"class":109},[95,2141,2142],{"class":105},"value",[95,2144,676],{"class":109},[95,2146,997],{"class":105},[95,2148,284],{"class":109},[95,2150,2137],{"class":105},[95,2152,676],{"class":109},[95,2154,2142],{"class":105},[95,2156,676],{"class":109},[95,2158,145],{"class":105},[95,2160,152],{"class":109},[10,2162,2163],{},"The browser's smoothing when it upscales is what gives the ink that soft look. We're getting the nice part for free, which is my favorite kind of free.",[10,2165,2166],{},"Each pixel is a blend of three colors. Wherever neither dye is covering the cell, the ground color shows through:",[86,2168,2170],{"className":88,"code":2169,"language":90,"meta":91,"style":91},"const walkerInk = fluid.sample('walker', x, y)\nconst pointerInk = fluid.sample('pointer', x, y)\nconst bare = Math.max(0, 1 - walkerInk - pointerInk)\nconst offset = (x + width * y) * 4\nfor (let channel = 0; channel \u003C 3; channel++) {\n  pixels[offset + channel] =\n    groundRgb[channel] * bare + walkerRgb[channel] * walkerInk + pointerRgb[channel] * pointerInk\n}\npixels[offset + 3] = 255\n",[46,2171,2172,2208,2241,2274,2301,2334,2352,2399,2403],{"__ignoreMap":91},[95,2173,2174,2176,2179,2181,2184,2186,2189,2191,2194,2196,2198,2200,2202,2204,2206],{"class":97,"line":98},[95,2175,102],{"class":101},[95,2177,2178],{"class":105}," walkerInk",[95,2180,110],{"class":109},[95,2182,2183],{"class":105}," fluid",[95,2185,676],{"class":109},[95,2187,2188],{"class":160},"sample",[95,2190,178],{"class":109},[95,2192,2193],{"class":2102},"'",[95,2195,72],{"class":2106},[95,2197,2193],{"class":2102},[95,2199,284],{"class":109},[95,2201,295],{"class":105},[95,2203,284],{"class":109},[95,2205,287],{"class":105},[95,2207,152],{"class":109},[95,2209,2210,2212,2215,2217,2219,2221,2223,2225,2227,2229,2231,2233,2235,2237,2239],{"class":97,"line":127},[95,2211,102],{"class":101},[95,2213,2214],{"class":105}," pointerInk",[95,2216,110],{"class":109},[95,2218,2183],{"class":105},[95,2220,676],{"class":109},[95,2222,2188],{"class":160},[95,2224,178],{"class":109},[95,2226,2193],{"class":2102},[95,2228,76],{"class":2106},[95,2230,2193],{"class":2102},[95,2232,284],{"class":109},[95,2234,295],{"class":105},[95,2236,284],{"class":109},[95,2238,287],{"class":105},[95,2240,152],{"class":109},[95,2242,2243,2245,2248,2250,2252,2254,2256,2258,2260,2262,2264,2266,2268,2270,2272],{"class":97,"line":155},[95,2244,102],{"class":101},[95,2246,2247],{"class":105}," bare",[95,2249,110],{"class":109},[95,2251,673],{"class":105},[95,2253,676],{"class":109},[95,2255,979],{"class":160},[95,2257,178],{"class":109},[95,2259,447],{"class":119},[95,2261,284],{"class":109},[95,2263,424],{"class":119},[95,2265,601],{"class":101},[95,2267,2178],{"class":105},[95,2269,601],{"class":101},[95,2271,2214],{"class":105},[95,2273,152],{"class":109},[95,2275,2276,2278,2281,2283,2285,2287,2289,2291,2293,2295,2297,2299],{"class":97,"line":186},[95,2277,102],{"class":101},[95,2279,2280],{"class":105}," offset",[95,2282,110],{"class":109},[95,2284,142],{"class":109},[95,2286,281],{"class":105},[95,2288,116],{"class":101},[95,2290,113],{"class":105},[95,2292,139],{"class":101},[95,2294,287],{"class":105},[95,2296,290],{"class":109},[95,2298,139],{"class":101},[95,2300,1597],{"class":119},[95,2302,2303,2305,2307,2309,2312,2314,2316,2318,2320,2322,2324,2326,2328,2330,2332],{"class":97,"line":193},[95,2304,1386],{"class":587},[95,2306,142],{"class":109},[95,2308,593],{"class":101},[95,2310,2311],{"class":105}," channel",[95,2313,110],{"class":109},[95,2315,409],{"class":119},[95,2317,606],{"class":109},[95,2319,2311],{"class":105},[95,2321,726],{"class":109},[95,2323,529],{"class":119},[95,2325,606],{"class":109},[95,2327,2311],{"class":105},[95,2329,620],{"class":101},[95,2331,290],{"class":109},[95,2333,534],{"class":109},[95,2335,2336,2339,2341,2344,2346,2348,2350],{"class":97,"line":218},[95,2337,2338],{"class":105},"  pixels",[95,2340,550],{"class":109},[95,2342,2343],{"class":105},"offset",[95,2345,116],{"class":101},[95,2347,2311],{"class":105},[95,2349,841],{"class":109},[95,2351,1159],{"class":109},[95,2353,2354,2357,2359,2362,2364,2366,2368,2370,2373,2375,2377,2379,2381,2383,2385,2388,2390,2392,2394,2396],{"class":97,"line":241},[95,2355,2356],{"class":105},"    groundRgb",[95,2358,550],{"class":109},[95,2360,2361],{"class":105},"channel",[95,2363,841],{"class":109},[95,2365,139],{"class":101},[95,2367,2247],{"class":105},[95,2369,116],{"class":101},[95,2371,2372],{"class":105}," walkerRgb",[95,2374,550],{"class":109},[95,2376,2361],{"class":105},[95,2378,841],{"class":109},[95,2380,139],{"class":101},[95,2382,2178],{"class":105},[95,2384,116],{"class":101},[95,2386,2387],{"class":105}," pointerRgb",[95,2389,550],{"class":109},[95,2391,2361],{"class":105},[95,2393,841],{"class":109},[95,2395,139],{"class":101},[95,2397,2398],{"class":105}," pointerInk\n",[95,2400,2401],{"class":97,"line":264},[95,2402,935],{"class":109},[95,2404,2405,2408,2410,2412,2414,2416,2418,2420],{"class":97,"line":269},[95,2406,2407],{"class":105},"pixels",[95,2409,550],{"class":109},[95,2411,2343],{"class":105},[95,2413,116],{"class":101},[95,2415,529],{"class":119},[95,2417,841],{"class":109},[95,2419,110],{"class":109},[95,2421,2422],{"class":119}," 255\n",[10,2424,2425,2426,2429,2430,2432,2433,2436],{},"Same RGBA rules as last time: the data is a ",[46,2427,2428],{},"Uint8ClampedArray",", so ",[46,2431,2343],{}," jumps four values per pixel, and anything over ",[46,2434,2435],{},"255"," just gets clamped so we don't have to worry about it.",[63,2438,2440],{"id":2439},"making-it-polite","Making It Polite",[10,2442,2443],{},"A hero animation that pins a CPU core while you're reading a post three screens down is not polite. So the component does a few things to stay out of the way:",[23,2445,2446,2460,2474,2484],{},[26,2447,2448,2451,2452,2455,2456,2459],{},[1363,2449,2450],{},"Pause when nobody is looking."," An ",[46,2453,2454],{},"IntersectionObserver"," tracks whether the canvas is on screen, and ",[46,2457,2458],{},"document.hidden"," covers background tabs. When either is false the loop keeps scheduling frames but skips the work.",[26,2461,2462,2465,2466,2469,2470,2473],{},[1363,2463,2464],{},"Respect reduced motion."," If ",[46,2467,2468],{},"prefers-reduced-motion"," is set, we run ",[46,2471,2472],{},"SETTLE_STEPS"," (300) updates up front, draw one still frame, and never start the loop. You still get the picture, it just doesn't move.",[26,2475,2476,2479,2480,2483],{},[1363,2477,2478],{},"Don't break scrolling on phones."," ",[46,2481,2482],{},"touch-action: pan-y"," lets vertical swipes scroll the page instead of getting swallowed by the canvas.",[26,2485,2486,2489,2490,2493,2494,676],{},[1363,2487,2488],{},"Stay SSR safe."," All the canvas work happens in ",[46,2491,2492],{},"onMounted",", so Nuxt can render the page on the server without needing ",[46,2495,2496],{},"\u003CClientOnly>",[10,2498,2499,2500,2503,2504,2507],{},"I also kept the solver completely free of Vue and the DOM. ",[46,2501,2502],{},"lib\u002FstableFluids.js"," just takes numbers in and gives numbers back. The ",[46,2505,2506],{},"FluidCanvas"," component owns the canvas, the input and the lifecycle, and that's it. It's the same humble component idea I keep coming back to: the logic can be tested and reused on its own, and the component stays small and boring.",[63,2509,2511],{"id":2510},"interaction","Interaction",[10,2513,2514],{},"There are two sources of ink.",[10,2516,2517],{},"The random walker from the old post is still in there. Every frame it nudges its heading a little, steps forward, and splats green ink in the direction it's facing:",[86,2519,2521],{"className":88,"code":2520,"language":90,"meta":91,"style":91},"const drift = (Math.random() - 0.5) * 0.5\nconst pullToCenter = (walker.y - height \u002F 2) * 0.004\nwalker.heading += drift - pullToCenter\nwalker.x += Math.cos(walker.heading) * WALKER_SPEED\nwalker.y += Math.sin(walker.heading) * WALKER_SPEED\n",[46,2522,2523,2556,2588,2606,2638],{"__ignoreMap":91},[95,2524,2525,2527,2530,2532,2534,2537,2539,2542,2545,2547,2549,2551,2553],{"class":97,"line":98},[95,2526,102],{"class":101},[95,2528,2529],{"class":105}," drift",[95,2531,110],{"class":109},[95,2533,142],{"class":109},[95,2535,2536],{"class":105},"Math",[95,2538,676],{"class":109},[95,2540,2541],{"class":160},"random",[95,2543,2544],{"class":109},"()",[95,2546,601],{"class":101},[95,2548,519],{"class":119},[95,2550,290],{"class":109},[95,2552,139],{"class":101},[95,2554,2555],{"class":119}," 0.5\n",[95,2557,2558,2560,2563,2565,2567,2569,2571,2573,2575,2577,2579,2581,2583,2585],{"class":97,"line":127},[95,2559,102],{"class":101},[95,2561,2562],{"class":105}," pullToCenter",[95,2564,110],{"class":109},[95,2566,142],{"class":109},[95,2568,72],{"class":105},[95,2570,676],{"class":109},[95,2572,314],{"class":105},[95,2574,601],{"class":101},[95,2576,755],{"class":105},[95,2578,579],{"class":101},[95,2580,120],{"class":119},[95,2582,290],{"class":109},[95,2584,139],{"class":101},[95,2586,2587],{"class":119}," 0.004\n",[95,2589,2590,2592,2594,2597,2599,2601,2603],{"class":97,"line":155},[95,2591,72],{"class":105},[95,2593,676],{"class":109},[95,2595,2596],{"class":105},"heading",[95,2598,844],{"class":101},[95,2600,2529],{"class":105},[95,2602,601],{"class":101},[95,2604,2605],{"class":105}," pullToCenter\n",[95,2607,2608,2610,2612,2614,2616,2618,2620,2623,2625,2627,2629,2631,2633,2635],{"class":97,"line":186},[95,2609,72],{"class":105},[95,2611,676],{"class":109},[95,2613,281],{"class":105},[95,2615,844],{"class":101},[95,2617,673],{"class":105},[95,2619,676],{"class":109},[95,2621,2622],{"class":160},"cos",[95,2624,178],{"class":109},[95,2626,72],{"class":105},[95,2628,676],{"class":109},[95,2630,2596],{"class":105},[95,2632,290],{"class":109},[95,2634,139],{"class":101},[95,2636,2637],{"class":105}," WALKER_SPEED\n",[95,2639,2640,2642,2644,2646,2648,2650,2652,2655,2657,2659,2661,2663,2665,2667],{"class":97,"line":193},[95,2641,72],{"class":105},[95,2643,676],{"class":109},[95,2645,314],{"class":105},[95,2647,844],{"class":101},[95,2649,673],{"class":105},[95,2651,676],{"class":109},[95,2653,2654],{"class":160},"sin",[95,2656,178],{"class":109},[95,2658,72],{"class":105},[95,2660,676],{"class":109},[95,2662,2596],{"class":105},[95,2664,290],{"class":109},[95,2666,139],{"class":101},[95,2668,2637],{"class":105},[10,2670,2671,2674,2675,2678],{},[46,2672,2673],{},"drift"," is the random part, and ",[46,2676,2677],{},"pullToCenter"," gets stronger the further the walker strays from the middle so it doesn't hug the edges. Once it walks off the right side it respawns on the left. It keeps the whole thing alive when nobody is touching it.",[10,2680,2681,2682,2685],{},"The pointer is the second source. On ",[46,2683,2684],{},"pointermove"," we convert the position into grid coordinates and remember how far it moved since the last event:",[86,2687,2689],{"className":88,"code":2688,"language":90,"meta":91,"style":91},"pointer = {\n  x, y,\n  deltaX: pointer ? x - pointer.x : 0,\n  deltaY: pointer ? y - pointer.y : 0,\n  isFresh: true,\n}\n",[46,2690,2691,2699,2711,2740,2767,2779],{"__ignoreMap":91},[95,2692,2693,2695,2697],{"class":97,"line":98},[95,2694,76],{"class":105},[95,2696,110],{"class":109},[95,2698,534],{"class":109},[95,2700,2701,2704,2706,2708],{"class":97,"line":127},[95,2702,2703],{"class":105},"  x",[95,2705,284],{"class":109},[95,2707,287],{"class":105},[95,2709,2710],{"class":109},",\n",[95,2712,2713,2716,2718,2720,2723,2725,2727,2729,2731,2733,2736,2738],{"class":97,"line":155},[95,2714,2715],{"class":355},"  deltaX",[95,2717,84],{"class":109},[95,2719,366],{"class":105},[95,2721,2722],{"class":101}," ?",[95,2724,295],{"class":105},[95,2726,601],{"class":101},[95,2728,366],{"class":105},[95,2730,676],{"class":109},[95,2732,281],{"class":105},[95,2734,2735],{"class":101}," :",[95,2737,409],{"class":119},[95,2739,2710],{"class":109},[95,2741,2742,2745,2747,2749,2751,2753,2755,2757,2759,2761,2763,2765],{"class":97,"line":186},[95,2743,2744],{"class":355},"  deltaY",[95,2746,84],{"class":109},[95,2748,366],{"class":105},[95,2750,2722],{"class":101},[95,2752,287],{"class":105},[95,2754,601],{"class":101},[95,2756,366],{"class":105},[95,2758,676],{"class":109},[95,2760,314],{"class":105},[95,2762,2735],{"class":101},[95,2764,409],{"class":119},[95,2766,2710],{"class":109},[95,2768,2769,2772,2774,2777],{"class":97,"line":193},[95,2770,2771],{"class":355},"  isFresh",[95,2773,84],{"class":109},[95,2775,2776],{"class":587}," true",[95,2778,2710],{"class":109},[95,2780,2781],{"class":97,"line":218},[95,2782,935],{"class":109},[10,2784,2785,2786,2789,2790,2793],{},"On the next update that delta gets multiplied by ",[46,2787,2788],{},"POINTER_FORCE"," and becomes velocity, so a fast flick throws the ink harder than a slow drag. The ",[46,2791,2792],{},"isFresh"," flag makes sure we only splat once per move event, otherwise holding the mouse still would keep pumping ink into the same spot.",[63,2795,2797],{"id":2796},"numbers","Numbers",[10,2799,2800,2801,2803],{},"I benchmarked ",[46,2802,1661],{}," on its own in Node 20 on an Apple M1, averaged over 2,000 steps:",[2805,2806,2807,2823],"table",{},[2808,2809,2810],"thead",{},[2811,2812,2813,2817,2820],"tr",{},[2814,2815,2816],"th",{},"Grid",[2814,2818,2819],{},"Cells",[2814,2821,2822],{},"Time per step",[2824,2825,2826,2838,2849,2860],"tbody",{},[2811,2827,2828,2832,2835],{},[2829,2830,2831],"td",{},"64×20",[2829,2833,2834],{},"1,280",[2829,2836,2837],{},"~0.5ms",[2811,2839,2840,2843,2846],{},[2829,2841,2842],{},"128×40",[2829,2844,2845],{},"5,120",[2829,2847,2848],{},"~2.1ms",[2811,2850,2851,2854,2857],{},[2829,2852,2853],{},"192×60",[2829,2855,2856],{},"11,520",[2829,2858,2859],{},"~4.8ms",[2811,2861,2862,2865,2868],{},[2829,2863,2864],{},"256×80",[2829,2866,2867],{},"20,480",[2829,2869,2870],{},"~8.4ms",[10,2872,2873],{},"The cost scales pretty much linearly with the cell count, which makes sense since every step is a handful of passes over the grid. At the default 128×40 the solver uses roughly 2ms of a 16.7ms frame, which leaves plenty of room for the render and everything else on the page. Somewhere past 256×80 you start eating into the frame budget on a fast machine, and a lot sooner on a slow phone. Try the grid sizes in the demo above and you can feel it.",[63,2875,2877],{"id":2876},"where-to-go-next","Where To Go Next",[10,2879,2880],{},"The obvious next step is WebGL. The same three steps map really nicely onto fragment shaders, one pass each, and you can run them at full screen resolution instead of a tiny grid. That's also where you'd add vorticity confinement, which puts back some of the small swirls that the semi-Lagrangian advection smooths away.",[10,2882,2883],{},"But for a hero on a personal blog, a 2D canvas and 150 lines turned out to be plenty.",[63,2885,2887],{"id":2886},"conclusion","Conclusion",[10,2889,2890],{},"This was one of those projects where the math looked scarier than it was. Once I understood that every frame is just add forces, advect and project, the whole thing clicked. And the \"go backwards\" trick in advection is one of those ideas that's so simple you wonder why nobody thought of it sooner.",[23,2892,2893,2900,2907,2915],{},[26,2894,2895],{},[14,2896,2899],{"href":2897,"rel":2898},"https:\u002F\u002Fgithub.com\u002Futroda\u002Fpersonal-site\u002Fblob\u002Fmain\u002Flib\u002FstableFluids.js",[42],"Solver source",[26,2901,2902],{},[14,2903,2906],{"href":2904,"rel":2905},"https:\u002F\u002Fgithub.com\u002Futroda\u002Fpersonal-site\u002Fblob\u002Fmain\u002Fcomponents\u002Fcontent\u002FFluidCanvas.vue",[42],"Component source",[26,2908,2909,2910,2914],{},"Jos Stam, ",[14,2911,2913],{"href":40,"rel":2912},[42],"\"Stable Fluids\"",", SIGGRAPH 1999",[26,2916,2909,2917,2922],{},[14,2918,2921],{"href":2919,"rel":2920},"https:\u002F\u002Fwww.dgp.toronto.edu\u002Fpublic_user\u002Fstam\u002Freality\u002FResearch\u002Fpdf\u002FGDC03.pdf",[42],"\"Real-Time Fluid Dynamics for Games\"",", GDC 2003",[2924,2925,2926],"style",{},"html pre.shiki code .s_wWq, html code.shiki .s_wWq{--shiki-default:#CB7676}html pre.shiki code .st-jp, html code.shiki .st-jp{--shiki-default:#BD976A}html pre.shiki code .s_pn2, html code.shiki .s_pn2{--shiki-default:#666666}html pre.shiki code .sxA9i, html code.shiki .sxA9i{--shiki-default:#4C9A91}html pre.shiki code .sux-A, html code.shiki .sux-A{--shiki-default:#758575DD}html pre.shiki code .sCK9x, html code.shiki .sCK9x{--shiki-default:#80A665}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .sm68I, html code.shiki .sm68I{--shiki-default:#B8A965}html pre.shiki code .s3QIE, html code.shiki .s3QIE{--shiki-default:#4D9375}html pre.shiki code .sNJcY, html code.shiki .sNJcY{--shiki-default:#C98A7D77}html pre.shiki code .s7rlk, html code.shiki .s7rlk{--shiki-default:#C98A7D}",{"title":91,"searchDepth":127,"depth":127,"links":2928},[2929,2932,2938,2939,2940,2941,2942,2943],{"id":65,"depth":127,"text":66,"children":2930},[2931],{"id":384,"depth":155,"text":385},{"id":459,"depth":127,"text":460,"children":2933},[2934,2935,2936,2937],{"id":466,"depth":155,"text":467},{"id":951,"depth":155,"text":952},{"id":1342,"depth":155,"text":1343},{"id":1665,"depth":155,"text":1666},{"id":2035,"depth":127,"text":2036},{"id":2439,"depth":127,"text":2440},{"id":2510,"depth":127,"text":2511},{"id":2796,"depth":127,"text":2797},{"id":2876,"depth":127,"text":2877},{"id":2886,"depth":127,"text":2887},"A pointer-reactive ink simulation in plain JavaScript and a 2D canvas, built on Jos Stam's stable fluids.",false,"md",{},"\u002Fposts\u002Fstable-fluids-in-150-lines","2026-03-15",{"title":5,"description":2944},"posts\u002Fstable-fluids-in-150-lines",[2953,2954,2955,2956],"Experiment","Canvas","Performance","Vue",null,"FIR_uWJat1-8DBSqwoqQAU1MFtba6MG63VA--2eb2yM",[2960,2964],{"title":2961,"path":2962,"stem":2963,"children":-1},"The Architecture Behind a Five-Month Frontend Rewrite","\u002Fposts\u002Farchitecture-behind-a-five-month-rewrite","posts\u002Farchitecture-behind-a-five-month-rewrite",{"title":2965,"path":2966,"stem":2967,"children":-1},"Building an Enterprise SaaS MVP With a Team of Two","\u002Fposts\u002Fenterprise-saas-mvp-team-of-two","posts\u002Fenterprise-saas-mvp-team-of-two",1795972260582,"cf264a42-3819-49c9-96b2-f127808b3650"]