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

              
                <?xml version="1.0" encoding="utf-8" ?>
<svg id="canvas" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"></svg>

<button class="btn btn-generate">
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#00f" viewBox="0 0 16 16"><path d="M1.161 8a6.84 6.84 0 1 0 6.842-6.84.58.58 0 1 1 0-1.16 8 8 0 1 1-6.556 3.412l-.663-.577a.58.58 0 0 1 .227-.997l2.52-.69a.58.58 0 0 1 .728.633l-.332 2.592a.58.58 0 0 1-.956.364l-.643-.56A6.812 6.812 0 0 0 1.16 8z"/><path d="M6.641 11.671V8.843h1.57l1.498 2.828h1.314L9.377 8.665c.897-.3 1.427-1.106 1.427-2.1 0-1.37-.943-2.246-2.456-2.246H5.5v7.352h1.141zm0-3.75V5.277h1.57c.881 0 1.416.499 1.416 1.32 0 .84-.504 1.324-1.386 1.324h-1.6z"/></svg>
</button>

<button class="btn btn-copy">
  <svg class="clipboard show" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#00f" viewBox="0 0 16 16"><path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/><path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/></svg>
  <svg class="clipboard-done" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#00f" class="bi bi-clipboard-check" viewBox="0 0 16 16"><path d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"/></svg>
</button>

<button class="btn btn-save">
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#00f" viewBox="0 0 16 16"><path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/><path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/></svg>
</button>

<button class="btn btn-save-image">
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#00f" viewBox="0 0 16 16"><path d="M6.002 5.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0z"/><path d="M1.5 2A1.5 1.5 0 0 0 0 3.5v9A1.5 1.5 0 0 0 1.5 14h13a1.5 1.5 0 0 0 1.5-1.5v-9A1.5 1.5 0 0 0 14.5 2h-13zm13 1a.5.5 0 0 1 .5.5v6l-3.775-1.947a.5.5 0 0 0-.577.093l-3.71 3.71-2.66-1.772a.5.5 0 0 0-.63.062L1.002 12v.54A.505.505 0 0 1 1 12.5v-9a.5.5 0 0 1 .5-.5h13z"/></svg>
</button>

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

CSS

              
                body {
  margin: 60px 20px;
  height: calc(100vh - 120px);
  background: #ddd;
}

svg#canvas {
  width: 100%;
  height: 100%;
}

.btn {
  display: flex;
  border: none;
  position: absolute;
  right: 10px;
  background: #fff;
  padding: 8px;
  border-radius: 4px;
  box-shadow: 1px 1px 2px 0px rgb(0 0 0 / 5%);
}

.btn:hover {
  cursor: pointer;
  background: #00f;
}

.btn:hover svg {
  fill: #fff;
}

.btn:focus {
  outline: none;
}

.btn-generate {
  top: 10px;
}

.btn-copy {
  top: 50px;
}

.btn-save {
  top: 90px;
}

.btn-save-image {
  top: 130px;
}

.btn-generate svg,
.btn-copy svg,
.btn-save svg,
.btn-image svg{
  display: block;
}

.btn-copy svg {
  display: none;
}

.btn-copy svg.show {
  display: block;
}

@media only screen and (max-width: 767px) {
  .btn {
    top: 10px;
  }
  .btn-generate {
    right: 10px;
  }

  .btn-copy {
    right: 50px;
  }

  .btn-save {
    right: 90px;
  }
  
  .btn-save-image {
    right: 130px;
  }
}

.stamp {
  text-align: center;
  font-family: Helvetica;
  font-size: 8px;
  color: #000;
  letter-spacing: 2px;
  display: block;
  position: absolute;
  bottom: 20px;
  left: 0;
  width: 100%;
}
              
            
!

JS

              
                import copy from "https://cdn.skypack.dev/copy-to-clipboard@3.3.1";

//Set up Canvas
var w = 1000;
var h = 1000;
var canvas = SVG('#canvas').css('background', 'transparent');
canvas.viewbox(0, 0, w, h);

//Set up random factors
var artworkID;
var offsetW =  Math.round(w / 10);
var offsetH =  Math.round(h / 10);
var btlx, btly, btrx, btry, bblx, bbly, bbrx, bbry,
    eyelw0, eyelx0, eyely0, eyelw1, eyelx1, eyely1, eyerw0, eyerx0, eyery0, eyerw1, eyerx1, eyery1,
    mouthlx, mouthly, mouthrx, mouthry,
    dirx, diry,
    app_cs, app_csSeed;

//Set up colour scheme
var cscheme = new Array;
cscheme[0] = ["#eee"];

//Rendering
generate();

