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

              
                
              
            
!

CSS

              
                html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  background: #FFF;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

canvas {
  margin: 0;
}
              
            
!

JS

              
                // Coding Challenge 127 Brownian Tree Snowflake
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/127-snowflake-brownian.html
// https://www.youtube.com/watch?v=XUA8UREROYE

// Rewritten with modifications from the Processing version by BoltMan
// https://github.com/BoltManGuy/Brownian-Snowflake-Optimized

let current;
let tree;
let completed = false;
let radius;
let ratio;

// ---- (Params) ---- //
let animate = true; // Not used
let animateIters = 25; // Animation Speed
let particleSize = 3; // Size Used for the particles
let particleMove = 2; // Distance the particles move each cycle;
let particleLength = 5;
let particleWidth = 2;
let fixedAngle = false; // Try fixedAngle = true; it's pretty cool
let sym = 6; // How many sides should your snowflake have?
let bias = 0.15; // How pointy do you wany your snowflake?
// ---- (Params) ---- //

function setup() {
  createCanvas(windowWidth, windowHeight);
  background(0);
  colorMode(HSB);
  radius = min(width, height) / 2;
  ratio = tan(PI/sym);
  if (animate) {
    resetSnowflake();
  }
}

function resetSnowflake() {
  completed = false;
  background(0);
  let rectangle = new Rectangle(width/2, height/2, width/2, height/2);
  tree = new BinTree(rectangle, null);
  current = newParticle();
}

function newParticle() {
  return new Particle(p5.Vector.fromAngle(random(PI/(sym*(1-bias)))).setMag(radius));
}

function mouseClicked() {
  resetSnowflake();
}

function draw() {
  translate(width/2, height/2);
  rotate(PI/sym);
  
  if (!completed) {
    for (let i = 0; i < animateIters; i++) {
      current = newParticle();
      let count = 0;
      while (!current.finished() &&!current.intersects(tree.query(new Rectangle(current.pos.x, current.pos.y, current.radius*3, current.radius*3)))) { // I should fix this
        current.update();
        count++;
        if (count > 500) {break} // This is here to stop infinte loops
      }
      if (count == 0) {completed = true}
      for (let rot = 0; rot < sym; rot++) {
        rotate(PI/(sym/2));
        current.draw();
        push();
        scale(1, -1);
        current.draw();
        pop();
      }
      tree.insert(new Leaf(current.pos, current));
    }
  }
}

class Particle {
  constructor(pos) {
    this.pos = pos;
    this.vel = createVector(0, 0);
    this.angle = floor(random(sym)) * (TWO_PI/sym);
    this.length = particleLength;
    this.radius = particleSize/2;
  }
  
  update() {
    this.vel.sub(this.pos);
    this.vel.setMag(particleMove);
    this.vel.add(p5.Vector.random2D());
    this.vel.setMag(particleMove);
    this.pos.add(this.vel);
    this.vel = createVector(0, 0);
    this.pos = p5.Vector.fromAngle(constrain(this.pos.heading(), 0, PI/sym)).setMag(this.pos.mag());
  }
  
  draw() {
    push();
    translate(this.pos.x, this.pos.y);
    if (fixedAngle) {
      rotate((TWO_PI/sym)*2)
    } else {
      rotate(this.angle);
    }
    stroke(220, 20, 100, 0.2);
    strokeWeight(particleWidth);
    // line(-this.length, 0, this.length, 0);
    line(0, -this.length, 0, this.length)
    pop();
  }
  
  intersects(others) {
    for (let o of others) {
      if (this.pos.dist(o.pos) < (this.radius + o.radius)) {
        return true;
      }
    }
    return false;
  }
  
  finished() {
    return this.pos.x < 1;
  }
}
              
            
!
999px

Console