Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <canvas id="canv"></canvas>
<p>Cocoon</p>
<!--
Titillating Tip:
After connecting a few, wait a second for them  to settle down, then add another from further away to see how cocoon movement / responds to force based on distance && velocity.
!-->

              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Titillium+Web:700);
body{
  width:100%; 
  overflow:auto;
  user-select:none;
}
p{
  top:2em;
  width:100%; 
  text-align:center;
  position:absolute;
  color:  hsla(175, 65%, 5%, 1);
  font-size:5em;
  font-family: 'Titillium Web', sans-serif;
  letter-spacing:40px;
}
canvas{
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
}
              
            
!

JS

              
                var w = window.innerWidth;
var h = window.innerHeight;
var spr_d = (w > h) ?h/3 : w/3;
var $;
var FPS = 30;
var num = 5;
var grav = 0;
var fric = 0.001;
var spr;
var node2;
var parts = [];
var springs = []; 
var count = 0;
    
window.onload = function() {
  node2 = new Node2();
	c = document.getElementById('canv');
	c.width = w;
	c.height = h;
	$ = c.getContext('2d');
  $.fillStyle = 'hsla(175, 5%, 95%, 1)';
  $.fillRect(0,0,w,h);
  $.fill();
  $.strokeStyle = 'hsla(175, 15%, 5%, .65)';
  $.lineWidth = 1;
 
    var p = new Part();
    p.setup({
        x : w/2,
        y : h/2,
        len : 0,
        ang : 0,
        $$: $,
        rad : 10,
        fric : fric,
        frc : {x:0, y:grav},
        color: 'hsla(175, 65%, 5%, 1)',
        fix: true
    });
    parts.push(p);
    
    if(document.addEventListener){
        document.addEventListener('click', add); 
    
    }else if(document.attachEvent){
    }
	setInterval( function(){
        if(parts.length){
            upd();
            window.requestAnimationFrame(draw);
        }
	}, 1000 / FPS );
}
function upd(){
    for(var i = 0; i<parts.length; i++){
        parts[i].reset({x:Math.random()-0.5, y:Math.random()-0.5});
    }
    for(i = 0; i<springs.length; i++){
        springs[i].upd();
    }
    for(i = 0; i<parts.length; i++){
        parts[i].upd();
    }
    
}
function draw(){
   $.fillStyle = 'hsla(175, 5%, 95%, .5)';
   $.fillRect(0,0,w,h);
   $.fill();
    for(var i = 0; i<springs.length; i++){
        springs[i].draw();
    }
    for(i = 0; i<parts.length; i++){
        parts[i].draw();
    }
}
function add(e){
    parts.push(create(e.clientX, e.clientY));
    for(i = parts.length-2; i>=0; i--){
        var spr = new Spring({
            dist : 260,
            springy : 0.01,
            pa : parts[parts.length-1],
            pb : parts[i],
            $$ : $
        });
        springs.push(spr);
        
    }
}

var Node2 = function(_x, _y){
    this.x = _x || 0;
    this.y = _y || 0;
};
Node2.prototype = {
    x : 0,
    y : 0,
    val : function(_x, _y) {
        if(_x) this.x = _x;
        if(_y) this.y = _y;
        return this;
    },
    len: function(_x, _y){
        this.val(_x, _y);
        return Math.sqrt(this.x*this.x+this.y*this.y);
    },
    norm: function(_x, _y){
        this.val(_x, _y);
        var len = this.len();
        return {x:this.x/len, y:this.y/len};
    },
    sum : function(obj1, obj2){
		this.x = obj1.x + obj2.x;
		this.y = obj1.y + obj2.y;
        return {x: this.x, y: this.y};
    },
    diff : function(obj1, obj2){
        this.x = obj1.x - obj2.x;
		this.y = obj1.y - obj2.y;
        return {x: this.x, y: this.y};
    },
	scale : function(obj, scale){
		this.x = obj.x * scale;
		this.y = obj.y * scale;
        return {x: this.x, y: this.y}; 
	}
};

