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.spinner
div.window#controlpanel
  span Parameters
div.window
  span Rendered preview
  span.winTitlebarInfo.wavelength
  div.octave_warning.warning
    a(href='https://github.com/minetest/minetest/pull/9394') Disallowed by Minetest - too many octaves
  div.noisepoints_warning.warning Disallowed by Minetest - lacunarity too low
  div
    canvas.content.main
div.window
  span Zoomed view of top left corner
  span.winTitlebarInfo.zoomfactor
  div
    canvas.zoom.content
              
            
!

CSS

              
                $border: hsl(0, 0,10);

body {
  background: $border;
}
.warning, .warning:visited, .warning:link {
  display: none;
  background: red;
  font-weight: bold;
  text-shadow: none;
  margin-bottom: 8px;
  padding: 2px;
  line-height: 1;
  text-align: center;
}

.spinner {
  position: absolute;
  visibility: visible;
  left: 50%;
  right: 50%;
  top: 40%;
  width: 60px;
  height: 60px;
  border: 4px solid #777;
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s ease-in-out infinite;
  -webkit-animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to { -webkit-transform: rotate(360deg); }
}
@-webkit-keyframes spin {
  to { -webkit-transform: rotate(360deg); }
}


.window {
  display: inline-block;
  vertical-align: top;
  padding: 8px;
  margin: 0;
  background: $border;

	font-family: 'Lato', 'Lucida Grande', 'Lucida Sans Unicode', Tahoma, Sans-Serif;
	white-space: nowrap;
	font-weight: bold;
	color: #aaaebc;
	font-size: 1.1em;
}
.window span {
  display: inline-block;
  margin-bottom: 8px;
	padding-left: 2px;
}
.window .content {
  background: white;
  border: 2px solid hsl(0, 0,70);
}
.window div {
  //line-height: 0; // shouldn't be needed - must have some whitespace getting inserted somewhere
}

.window2 {
  display: inline-block;
  //position: absolute;
  //left: 150px;
  //top: 75px;
  //width: 400px;
  vertical-align: top;
  padding: 7px;
  //height: 400px;
  border: 1px solid rgba(255,255,255,0.6);
  background: rgba(178,215,255,0.75);
  border-radius: 8px;
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
  -o-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
  -webkit-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
  -moz-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
  text-shadow: 0 0 15px #fff, 0 0 15px #fff;
}

.window2 span {
  display: inline-block;
  margin-bottom: 8px;
}
.winCaption {
  background: hsl(0, 0%, 10%);
  color: hsl(0, 0%, 70%);
  margin-top: 8px;
  padding: 4px 8px;
  text-shadow: none;
  margin-bottom: 0px; 
  background: hsl(0, 0%, 10%);
  color: hsl(0, 0%, 70%);
  border-radius: 2px;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;  
}
.winTitlebarInfo {
  float: right;
  text-shadow: none;
  font-size: 75%;
  font-weight: normal;
  margin: 4px;
}

.window .winField {
  font-size: 1em;
  //display: inline-block;
  margin: 8px 0 8px 0;
  border: 1px solid rgba(0,0,0,0.5);
  padding: 4px 8px;
  border-radius: 2px;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
  background: rgba(255,255,255,0.75);
  width: 44%;
  /*
  box-shadow: 0 0 2px #fff, 0 0 1px #fff, inset 0 0 3px #fff;
  -o-box-shadow: 0 0 2px #fff, 0 0 1px #fff, inset 0 0 3px #fff;
  -webkit-box-shadow: 0 0 2px #fff, 0 0 1px #fff, inset 0 0 3px #fff;
  -moz-box-shadow: 0 0 2px #fff, 0 0 1px #fff, inset 0 0 3px #fff;
  */
}


.window .winField + .winField {
  margin-left: 12px;
}

.window2 .content {
  background: #fff;
  border: 1px solid #000;
  border-radius: 2px;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
  //padding: 10px;
  //height: 279px;
  box-shadow: 0 0 5px #fff, 0 0 1px #fff, inset 0 1px 2px #aaa;
  -o-box-shadow: 0 0 5px #fff, 0 0 1px #fff, inset 0 1px 2px #aaa;
  -webkit-box-shadow: 0 0 5px #fff, 0 0 1px #fff, inset 0 1px 2px #aaa;
  -moz-box-shadow: 0 0 5px #fff, 0 0 1px #fff, inset 0 1px 2px #aaa;
  text-shadow: none;
}
              
            
!