function generate() {
  //Setup
  app_cs = rand(cscheme.length);
  app_csSeed = rand(cscheme[app_cs].length);
  btlx = rand(w - (offsetW * 2) - w / 2) + offsetW;
  btly = rand(h - (offsetH * 2) - h / 2) + offsetH;
  btrx = rand(w - (offsetW * 2) - w / 2) + Math.round(w / 2);
  btry = rand(h - (offsetH * 2) - h / 2) + offsetH;
  bblx = rand(w - (offsetW * 2) - w / 2) + offsetW;
  bbly = rand(h - (offsetH * 2) - h / 2) + Math.round(h / 2);
  bbrx = rand(w - (offsetW * 2) - w / 2) + Math.round(w / 2);
  bbry = rand(h - (offsetH * 2) - h / 2) + Math.round(h / 2);
 		
  eyelw0 = rand(w / 6) + 30;
  eyelx0 = rand(w / 4);
  eyely0 = rand(h / 10);
  eyelw1 = rand(eyelw0 / 2 - 10) + 10;
  eyelx1 = rand((eyelw0 - eyelw1) / 3);
  eyely1 = rand((eyelw0 - eyelw1) / 3);
  eyerw0 = rand(w / 6) + 30;
  eyerx0 = rand(w / 4);
  eyery0 = rand(h / 10);
  eyerw1 = rand(eyerw0 / 2 - 10) + 10;
  eyerx1 = rand((eyerw0 - eyerw1) / 3);
  eyery1 = rand((eyerw0 - eyerw1) / 3);
 
  mouthlx = rand(w - (offsetW * 2) - w / 2) + offsetW;
  mouthly = rand(h / 3);
  mouthrx = rand(w - (offsetW * 2) - w / 2) + Math.round(w / 2);
  mouthry = rand(h / 3);
						
  //349358587266172540641629-1350232-521224-12123989-161813-362113673254
  //getArt(349,358,587,266,172,540,641,629,135,2,32,52,12,24,121,23,989,16,18,13,362,113,673,254);
  
  //Start
  canvas.clear();
  canvas.rect(w, h).fill(cscheme[app_cs][app_csSeed]);
  drawBob(
    btlx, btly, btrx, btry, bblx, bbly, bbrx, bbry,
    eyelw0, eyelx0, eyely0, eyelw1, eyelx1, eyely1, eyerw0, eyerx0, eyery0, eyerw1, eyerx1, eyery1,
    mouthlx, mouthly, mouthrx, mouthry
  );
  drawStamp(
    btlx, btly, btrx, btry, bblx, bbly, bbrx, bbry,
    eyelw0, eyelx0, eyely0, eyelw1, eyelx1, eyely1, eyerw0, eyerx0, eyery0, eyerw1, eyerx1, eyery1,
    mouthlx, mouthly, mouthrx, mouthry
  );
}

function getArt(
    r0, r1, r2, r3, r4, r5, r6, r7,
    r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19,
    r20, r21, r22, r23
  ) {
  btlx = r0;
  btly = r1;
  btrx = r2;
  btry = r3;
  bblx = r4;
  bbly = r5;
  bbrx = r6;
  bbry = r7;
  
  eyelw0 = r8;
  eyelx0 = r9;
  eyely0 = r10;
  eyelw1 = r11;
  eyelx1 = r12;
  eyely1 = r13;
  eyerw0 = r14;
  eyerx0 = r15;
  eyery0 = r16;
  eyerw1 = r17;
  eyerx1 = r18;
  eyery1 = r19;
      
  mouthlx = r20;
  mouthly = r21;
  mouthrx = r22;
  mouthry = r23;
}

//Display artwork stamp
function drawStamp(
    r0, r1, r2, r3, r4, r5, r6, r7,
    r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19,
    r20, r21, r22, r23
  ) {
  artworkID = num(r0)+num(r1)+num(r2)+num(r3)+num(r4)+num(r5)+num(r6)+num(r7)
    +"-"+num(r8)+num(r9)+num(r10)+"-"+num(r11)+num(r12)+num(r13)
    +"-"+num(r14)+num(r15)+num(r16)+"-"+num(r17)+num(r18)+num(r19)
    +"-"+num(r20)+num(r21)+num(r22)+num(r23);
  
  document.querySelector(".stamp").innerHTML = "Ref: "+artworkID;
}


