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="controls">
    <h2>Controls</h2>
    <label>
    Seed
    <input type="number" name="seed" id="seed" value="1" min="1">
    </label>
    <label>
    Unit size
    <div class="val"></div>
    <input type="range" name="unit" id="unit" min="5" max="100" value="50" step="1">
    </label>
    <label>
    Terrain dimensions
    <div class="val"></div>
    <input type="range" name="dims" id="dims" min="1" max="16" value="8" step="1">
    </label>
    <label>
    Noise step
    <div class="val"></div>
    <input type="range" name="step" id="step" min="0.01" max="0.2" value="0.1" step="0.01">
    </label>
    <label>
    Move ◀▶
    <div class="val"></div>
    <input type="range" name="timex" id="timex" min="-200" max="200" value="30" step="1">
    </label>
    <label>
    Move ▲▼
    <div class="val"></div>
    <input type="range" name="timez" id="timez" min="-200" max="200" value="-72" step="1">
    </label>
    <label>
    Water level
    <div class="val"></div>
    <input type="range" name="water" id="water" min="0" max="2" value="1" step="0.05">
    </label>
    <label>
    Reset rotation
    <button id="reset_rotation"><i class="fa fa-refresh"></i></button>
    </label>
  </div>
  <div class="terrain">
    <!-- <div class="side top"></div>
    <div class="side bottom"></div>
    <div class="side left"></div>
    <div class="side right"></div>
    <div class="side front"></div>
    <div class="side back"></div> -->

    <div class="water">
      <div class="side bottom"></div>
      <div class="side top"></div>
      <div class="side right"></div>
      <div class="side left"></div>
      <div class="side back"></div>
      <div class="side front"></div>
    </div>

    <div class="cubes"></div>
  </div>
              
            
!

CSS

              
                :root {
  --unit: 20;
  --width: calc(var(--unit) * 1px);
  --height: calc(var(--unit) * 1px);
}

* {
  margin: 0;
  padding: 0;
  clear: both;
  box-sizing: border-box;
}

body {
  font-family: Helvetica, sans-serif;
  perspective: 5000px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: beige;
}

.terrain, .cube, .side {
  width: var(--width);
  height: var(--height);
  position: absolute;
  transform-origin: bottom left;
}

.side {
  border: calc(var(--unit) / 20 * 1px) solid rgba(0, 0, 0, 0.5);
}

.terrain, .water, .cubes, .cube {
  transform-style: preserve-3d;
}

.terrain .top, .water .top {
  transform: rotateX(90deg) translateZ(calc(var(--width))) translateY(calc(var(--width)));
  height: var(--width);
}
.terrain .bottom, .water .bottom {
  transform: rotateX(-90deg) translateZ(calc(var(--height) - var(--width)));
  height: var(--width);
}
.terrain .left, .water .left {
  transform: rotateY(-90deg);
}
.terrain .right, .water .right {
  transform: rotateY(90deg) translateZ(calc(var(--width))) translateX(calc(-1 * var(--width)));
}
.terrain .front, .water .front {
  transform: translateZ(calc(var(--width)));
}

.terrain, .water {
  --width: calc(var(--unit) * 20px);
}
.terrain {
  transform:
    rotateY(calc(var(--ry) * 1deg))
    rotateX(calc(var(--rx) * 1deg))
    rotateZ(calc(var(--rz) * 1deg))
    translateX(200px)
  ;
  cursor: grab;
  --height: calc(var(--unit) * 2px);
}
.water {
  transform-origin: top center;
  --height: calc(var(--unit) * 1px);
  transform: rotateZ(180deg) translateY(calc(var(--unit) * -2px));
}
.water .side {
  border: calc(var(--unit) / 20 * 1px) solid rgba(0, 17, 115, 0.5);
}

.cubes {
  transform:
    translateZ(calc(var(--width) - var(--unit) * 1px))
    translateY(calc(var(--height) - var(--unit) * 1px))
  ;
}

