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

              
                
              
            
!

CSS

              
                /**
 * CSS for Hartcore
 * Alexandrix @2015
 */

*,
body,
html {
	margin: 0;
	padding: 0;
	white-space: nowrap;
	position: absolute;
	overflow: hidden;
}
html,
body {
	height: 100%;
	width: 100%;
	background: rgba(250,250,250,1);
}
canvas#rhodo {
  background: black;
}
canvas#rhodo.demo {
  z-index: 5;
  background: none;
}
canvas#rhodo2.demo {
  z-index: 4;
}
canvas#rhodo3.demo {
  z-index: 3;
}
canvas#rhodo4.demo {
  z-index: 2;
}
              
            
!

JS

              
                /**
 * JS for Hartcore
 * Alexandrix @2015
 * Main functions
 */
var demoMode = false;

var hc = {
  worlds: [],
  startTime: null,
  anim: null,
  summon: function() {
    for (var key in arguments) {
    	this.worlds.push(arguments[key]);
    }
  },
  ignite: function() {
    this.startTime = new Date().getTime();
    this.igniteWorlds();
    this.frame();
  },
  frame: function() {
    hc.paint(new Date().getTime());
    hc.anim = window.requestAnimationFrame(hc.frame);
  },
  paint: function(t) {
    for (var key in hc.worlds) {
      (function(k) {
        setTimeout(function() {
          hc.worlds[k].world.update(t - hc.startTime);
        }, hc.worlds[k].timeout);
      })(key);
    }
  },
  igniteWorlds: function() {
    for (var key in hc.worlds) {
 			hc.worlds[key].world.ignite(hc.worlds[key].args);
    }
    
    // Activate demo mode on canvases
    if (demoMode) { jQuery('canvas').addClass('demo'); }
  }
};

/**
 * JS for Hartcore hc_rhodo
 * Alexandrix @2015
 * World Rhodo
 */
