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

              
                <p><img id="img"  src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAAAAD/4QMtaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6NTY6MjcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHR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ySAO/ZvNdqNN+Glupjt5NSwPQ3kYtVqq/EDySDKSozEAbrLVXU0mcHSSA3JYkSuCbozSwMpSdTCpAAksH8ef+Sa/pX2lfPd/j/+6X9vzQFtKbzl5XNxpWUFT25stt+47cJTinJpYJYtnz245Sm3P6m3WvaAaokYGczBgW7gdxrsu087b+D/ABPS3zpYm+78CpcRdnb3CcU5J4Som8H1w7GWvkP9Ev8Al/7kBR7kJSk8DO3Zkmm0ejDSmzZh0A0K0HaodGBhLuA0OOlZmO48NrHNmV1ulO81bxvSkB5wAAAADKMdTojdenReWskI+CFVRyl7aI0MCAAAAAH/2Q=="></p>
<div>
<canvas id="_canvas"> </canvas>
<canvas id="canvas"> </canvas>
</div>

              
            
!

CSS

              
                body {
  background-color: #222;
}

canvas {
  background-color: #000;
  margin:5px;
}

div {
  display: block;
  margin: 0 auto;
  margin: calc(50vh - 150px - 5em) auto;
  width: 630px;
}

#img {
  display: none;
}
              
            
!

JS

              
                var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var _canvas = document.getElementById("_canvas");
var _ctx = _canvas.getContext("2d");
var cw = canvas.width = _canvas.width = 291,
  cx = cw / 2;
var ch = canvas.height = _canvas.height = 303,
  cy = ch / 2;

var rad = Math.PI / 180;

var img = document.getElementById("img");
var w = img.width;
var h = img.height;
_ctx.drawImage(img, 0, 0, w, h);
ctx.strokeStyle = "white";
ctx.lineWidth = 2;
var _imgData = _ctx.getImageData(0, 0, cw, ch);
var _pixels = _imgData.data;

for (var i = 0; i < _pixels.length; i += 4) {
  if (_pixels[i + 0] > 0 && _pixels[i + 0] < 70) {
    _pixels[i + 0] = 0;
    _pixels[i + 1] = 0;
    _pixels[i + 2] = 0
  } else {
    _pixels[i + 0] = 250;
    _pixels[i + 1] = 250;
    _pixels[i + 2] = 250
  }

}

_ctx.putImageData(_imgData, 0, 0);
_ctx.beginPath();


var imgData = _ctx.getImageData(0, 0, cw, ch);
var pixels = imgData.data;

var tree;
var max_dist = 8;
var min_dist = 3;
var numLeaves = 0;

var roots = [{x:0,y:280},{x:290,y:280},{x:145,y:10},{x:145,y:200}, {x:90,y:90}, {x:210,y:90}, {x:125,y:125},{x:180,y:125}]
////////////////////////////////////////
function Tree() {
  this.leaves = [];
  this.branches = [];
  

  for (var x = 0; x < imgData.width; x++) {
    for (var y = 0; y < imgData.height; y++) {
      var i = (x + y * imgData.width) * 4;
      if (pixels[i] == 0 && Math.random() > .8) {
        this.leaves.push(new Leaf(x, y));
        numLeaves++
      }

    }
  }
console.log(numLeaves)
  this.show = function() {
    for (var i = 0; i < this.leaves.length; i++) {
      this.leaves[i].draw();
    }
    for (var i = 0; i < this.branches.length; i++) {
      this.branches[i].draw();
    }
  }
 
  
    for (var i = 0; i < roots.length; i++) {
    var rootPos = new Vector(roots[i].x, roots[i].y);
    var dir = new Vector(0, -1);
    var root = new Branch(null, rootPos, dir);
    
    this.branches.push(root);
  }


  this.grow = function() {
      for (var i = 0; i < this.leaves.length; i++) {
        var leaf = this.leaves[i];

        var closestBranch = null;
        var record = 1000;
        for (var j = 0; j < this.branches.length; j++) {
          var branch = this.branches[j];
          var d = dist(leaf.pos, branch.pos);

          if (d < min_dist) {
            leaf.reached = true;
            closestBranch = null;
            break;
          } else if (d > max_dist) {
            // ignore, do nothing 
          } else if (closestBranch == null || d < record) {
            closestBranch = branch;
            record = d;
          }
        } //for j loop

        if (closestBranch != null) {
          var newDir = leaf.pos.copy().sub(closestBranch.pos);
          newDir.normalize();
          closestBranch.dir.add(newDir);
          closestBranch.count++;
        }

      } // for i loop

      // delete all the reached leaves
      for (var i = this.leaves.length - 1; i >= 0; i--) {
        if (this.leaves[i].reached) {
          this.leaves.splice(i, 1)
        }
      }

      for (var i = this.branches.length - 1; i >= 0; i--) {
        var branch = this.branches[i];
        if (branch.count > 0) {
          branch.dir.div(branch.count);

          this.branches.push(branch.next());
        }
        branch.reset()
      }
    } //this.grow
}
////////////////////////////////////////

////////////////////////////////////////
function Leaf(x, y) {
  this.pos = new Vector(x, y);
  this.r = 2;
  this.reached = false;
  this.draw = function() {
    ctx.beginPath();
    ctx.rect(this.pos.x, this.pos.y, this.r, this.r)
  }
}
////////////////////////////////////////

////////////////////////////////////////
function Branch(parent, pos, dir) {
  this.pos = pos;
  this.parent = parent;
  this.dir = dir;
  this.origDir = dir;
  this.count = 0;
  this.branchLength = 3;

  this.d = dist(this.pos, {
    x: cx,
    y: cy
  });

  this.reset = function() {
    this.dir = this.origDir.copy();
    this.count = 0;

  }

  this.draw = function() {
    if (this.parent != null) {
      //var index = (~~this.pos.x + ~~this.pos.y * imgData.width )*4;
      //var rgb = pixels[index];
      //ctx.strokeStyle = "rgb("+rgb+","+rgb+","+rgb+")"
      ctx.beginPath();
      ctx.moveTo(this.pos.x, this.pos.y);
      ctx.lineTo(this.parent.pos.x, this.parent.pos.y);
      ctx.stroke();

    }
  }

  this.next = function() {
    //this.branchLength = map(this.d, 0, R, 16, 4);
    var nextDir = this.dir.copy().mult(this.branchLength)

    var parent = this;
    var nextPos = this.pos.copy().add(nextDir);
    var nextBranch = new Branch(parent, nextPos, this.dir.copy());
    return nextBranch;
  }
}
////////////////////////////////////////

tree = new Tree();
tree.show();

function Draw() {
  requestId = window.requestAnimationFrame(Draw);
  ctx.clearRect(0, 0, cw, ch);
  tree.grow();
  tree.show();

  if (tree.branches.length > numLeaves) {
    window.cancelAnimationFrame(requestId);
  }
}
requestId = window.requestAnimationFrame(Draw);

function dist(p1, p2) {
  var dx = p2.x - p1.x;
  var dy = p2.y - p1.y;
  return Math.sqrt(dx * dx + dy * dy);
}

function map(n, a, b, _a, _b) {
  var d = b - a;
  var _d = _b - _a;
  var u = _d / d;
  return _a + n * u;
}
              
            
!
999px

Console