/* colors */
.cube .left, .cube .right {
  background: radial-gradient(circle, rgba(237,111,0,1) 0%, rgba(99,59,0,1) 100%);
}
.cube .top {
  background: radial-gradient(circle, rgba(0,237,79,1) 0%, rgba(0,99,20,1) 100%);
}
.cube .front, .cube .bottom, .cube .back {
  background: radial-gradient(circle, rgba(156,73,0,1) 0%, rgba(62,37,0,1) 100%);
}
.water .right, .water .left {
  background: radial-gradient(circle, rgba(0, 82, 198, 0.5) 0%, rgba(0, 89, 192, 0.5) 100%);
}
.water .bottom {
  background: radial-gradient(circle, rgba(0, 91, 237, 0.5) 0%, rgba(0, 59, 177, 0.5) 100%);
}
.water .front, .water .back {
  background: radial-gradient(circle, rgba(0, 127, 238, 0.5) 0%, rgba(0, 26, 171, 0.5) 100%);
}

/* always on the bottom */
.cube {
  --width: calc(var(--unit) * 1px);
  --height: calc(var(--unit) * 0.5px);
}
.cube .side {
  background-position: calc(-2 * calc(var(--width) / 2)) calc(-2 * calc(var(--height) / 2));
  background-size: calc(var(--width) * 2) calc(var(--height) * 2);
}
.cube .top {
  transform: rotateX(90deg) translateZ(calc(var(--height))) translateY(calc(var(--width)));
  height: var(--width);
}
.cube .bottom {
  transform: rotateX(-90deg);
  height: var(--width);
}
.cube .left {
  transform: rotateY(-90deg) translateY(calc(-1 * (var(--height) - var(--width))));
}
.cube .right {
  transform: rotateY(90deg) translateZ(calc(var(--width))) translateX(calc(var(--width) * -1)) translateY(calc(-1 * (var(--height) - var(--width))));
}
.cube .front {
  transform: translateZ(calc(var(--width))) translateY(calc(-1 * (var(--height) - var(--width))));
}
.cube .back {
  transform: rotateY(180deg) translateX(calc(var(--width) * -1)) translateY(calc(-1 * (var(--height) - var(--width))));
}

/* controls */
.controls {
  width: 400px;
  position: absolute;
  left: 0;
  display: flex;
  flex-direction: column;
  align-items: start;
  justify-content: space-around;
  text-align: center;
  gap: 20px;
  padding: 20px;
  box-sizing: border-box;
}
h2 {
  width: 100%;
}
label {
  display: flex;
  gap: 10px;
  align-items: center;
}
input {
  position: absolute;
  right: 0;
  width: 200px;
}
.val {
  background-color: rgb(74, 255, 119);
  outline: 3px solid rgb(74, 255, 119);
}
#timez, #water {
  transform: rotate(-90deg);
  width: 100px;
  margin-right: 142px;
}
label:has(#timez, #water) {
  height: 100px;
}
#reset_rotation {
  font-size: 20px;
  background-color: rgb(74, 255, 119);
  border: 1px solid rgb(7, 106, 0);
  border-radius: 50px;
  width: 30px;
  height: 30px;
  text-align: center;
  cursor: pointer;
  position: absolute;
  right: 170px;
}
              
            
!

JS

              
                const root = document.documentElement
const cubes = document.querySelector('.cubes')
const cube = `
<div class="cube">
  <div class="side top"></div>
  <div class="side bottom"></div>
  <div class="side left"></div>
  <div class="side right"></div>
  <div class="side front"></div>
  <div class="side back"></div>
</div>
`

const settings = {
  rx: -40,
  ry: 30,
  rz: 0
}

let simplex

init()
update(settings)

function init() {
  ;[
    ['seed', newNoise, update],
    ['unit', newUnit, update],
    ['dims', newDims, update],
    ['water', newWater, undefined],
    ['timex', undefined, update],
    ['timez', undefined, update],
    ['step', undefined, update],
  ].forEach(el => {
    const element = document.querySelector(`#${el[0]}`)
    const valEl = element.parentElement.querySelector('.val')

    const func = e => {
      settings[el[0]] = Number(element.value)
      if (valEl) valEl.innerHTML = element.value
      if (el[1]) el[1](settings)
      if (e && e.isTrusted && el[2]) el[2](settings) // call update when necessary
    }
    element.addEventListener('input', func)
    element.addEventListener('mousedown', e => { e.stopPropagation() })
    func()
  })
  setRotation(settings)
}