var hc_rhodo = {
  name: "rhodo",
  ignite: function(args) {
    // Store arguments
    //// Base hue (see seedRadiusToHue) and luminosity
    this.hue = args.hue || 0;
    this.lum = args.lum || '50%';
    this.sat = args.sat || '80%';
    //// Branches
    this.seedsPop = args.seedsPop || 5;
    //// Base width (check out seedRadiusToLineWidth)
    this.sepalBase = args.sepalBase || 15;
    //// Branch length (check out seedRadiusToLineWidth)
    this.sepalLength = args.sepalLength || 10;
    //// Angle offset for flower branches
    this.extraAngle = args.extraAngle || 0;
    //// Birth speed
    this.eraDuration = args.eraDuration || 25;
    //// Design functions mapping radius to various visual parameters
    this.seedRadiusToHue = args.seedRadiusToHue || (function(r) {
      return (this.hue + r/20);
    });
    this.seedRadiusToSaturation = args.seedRadiusToSaturation || (function(r) {
      return this.sat;
    });
    this.seedRadiusToLightness = args.seedRadiusToLightness || (function(r) {
      return this.lum;
    });
    this.seedRadiusToLineWidth = args.seedRadiusToLineWidth || (function(r) {
      return (2 * Math.max(this.sepalBase - r / this.sepalLength, 0));
  	});
    this.seedRadiusToOpacity = args.seedRadiusToOpacity || (function(r) {
      return 0.01;
  	});
    
    // Initialize own canvas
    var canvas = document.createElement('canvas');
    canvas.class = 'hart';
    canvas.id = this.name;
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    document.body.appendChild(canvas);
	
    // Get and store canvas context
    this.canvas = document.getElementById(canvas.id);
    this.ctx = this.canvas.getContext('2d');

    // Variable 1
    this.var1 = 0;
    this.var1count = 0;
    this.var1var = 200;

    // Variable 2
    this.var2 = 0;

    // Variable 3 (births)
    this.eraCount = 0;//Needs this.eraDuration

    // Particles
    this.seeds = [];
    //// Start point
    this.xC = this.canvas.width / 2;
    this.yC = this.canvas.height / 2;
    //// Unleash generation 1
    for (var i = 0; i < this.seedsPop; i++) {
      var datsMyAngle = i / this.seedsPop * 2 * Math.PI;
      this.birth({
        rLast: 0,
        r: 0,
        rSpeed: 0,
        thetaLast: this.extraAngle + datsMyAngle,
        theta: this.extraAngle + datsMyAngle,
        thetaSpeed: 0 // Add r and theta to contructor
      });
    }
    
  },
  background: function() {
    // Not called, background in CSS this time.
    this.ctx.rect(0, 0, this.canvas.width, this.canvas.height);
    this.ctx.fillStyle = "#000";
    this.ctx.fill();
  },
  update: function(t) {
    this.evolve(t);
    this.move(t);
    this.draw();
  },
  evolve: function(t) {
    // Laws (increasing a variable)
    this.evolutionVar1();
    this.evolutionVar2(t);
    this.evolutionVar3();
    // Effects (when this variable reaches a threshold, do sth)
  },
  evolutionVar1: function() {
    this.var1count++;
    this.var1 = 0.5 + 0.5 * Math.sin(this.var1count / this.var1var * 2 * Math.PI);
  },
  evolutionVar2: function(t) {
    this.var2 = Math.sin(t / 700);
  },
  evolutionVar3: function() {
    this.eraCount++;
  },  
  birth: function(seed) {
    this.seeds.push(seed);
  },
  move: function(t) {
    // Time flies, buddy.
    var t = t / 500;
		
    // Move all particles
    for (var i = 0; i < this.seeds.length; i++) {
      var seed = this.seeds[i];
      seed.rLast = seed.r;
      seed.thetaLast = seed.theta;

      // Mirror seeds alternatively (not used)
      //var way = (i % 2 == 0 ? 1 : -1);
			
      // Core motion laws
      seed.rSpeed = 0.8 + 0.5 * Math.sin(t);
      seed.thetaSpeed += 0.0005 * (-0.5 + Math.random());

      // Calmers
      seed.thetaSpeed = Math.max(Math.min(seed.thetaSpeed, 0.005), -0.005);
			
      // Dummy physics
      seed.theta += seed.thetaSpeed;
      seed.r += seed.rSpeed;
    }
    
    // Give birth to new particles every now and then
    if (this.eraCount >= this.eraDuration && this.seeds.length < 500) {
      // Take the (this.seedPop) last particles and clone
      var len = this.seeds.length;
      for (var j = len - 1; j >= len - this.seedsPop; j--) {
        var seedClone = _.clone(this.seeds[j]);
        
        // Give extra motion for a better life :')
        var splitRatio = 0.33;//will bend sepals even more with values close to 0 or 1
        var thetaSpeedExtra = 0.0005 * (Math.random() > splitRatio ? 1 : -1);
        seedClone.thetaSpeed += thetaSpeedExtra;
   			
        // Push to this.seeds
        this.birth(seedClone);
      }
      this.eraCount = 0;//nope, you don't want this code to freeze your browser
    }
  },
  draw: function() {
    if (demoMode) this.background();
    for (var key in this.seeds) {
      var seed = this.seeds[key];
      this.drawFlower(seed);
    }
  },
  drawFlower: function(seed) {  
		// Switch to XY space
    var sSold = this.spaceShift(seed.rLast, seed.thetaLast);
    var sSnew = this.spaceShift(seed.r, seed.theta);
    
    // HSLA
    var h = this.seedRadiusToHue(seed.r);
    var sat = this.seedRadiusToSaturation(seed.r);
    var lum = this.seedRadiusToLightness(seed.r);
    var opa = this.seedRadiusToOpacity(seed.r);
	
    // Demo mode opacity forcing
    if (demoMode) opa = 1;
    // Line width
    var wLine = this.seedRadiusToLineWidth(seed.r);
    
    // 1: Backlight
    this.ctx.strokeStyle = 'hsla(' + h + ', ' + sat + ', ' + lum + ", " + opa + ")";
    this.ctx.lineWidth = wLine;
    this.ctx.lineCap = "round";
    this.ctx.beginPath();
    this.ctx.moveTo(sSold.x, sSold.y);
    this.ctx.lineTo(sSnew.x, sSnew.y);
    this.ctx.stroke();

    // 2: Front stroke
    this.ctx.strokeStyle = 'hsla(' + h + ', ' + sat + ', ' + lum + ", " + opa/2 + ")";
    this.ctx.lineWidth = wLine;
    this.ctx.lineCap = "round";
    this.ctx.beginPath();
    this.ctx.moveTo(sSold.x, sSold.y);
    this.ctx.lineTo(sSnew.x, sSnew.y);
    this.ctx.stroke();
  },
  spaceShift: function(r, theta) {
    var x = this.xC + r * Math.cos(theta);
    var y = this.yC - r * Math.sin(-theta);
    return {
      x: x,
      y: y
    };
  }
};

