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

              
                <!--
	Random Alien-Avatar Generator

  Create a random alien my friend. 


  2012 by Tim Pietrusky
  timpietrusky.com
-->

<h1>Random Alien-Avatar Generator</h1>	

<canvas id="random_avatar" width="300" height="300">
		Your browser doesn't support the Alien-Avatar Generator.
</canvas>

<nav>
  <button role="generate">Generate</button>
  <button role="download">Download</button>
</nav>

<h1 class="download">Right click / save as</h1>
<img role="result" />
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Squada+One);

body {
  margin:1em 0;
}

* {
  margin: 5px auto;
	padding: 0;
  background: #222;
  color:#000;
  float:left;
  font: 40px bold 'Squada One', cursive;
}

canvas {
  box-shadow:0 0 1em rgba(0,0,0,.5);
}

h1 {
  font-size:1.4em;
  margin-right:20px;
  padding:0 20px;
  width:300px;
  text-align:center;
  color: #111;
  text-shadow: 
    0 0 10px rgba(200,200,200,.3),
    0 0 45px rgba(200,200,200,.2),
    0 -1px 1px #333, 
    0 1px 1px #666, 
    0 2px 2px #333;
}

h1.download {
  opacity:0;
}

nav {
  position:absolute;
  right:0;
  top:0;
  font-size:15px;
  background:none;
  padding:1em 0;
}

/**
 * Button by Joshua Hibbert
 * https://codepen.io/joshnh/details/32/2
 */
button {
    background-color: #999;
    background-image: linear-gradient(hsla(0,0%,100%,.05), hsla(0,0%,0%,.1));
    border: none;
    border-radius: .5em;
    box-shadow: inset 0 0 0 1px hsla(0,0%,0%,.25),
                inset 0 2px 0 hsla(0,0%,100%,.1),
                inset 0 1.2em 0 hsla(0,0%,100%,.1),
                inset 0 -.2em 0 hsla(0,0%,100%,.1),
                inset 0 -.25em 0 hsla(0,0%,0%,.5),
                0 .25em .25em hsla(0,0%,0%,.1);
    color: #333;
    cursor: pointer;
    display: inline-block;
    font-family: sans-serif;
    font-size: 1em;
    font-weight: bold;
    line-height: 1.5;
    margin: 0 .5em 1em;
    padding: .5em 1.5em .75em;
    position: relative;
    text-decoration: none;
    text-shadow: 0 1px 1px hsla(0,0%,100%,.25);
    vertical-align: middle;
}

button:hover {
    outline: none;
}

button:hover,
button:focus {
    box-shadow: inset 0 0 0 1px hsla(0,0%,0%,.25),
                inset 0 2px 0 hsla(0,0%,100%,.1),
                inset 0 1.2em 0 hsla(0,0%,100%,.1),
                inset 0 -.2em 0 hsla(0,0%,100%,.1),
                inset 0 -.25em 0 hsla(0,0%,0%,.5),
                inset 0 0 0 3em hsla(0,0%,100%,.2),
                0 .25em .25em hsla(0,0%,0%,.1);
}

button:active {
    box-shadow: inset 0 0 0 1px hsla(0,0%,0%,.25),
                inset 0 2px 0 hsla(0,0%,100%,.1),
                inset 0 1.2em 0 hsla(0,0%,100%,.1),
                inset 0 0 0 3em hsla(0,0%,100%,.2),
                inset 0 .25em .5em hsla(0,0%,0%,.05),
                0 -1px 1px hsla(0,0%,0%,.1),
                0 1px 1px hsla(0,0%,100%,.25);
    margin-top: .25em;
    outline: none;
    padding-bottom: .5em;
}
              
            
!

JS

              
                // Canvas
var canvas = $('#random_avatar').get(0),
    ctx,
    px = 30,
    px_s = 15;

    $(canvas).css(['width'], [600]);

// Download
var download = $('button[role="download"]');
download.click(function(e) {
  var img = canvas.toDataURL("image/png");
  $('img[role="result"]').attr('src', img);
  
  $('h1.download').css('opacity', 1);
});
  
// Generate
var generate = $('button[role="generate"]');
generate.click(function(e) {
  ravatar();
});

Canvas = {
  el : {
    dom : $('#random_avatar').get(0),
    jq : null
  },
  ctx : null,
  size : {
    width : 300,
    height: 300
  },
  
  init : function() {
    this.el.jq = $(this.el.dom);
    
    if (this.el.dom.getContext) {
      this.ctx = this.el.dom.getContext('2d');
      
      this.el.jq.css('width', this.size.width);
      this.el.jq.css('height', this.size.height);
    } else {
      // Not supported
    }
  }
}

Canvas.init();

/**
 * Create a random Alien-Avatar
 */
function ravatar() {
        ctx = canvas.getContext('2d');

        // Background gradient
        var cxlg = ctx.createLinearGradient(0, 0, 300, 300);
        cxlg.addColorStop(0, '#555');
        cxlg.addColorStop(0.5, '#ccc');
        cxlg.addColorStop(1.0, '#666');
        ctx.fillStyle = cxlg;

        ctx.fillRect(0,0,300,300);
        ctx.fillRect(300,0,300,300);
        ctx.fillRect(0,300,300,300);

        // Face
        face();

        // Eyes
        eyes();
      
        // Mouth
        mouth();

        // Hair
        hair();

        // Body
        body();
}

