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

              
                /**
 * CSS for Hartcore
 * Alexandre Andrieux @2015
 */

*,
body,
html {
	margin: 0;
	padding: 0;
	white-space: nowrap;
	overflow: hidden;
}
html,
body {
	height: 100%;
	width: 100%;
	background: rgb(250, 250, 250);
}
canvas {
	z-index: 0;
}
#black {
	z-index: 0;
}

              
            
!

JS

              
                /**
 * JS for Hartcore
 * Alexandre Andrieux (Icosacid) @2016
 * Main functions
 */
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);
    }
  }
};

/**
 * JS for Hartcore hc_base
 * Alexandre Andrieux @2016
 * Base structure for a new world
 */
var hc_base = {
	name: "base",
	ignite: function(args) {
    // Store arguments
    this.hue = args.hue || 0;// That's an example.
    this.seedsPop = args.seedsPop || 3;
    
    // 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');
    
    // Scale parameter
    this.canvasBase = Math.min(this.canvas.width, this.canvas.height);
    
    // Central point coordinates
    this.xC = this.canvas.width / 2;
		this.yC = this.canvas.height / 2;
    
    // Steps
    this.stepCount = 0;
    this.birthPeriod = 5;
    
    // Particles
    this.seeds = [];
    
    // Attracting star
    this.xStar = this.xC; // Initial
    this.yStar = this.yC; // Initial
    this.starRadius = 0;
    this.starAngle = 0;
    
    // Physical properties
    this.gravity = 400
    
    //// Unleash generation 1
    for (var i = 0; i < this.seedsPop; i++) {
    	this.birth();
    }
    
    // Draw background
    this.background(1);
	},
	background: function(opa) {
		this.ctx.rect(0, 0, this.canvas.width, this.canvas.height);
		this.ctx.fillStyle = "rgba(0, 0, 0, " + opa + ")";
		this.ctx.fill();
	},
	update: function(t) {
		this.evolve(t);
		this.move();
		this.draw();
	},
	evolve: function(t) {
		// Laws (increasing a variable)
    this.stepCount++;
    this.evolutionStar();
    
		// Effects (when this variable reaches a threshold, do sth)
		if (this.stepCount % this.birthPeriod == 0 && this.stepCount < 60000) {
      this.birth();
    }
	},
	evolutionStar: function() {
    this.starAngle = 0.05 * this.stepCount / Math.PI / 4;
    this.starRadius = 0;
    this.xStar = this.xC + this.starRadius * Math.cos(this.starAngle);
    this.yStar = this.yC + this.starRadius * Math.sin(this.starAngle);
	},
  birth: function(seed) {
    var launchAngle = - Math.PI * (0.5 + 0.5 * Math.sin(1 + 0.05 * Math.sqrt(2) * this.stepCount) );
    var launchSpeed = 0.015;
    var launchY = this.yC * 1.2;
    var launchX = this.xC * 0.8;
    var seed = seed || {
        xLast: launchX,
        x: launchX,
        xSpeed: launchSpeed * Math.cos(launchAngle),
        yLast: launchY,
        y: launchY,
        ySpeed: launchSpeed * Math.sin(launchAngle),
        age: 0
    };
    this.seeds.push(seed);
  },
	move: function() {
    // Move all particles
		for (var i = 0; i < this.seeds.length; i++) {
      var seed = this.seeds[i];
      // Get older
      seed.age++;
      // Save last position
      seed.xLast = seed.x;
      seed.yLast = seed.y;
      // Acceleration and calmer
      var distToStar = Math.sqrt(Math.pow(seed.x - this.xStar, 2) + Math.pow(seed.y - this.yStar, 2));
      var acc = -this.gravity * Math.min(Math.pow(distToStar, -2), 0.000001);
      // Speed
      seed.xSpeed += acc / distToStar * (seed.x - this.xStar);
      seed.ySpeed += acc / distToStar * (seed.y - this.yStar);
      // Position, with added canvas base size in order to maintain patterns accross zoom levels
      seed.x += seed.xSpeed * this.canvasBase;
      seed.y += seed.ySpeed * this.canvasBase;
      
    }
	},
	draw: function() {
    // Asteroids mode
    //this.background(0.05);
    
    // Star
    /*this.ctx.beginPath();
    this.ctx.arc(this.xStar, this.yStar, 1, 0, 2 * Math.PI, false);
    this.ctx.fillStyle = 'hsla(0, 100%, 100%, 0.002)';
    this.ctx.fill();*/
    
    // Particles
    for (var key in this.seeds) {
      var seed = this.seeds[key];
      
      // HSLA
      var hsla = this.traceLaws(seed);
      
      // Line width
      var wLine = 1;

      // Stroke
      this.ctx.strokeStyle = 'hsla(' + hsla.h + ', ' + hsla.s + ', ' + hsla.l + ", " + hsla.a + ")";
      this.ctx.lineWidth = wLine;
      this.ctx.lineCap = "round";
      this.ctx.beginPath();
      this.ctx.moveTo(seed.xLast, seed.yLast);
      this.ctx.lineTo(seed.x, seed.y);
      this.ctx.stroke();

    }
	},
  traceLaws: function(seed) {
    // Booleans to active one of the possible laws
    var linearFade = false;
    var triangular = true;
    var hash = false;
    var periodicHash = true;
    
    var age = seed.age - (-1); // Avoid 0
    
    // Start values
    var hue = 200;
    var sat = 80;
    var lum = 70;
    var opa = 0.19;
    
    // Linearly decreasing
    if (linearFade) {
      opa *= Math.max(0, 1 - age / 500);
    }
    
    // Linear fade in and fade out
    if (triangular) {
      var ageOfBirth = 0;
      var ageOfTop = 150;
      var ageOfDeath = 250;
      
      if (age <= ageOfBirth) {
        opa = 0;
      } else if (age <= ageOfTop) {
        // Linear increase
        opa *= (age - ageOfBirth) / (ageOfTop - ageOfBirth);
      } else if (age < ageOfDeath) {
        // Linear decrease
        opa *= 1 - (age - ageOfTop) / (ageOfDeath - ageOfTop);
      } else {
        opa = 0;
      }
    }
    
    // Hashed into zones of different intensities
    if (hash) {
      if (age < 100) {
        opa *= 0;
      } else if (age > 100 && age < 400) {
        opa *= 1;
      } else {
        opa = 0;
      }
    }
    
    // Periodic hashing
    if (periodicHash) {
      if (age/10 % 2 < 1) {
       opa /= 2;
      } else if (age/10 % 2 < 2) {
        
      }
    }
    
    var speed = Math.sqrt(Math.pow(seed.xSpeed, 2) + Math.pow(seed.ySpeed, 2));
    
    hue += 7000 * speed;
    lum = 70 - 1000 * speed;
      
    return {
      h: hue,
      s: sat + '%',
      l: lum + '%',
      a: opa
    };
  },
  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
    };
	}
};

// To clone worlds and summon with different arguments
/*
------- Add Underscore.js, and uncomment this -------
var hc_base2 = _.cloneDeep(hc_base);
hc_base2.name = 'base2';
*/

// Go go go!
hc.summon({
  world: hc_base,
  timeout: 0,
  args: {
    seedsPop: 1
  }
});
hc.ignite();
              
            
!
999px

Console