// Second flower
var hc_rhodo2 = _.cloneDeep(hc_rhodo);
hc_rhodo2.name = 'rhodo2';

// Third flower
var hc_rhodo3 = _.cloneDeep(hc_rhodo);
hc_rhodo3.name = 'rhodo3';

// Fourth flower
var hc_rhodo4 = _.cloneDeep(hc_rhodo);
hc_rhodo4.name = 'rhodo4';

// Flower time
hc.summon({
  world: hc_rhodo,
  timeout: 9000,
  args: {
    hue: 195,
    lum: '100%',
    sepalBase: 30,
    sepalLength: 15,
    eraDuration: 25,
    extraAngle: 0,
    seedRadiusToLineWidth: function(r) {
      //return (this.sepalBase * Math.exp( - r / this.sepalLength ));
      return (this.sepalBase - r / this.sepalLength);
  	},
    seedRadiusToOpacity: function(r) {
      return (0.07 * (1 - Math.exp(-r/150)) );
    }
  }
}, {
  world: hc_rhodo2,
  timeout: 6000,
  args: {
    //hue: 195,
    hue: 5,
    lum: '30%',
    sepalBase: 50,
    sepalLength: 135,
    eraDuration: 8,
    extraAngle: 0.314,
    seedRadiusToHue: function(r) {
      var result = this.hue + r / 12;
      return result;
    },
    seedRadiusToLightness: function(r) {
      return '45%';
    },
    seedRadiusToLineWidth: function(r) {
      return (this.sepalBase * Math.exp( - r / this.sepalLength ));
  	},
    seedRadiusToOpacity: function(r) {
      return (0.1 * (1 - Math.exp(-r/100)) );
    }
  }
}, {
  world: hc_rhodo3,
  timeout: 3000,
  args: {
    hue: 200,
    lum: '100%',
    sepalBase: 15,
    sepalLength: 15,
    extraAngle: 0.628,
    seedRadiusToHue: function(r) {
      return (this.hue + r / 6);
    },
    seedRadiusToOpacity: function(r) {
      return (0.05);
  	}
  }
}, {
  world: hc_rhodo4,
  timeout: 0,
  args: {
    hue: 0,
    sepalBase: 20,
    sepalLength: 35,
    eraDuration: 3,
    extraAngle: 0.628,
    seedRadiusToOpacity: function(r) {
      return (0.03);
  	},
    seedRadiusToLineWidth: function(r) {
      return (this.sepalBase * Math.exp( - r / this.sepalLength ));
  	},
    seedRadiusToHue: function(r) {
      return (this.hue + r / 2);
    },
    seedRadiusToSaturation: function(r) {
      return '85%';
    },
    seedRadiusToLightness: function(r) {
      return '35%';
    }
  }
});

// Go, go, go!
hc.ignite()
              
            
!
999px

Console