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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

              
                
              
            
!

JS

              
                var HALF_PI = Math.PI / 2;

var two = new Two({
  type: Two.Types.canvas,
  fullscreen: true,
  autostart: true
}).appendTo(document.body);

two.renderer.domElement.style.background = 'rgb(250, 250, 175)';

var physics = new Physics();

var mass = 20;
var strength = 5;
var drag = 0.85;

var spring = physics.makeSpring(
  physics.makeParticle(mass, 0, 0),
  physics.makeParticle(mass, 0, 0),
  strength, drag, 0
);

spring.group = two.makeGroup();

spring.a.makeFixed();
spring.path = new Two.Path([
  new Two.Anchor(), new Two.Anchor()
], false, false);
spring.path.noFill().stroke = 'rgb(255, 100, 100)';

spring.a.position = spring.path.vertices[0];
spring.b.position = spring.path.vertices[1];

spring.a.position.path = new Two.Circle(0, 0, 5);
spring.b.position.path = new Two.Circle(0, 0, 5);
spring.a.position.path.noStroke().fill = 'rgb(255, 100, 100)';
spring.b.position.path.noStroke().fill = 'rgb(255, 100, 100)';

spring.a.position.path.translation = spring.a.position;
spring.b.position.path.translation = spring.b.position;

spring.group
  .add(spring.path, spring.a.position.path, spring.b.position.path);

var mouse = new Two.Vector();
mouse.normal = new Two.Vector();
mouse.normal.update = function() {
  mouse.normal.copy(mouse);
  mouse.normal.x /= two.width;
  mouse.normal.y /= two.height;
  mouse.normal.multiplyScalar(2);
  mouse.normal.x -= 1;
  mouse.normal.y -= 1;
};

var mousedown = function(e) {
  mouse.set(e.clientX, e.clientY);
  mouse.normal.update();
  spring.b.makeFixed();
  spring.b.position.copy(mouse).subSelf(two.scene.translation);
  window.addEventListener('mousemove', mousemove, false);
  window.addEventListener('mouseup', mouseup, false);
};
var mousemove = function(e) {
  mouse.set(e.clientX, e.clientY);
  mouse.normal.update();
  spring.b.position.copy(mouse).subSelf(two.scene.translation);
  window.addEventListener('mousemove', mousemove, false);
  window.addEventListener('mouseup', mouseup, false);
};
var mouseup = function(e) {
  spring.b.fixed = false;
  window.removeEventListener('mousemove', mousemove, false);
  window.removeEventListener('mouseup', mouseup, false);
};
window.addEventListener('mousedown', mousedown, false);

var amount = 32;
var joints = [];
var height = 250;
var angle = 0;

for (var i = 0; i < amount; i++) {
  var pct = i / (amount - 1);
  var x = 0;
  var y = pct * height;
  var a = new Two.Anchor(x, y);
  joints.push(a);
}

var trunk = new Two.Path(Two.Utils.map(Two.Utils.range(joints.length / 2), function() {
  return new Two.Anchor();
}), false, false);
trunk.noFill().stroke = 'rgb(150, 100, 75)';
trunk.linewidth = 12;
trunk.cap = 'round';

var range = 0.85;
var leaves = new Two.Path(Two.Utils.map(Two.Utils.range(joints.length * range * 2 - 1), function() {
  return new Two.Anchor(0, 0);
}), true, true);
leaves.noStroke().fill = 'rgb(125, 200, 50)';

var tree = two.makeGroup(trunk, leaves);
tree.translation.y -= 50;

two.add(tree);

two.scene.translation.set(two.width / 2, two.height / 2 + 100);

two.bind('update', function() {

  physics.update();

  angle = spring.b.position.x - spring.a.position.x;
  angle /= two.width / 4;

  var dist = height / amount;

  for (var i = 0; i < joints.length; i++) {

    var joint = joints[i];

    if (i <= 0) {
      continue;
    }

    var pj = joints[i - 1];
    var theta = i * Math.PI * angle / 180 - HALF_PI;

    joint.x = dist * Math.cos(theta) + pj.x;
    joint.y = dist * Math.sin(theta) + pj.y;

    if (i < trunk.vertices.length) {
      trunk.vertices[i].copy(joint);
    }

  }

  for (var j = 0; j < leaves.vertices.length / 2; j++) {

    var k = leaves.vertices.length - (j + 1);
    var pct = j / (leaves.vertices.length / 2);
    var ji = Math.floor((pct * range + (1 - range)) * joints.length);
    var joint = joints[ji];
    var ab;

    if (ji <= 0) {
      ab = Two.Utils.angleBetween(joint, joints[ji + 1]) + Math.PI;
    } else {
      ab = Two.Utils.angleBetween(joint, joints[ji - 1]);
    }

    var girth = 50 * Math.sin(Math.pow(pct, 0.33) * Math.PI);

    leaves.vertices[j].x = girth * Math.cos(ab - HALF_PI) + joint.x;
    leaves.vertices[j].y = girth * Math.sin(ab - HALF_PI) + joint.y;

    leaves.vertices[k].x = girth * Math.cos(ab + HALF_PI) + joint.x;
    leaves.vertices[k].y = girth * Math.sin(ab + HALF_PI) + joint.y;

  }

});
              
            
!
999px

Console