Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="canvas-container">
  <canvas id="canvas"></canvas>
</div>
<div class="controls">
  <fieldset>
    <legend>Rain</legend>

    <input type="radio" name="rain" id="no-rain" value="0" />
    <label for="no-rain">☁️</label>

    <input type="radio" name="rain" id="medium-rain" value="1" checked />
    <label for="medium-rain">🌧️</label>
  </fieldset>

  <fieldset>
    <legend>Evaporate</legend>

    <input type="radio" name="evaporation" id="slow-evaporation" value="1" checked />
    <label for="slow-evaporation">🌤️</label>

    <input type="radio" name="evaporation" id="fast-evaporation" value="2" />
    <label for="fast-evaporation">☀️</label>
  </fieldset>
</div>
              
            
!

CSS

              
                body {
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: #111;
  color: #eee;
  font: normal 16px/1 "Operator Mono", menlo, monaco, monospace;
}

.canvas-container {
  position: relative;
  width: clamp(200px, 800px, 80vmin);
  height: clamp(200px, 800px, 80vmin);
}

canvas {
  position: absolute;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  border-radius: 2em;
  box-shadow: 0 .5em 1em #000a;
  cursor: pointer;
}

.controls {
  display: flex;
  gap: 1em;
}

.controls fieldset {
  border-radius: 0.5em;
  text-align: center;
}

.controls label {
  display: inline-block;
  font-size: 1.5em;
  padding: 0.2em;
  cursor: pointer;
}

.controls input {
  appearance: none;
  opacity: 0;
  position: absolute;
}

.controls input:checked + label {
  border-radius: 50%;
  box-shadow: 0 0 0 2px #ff0a;
}

.controls input:focus + label {
  background: #ff03;
}
              
            
!

JS

              
                const { abs, min, sin, sqrt, pow, PI: π } = Math
const query = document.querySelector.bind(document)
const queryAll = document.querySelectorAll.bind(document)
const canvas = query('canvas')
const width = 130
const height = 130
const scale = 1.6
const surfaceScale = 10
const detailScale = 20
canvas.width = width
canvas.height = height
const ctx = canvas.getContext('2d')
const noop =_=>_
const random = (max = 1) => Math.random() * max
const getArray = (length, mapper) => Array.from(Array(length), mapper || noop)
const calcDistance = (p1, p2) => sqrt(pow(abs(p1.x - p2.x), 2) + pow(abs(p1.y - p2.y), 2))

const tickFPS = 60
const wetness = 0.015
const erosionBase = 0.002

const controls = {
  rain: 1,
  evaporation: 1
}

const generateLand = (width, height) => {
  noise.seed(random())
  const xScale = width / scale
  const yScale = height / scale
  const xSurfaceScale = width / surfaceScale
  const ySurfaceScale = height / surfaceScale
  const xDetailScale = width / detailScale
  const yDetailScale = height / detailScale
  return getArray(height, (_, y) => {
    return getArray(width, (_, x) => {
      const base = noise.simplex2(x / xScale, y / yScale)
      const surface = noise.simplex2(x / xSurfaceScale, y / ySurfaceScale)
      const detail = noise.simplex2(x / xDetailScale, y / yDetailScale)
      return base + surface / 20 + detail / 30
    })
  })
}

const generateCells = (width, height) => {
  return getArray(height, (_, y) => {
    return getArray(width, (_, x) => 0)
  })
}

const resetState = () => {
  return {
    isRaining: false,
    water: [],
    land: generateLand(width, height),
    wetland: generateCells(width, height),
    vegetation: generateCells(width, height),
    frame: 0
  }
}

let state = resetState()

const rain = () => {
  const amount = 80
  state.isRaining = controls.rain === 1 && (state.frame === 0 || random() > .8)
  if (state.isRaining) {
    if (controls.rain === 1 && random() > .5) {
      state.isRaining = false
    } else {
      for (let i = 0; i < amount; i++) {
        const x = parseInt(random(width))
        const y = parseInt(random(height))
        state.water.push({ x, y })
      }
    }
  }
}

const evaporate = () => {
  const waterToKeep = state.water.length / (controls.evaporation === 1 ? 1.02 : 1.5)
  const toSlice = state.water.length - waterToKeep
  state.water = state.water.slice(-waterToKeep)
  if (state.water.length === 1) {
    state.water = []
  }
}

const dry = () => {
  const speed = controls.evaporation === 1 ? 1.0001 : 1.005
  state.wetland.forEach((row, y) => {
    row.forEach((cell, x) => {
      if (!(cell > 0)) {
        return
      }
      const water = state.water.find(water => {
        return x === water.x &&  y === water.y
      })
      if (!water && cell > 0.2) {
        state.wetland[y][x] /= speed
      }
    })
  })
}

