HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!-- Pseudo 3d planet. Could use some work. Clouds are a little goofy...-->
<canvas id="c"></canvas>
* {
margin: 0;
padding: 0;
overflow:hidden;
font-family:sans-serif;
}
body {
background:#2c3e50;
}
canvas {
margin:0 auto;
display:block;
}
/*
Copyright (c) 2013 Mike Ferron mikeferron.com, ferronsays at codepen.io (https://codepen.io/ferronsays)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
var scene;
settings = {
POINTS : 200,
RADIUS : 200,
ROTATION : 0.006,
MAX_SPEED : 2,
FOV : 400,
Z_MAX : 388,
MAX_SPEED : 0.015,
MAX_FORCE : 0.10,
MAX_SIZE : 5,
DESIRED_SEPARATION : .5,
NEIGHBOR_RADIUS : 50,
SEPARATION_WEIGHT : 2,
ALIGNMENT_WEIGHT : 1,
COHESION_WEIGHT : 1
};
var PointStyle = {
earth : 0,
water : 1,
cloud : 2
}
var Radii = [140, 120, 170];
var Scales = [15, 20, 6];
var colors = ['#27ae60', '#2980b9', '#ecf0f1'];
Scene = function(canvas_id) {
this.init(canvas_id);
};
Scene.prototype.init = function(canvas_id) {
this.current_time = new Date().getTime();
this.dt = 0;
this.canvas = document.getElementById(canvas_id);
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.width = this.canvas.width;
this.height = this.canvas.height;
this.ctx = this.canvas.getContext('2d');
this.ctx.clearRect(0, 0, this.width, this.height);
this.ctx.fillStyle = 'rgb(44, 62, 80)';
this.ctx.fillRect(0, 0, this.width, this.height);
this.points = new Array();
this.clouds = new Array();
//water/earth
for(var i = 0; i < 1800; i++)
{
if(i > 1600)
type = 0;
else
type = 1;
theta = Math.random()*2*Math.PI;
phi = Math.acos(Math.random()*2-1);
x0 = Radii[type]*Math.sin(phi)*Math.cos(theta);
y0 = Radii[type]*Math.sin(phi)*Math.sin(theta);
z0 = Radii[type]*Math.cos(phi);
var sign = Math.random() > 0.5 ? 1 : -1;
vel = new Vector();
var point = new Point(
new Vector(x0, y0, z0),
vel,
theta,
phi,
this.ctx);
point.type = type;
this.points.push(point);
}
//clouds
for(var i = 0; i < 200; i++)
{
var type = 2;
theta = Math.random()*2*Math.PI;
phi = Math.acos(Math.random()*2-1);
x0 = Radii[type]*Math.sin(phi)*Math.cos(theta);
y0 = Radii[type]*Math.sin(phi)*Math.sin(theta);
z0 = Radii[type]*Math.cos(phi);
var sign = Math.random() > 0.5 ? 1 : -1;
var vel = new Vector(sign*Math.random()/200, sign*Math.random()/200, 0);
var point = new Point(
new Vector(x0, y0, z0),
vel,
theta,
phi,
this.ctx);
point.type = type;
this.clouds.push(point);
}
};
Scene.prototype.enable = function() {
var that = this;
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame
})();
this.animate(new Date().getTime());
function doResize()
{
that.canvasResize();
}
var endResize;
window.onresize = function(e) {
clearTimeout(endResize);
endResize = setTimeout(doResize, 100);
};
return this;
};
Scene.prototype.animate = function(time)
{
var that = this;
this.animationFrame = requestAnimFrame( function(){ that.animate(new Date().getTime());} );
this.update(time);
};
Scene.prototype.disable = function() {
window.cancelAnimationFrame(this.animationFrame);
return this;
};
Scene.prototype.canvasResize = function()
{
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.width = this.canvas.width;
this.height = this.canvas.height;
};
Scene.prototype.update = function(time) {
this.dt = time - this.current_time;
this.current_time = time;
this.draw();
for (i in this.points) {
this.points[i].step();
}
this.points.sort(zSort);
for (i in this.points) {
this.points[i].draw();
}
for (i in this.clouds) {
this.clouds[i].step(this.clouds);
}
this.clouds.sort(zSort);
for (i in this.clouds) {
this.clouds[i].draw();
}
};
Scene.prototype.draw = function()
{
//this.ctx.clearRect(0, 0, this.width, this.height);
this.ctx.fillStyle = 'rgba(44, 62, 80, 0.1)';
this.ctx.fillRect(0, 0, this.width, this.height);
};
/*
*
*/
Point = function(location, velocity, theta, phi, ctx){
this.init(location, velocity, theta, phi, ctx);
};
Point.prototype.init = function(location, velocity, theta, phi, ctx){
this.pos = location.copy();
this.vel = velocity.copy();
this.theta = theta;
this.phi = phi;
this.ctx = ctx;
};
Point.prototype.step = function(neighbors)
{
if(this.type == PointStyle.cloud)
{
var z = this.pos.z;
var acceleration = this.flock(neighbors);
this.vel.add(acceleration).limit(settings.MAX_SPEED);
this.pos.add(this.vel);
this.pos.z = z;
this.rotateX(this.vel.x);
this.rotateY(this.vel.y);
this.rotateY(settings.ROTATION/2);
}else{
this.rotateY(settings.ROTATION);
}
};
Point.prototype.rotateY = function(angle)
{
x = this.pos.x;
z = this.pos.z;
cosRY = Math.cos(angle);
sinRY = Math.sin(angle);
tempz = z;
tempx = x;
x= (tempx*cosRY)+(tempz*sinRY);
z= (tempx*-sinRY)+(tempz*cosRY);
this.pos.x = x;
this.pos.z = z;
};
Point.prototype.rotateX = function(angle)
{
y = this.pos.y;
z = this.pos.z;
cosRY = Math.cos(angle);
sinRY = Math.sin(angle);
tempz = z;
tempy = y;
y= (tempy*cosRY)+(tempz*sinRY);
z= (tempy*-sinRY)+(tempz*cosRY);
this.pos.y = y;
this.pos.z = z;
};
Point.prototype.rotateZ = function(angle)
{
x = this.pos.x;
y = this.pos.y;
cosRY = Math.cos(angle);
sinRY = Math.sin(angle);
tempy = y;
tempx = x;
x= (tempx*cosRY)+(tempy*sinRY);
y= (tempx*-sinRY)+(tempy*cosRY);
this.pos.x = x;
this.pos.y = y;
}
Point.prototype.flock = function(neighbors)
{
var separation = this.separate(neighbors).multiply(settings.SEPARATION_WEIGHT);
var alignment = this.align(neighbors).multiply(settings.ALIGNMENT_WEIGHT);
var cohesion = this.cohere(neighbors).multiply(settings.COHESION_WEIGHT);
return separation.add(alignment).add(cohesion);
};
Point.prototype.cohere = function(neighbors){
var sum = new Vector(0, 0);
var count = 0;
for(boid in neighbors)
{
var d = this.pos.distance(neighbors[boid].pos);
if(d > 0 && d < settings.NEIGHBOR_RADIUS)
{
if(Math.abs(this.pos.z - neighbors[boid].pos.z) < 20)
{
sum.add(neighbors[boid].pos);
count++;
}
}
}
if( count > 0)
return this.steer_to(sum.divide(count));
else
return sum;
};
Point.prototype.steer_to = function(target)
{
var desired = Vector.subtract(target, this.pos);
var d = desired.magnitude();
var steer;
if(d > 0)
{
desired.normalize();
if(d < 100)
desired.multiply(settings.MAX_SPEED*(d/100));
else
desired.multiply(settings.MAX_SPEED);
steer = desired.subtract(this.vel);
steer.limit(settings.MAX_FORCE);
}else{
steer = new Vector(0, 0);
}
return steer;
};
Point.prototype.align = function(neighbors){
var mean = new Vector();
var count = 0;
for(boid in neighbors)
{
var d = this.pos.distance(neighbors[boid].pos);
if(d > 0 && d < settings.NEIGHBOR_RADIUS)
{
if(Math.abs(this.pos.z - neighbors[boid].pos.z) < 20)
{
mean.add(neighbors[boid].vel);
count++;
}
}
}
if (count > 0)
mean.divide(count)
mean.limit(settings.MAX_FORCE);
return mean;
};
Point.prototype.separate = function(neighbors){
var mean = new Vector();
var count = 0;
for(boid in neighbors)
{
var d = this.pos.distance(neighbors[boid].pos);
if(d > 0 && d < settings.DESIRED_SEPARATION)
{
if(Math.abs(this.pos.z - neighbors[boid].pos.z) < 20)
{
mean.add(Vector.subtract(this.pos, neighbors[boid].pos).normalize().divide(d));
count++;
}
}
}
if (count > 0)
mean.divide(count);
return mean;
};
Point.prototype.influence = function()
{
var g = new Vector();
return g;
};
Point.prototype.draw = function(){
x3d = this.pos.x;
y3d = this.pos.y;
z3d = this.pos.z;
var scale = settings.FOV/(settings.FOV+z3d);
var x2d = (x3d * scale) + this.ctx.canvas.width/2;
var y2d = (y3d * scale) + this.ctx.canvas.height/2;
if(this.pos.z > 25)
return;
this.ctx.save();
this.ctx.fillStyle = colors[this.type];
this.ctx.beginPath();
this.ctx.arc(x2d, y2d, Math.abs(scale*Scales[this.type]), 0, 2*Math.PI, false);
this.ctx.closePath();
this.ctx.fill();
this.ctx.restore();
};
//from https://github.com/hornairs/blog/blob/master/assets/coffeescripts/flocking/vector.coffee
Vector = (function() {
var name, _fn, _i, _len, _ref;
_ref = ['add', 'subtract', 'multiply', 'divide'];
_fn = function(name) {
return Vector[name] = function(a, b) {
return a.copy()[name](b);
};
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
name = _ref[_i];
_fn(name);
}
function Vector(x, y, z) {
var _ref;
if (x == null) {
x = 0;
}
if (y == null) {
y = 0;
}
if (z == null) {
z = 0;
}
_ref = [x, y, z], this.x = _ref[0], this.y = _ref[1], this.z = _ref[2];
}
Vector.prototype.copy = function() {
return new Vector(this.x, this.y, this.z);
};
Vector.prototype.magnitude = function() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};
Vector.prototype.normalize = function() {
var m;
m = this.magnitude();
if (m > 0) {
this.divide(m);
}
return this;
};
Vector.prototype.limit = function(max) {
if (this.magnitude() > max) {
this.normalize();
return this.multiply(max);
} else {
return this;
}
};
Vector.prototype.heading = function() {
return -1 * Math.atan2(-1 * this.y, this.x);
};
Vector.prototype.eucl_distance = function(other) {
var dx, dy, dz;
dx = this.x - other.x;
dy = this.y - other.y;
dz = this.z - other.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
};
Vector.prototype.distance = function(other, dimensions) {
var dx, dy, dz;
if (dimensions == null) {
dimensions = false;
}
dx = Math.abs(this.x - other.x);
dy = Math.abs(this.y - other.y);
dz = Math.abs(this.z - other.z);
if (dimensions) {
dx = dx < dimensions.width / 2 ? dx : dimensions.width - dx;
dy = dy < dimensions.height / 2 ? dy : dimensions.height - dy;
}
return Math.sqrt(dx * dx + dy * dy + dz * dz);
};
Vector.prototype.subtract = function(other) {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
return this;
};
Vector.prototype.add = function(other) {
this.x += other.x;
this.y += other.y;
this.z += other.z;
return this;
};
Vector.prototype.divide = function(n) {
var _ref;
_ref = [this.x / n, this.y / n, this.z / n], this.x = _ref[0], this.y = _ref[1], this.z = _ref[2];
return this;
};
Vector.prototype.multiply = function(n) {
var _ref;
_ref = [this.x * n, this.y * n, this.z * n], this.x = _ref[0], this.y = _ref[1], this.z = _ref[2];
return this;
};
Vector.prototype.dot = function(other) {
return this.x * other.x + this.y * other.y + this.z * other.z;
};
Vector.prototype.projectOnto = function(other) {
return other.copy().multiply(this.dot(other));
};
Vector.prototype.wrapRelativeTo = function(location, dimensions) {
var a, d, key, map_d, v, _ref;
v = this.copy();
_ref = {
x: "width",
y: "height"
};
for (a in _ref) {
key = _ref[a];
d = this[a] - location[a];
map_d = dimensions[key];
if (Math.abs(d) > map_d / 2) {
if (d > 0) {
v[a] = (map_d - this[a]) * -1;
} else {
v[a] = this[a] + map_d;
}
}
}
return v;
};
Vector.prototype.invalid = function() {
return (this.x === Infinity) || isNaN(this.x) || this.y === Infinity || isNaN(this.y) || this.z === Infinity || isNaN(this.z);
};
return Vector;
})();
function initialize() {
scene = new Scene("c");
scene.enable();
}
function zSort (a, b) {
return (b.pos.z - a.pos.z);
}
window.onload = function() {
initialize();
};
Also see: Tab Triggers