JS

              
                // Noise parameters are explained here:
// https://minetest.gitlab.io/minetest/perlin-noise/#noise-parameters

// Perlin values below minDisplayedNoiseValue are 
// drawn blue, values above maxDisplayedNoiseValue 
// are cyan, and values within the range are greyscale.

// Due to precision of javascript's number type, this 
// emulator may get it wrong in extreme cases. The BigInts
// option can correct for that, but is slow and rarely needed.

function postprocess_perlinValue(value) {
  // if you want to make a programatic adjustment
  // to the value before it's displayed then that
  // can be done here.
  return value;
}

const MAGIC_X    = 1619;
const MAGIC_Y    = 31337;
const MAGIC_Z    = 52591;
const MAGIC_SEED = 1013;

const NOISE_MAGIC_X    = 1619n;
const NOISE_MAGIC_Y    = 31337n;
const NOISE_MAGIC_Z    = 52591n;
const NOISE_MAGIC_SEED = 1013n;



var opts;

var Options = function() {
  this.imageSize = 528;
  this.minDisplayedNoiseValue = -1;
  this.maxDisplayedNoiseValue = 1;
  this.colorizeClippedValues = true;
  this.offset = 0.0;
  this.scale = 1;
  this.spread_x = 64;
  this.spread_y = 64;
  this.octaves = 6;
  this.persistence = 0.5;
  this.lacunarity = 2;
  this.eased = true;
  this.absvalue = false;
  this.absFinalValue = false;
  this.useBigInts = false;
  this.zoomFactor = 10;
  
  this.seed = 1;
  
  this.newSeed = () => {
    newSeed(Math.random());
    drawCanvas();
  };
};


function minetest_noise2d(x, y, seed)
{
  // Due to Javascript lacking good integer types, this result will
  // tend toward 1 as y becomes large.
  // minetest_noise2d_BigInts() fixes this problem but is ridiculously slow.
  //var n = (MAGIC_X * x + MAGIC_Y * y + MAGIC_SEED * seed) & 0x7fffffff;
  
  var n = (
      ((
        ((MAGIC_X    * x)  & 0x7fffffff) + 
        ((MAGIC_Y    * y)  & 0x7fffffff) 
      ) & 0x7fffffff) +
      ((MAGIC_SEED * seed) & 0x7fffffff)
    ) & 0x7fffffff;
  n = (n >>> 13) ^ n;
/*
  n = ( 
    ((
      n * ( 
        (( 
          (( 
            ((
              n * n
            ) & 0x7fffffff) * 
            60493
          ) & 0x7fffffff) + 
          19990303
        ) & 0x7fffffff) 
      )) & 0x7fffffff) + 
    1376312589) & 0x7fffffff;
  */
  
  n = ( ((n * ( (( (( ((n * n) & 0x7fffffff) * 60493) & 0x7fffffff) + 19990303) & 0x7fffffff) )) & 0x7fffffff) + 1376312589) & 0x7fffffff;
  
  //n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
  return 1 - (Math.floor(n) / 0x40000000);
}

function minetest_noise2d_BigInts(x, y, seed)
{
  // In Minetest, n is an unsigned int, which Javascript does not possess, so 
  // divide and shift operations on it must be performed unsigned.
  // (Before a bitwise operation is performed, JavaScript converts numbers to 
  // 32 bits signed integers)
  //
  // Note that in two's complement arithmetic, the arithmetic operations of 
  // add, subtract, and multiply are bit-wise identical if the two operands 
  // are regarded as both being signed or both being unsigned.
  //var n = (NOISE_MAGIC_X * BigInt(x) + NOISE_MAGIC_Y * BigInt(y) + NOISE_MAGIC_SEED * BigInt(seed)) & 0x7fffffffn;
  //n = (n >> 13n) ^ n;
  var n = (
    ((MAGIC_X    * x)    & 0x7fffffff) + 
    ((MAGIC_Y    * y)    & 0x7fffffff) + 
    ((MAGIC_SEED * seed) & 0x7fffffff)
  ) & 0x7fffffff;
  var nn = BigInt((n >>> 13) ^ n);
  
  nn = (nn * (nn * nn * 60493n + 19990303n) + 1376312589n) & 0x7fffffffn;
  return 1 - Number(nn) / 0x40000000;
}