const waterflow = () => {
  state.water.forEach(water => {
    if (water.y < 0 || water.y > height - 1) {
      return
    }
    const selfRow = state.land[water.y]
    const aboveRow = state.land[water.y - 1] || selfRow
    const belowRow = state.land[water.y + 1] || selfRow
    const flowMap = [{
      y: -1, x: -1,
      height: aboveRow[water.x - 1] || 1
    }, {
      y: -1, x: 0,
      height: aboveRow[water.x]
    }, {
      y: -1, x: 1,
      height: aboveRow[water.x + 1] || 1
    }, {
      y: 0, x: -1,
      height: selfRow[water.x - 1] || 1
    }, {
      y: 0, x: 0,
      height: selfRow[water.x]
    }, {
      y: 0, x: 1,
      height: selfRow[water.x + 1] || 1
    }, {
      y: 1, x: -1,
      height: belowRow[water.x - 1] || 1
    }, {
      y: 1, x: 0,
      height: belowRow[water.x]
    }, {
      y: 1, x: 1,
      height: belowRow[water.x + 1] || 1
    }]
    const nextFlow = flowMap.sort((cellA, cellB) => cellA.height - cellB.height)[0]
    const blockedByOtherWater = state.water.find(({ x, y }) => {
      const sameX = x === water.x + nextFlow.x
      const sameY = y === water.y + nextFlow.y
      return sameX && sameY
    })
    if (blockedByOtherWater) {
      water.isFlowing = false
      return
    }
    water.isFlowing = true
    state.wetland[water.y][water.x] += (state.wetland[water.y][water.x] < 1 ? wetness : 0)

    const currentHeight = state.land[water.y][water.x]
    const nextHeight = state.land[water.y + nextFlow.y] ? state.land[water.y + nextFlow.y][water.x + nextFlow.x] : currentHeight
    const heightDiff = abs(currentHeight - nextHeight)
    let erosion = erosionBase * heightDiff
    state.land[water.y][water.x] -= (state.land[water.y][water.x] > 0 ? erosion : 0)
    if (state.land[water.y - 1]) {
      state.land[water.y - 1][water.x - 1] -= (state.land[water.y - 1][water.x - 1] > 0 ? erosion/2 : 0)
      state.land[water.y - 1][water.x - 2] -= (state.land[water.y - 1][water.x - 2] > 0 ? erosion/4 : 0)
      state.land[water.y - 1][water.x + 1] -= (state.land[water.y - 1][water.x + 1] > 0 ? erosion/2 : 0)
      state.land[water.y - 1][water.x + 2] -= (state.land[water.y - 1][water.x + 2] > 0 ? erosion/4 : 0)
    }
    if (state.land[water.y + 1]) {
      state.land[water.y + 1][water.x - 1] -= (state.land[water.y + 1][water.x - 1] > 0 ? erosion/2 : 0)
      state.land[water.y + 1][water.x - 2] -= (state.land[water.y + 1][water.x - 2] > 0 ? erosion/4 : 0)
      state.land[water.y + 1][water.x + 1] -= (state.land[water.y + 1][water.x + 1] > 0 ? erosion/2 : 0)
      state.land[water.y + 1][water.x + 2] -= (state.land[water.y + 1][water.x + 2] > 0 ? erosion/4 : 0)
    }
    state.land[water.y][water.x - 1] -= (state.land[water.y][water.x - 1] > 0 ? erosion/1.8 : 0)
    state.land[water.y][water.x - 2] -= (state.land[water.y][water.x - 2] > 0 ? erosion/3.5 : 0)
    state.land[water.y][water.x + 1] -= (state.land[water.y][water.x + 1] > 0 ? erosion/1.8 : 0)
    state.land[water.y][water.x + 2] -= (state.land[water.y][water.x + 2] > 0 ? erosion/3.5 : 0)
    const vSpread0 = [0.0002, 0.00005, 0.00001]
    const vSpread1 = [0.0001, 0.000025, 0.000005]
    const vSpread2 = [0.00005, 0.0000125, 0.0000025]
    state.vegetation[water.y][water.x - 1] += (state.vegetation[water.y][water.x - 1] < 1 ? vSpread0[0] : 0)
    state.vegetation[water.y][water.x - 2] += (state.vegetation[water.y][water.x - 2] < 1 ? vSpread0[1] : 0)
    state.vegetation[water.y][water.x - 3] += (state.vegetation[water.y][water.x - 3] < 1 ? vSpread0[2] : 0)
    state.vegetation[water.y][water.x + 1] += (state.vegetation[water.y][water.x + 1] < 1 ? vSpread0[0] : 0)
    state.vegetation[water.y][water.x + 2] += (state.vegetation[water.y][water.x + 2] < 1 ? vSpread0[1] : 0)
    state.vegetation[water.y][water.x + 3] += (state.vegetation[water.y][water.x + 3] < 1 ? vSpread0[2] : 0)
    if (state.vegetation[water.y - 1]) {
      state.vegetation[water.y - 1][water.x - 1] += (state.vegetation[water.y - 1][water.x - 1] < 1 ? vSpread1[0] : 0)
      state.vegetation[water.y - 1][water.x - 2] += (state.vegetation[water.y - 1][water.x - 2] < 1 ? vSpread1[1] : 0)
      state.vegetation[water.y - 1][water.x - 3] += (state.vegetation[water.y - 1][water.x - 3] < 1 ? vSpread1[2] : 0)
      state.vegetation[water.y - 1][water.x + 1] += (state.vegetation[water.y - 1][water.x + 1] < 1 ? vSpread1[0] : 0)
      state.vegetation[water.y - 1][water.x + 2] += (state.vegetation[water.y - 1][water.x + 2] < 1 ? vSpread1[1] : 0)
      state.vegetation[water.y - 1][water.x + 3] += (state.vegetation[water.y - 1][water.x + 3] < 1 ? vSpread1[2] : 0)
    }
    if (state.vegetation[water.y - 2]) {
      state.vegetation[water.y - 2][water.x - 1] += (state.vegetation[water.y - 2][water.x - 1] < 1 ? vSpread2[0] : 0)
      state.vegetation[water.y - 2][water.x - 2] += (state.vegetation[water.y - 2][water.x - 2] < 1 ? vSpread2[1] : 0)
      state.vegetation[water.y - 2][water.x - 3] += (state.vegetation[water.y - 2][water.x - 3] < 1 ? vSpread2[2] : 0)
      state.vegetation[water.y - 2][water.x + 1] += (state.vegetation[water.y - 2][water.x + 1] < 1 ? vSpread2[0] : 0)
      state.vegetation[water.y - 2][water.x + 2] += (state.vegetation[water.y - 2][water.x + 2] < 1 ? vSpread2[1] : 0)
      state.vegetation[water.y - 2][water.x + 3] += (state.vegetation[water.y - 2][water.x + 3] < 1 ? vSpread2[2] : 0)
    }
    if (state.vegetation[water.y + 1]) {
      state.vegetation[water.y + 1][water.x - 1] += (state.vegetation[water.y + 1][water.x - 1] < 1 ? vSpread1[0] : 0)
      state.vegetation[water.y + 1][water.x - 2] += (state.vegetation[water.y + 1][water.x - 2] < 1 ? vSpread1[1] : 0)
      state.vegetation[water.y + 1][water.x - 3] += (state.vegetation[water.y + 1][water.x - 3] < 1 ? vSpread1[2] : 0)
      state.vegetation[water.y + 1][water.x + 1] += (state.vegetation[water.y + 1][water.x + 1] < 1 ? vSpread1[0] : 0)
      state.vegetation[water.y + 1][water.x + 2] += (state.vegetation[water.y + 1][water.x + 2] < 1 ? vSpread1[1] : 0)
      state.vegetation[water.y + 1][water.x + 3] += (state.vegetation[water.y + 1][water.x + 3] < 1 ? vSpread1[2] : 0)
    }
    if (state.vegetation[water.y + 2]) {
      state.vegetation[water.y + 2][water.x - 1] += (state.vegetation[water.y + 2][water.x - 1] < 1 ? vSpread2[0] : 0)
      state.vegetation[water.y + 2][water.x - 2] += (state.vegetation[water.y + 2][water.x - 2] < 1 ? vSpread2[1] : 0)
      state.vegetation[water.y + 2][water.x - 3] += (state.vegetation[water.y + 2][water.x - 3] < 1 ? vSpread2[2] : 0)
      state.vegetation[water.y + 2][water.x + 1] += (state.vegetation[water.y + 2][water.x + 1] < 1 ? vSpread2[0] : 0)
      state.vegetation[water.y + 2][water.x + 2] += (state.vegetation[water.y + 2][water.x + 2] < 1 ? vSpread2[1] : 0)
      state.vegetation[water.y + 2][water.x + 3] += (state.vegetation[water.y + 2][water.x + 3] < 1 ? vSpread2[2] : 0)
    }
    if (state.vegetation[water.y][water.x - 1] > 0.25 && state.vegetation[water.y][water.x + 1] > 0.25) {
      water.isInLake = true
    }
    water.x += nextFlow.x
    water.y += nextFlow.y
  })
}

