css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>SVG Lines Genarative Art</h1>
<button class="go one">Draw lines</button><button class="go two">Draw colours</button><button class="go three">Draw all sorts</button>
<button class="go save">Save it</button>
<svg class="svg" width="100%" height="100%" viewbox="0 0 600 600" id="svg"></svg>
<canvas id="canvas" width="600" height="600"></canvas>
<div id="png-container"></div>
            
          
!
            
              * {margin: 0; padding: 0;}
body {
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
h1 {
  margin: 0; padding: 0;
  position: absolute; left: 98%; top: 2%; white-space: nowrap; transform: rotate(90deg); transform-origin: left top;
}
svg {
   position:fixed; top:0; left:0; height:100%; width:100%;
   pointer-events: none; z-index: 0;
}
.go {
  padding: 1em;
  position: fixed;
  border: 0;
  border-right: 3px solid #ED5138;
  background: #DFDE64;
  height: 40px;
}

.one {top: 10px; left: 10px;}
.two {top: 60px; left: 10px;}
.three {top: 110px; left: 10px;}
.save {top: 180px; left: 10px; background-color:#86D2F3;}

#canvas {display: none;}

#png-container {
    display: none;
    height: 400px;
    width: 400px;
    margin: 0 auto;
    border: 10px solid #F3D400;
    padding: 20px;
}

#png-container img {
  max-height: 100%;
  max-width: 100%;
}
            
          
!
            
              var lines = [];
var count;
var s = Snap("#svg");
function buildPath(c) {
  count++;
  var p0 = r(0,600);
  var p1 = r(0,600);
  var p2 = r(100,400);
  var p3 = r(100,400);
  var p4 = r(100,500);
  var p5 = r(100,500);
  var clr, clrfill;
  if(c=="c" || c=="a") {clr = colour();} else {clr = "#ff0082";}
  if(c=="a") {clrfill = colour();} else {clrfill = "none";}
  lines[count] = s.path("M300,0 C"+p0+","+p1+" "+p2+","+p3+" "+p4+","+p5+" S200,300 300,600").attr({
  fill: clrfill,
  "fill-opacity": 0.3,
  stroke: clr,
  strokeWidth: 3
  });  
  
  var pathLength = Snap.path.getTotalLength(lines[count]);
  lines[count].node.style.strokeDasharray = pathLength + ' ' + pathLength;
  lines[count].node.style.strokeDashoffset = pathLength;
  
  Snap.animate(pathLength, 0, function( value ) {
      lines[count].node.style.strokeDashoffset = value;
    }, 400);
}

var pngContainer = document.querySelector('#png-container');

var int;
function go(c) {
  img = null;
  svg = null;
  pngContainer.innerHTML = '';
  pngContainer.style.display = 'none';
  if (int) {clearInterval(int);}
  s.clear();
  int = setInterval(function() {
    buildPath(c);
  }, 1200);
}

function colour() {
  return '#'+Math.floor(Math.random()*16777215).toString(16);
}

function r(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

document.querySelector(".one").addEventListener("click", function() {go('n')});
document.querySelector(".two").addEventListener("click", function() {go('c')});
document.querySelector(".three").addEventListener("click", function() {go('a')});
document.querySelector(".save").addEventListener("click", function() {saveIt()});

var canvas, img, svg;
function saveIt() {
  var svgString = new XMLSerializer().serializeToString(document.querySelector('svg'));
  clearInterval(int);
  s.clear();
  canvas = document.getElementById("canvas");
  var ctx = canvas.getContext("2d");
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  var DOMURL = self.URL || self.webkitURL || self;
  img = new Image();
  svg = new Blob([svgString], {type: "image/svg+xml;charset=utf-8"});
  var url = DOMURL.createObjectURL(svg);
  img.onload = function() {
      ctx.drawImage(img, 0, 0);
      var png = canvas.toDataURL("image/png");
      pngContainer.innerHTML = '<img src="'+png+'"/>';
      pngContainer.style.display = 'block';
      DOMURL.revokeObjectURL(png);
  };
  img.src = url;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console