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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="container">
  <div class="row mt-3">
    <div class="col-md-6">
      Created by <a href="http://twitter.com/msurguy" target="_blank">@msurguy</a> as a part of toolkit for <a href="https://drawingbots.net">Drawingbots.net</a>.
      <div class="row">
        <div class="col-md-6">
          <button id="download" class="btn btn-lg btn-primary btn-block">Download SVG*</button>
      <p>Please change variables in the source code to experiment</p>
        </div>
        <div class="col-md-6">
          <button id="generate" class="btn btn-lg btn-primary btn-block">
        Generate Random
      </button>
        </div>
      </div>
    </div>
    <div class="col-md-6">
      <small>Current formula:</small>
      <pre class="code small" id="function">
function vectorField(p) { 
    return {
      x: Math.cos((Math.cos(p.y)-p.x*p.y)),
      y: (p.x)
    }; 
}
      </pre>
    </div>
  </div>
 
  <div class="row">
    <div class="col-md-12">
       <div id="drawing"></div>
    </div>
  </div>
  <div class="row">
    <p>Credits: <a href="http://twitter.com/anvaka">@anvaka's</a> streamlines library, <a href="http://twitter.com/mattdesl">@mattdesl's</a> path simplification library</p>
    <p>*The formula that you used to generate the SVG will be embedded as TITLE element of the SVG</p>
  </div>
  
</div>


              
            
!

CSS

              
                #drawing {
  border: 1px #CCC solid;
  padding: 10px;
  width:620px;
  height: 620px;
  overflow:scroll;
}
              
            
!

JS

              
                const config ={
   separationDistance: 0.2, // Separation distance between new streamlines.
   simplification: 0.1, // line simplification amount (0.1-2)
   paperWidth : 600, // width and height of SVG canvas
   paperHeight: 600,
   boundingBox: 
  {left: -5, top: -5, width: 10, height: 10} // This is the "zoom" level of the rendering
 } 

// What function to produce streamlines for
let vectorField = function(p) {
  return {
      x: Math.cos((Math.cos(p.y)-p.x*p.y)),
      y: (p.x)
    }; 
}

let SVGcanvas = SVG('drawing').size(config.paperWidth, config.paperHeight);
let width = SVGcanvas.width();
let height = SVGcanvas.height();

const seedPoint = {
  x: config.boundingBox.left + Math.random() * config.boundingBox.width,
  y: config.boundingBox.top + Math.random() * config.boundingBox.height
}

function compileVectorFieldFunction(code) {
  try {
    let creator = new Function(code + '\nreturn vectorField;');
    let vectorField = creator();
    vectorField(seedPoint); // just a test.
    return vectorField;
  } catch (e) {
    alert(e.message);
    //fieldCode.error = e.message;
    return null;
  }
}
let streamlinesProcess = null;

const generateStreamlines = function() {
  SVGcanvas.clear();
  const description = document.createElement("title");
description.innerHTML = document.getElementById("function").innerHTML.trim();

SVGcanvas.node.appendChild(description);  
  const svgGroup = SVGcanvas.group();
  
  streamlinesProcess = streamlines({
  // As usual, define your vector field:
//  vectorField(p) { return {x: Math.sin(p.x*p.x)*p.y, y: p.x}; },
  vectorField,
  onStreamlineAdded(points) {
    let transformedPoints = [];
    for (let i = 0; i < points.length; i++){
      let tx = (points[i].x - config.boundingBox.left)/ config.boundingBox.width;
      let ty = (points[i].y - config.boundingBox.top)/ config.boundingBox.height;
      transformedPoints.push([Math.round(tx * width * 10 ) / 10, Math.round(((1 - ty) * height ) * 10 ) / 10 ]);
    }
    let simplifiedPath = simplify(transformedPoints, config.simplification);
    let polyline = svgGroup.polyline(simplifiedPath).fill('none').stroke({width:1});
  
  },
  seed: seedPoint,
  boundingBox: config.boundingBox,
  // Separation distance between new streamlines.
  dSep: config.separationDistance,

  // Distance between streamlines when integration should stop.
  dTest: 0.001,
  timeStep: 0.01
});
  streamlinesProcess.run();
}

generateStreamlines();

let downloadButtonEl = document.getElementById("download");

downloadButtonEl.addEventListener("click", writeDownloadLink);

let regenerateButtonEl = document.getElementById("generate");
regenerateButtonEl.addEventListener("click", function(){
  let func = generate();
  document.getElementById("function").innerHTML = func;
  vectorField = compileVectorFieldFunction(func);
  
  streamlinesProcess.dispose();
  generateStreamlines();
});

// util functions
function writeDownloadLink(){
 var svgDoctype = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';

  // serialize our SVG XML to a string.
  var svgString = (new XMLSerializer()).serializeToString(document.body.querySelector("svg"));

  // reduce the SVG path by cutting off floating point values after the first digit beyond floating point (~50% less MBs)
  svgString = svgString.replace(/([\-+]?\d{1,}\.\d{3,}([eE][\-+]?\d+)?)/g, function (x) {
    return (+x).toFixed(1)
  });

  var blob = new Blob([svgDoctype+svgString], {type: 'image/svg+xml;charset=utf-8'});

  /* This portion of script saves the file to local filesystem as a download */
  var svgUrl = URL.createObjectURL(blob);

  var downloadLink = document.createElement("a");
  downloadLink.href = svgUrl;
  downloadLink.download = "streamlines" + Date.now() + ".svg";
  document.body.appendChild(downloadLink);
  downloadLink.click();
  document.body.removeChild(downloadLink);
}

// square distance from a point to a segment
function getSqSegDist(p, p1, p2) {
    var x = p1[0],
        y = p1[1],
        dx = p2[0] - x,
        dy = p2[1] - y;

    if (dx !== 0 || dy !== 0) {

        var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);

        if (t > 1) {
            x = p2[0];
            y = p2[1];

        } else if (t > 0) {
            x += dx * t;
            y += dy * t;
        }
    }

    dx = p[0] - x;
    dy = p[1] - y;

    return dx * dx + dy * dy;
}

function simplifyDPStep(points, first, last, sqTolerance, simplified) {
    var maxSqDist = sqTolerance,
        index;

    for (var i = first + 1; i < last; i++) {
        var sqDist = getSqSegDist(points[i], points[first], points[last]);

        if (sqDist > maxSqDist) {
            index = i;
            maxSqDist = sqDist;
        }
    }

    if (maxSqDist > sqTolerance) {
        if (index - first > 1) simplifyDPStep(points, first, index, sqTolerance, simplified);
        simplified.push(points[index]);
        if (last - index > 1) simplifyDPStep(points, index, last, sqTolerance, simplified);
    }
}

// simplification using Ramer-Douglas-Peucker algorithm
function simplify(points, tolerance) {
    if (points.length<=1)
        return points;
    tolerance = typeof tolerance === 'number' ? tolerance : 1;
    var sqTolerance = tolerance * tolerance;
    
    var last = points.length - 1;

    var simplified = [points[0]];
    simplifyDPStep(points, 0, last, sqTolerance, simplified);
    simplified.push(points[last]);

    return simplified;
}
              
            
!
999px

Console