const vegetate = () => {
  state.vegetation.forEach((row, y) => {
    row.forEach((cell, x) => {
      let waterNearby = false
      let growth = 0
      for (let i = 0; i < state.water.length - 1; i++) {
        const water = state.water[i]
        const distance = calcDistance(water, { x, y })
        if (distance < 3) {
          waterNearby = water
          growth = ((1 - cell) / 30_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 6) {
          waterNearby = water
          growth = ((1 - cell) / 90_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 9) {
          waterNearby = water
          growth = ((1 - cell) / 150_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 12) {
          waterNearby = water
          growth = ((1 - cell) / 270_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 15) {
          waterNearby = water
          growth = ((1 - cell) / 510_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 18) {
          waterNearby = water
          growth = ((1 - cell) / 990_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 20) {
          waterNearby = water
          growth = ((1 - cell) / 1_950_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 21) {
          waterNearby = water
          growth = ((1 - cell) / 3_830_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 25) {
          waterNearby = water
          growth = ((1 - cell) / 7_590_000) * (1 - state.land[y][x])
          break
        }
        if (distance < 30) {
          waterNearby = water
          growth = ((1 - cell) / 15_110_000) * (1 - state.land[y][x])
          break
        }
      }
      if (!waterNearby) {
        if (cell > 0.3) {
          state.vegetation[y][x] -= 0.01
        } else if (cell >= 0.001) {
          state.vegetation[y][x] -= 0.001
        } else {
          state.vegetation[y][x] = 0
        }
        return
      }
      if (!(cell > 0)) {
        return
      }
      state.vegetation[y][x] += (state.vegetation[y][x] < 1 ? growth : 0)
      state.vegetation[y][x + 1] += (state.vegetation[y][x + 1] < 1 ? growth : 0)
      state.vegetation[y][x - 1] += (state.vegetation[y][x - 1] < 1 ? growth : 0)
      state.vegetation[y][x + 2] += (state.vegetation[y][x + 1] < 1 ? growth / 2 : 0)
      state.vegetation[y][x - 2] += (state.vegetation[y][x - 1] < 1 ? growth / 2 : 0)
      state.vegetation[y][x + 3] += (state.vegetation[y][x + 1] < 1 ? growth / 3 : 0)
      state.vegetation[y][x - 3] += (state.vegetation[y][x - 1] < 1 ? growth / 3 : 0)
      if (state.vegetation[y + 1]) {
        state.vegetation[y + 1][x] += (state.vegetation[y + 1][x] < 1 ? growth : 0)
        state.vegetation[y + 1][x - 1] += (state.vegetation[y + 1][x - 1] < 1 ? growth / 2 : 0)
        state.vegetation[y + 1][x + 1] += (state.vegetation[y + 1][x + 1] < 1 ? growth / 2 : 0)
        state.vegetation[y + 1][x - 2] += (state.vegetation[y + 1][x - 2] < 1 ? growth / 3 : 0)
        state.vegetation[y + 1][x + 2] += (state.vegetation[y + 1][x + 2] < 1 ? growth / 3 : 0)
      }
      if (state.vegetation[y - 1]) {
        state.vegetation[y - 1][x] += (state.vegetation[y - 1][x] < 1 ? growth : 0)
        state.vegetation[y - 1][x - 1] += (state.vegetation[y - 1][x - 1] < 1 ? growth / 2 : 0)
        state.vegetation[y - 1][x + 1] += (state.vegetation[y - 1][x + 1] < 1 ? growth / 2 : 0)
        state.vegetation[y - 1][x - 2] += (state.vegetation[y - 1][x - 2] < 1 ? growth / 3 : 0)
        state.vegetation[y - 1][x + 2] += (state.vegetation[y - 1][x + 2] < 1 ? growth / 3 : 0)
      }
      if (state.vegetation[y + 2]) {
        state.vegetation[y + 2][x] += (state.vegetation[y + 1][x] < 1 ? growth / 2 : 0)
        state.vegetation[y + 2][x - 1] += (state.vegetation[y + 1][x - 1] < 1 ? growth / 3 : 0)
        state.vegetation[y + 2][x + 1] += (state.vegetation[y + 1][x + 1] < 1 ? growth / 3 : 0)
        state.vegetation[y + 2][x - 2] += (state.vegetation[y + 1][x - 2] < 1 ? growth / 4 : 0)
        state.vegetation[y + 2][x + 2] += (state.vegetation[y + 1][x + 2] < 1 ? growth / 4 : 0)
      }
      if (state.vegetation[y - 2]) {
        state.vegetation[y - 2][x] += (state.vegetation[y - 2][x] < 1 ? growth / 2 : 0)
        state.vegetation[y - 2][x - 1] += (state.vegetation[y - 2][x - 1] < 1 ? growth / 3 : 0)
        state.vegetation[y - 2][x + 1] += (state.vegetation[y - 2][x + 1] < 1 ? growth / 3 : 0)
        state.vegetation[y - 2][x - 2] += (state.vegetation[y - 2][x - 2] < 1 ? growth / 4 : 0)
        state.vegetation[y - 2][x + 2] += (state.vegetation[y - 2][x + 2] < 1 ? growth / 4 : 0)
      }
      if (waterNearby.x === x && waterNearby.y === y) {
        if (cell > 0.25 && cell < 1) {
          state.vegetation[y][x] += 0.001
        } else {
          state.vegetation[y][x] /= 1.001
        }
      }
    })
  })
}

const tick = () => {
  evaporate()
  dry()
  rain()
  waterflow()
  vegetate()
  state.frame++
}
setInterval(tick, 1000/tickFPS)

const drawLand = () => {
  state.land.forEach((row, y) => {
    row.forEach((cell, x) => {
      const hue = 40 - cell * 10
      const saturation = 15 + cell * 5
      const lightness = 50 - cell * 20
      ctx.fillStyle = `hsl(${hue}, ${saturation}%, ${lightness}%)`
      ctx.fillRect(x, y, 1, 1)
    })
  })
}

const drawWetland = () => {
  state.wetland.forEach((row, y) => {
    row.forEach((cell, x) => {
      const hue = 50 + cell * 10
      const saturation = 20 + cell * 5
      const lightness = 45 - cell * 5
      const alpha = min(cell, 0.9)
      ctx.fillStyle = `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`
      ctx.fillRect(x, y, 1, 1)
    })
  })
}

const drawWater = () => {
  state.water.forEach(water => {
    let hue = 200
    let saturation = 70.3
    let lightness = 32
    let alpha = 1
    if (water.isFlowing) {
      hue = hue - 2 + random(4)
      saturation = saturation - 2 + random(4)
      lightness = lightness - 7 + random(14)
      alpha = 0.4 + random(0.3)
      if (water.isInLake) {
        hue = 200 + random(5)
        saturation = 20 + random(5)
      }
    }
    const color = `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`
    ctx.fillStyle = color
    ctx.fillRect(water.x, water.y, 1, 1)
  })
}

const drawVegetation = () => {
  state.vegetation.forEach((row, y) => {
    row.forEach((cell, x) => {
      if (!(cell > 0)) {
        return
      }
      let hue = min(200, 70 + cell * 600) - 5 * noise.simplex2(x / detailScale, y / detailScale)
      let saturation = 30 + cell * 5 + 3 * noise.simplex2(x / detailScale, y / detailScale)
      let lightness = 30 - cell * 15 - 3 * noise.simplex2(x / detailScale, y / detailScale)
      let alpha = cell * 20
      const forestBelt = [0.06, 0.175]
      if (cell > forestBelt[0] && cell < forestBelt[1]) {
        hue = (70 + forestBelt[0] * 600) + (sin((forestBelt[1] - cell) * ((π/2)/(forestBelt[1]-forestBelt[0]))) * 10)
        saturation += (sin((forestBelt[1] - cell) * (π/(forestBelt[1]-forestBelt[0]))) * 5)
        lightness -= (sin((forestBelt[1] - cell) * (π/(forestBelt[1]-forestBelt[0]))) * 5)
      }
      // lake
      if (cell > 0.3) {
        hue = hue - 5 + random(10)
        saturation = saturation - 5 + random(10)
        alpha = alpha - 0.1 + random(0.2)
      }
      ctx.fillStyle = `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`
      ctx.fillRect(x, y, 1, 1)
    })
  })
}

const draw = () => {
  drawLand()
  drawWetland()
  drawWater()
  drawVegetation()
  requestAnimationFrame(draw)
}

requestAnimationFrame(draw)

canvas.addEventListener('click', () => {
  state = resetState()
})

Array.from(queryAll('input[name="rain"]')).forEach(input => {
  input.addEventListener('change', () => {
    controls.rain = +input.value
  })
})

Array.from(queryAll('input[name="evaporation"]')).forEach(input => {
  input.addEventListener('change', () => {
    controls.evaporation = +input.value
  })
})
              
            
!
999px

Console