ravatar();

/**
 * Face
 */
function face() {
    var faces = [
        [ // F@ face
          [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3.5],
          [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4],
          [2, 5], [3, 5], [4, 5], [5, 5], [6, 5], [7, 5],
          [2, 6], [3, 6], [4, 6], [5, 6], [6, 6], [7, 5.5],
        ],
        [ // Normal face
          [3, 3], [4, 3], [5, 3], [6, 3],
          [3, 4], [4, 4], [5, 4], [6, 4],
          [3, 5], [4, 5], [5, 5], [6, 5],
          [3, 6], [4, 6], [5, 6],
        ],
        [ // Alien face
          [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [8, 3],
          [1, 4], [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4], [8, 4],
          [3, 5], [4, 5], [5, 5], [6, 5],
          [3, 6], [4, 6], [5, 6],
        ]
    ];

    // Face
    draw(
      randomColor(), 
      faces[randomBetween(faces.length)]
    );
}

/**
 * Eyes
 */
function eyes() {
    var eyes = [
          [
           [4, 4], [6, 4]
          ]
    ]

    // Eyes
    draw(randomColor(), eyes[randomBetween(eyes.length)]);

    var pupil = [
         [[4.5, 4], [6.5, 4]],
         [[4.5, 4.5], [6.5, 4.5]],
         [[4, 4.5], [6, 4.5]],
         [[4, 4], [6.5, 4.5]],
         [[4.5, 4.5], [6, 4]],
         []
    ];

    // Pupil
    draw(
      randomColor(), 
      pupil[randomBetween(pupil.length)], 
      px_s
    );
}

function mouth() {
  // Mouth
  var mouths = [
    [[4, 6], [5, 6]]
  ];
  
  draw(
    randomColor(), 
    mouths[randomBetween(mouths.length)]
  );
  
  // Decorations
  var decorations = [
  	[[5, 6]],
    [[4, 6], [4.5, 6.5], [5, 6], [5.5, 6.5]],
    []
  ];
  
  draw(
    randomColor(), 
    decorations[randomBetween(decorations.length)],
    px_s
  );
}

/**
 * Hair
 */
function hair() {
    var hair = [
          [
                     [4, .5], [5, .5], [6,0],
           [3, 1.5], [4, 1],  [5, 1], [6, 1],
           [3, 2.5], [4, 2],  [5, 2], [6, 2],
          ],
          [
           [4, .5], [5, .5],[6,0],[7,0],
           [2, 1.5],[3, 1.5], [4, 1],  [5, 1], [6, 1],
           [2, 2.5], [3, 2.5], [4, 2],  [5, 2], [6, 2], [7, 2],
          ],
          [
           [4, .5], [5, .5],
           [2, 1.5],[3, 1.5], [4, 1.5],  [5, 1.5], [6, 1.5], [7, 1.5],
           [1, 2.5],[2, 2.5], [3, 2.5], [4, 2.5],  [5, 2.5], [6, 2.5], [7, 2.5], [8, 2.5]
          ],
          [
           [2, .5], [7, .5],
           [2, 1.5], [3, 2], [4, 1.5], [5, 1.5], [6, 2], [7, 1.5],
           [2, 2.5], [4, 2.5],  [5, 2.5], [7, 2.5],
          ],
          []
    ];

    draw(
      randomColor(), 
      hair[randomBetween(hair.length)]
    );
}

/**
 * Body
 */
function body() {
    var bodys = [
         [
                  [2, 7], [3, 7], [4, 7], [5, 7], [6, 7],
          [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [6, 8], [7, 8],
          [1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9]
         ],
         [
          [2, 7], [3, 7], [4, 7], [5, 7], [5, 7], [6, 7], [7, 7],
  [0, 8], [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [6, 8], [7, 8], [8, 8], [9, 8],
  [0, 9], [1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9], [8, 9], [9, 9]
         ]
    ];

    // Body
    draw(randomColor(), bodys[randomBetween(bodys.length)]);

    // Decorations
    var body_decorations = [
           [ // Tie
             [3, 7], [5, 7],
             [4, 8],
             [4, 9],
           ],
           []
    ];

    draw(
      randomColor(), 
      body_decorations[randomBetween(body_decorations.length)]
    );

    // Decorations 2
    var body_decorations_2 = [
            [
                [3.5, 7.5], [5, 7], [5, 7],
                [4, 8],
                [4, 9],
            ],
            [
             [3, 8.5], [5.5, 8.5],
             [2.5, 9], [6, 9],
             [2.5, 9.5], [5.5, 9.5]
            ],
     ];

     draw(
       randomColor(),
       body_decorations_2[randomBetween(body_decorations_2.length)], 
       px_s
     );
}

/**
 * Draw something.
 */
function draw(color, coords, size) {
    $.each(coords, function(i, v) {

        var _size = px;

        if (size != undefined) {
            _size = size;
        }

        ctx.fillStyle = color;
        ctx.fillRect(coords[i][0] * px, coords[i][1] * px, _size, _size);
    });
}

/**
 * Return a random value not greater than max.
 */
function randomBetween(max) {
    var r;
    do {r = Math.random();} while(r == 1.0);
    return parseInt(r * max);
}

/*
 * Return a random color as hex.
 */
function randomColor() {
    return '#' + Math.floor(Math.random()*16777215).toString(16);
}
              
            
!
999px

Console