function create(_x, _y){
    var p = new Part();
    p.setup({
        x : _x,
        y : _y,
        len : Math.random()*20,
        ang : Math.random()*360,
        $$: $,
        rad : 10,
        fric : fric,
        frc : {x:0, y:grav},
        color: 'hsla(175, 65%, 5%, .9)'
    });
    return p;
}

var Part = function() {
    this.pos = {x:0, y:0};
    this.frc = {x:0, y:0};
    this.vel = {x:0, y:0};
    this.rad = 10;
    this.fric = 0.1;
    this.color = 'hsla(175, 65%, 5%, .75)';
    this.fix = false;
};
Part.prototype = {
    $$: null,
    setup: function(obj) {
        this.pos = {x:obj.x, y:obj.y};
        this.rad = obj.rad;
        this.fric = obj.fric;
        this.$$ = obj.$$;
        this.frc = obj.frc;
        var rad = obj.ang * Math.PI / 180;
        var velX = Math.round(Math.cos(rad)*obj.len);
        var velY = Math.round(Math.sin(rad)*obj.len);
        this.vel = { x:velX, y:velY };
        if(obj.color) this.color = obj.color;
        if(obj.fix) this.fix = true;
    },
    upd: function(){
		var _frc = node2.scale(this.vel, this.fric);
        this.frc = node2.diff(this.frc, _frc);
        this.vel = node2.sum(this.vel, this.frc);
        if(!this.fix) {
            this.pos = node2.sum(this.pos, this.vel);
        }
		var cocoon = false;
        if( this.pos.x > w - this.rad) {
            this.pos.x = w - this.rad;
            this.vel.x *= -1;
			cocoon = true;
        } else if( this.pos.x < this.rad ) {
			this.pos.x = this.rad;
            this.vel.x *= -1;
			cocoon = true;
		}
        if( this.pos.y > h - this.rad ) {
            this.pos.y = h - this.rad;
            this.vel.y *= -1;
			cocoon = true;
        } else if( this.pos.y < this.rad ) {
			this.pos.y = this.rad;
            this.vel.y *= -1;
			cocoon = true;
		}
		if( cocoon ) {
			this.vel = node2.scale(this.vel, 0.3);
		}
    },
    draw: function(){
        if(!this.fix){
            this.$$.beginPath();
            this.$$.fillStyle = this.color;
            this.$$.arc(Math.round(this.pos.x), Math.round(this.pos.y), this.rad, 0, Math.PI*2, true);
            this.$$.fill();
            this.$$.beginPath();
            this.$$.fillStyle = 'hsla(58, 95%, 55%, 1)';	
            this.$$.arc(Math.round(this.pos.x), Math.round(this.pos.y), 3, 0, Math.PI*2, true);
            this.$$.fill();
        }
    },
    reset: function(_frc){
        this.frc = _frc;
    },
    addFrc: function(_frc){
        this.frc = node2.sum(this.frc, _frc);
    }
}

var Spring = function(obj){
	this.dist = obj.dist;
	this.springy = obj.springy;
	this.pa = obj.pa;
	this.pb = obj.pb;
	this.$$ = obj.$$;
};
Spring.prototype = {
	upd : function(){
        var diff = node2.diff(this.pa.pos, this.pb.pos);
        var curDis = node2.len(diff.x, diff.y);
        var sprFrc = this.springy * (this.dist - curDis);
        var param = node2.norm(diff.x, diff.y);
        
        this.pa.addFrc({x:param.x*sprFrc, y:param.y*sprFrc});
        this.pb.addFrc({x:-param.x*sprFrc, y:-param.y*sprFrc});
	},
	draw : function() {
        if( !this.pa.fix && !this.pb.fix){
            var diff = node2.diff(this.pa.pos, this.pb.pos);
            var curDis = node2.len(diff.x, diff.y);
            var alpha = 1-curDis/spr_d;
            this.$$.beginPath();
            this.$$.strokeStyle = 'hsla(175, 15%, 5%, ),'+alpha+')';
            this.$$.moveTo(this.pa.pos.x, this.pa.pos.y);
            this.$$.lineTo(this.pb.pos.x, this.pb.pos.y);
            this.$$.stroke();
      }
	}
}

              
            
!
999px

Console