const UINT32_MAGIC_X    = UINT32(1619);
const UINT32_MAGIC_Y    = UINT32(31337);
const UINT32_MAGIC_SEED = UINT32(1013);
const UINT32_RNG_PRIME1 = UINT32(60493);
const UINT32_RNG_PRIME2 = UINT32(19990303);
const UINT32_RNG_PRIME3 = UINT32(1376312589);
const UINT32_MASK31     = UINT32(0x7fffffff);

function minetest_noise2d_jscuint(x, y, seed)
{
  // In Minetest, n is an unsigned int, which Javascript does not possess, so 
  // divide and shift operations on it must be performed unsigned.
  // (Before a bitwise operation is performed, JavaScript converts numbers to 
  // 32 bits signed integers)
  //
  // Note that in two's complement arithmetic, the arithmetic operations of 
  // add, subtract, and multiply are bit-wise identical if the two operands 
  // are regarded as both being signed or both being unsigned.
  //var n = (NOISE_MAGIC_X * BigInt(x) + NOISE_MAGIC_Y * BigInt(y) + NOISE_MAGIC_SEED * BigInt(seed)) & 0x7fffffffn;
  //n = (n >> 13n) ^ n;
  var n = UINT32(x).multiply(UINT32_MAGIC_X)
    .add(UINT32(y).multiply(UINT32_MAGIC_Y))
    .add(UINT32(seed).multiply(UINT32_MAGIC_SEED))
    .and(UINT32_MASK31)
         
  var nn = n.clone().shiftRight(13).xor(n)
  
  nn = nn.clone().multiply(
    nn.clone().multiply(nn).multiply(UINT32_RNG_PRIME1).add(UINT32_RNG_PRIME2)
  ).add(UINT32_RNG_PRIME3).and(UINT32_MASK31)
         
  //nn = (nn * (nn * nn * 60493n + 19990303n) + 1376312589n) & 0x7fffffffn;
  return 1 - nn.toNumber() / 0x40000000;
}



function minetest_noise2d_gradient(x, y, seed, eased)
{
  seed = Math.floor(seed);
  
	// Calculate the integer coordinates
	var x0 = Math.floor(x);
	var y0 = Math.floor(y);  
	// Calculate the remaining part of the coordinates
	var x1 = x - x0;
	var y1 = y - y0;
  
	// Get values for corners of square
  var v00, v10, v01, v11
  if (opts.useBigInts) {
    // slooooow
    /*
    v00 = minetest_noise2d_BigInts(x0, y0, seed);
    v10 = minetest_noise2d_BigInts(x0+1, y0, seed);
    v01 = minetest_noise2d_BigInts(x0, y0+1, seed);
    v11 = minetest_noise2d_BigInts(x0+1, y0+1, seed);
    //*/
    
    v00 = minetest_noise2d_jscuint(x0, y0, seed);
    v10 = minetest_noise2d_jscuint(x0+1, y0, seed);
    v01 = minetest_noise2d_jscuint(x0, y0+1, seed);
    v11 = minetest_noise2d_jscuint(x0+1, y0+1, seed);
    //*/
  } else {
    v00 = minetest_noise2d(x0, y0, seed);
    v10 = minetest_noise2d(x0+1, y0, seed);
    v01 = minetest_noise2d(x0, y0+1, seed);
    v11 = minetest_noise2d(x0+1, y0+1, seed);    
  }

  // Interpolate  
  return eased ?
    biLinearInterpolation(v00, v10, v01, v11, x1, y1) :
    biLinearInterpolationNoEase(v00, v10, v01, v11, x1, y1);
}

/*
 * Ease function used by Minetest
 */
function biLinearInterpolation(v00, v10,v01, v11, x, y)
{
  var tx = x * x * x * (x * (6.0 * x - 15.0) + 10.0); // easeCurve(x);
  var ty = y * y * y * (y * (6.0 * y - 15.0) + 10.0); // easeCurve(y);
  var u = v00 + (v10 - v00) * tx; // linearInterpolation(v00, v10, tx);
  var v = v01 + (v11 - v01) * tx; // linearInterpolation(v01, v11, tx);
  return    u + (v   -   u) * ty; // linearInterpolation(u, v, ty);
}