function generate(settings) {
  let tmp = ''
  for (let i = 0; i < Math.pow(settings.dims, 2); i++) {
    tmp += cube
  }
  cubes.innerHTML = tmp // call DOM once
}

function update(settings) {
  let z, x
  Array.from(cubes.querySelectorAll('.cube')).forEach((cube, index) => {
    z = (index % settings.dims)
    x = Math.floor(index / settings.dims)
    gsap.set(cube, {
      z: z * -settings.unit,
      x: x * settings.unit,
      '--height': `${(simplex.noise2D(x*settings.step + settings.timex / 100, z*settings.step + settings.timez / 100) + 1) * settings.unit}px`})
  })
}

function newNoise(settings) {
  simplex = new SimplexNoise(settings.seed)
}

function newUnit(settings) {
  gsap.set(root, {'--unit': settings.unit})
  gsap.set('.water', {'--height': `${settings.unit * settings.water}px`})
  gsap.set('.terrain, .water', {'--width': `${settings.unit * settings.dims}px`})
  gsap.set('.terrain', {'transform-origin': `${200 + settings.unit * settings.dims / 2}px 50px ${settings.unit * settings.dims / 2}px`})
}

function newWater(settings) {
  gsap.set('.water', {'--height': `${settings.unit * settings.water}px`})
}

function newDims(settings) {
  gsap.set('.terrain, .water', {'--width': `${settings.unit * settings.dims}px`})
  gsap.set('.terrain', {'transform-origin': `${200 + settings.unit * settings.dims / 2}px 50px ${settings.unit * settings.dims / 2}px`})
  generate(settings)
}

function debounce(func, wait) {
  let timeout

  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout)
      func(...args)
    }

    clearTimeout(timeout)
    timeout = setTimeout(later, wait)
  }
}

function throttle(callback, limit) {
  var waiting = false
  return function() {
    if (!waiting) {
      callback.apply(this, arguments)
      waiting = true
      setTimeout(function() {
        waiting = false
      }, limit)
    }
  }
}

function setRotation(settings) {
  gsap.set(root, { '--ry': settings.ry, '--rx': settings.rx, '--rz': settings.rz })
}

// Dragging
const terrain = document.querySelector('.terrain')
window.addEventListener('mousedown', e => {
  e.preventDefault()
  e.stopPropagation()

  let x = e.clientX
  let y = e.clientY

  const move = throttle(e => {
    let mx = e.clientX
    let my = e.clientY
    let dx = mx - x
    let dy = my - y
    x = mx
    y = my

    settings.ry += dx * 0.2
    settings.rx += dy * 0.2 * ((settings.ry > 90 && settings.ry < 270) ? 1 : -1) // fix antirotation
    settings.rx = (settings.rx + 360) % 360
    settings.ry = (settings.ry + 360) % 360
    setRotation(settings)
  }, 30)

  function removeListener(e) {
    window.removeEventListener('mousemove', move)
    window.removeEventListener('mouseup', removeListener)
    window.removeEventListener('mouseleave', removeListener)
  }

  window.addEventListener('mousemove', move)
  window.addEventListener('mouseup', removeListener)
  window.addEventListener('mouseleave', removeListener)
})

const resetBtn = document.querySelector('#reset_rotation')
const resetIcon = resetBtn.querySelector('i')
let isSpinning = false

resetBtn.addEventListener('mousedown', e => { e.stopPropagation() })
resetBtn.addEventListener('click', () => {
  if (!isSpinning) {
    resetIcon.classList.add('fa-spin')
    isSpinning = true
  }

  settings.rx = -40
  settings.ry = 30
  settings.rz = 0
  setRotation(settings)

  setTimeout(() => {
    if (isSpinning) {
      resetIcon.classList.remove('fa-spin')
      isSpinning = false
    }
  }, 1000)
})

              
            
!
999px

Console