//Set up Bob
function drawBob(
  btlx, btly, btrx, btry, bblx, bbly, bbrx, bbry,
  eyelw0, eyelx0, eyely0, eyelw1, eyelx1, eyely1, eyerw0, eyerx0, eyery0, eyerw1, eyerx1, eyery1,
  mouthlx, mouthly, mouthrx, mouthry
) {
  
  //Body
  var btrx2 = Math.max(btrx, bbrx) + 50;
  var body = new SVG.PathArray([
    ['M', bblx, bbly],
    ['C', btlx, btly, btlx, btly, (Math.max(btlx, btrx2) - Math.min(btlx, btrx2)) / 2 + Math.min(btlx, btrx2), (Math.max(btly, btry) - Math.min(btly, btry)) / 2 + Math.min(btly, btry)],
    ['C', Math.max(btrx, bbrx) + 50, btry, Math.max(btrx, bbrx) + 50, btry, bbrx, bbry],
    ['Z']
  ])
  
  /*
  var line0 = canvas.polyline([
      btlx, btly,
      w / 2, Math.min(btly, btry) - 25,
      Math.max(btrx, bbrx) + 50, btry,
      bbrx, bbry,
      bblx, bbly,
      btlx, btly
    ])
    .cx(w / 2)
    .cy(h / 2)
    .fill("none")
    .stroke({color: "#f00", opacity: 1, width: 1})
  */
  
  var color0 = new SVG.Color.random('vibrant')
  var shape0 = canvas.path(body)
    .cx(w / 2)
    .cy(h / 2)
    .fill(color0)
    .stroke({color: color0, opacity: 1, width: 1})
  
  //Mouth
  /*
  var line0 = canvas.polyline([
      mouthlx, mouthly,
      mouthrx, mouthry
    ])
    .cx(w / 2)
    .cy(h / 1.5)
    .fill("none")
    .stroke({color: "#f00", opacity: 1, width: 1})
  */
  var mouthType = rand(2);
    
  if(mouthType == 0) { 
    var mouth = new SVG.PathArray([
      ['M', mouthlx, mouthly],
      ['C', mouthlx + 20, mouthly + 100, mouthrx - 20, mouthry + 100, mouthrx, mouthry],
      ['Z']
    ])
  } else {
     var mouth = new SVG.PathArray([
      ['M', mouthlx, mouthly],
      ['C', mouthlx + 20, mouthly - 100, mouthrx - 20, mouthry - 100, mouthrx, mouthry],
      ['Z']
    ])
  }
  
  var shape1 = canvas.path(mouth)
    .cx(w / 2)
    .cy(h / 1.5)
    .fill("#f99")
    .stroke({color:"#f99", opacity: 1, width: 1})
  
  var eyeGroup0 = canvas.group();
  var eyeGroup1 = canvas.group();
  var eye0 = canvas.circle(eyelw0).fill("#fff")
    .cx(w / 2 - eyelx0 - eyelw0 / 2)
    .cy(h / 3 + eyely0 * (rand(2) == 0 ? 1 : -1))
  var eye1 = canvas.circle(eyerw0).fill("#fff")
    .cx(w / 2 + eyerx0 + eyerw0 / 2)
    .cy(h / 3 + eyely0 * (rand(2) == 0 ? 1 : -1))
  var eyes0 = canvas.circle(eyelw1).fill("#000")
    .cx(eye0.attr('cx') + eyelx1 * (rand(2) == 0 ? 1 : -1))
    .cy(eye0.attr('cy') + eyely1 * (rand(2) == 0 ? 1 : -1))
  var eyes1 = canvas.circle(eyerw1).fill("#000")
    .cx(eye1.attr('cx') + eyerx1 * (rand(2) == 0 ? 1 : -1))
    .cy(eye1.attr('cy') + eyery1 * (rand(2) == 0 ? 1 : -1))
  
  eyeGroup0.add(eye0).add(eyes0)
  eyeGroup1.add(eye1).add(eyes1)
}

function rand(num) {
  return Math.floor(Math.random() * num);
}

function num(int, isSingle) {
  if(int.toString().length == 1 && !isSingle) {
    return '0'+int.toString();
  }else {
    return int.toString();
  }
}

//Action - Generate
document.querySelector(".btn-generate").addEventListener("click", generate);

//Action - Copy to clipboard
//Credit to @georgedoescode https://codepen.io/georgedoescode/pen/dyNVNjG
document.querySelector(".btn-copy").addEventListener("click", function() {
  copy(canvas.node.outerHTML);
  document.querySelector("svg.clipboard").classList.remove("show");
  document.querySelector("svg.clipboard-done").classList.add("show");
  
  setTimeout(function () {
    document.querySelector("svg.clipboard").classList.add("show");
    document.querySelector("svg.clipboard-done").classList.remove("show");
  }, 1500);
});

//Action - Save as svg
//Credit to @terabaud https://codepen.io/terabaud/pen/mdJqeYz
function save() {
  var anchor = document.createElement('a');
  anchor.setAttribute('download', 'bob-'+artworkID+'.svg');
  var code = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"".concat(canvas.viewbox(), "\">").concat(canvas.node.innerHTML, "</svg>");
  anchor.setAttribute("href", "data:application/octet-stream;base64," + btoa(code));
  anchor.setAttribute('target', '_blank');
  return anchor;
}

document.querySelector(".btn-save").addEventListener('click', function() {
  save()['click']();
});

//Action - Save as png
//Credit to https://github.com/exupero/saveSvgAsPng
document.querySelector(".btn-save-image").addEventListener('click', function() {
  saveSvgAsPng(document.getElementById("canvas"), 'bob-'+artworkID+".png", {encoderOptions: 1, scale: 1});
});
              
            
!
999px

Console