/*
 * NoEase interp function used by Minetest
 */
function biLinearInterpolationNoEase(v00, v10,v01, v11, x, y)
{
  var u = v00 + (v10 - v00) * x; // linearInterpolation(v00, v10, x);
  var v = v01 + (v11 - v01) * x; // linearInterpolation(v01, v11, x);
  return    u + (v   -   u) * y; // linearInterpolation(u, v, y);
}	


function showSpinner(visible) {
  var spinner = document.querySelector('div.spinner');  
  spinner.style.visibility = visible ? 'visible' : 'hidden';
}

/*
 * Draw on canvas
 */
// renders perlin noise on canvas with given ease function applied to value
function updatePreview() {
  //drawCanvas();
    
  showSpinner(true);
  setTimeout(
    function() {
      drawCanvas();
      showSpinner(false);
    }, 
    1
  );
}
  
function drawCanvas() {  
  var canvas = document.querySelector('canvas.main');
  canvas.width = opts.imageSize;
  canvas.height = opts.imageSize;
  var ctx = canvas.getContext('2d');
  var image = ctx.createImageData(canvas.width, canvas.height);
  var data = image.data;
  
  if (opts.maxDisplayedNoiseValue < opts.minDisplayedNoiseValue) {
    var temp = opts.maxDisplayedNoiseValue;
    opts.maxDisplayedNoiseValue = opts.minDisplayedNoiseValue;
    opts.minDisplayedNoiseValue = temp;    
  }
  
  // Minetest Protects against an octave having a spread < 1
  // Maximum possible spread value factor
  var octacve_warning = document.querySelector('div.octave_warning');
  var noisepoints_warning = document.querySelector('div.noisepoints_warning');
  var wavelength = document.querySelector('.wavelength');
	var ofactor = (opts.lacunarity > 1.0) ?
		Math.pow(opts.lacunarity, opts.octaves - 1) :
		opts.lacunarity;  
  
  wavelength.textContent = " wavelength " + 
    (Math.round(Math.max(opts.spread_x / ofactor,opts.spread_y / ofactor) * 100) / 100)
  
  octacve_warning.style.display =   
	  (opts.spread_x / ofactor < 1.0 || opts.spread_y / ofactor < 1.0) ?
    "block" : "none";

  /*
  noisepoints_warning.style.display = 
    (opts.imageSize * ofactor / Math.min(opts.spread_x, opts.spread_y) > 1000000000) ?
    "block" : "none";*/
  
  var seed = opts.seed
  
  for (var px_x = 0; px_x < canvas.width; px_x++) {
    for (var px_y = 0; px_y < canvas.height; px_y++) {
      
      var a = 0;
      var f = 1;
      var g = 1;
      
      var x = px_x / opts.spread_x;
      var y = px_y / opts.spread_y;

      for (var i = 0; i < opts.octaves; i++) {
        
        var noiseval = minetest_noise2d_gradient(x * f, y * f, seed + i, opts.eased);        
        if (opts.absvalue) {
          noiseval = Math.abs(noiseval);
        }
        
        a += g * noiseval;
        f *= opts.lacunarity;
        g *= opts.persistence;
      }
      
      var value = opts.offset + a * opts.scale
      
      if (opts.absFinalValue) {
        // absFinalValue is something the lua mod might do with the perlin noise value
        // it's not a Minetest engine flag like absvalue which applies the abs to each octave.
        value = Math.abs(value);
      }      
      value = postprocess_perlinValue(value);
      
      // set the canvas color based on where noiseval falls between
      // minDisplayedNoiseValue and maxDisplayedNoiseValue
      var clipped = opts.colorizeClippedValues && (value > opts.maxDisplayedNoiseValue || value < opts.minDisplayedNoiseValue);
      value = Math.min(opts.maxDisplayedNoiseValue, Math.max(opts.minDisplayedNoiseValue, value));
      value = 255 * (value - opts.minDisplayedNoiseValue) / (opts.maxDisplayedNoiseValue - opts.minDisplayedNoiseValue);
           
      var cell = (px_x + px_y * canvas.width) * 4;
      data[cell] = data[cell + 1] = data[cell + 2] = Math.floor(value);      
      if (clipped) {
        data[cell + 0] = 0;
        data[cell + 2] = 255;
      }
      
      data[cell + 3] = 255; // alpha
    }
  }
  
  ctx.putImageData(image, 0, 0);
  
  drawZoomed(image, opts.zoomFactor);
  showSpinner(false);  
}

function drawZoomed(image, factor) {
  
  document.querySelector(".zoomfactor").textContent = "zoom ×" + factor;
  
  var sourceSize = Math.floor(opts.imageSize / factor);

  var tempCanvas = document.createElement("canvas");
  var tempCtx = tempCanvas.getContext("2d");
  tempCanvas.width = tempCanvas.height = sourceSize;
  tempCtx.putImageData(image, 0, 0);
  
  var canvas = document.querySelector('canvas.zoom');
  canvas.width = canvas.height = sourceSize * factor;
  var ctx = canvas.getContext('2d');  
  ctx.scale(factor, factor);
  ctx.imageSmoothingEnabled = false;
  ctx.drawImage(tempCanvas, 0, 0);
}

function maxLacunarity() {
  var minSpread = Math.min(opts.spread_x, opts.spread_y);
  var result = Math.pow(minSpread, 1.0 / (opts.octaves - 1))
  return Math.trunc((result) * 100) / 100
}
/*
	var ofactor = (opts.lacunarity > 1.0) ?
		Math.pow(opts.lacunarity, opts.octaves - 1) :
		opts.lacunarity;  
  
	if (opts.spread_x / ofactor < 1.0 ||
			opts.spread_y / ofactor < 1.0) {
    octacve_warning.style.visibility = "visible";
	} else {
    octacve_warning.style.visibility = "hidden";    
  }
*/


function init() {
  opts = new Options();
    
  const pane = new Tweakpane({
    //title: 'Parameters',
    container: document.getElementById('controlpanel'),
  });
  pane.element.parentElement.style.width = '400px';
  pane.element.style.fontSize = "12px";

  const pane_noise = pane.addFolder({ expanded: true, title: 'Noise params' });
  pane_noise.addInput(opts, 'offset');
  pane_noise.addInput(opts, 'scale');
  pane_noise.addInput(opts, 'spread_x', {label: 'spread.x'});
  pane_noise.addInput(opts, 'spread_y', {label: 'spread.y'});
  pane_noise.addInput(opts, 'octaves', {min: 1, max: 16, step: 1});  
  pane_noise.addInput(opts, 'persistence', {min: 0, max: 10, step: 0.01});
  const control_lacunarity = pane_noise.addInput(opts, 'lacunarity', {min: 0, max: 10, step: 0.01});
  pane_noise.addButton({ title: 'Find maximum lacunarity (but 2.0 is better)' }).on('click', () => { 
    opts.lacunarity = maxLacunarity();
    control_lacunarity.refresh();
  });  
  pane_noise.addInput(opts, 'eased', {label: 'Flags: eased'});
  pane_noise.addInput(opts, 'absvalue', {label: 'Flags: absvalue'});
  pane_noise.addInput(opts, 'absFinalValue', {label: 'Apply abs() to final value'});
  pane_noise.addSeparator();
  
  const control_seed = pane_noise.addInput(opts, 'seed', {label: "Seed", step: 1});
  pane_noise.addButton({ title: 'New seed' }).on('click', () => { 
    opts.seed = Math.floor(Math.random() * 0xFFFFFFFF);
    control_seed.refresh();
  });
  
  pane.addSeparator();
  const pane_preview = pane.addFolder({ expanded: true, title: 'Preview rendering options' });
  pane_preview.addInput(opts, 'imageSize', {label: 'Image size', min: 16, max: 1000, step: 1});
  pane_preview.addInput(opts, 'zoomFactor', {label: 'Zoom factor', min: 1, max: 30, step: 1});
  pane_preview.addInput(opts, 'minDisplayedNoiseValue', {label: 'Min. displayed noise value'});
  pane_preview.addInput(opts, 'maxDisplayedNoiseValue', {label: 'Max. displayed noise value'});  
  pane_preview.addInput(opts, 'colorizeClippedValues', {label: 'Colorize clipped values'});
  pane_preview.addInput(opts, 'useBigInts', {label: 'Use BigInts (slooow)'});
  
  
  pane.on('change', (value) => {
    updatePreview();
  });
  
  updatePreview();
}


init();
//window.addEventListener('load',init);
              
            
!
999px

Console