Advertisement
here2share

Gray-Scott Reaction-Diffusion

Dec 16th, 2022
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>Gray-Scott Reaction-Diffusion Automaton</title>
  5.   <style>
  6.     body {
  7.       margin: 0;
  8.       padding: 0;
  9.     }
  10.  
  11.     canvas {
  12.       width: 100%;
  13.       height: 100%;
  14.     }
  15.   </style>
  16. </head>
  17. <body>
  18.   <canvas id="canvas"></canvas>
  19.   <div id="controls">
  20.     <label for="f-slider">f:</label>
  21.     <input type="range" min="0" max="1" step="0.01" value="0.025" id="f-slider">
  22.     <label for="k-slider">k:</label>
  23.     <input type="range" min="0" max="1" step="0.01" value="0.06" id="k-slider">
  24.     <button id="reset-button">Reset</button>
  25.   </div>
  26.   <script>
  27.     const canvas = document.getElementById('canvas');
  28.     const ctx = canvas.getContext('2d');
  29.     const width = canvas.width;
  30.     const height = canvas.height;
  31.     const fSlider = document.getElementById('f-slider');
  32.     const kSlider = document.getElementById('k-slider');
  33.     const resetButton = document.getElementById('reset-button');
  34.  
  35.     let f = 0.025;
  36.     let k = 0.06;
  37.     let u = new Float32Array(width * height);
  38.     let v = new Float32Array(width * height);
  39.  
  40.     for (let i = 0; i < width; i++) {
  41.       for (let j = 0; j < height; j++) {
  42.         const index = i + j * width;
  43.         u[index] = 1;
  44.         v[index] = 0;
  45.       }
  46.     }
  47.  
  48.     function update() {
  49.       for (let i = 1; i < width - 1; i++) {
  50.         for (let j = 1; j < height - 1; j++) {
  51.           const index = i + j * width;
  52.           const uc = u[index];
  53.           const vc = v[index];
  54.           const uNorth = u[index - width];
  55.           const uSouth = u[index + width];
  56.           const uEast = u[index + 1];
  57.           const uWest = u[index - 1];
  58.           const vNorth = v[index - width];
  59.           const vSouth = v[index + width];
  60.           const vEast = v[index + 1];
  61.           const vWest = v[index - 1];
  62.           const uDiff = uEast + uWest + uNorth + uSouth - 4 * uc;
  63.           const vDiff = vEast + vWest + vNorth + vSouth - 4 * vc;
  64.           u[index] += f * (uc - uc * uc - vc * uc) + uDiff;
  65.           v[index] += f * (vc - uc * vc - vc * vc) + vDiff;
  66.         }
  67.       }
  68.  
  69.     function render() {
  70.       for (let i = 0; i < width; i++) {
  71.         for (let j = 0; j < height; j++) {
  72.           const index = i + j * width;
  73.           const uValue = u[index];
  74.           const vValue = v[index];
  75.           const r = Math.floor((uValue - vValue + 1) * 255 / 2);
  76.           const g = Math.floor((uValue + vValue) * 255 / 2);
  77.           const b = Math.floor((uValue + vValue + 1) * 255 / 2);
  78.           ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
  79.           ctx.fillRect(i, j, 1, 1);
  80.         }
  81.       }
  82.     }
  83.  
  84.     function loop() {
  85.       update();
  86.       render();
  87.       requestAnimationFrame(loop);
  88.     }
  89.  
  90.     canvas.addEventListener('mousemove', (event) => {
  91.       const x = event.offsetX;
  92.       const y = event.offsetY;
  93.       const index = x + y * width;
  94.       u[index] = 1;
  95.       v[index] = 0;
  96.     });
  97.  
  98.     fSlider.addEventListener('input', (event) => {
  99.       f = event.target.value;
  100.     });
  101.  
  102.     kSlider.addEventListener('input', (event) => {
  103.       k = event.target.value;
  104.     });
  105.  
  106.     resetButton.addEventListener('click', () => {
  107.       for (let i = 0; i < width; i++) {
  108.         for (let j = 0; j < height; j++) {
  109.           const index = i + j * width;
  110.           u[index] = 1;
  111.           v[index] = 0;
  112.         }
  113.       }
  114.     });
  115.  
  116.     loop();
  117.   </script>
  118. </body>
  119. </html>
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement