Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
}

canvas {
  display: block;
  float: left;
}
              
            
!

JS

              
                var previewCanvas = document.createElement('canvas');
var width = previewCanvas.width = 100;
var height = previewCanvas.height = 100;
var pctx = previewCanvas.getContext('2d');

var PI = Math.PI;
var TAU = PI*2;


renderTallGrass();
renderTallGrass();
renderBushes();
renderForest();
renderBushes();
renderMountainRange();
renderForest();
renderMountainRange();
renderRiver();


var imgData = pctx.getImageData( 0, 0, width, height ).data;
var scale = Math.floor( Math.max( window.innerWidth, window.innerHeight ) / 20 );

document.body.style.width = width * scale + 'px';

var colorRenderers = {
  // empty
  // '#000000': function( ctx ) {
  //   ctx.fillStyle = '#FD9';
  //   circle( ctx, 0.5, 0.5, 0.25 );
  // },
  '#000000': function( ctx ) {
    ctx.fillStyle = '#FD9';
    // circle( ctx, 0.5, 0.5, 0.475 );
    emptyDot( ctx, 0.25, 0.25 );
    emptyDot( ctx, 0.25, 0.75 );
    emptyDot( ctx, 0.75, 0.25 );
    emptyDot( ctx, 0.75, 0.75 );
  },
  // water
  '#1199FF': function( ctx ) {
    ctx.fillStyle = '#19F';
    circle( ctx, 0.5, 0.5, 0.475 );
  },
  // mountain
  '#CC5500': function( ctx ) {
    ctx.fillStyle = '#C50';
    ctx.beginPath();
    ctx.moveTo( 0, 1 );
    ctx.lineTo( 0.5, 0 );
    ctx.lineTo( 1, 1 );
    ctx.lineTo( 0, 1 );
    ctx.fill();
    ctx.closePath();
  },
  // forest
  '#88CC88': function( ctx ) {
    ctx.fillStyle = '#2C5';
    ctx.beginPath();
    ctx.moveTo( 0, 1 );
    ctx.lineTo( 0.5, 0.5 );
    ctx.lineTo( 1, 1 );
    ctx.lineTo( 0, 1 );
    ctx.moveTo( 0, 0.5 );
    ctx.lineTo( 0.5, 0 );
    ctx.lineTo( 1, 0.5 );
    ctx.lineTo( 0, 0.5 );
    ctx.fill();
    ctx.closePath();
  },

  // tall grass
  '#CCEEBB': function( ctx ) {
    ctx.fillStyle = '#CDB';
    ctx.beginPath();
    ctx.moveTo( 0, 0.75 );
    ctx.lineTo( 0.25, 0 );
    ctx.lineTo( 0.25, 0.75 );
    ctx.lineTo( 0, 0.75 );
    ctx.moveTo( 0.5, 1 );
    ctx.lineTo( 0.5, 0.25 );
    ctx.lineTo( 0.75, 1 );
    ctx.lineTo( 0.5, 1 );
    ctx.fill();
    ctx.closePath();
  },
  // bushes/boulders
  '#CC2255': function( ctx ) {
    ctx.fillStyle = '#C90';
    ctx.save();
    ctx.beginPath();
    ctx.arc( 0.5, 0.5, 0.45, TAU * 3/8, TAU*9/8 );
    ctx.fill();
    ctx.closePath();
    ctx.restore();
  }

};

function circle( ctx, x, y, radius ) {
  ctx.beginPath();
  ctx.arc( x, y, radius, 0, TAU );
  ctx.fill();
  ctx.closePath();
}

function emptyDot( ctx, x, y ) {
  if ( Math.random() > 0.0 ) {
    circle( ctx, x, y, 0.125 );
  }
}


for ( var tileY = 0; tileY < height; tileY += 10 ) {
  for ( var tileX = 0; tileX < width; tileX += 10 ) {
    renderOverworldTile( tileX, tileY );
  }
}

function renderOverworldTile( tileX, tileY ) {
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  var tw = canvas.width = scale * 10;
  var th = canvas.height = scale * 10;

  ctx.fillStyle = '#FED';
  ctx.fillRect( 0, 0, tw, th );
  ctx.scale( scale, scale );

  // console.log('render tile', tileX, tileY);

  for ( var y=0; y<10; y++ ) {
    for ( var x=0; x<10; x++ ) {
      var x2 = tileX + x;
      var y2 = tileY + y;
      // console.log( x2, y2 );
      var i = ( y2*width + x2 ) * 4;
      var color = [ imgData[i], imgData[i+1], imgData[i+2] ];
      var hex = rgb2hex( color );
      var renderer = colorRenderers[ hex ];
      if ( renderer ) {
        ctx.save();
        ctx.translate( x, y );
        renderer( ctx );
        ctx.restore();
      }
    }
  }

  document.body.appendChild( canvas );
}


document.body.insertBefore( previewCanvas, container );

function renderForest() {
  curlyWalk({
    color: '#8C8',
    length: 200,
    angleVelocitySize: 1.5,
    walkSize: 2,
    minBrushSize: 2,
    maxBrushSize: 10,
    startBrushSize: 4,
    brushStepSize: 2,
  });
}

function renderTallGrass() {
  curlyWalk({
    color: '#CEB',
    maxBrushSize: 7,
    startBrushSize: 3,
    brushStepSize: 2,
    // angleVelocitySize: 1.5,
    // walkSize: 2,
    // minBrushSize: 1,
    // maxBrushSize: 3,
  });
}

function renderBushes() {
  curlyWalk({
    color: '#C25',
    walkSize: 2,
    minBrushSize: 1,
    maxBrushSize: 3,
    startBrushSize: 1,
    brushStepSize: 1,
  });
}

function renderMountainRange() {

  curlyWalk({
    color: '#C50',
    angleVelocitySize: 2,
    walkSize: 3,
    minBrushSize: 4,
    maxBrushSize: 12,
    startBrushSize: 4,
    brushStepSize: 2,
  });


}

function renderRiver() {

  var startTheta = Math.random() * TAU;

  var walkerX = Math.cos( startTheta ) * 80;
  var walkerY = Math.sin( startTheta ) * 80;

  var flowAngle = startTheta + TAU/2;
  var angle = flowAngle;
  var rSize = 3;

  pctx.fillStyle = '#19F';

  for ( var i=0; i < 400; i++ ) {
    // angleVelocity = (Math.random() - 0.5) * (Math.random() - 0.5) * 3;
    if ( Math.random() > 0.7 ) {
      angle = flowAngle + ( Math.random()*2 - 1 ) * TAU/4;
    }
    walkerX += Math.cos( angle );
    walkerY += Math.sin( angle );

    var rRand = Math.random();
    if ( rRand > 0.82 ) {
      rSize += 1;
    } else if ( rRand > 0.62 ) {
      rSize -= 1;
    }
    rSize = Math.max( 2, Math.min( rSize, 8 ) );
    var size = rSize;

    var x = Math.round( width/2 + walkerX - size/2 );
    var y = Math.round( height/2 + walkerY - size/2 );

    pctx.fillRect( x, y, size, size );
  }


}


function curlyWalk( props ) {

  props = extend({
    startSize: 30,
    length: 300,
    color: '#888',
    angleVelocitySize: 1.5,
    walkSize: 1,
    minBrushSize: 1,
    maxBrushSize: 5,
    brushStepSize: 1,
    startBrushSize: 1,
  }, props );

  var wx = ( Math.random()*2 - 1 ) * props.startSize;
  var wy = ( Math.random()*2 - 1 ) * props.startSize;

  var angleVelocity = 0;
  var angle = 0;

  pctx.fillStyle = props.color;

  for ( var i=0; i < props.length; i++ ) {

    angleVelocity = ( Math.random()*2 - 1 ) * props.angleVelocitySize;
    angle += angleVelocity;

    wx += Math.cos( angle ) * props.walkSize;
    wy += Math.sin( angle ) * props.walkSize;

    var rSize = props.startBrushSize;
    var rRand = Math.random();
    if ( rRand > 0.82 ) {
      rSize += props.brushStepSize;
    } else if ( rRand > 0.62 ) {
      rSize -= props.brushStepSize;
    }
    rSize = Math.max( props.minBrushSize, Math.min( rSize, props.maxBrushSize ) );

    var x = Math.round( width/2 + wx - rSize/2 );
    var y = Math.round( height/2 + wy - rSize/2 );

    pctx.fillRect( x, y, rSize, rSize );

  }

}


function extend( a, b ) {
  for ( var prop in b ) {
    a[ prop ] = b[ prop ];
  }
  return a;
}


function rgb2hex( rgb ) {
  var hex = rgb.map( function( value ) {
    var hexNum = value.toString(16).toUpperCase();
    // left pad 0
    hexNum = hexNum.length < 2 ? '0' + hexNum : hexNum;
    return hexNum;
  });

  return '#' + hex.join('');
}
              
            
!
999px

Console