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 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<script>
/**
Mersenne Twister Class
*/
(function(root, factory) {
'use strict';
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.MersenneTwister = factory();
}
}(this, function() {
/**
* A standalone, pure JavaScript implementation of the Mersenne Twister pseudo random number generator. Compatible
* with Node.js, requirejs and browser environments. Packages are available for npm, Jam and Bower.
*
* @module MersenneTwister
* @author Raphael Pigulla <pigulla@four66.com>
* @license See the attached LICENSE file.
* @version 0.2.3
*/
/*
* Most comments were stripped from the source. If needed you can still find them in the original C code:
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
*
* The original port to JavaScript, on which this file is based, was done by Sean McCullough. It can be found at:
* https://gist.github.com/banksean/300494
*/
'use strict';
var MAX_INT = 4294967296.0,
N = 624,
M = 397,
UPPER_MASK = 0x80000000,
LOWER_MASK = 0x7fffffff,
MATRIX_A = 0x9908b0df;
/**
* Instantiates a new Mersenne Twister.
*
* @constructor
* @alias module:MersenneTwister
* @since 0.1.0
* @param {number=} seed The initial seed value.
*/
var MersenneTwister = function(seed) {
if (typeof seed === 'undefined') {
seed = new Date().getTime();
}
this.mt = new Array(N);
this.mti = N + 1;
this.seed(seed);
};
/**
* Initializes the state vector by using one unsigned 32-bit integer "seed", which may be zero.
*
* @since 0.1.0
* @param {number} seed The seed value.
*/
MersenneTwister.prototype.seed = function(seed) {
var s;
this.mt[0] = seed >>> 0;
for (this.mti = 1; this.mti < N; this.mti++) {
s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30);
this.mt[this.mti] =
(((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + this.mti;
this.mt[this.mti] >>>= 0;
}
};
/**
* Initializes the state vector by using an array key[] of unsigned 32-bit integers of the specified length. If
* length is smaller than 624, then each array of 32-bit integers gives distinct initial state vector. This is
* useful if you want a larger seed space than 32-bit word.
*
* @since 0.1.0
* @param {array} vector The seed vector.
*/
MersenneTwister.prototype.seedArray = function(vector) {
var i = 1,
j = 0,
k = N > vector.length ? N : vector.length,
s;
this.seed(19650218);
for (; k > 0; k--) {
s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525))) +
vector[j] + j;
this.mt[i] >>>= 0;
i++;
j++;
if (i >= N) {
this.mt[0] = this.mt[N - 1];
i = 1;
}
if (j >= vector.length) {
j = 0;
}
}
for (k = N - 1; k; k--) {
s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
this.mt[i] =
(this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941)) - i;
this.mt[i] >>>= 0;
i++;
if (i >= N) {
this.mt[0] = this.mt[N - 1];
i = 1;
}
}
this.mt[0] = 0x80000000;
};
/**
* Generates a random unsigned 32-bit integer.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.int = function() {
var y,
kk,
mag01 = new Array(0, MATRIX_A);
if (this.mti >= N) {
if (this.mti === N + 1) {
this.seed(5489);
}
for (kk = 0; kk < N - M; kk++) {
y = (this.mt[kk] & UPPER_MASK) | (this.mt[kk + 1] & LOWER_MASK);
this.mt[kk] = this.mt[kk + M] ^ (y >>> 1) ^ mag01[y & 1];
}
for (; kk < N - 1; kk++) {
y = (this.mt[kk] & UPPER_MASK) | (this.mt[kk + 1] & LOWER_MASK);
this.mt[kk] = this.mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 1];
}
y = (this.mt[N - 1] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
this.mt[N - 1] = this.mt[M - 1] ^ (y >>> 1) ^ mag01[y & 1];
this.mti = 0;
}
y = this.mt[this.mti++];
y ^= (y >>> 11);
y ^= (y << 7) & 0x9d2c5680;
y ^= (y << 15) & 0xefc60000;
y ^= (y >>> 18);
return y >>> 0;
};
/**
* Generates a random unsigned 31-bit integer.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.int31 = function() {
return this.int() >>> 1;
};
/**
* Generates a random real in the interval [0;1] with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.real = function() {
return this.int() * (1.0 / (MAX_INT - 1));
};
/**
* Generates a random real in the interval ]0;1[ with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.realx = function() {
return (this.int() + 0.5) * (1.0 / MAX_INT);
};
/**
* Generates a random real in the interval [0;1[ with 32-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.rnd = function() {
return this.int() * (1.0 / MAX_INT);
};
/**
* Generates a random real in the interval [0;1[ with 32-bit resolution.
*
* Same as .rnd() method - for consistency with Math.random() interface.
*
* @since 0.2.0
* @returns {number}
*/
MersenneTwister.prototype.random = MersenneTwister.prototype.rnd;
/**
* Generates a random real in the interval [0;1[ with 53-bit resolution.
*
* @since 0.1.0
* @returns {number}
*/
MersenneTwister.prototype.rndHiRes = function() {
var a = this.int() >>> 5,
b = this.int() >>> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
};
var instance = new MersenneTwister();
/**
* A static version of [rnd]{@link module:MersenneTwister#rnd} on a randomly seeded instance.
*
* @static
* @function random
* @memberof module:MersenneTwister
* @returns {number}
*/
MersenneTwister.random = function() {
return instance.rnd();
};
return MersenneTwister;
}));
</script>
<canvas id="canvas"></canvas>
body {
margin: 0;
padding: 0;
background-color: #000;
overflow: hidden;
}
canvas {
position: absolute;
top: 0; left: 0;
display: block;
width: 100%;
height: 100%;
}
#stats {
position: absolute;
top: 0;
left: 0;
z-index: 666
}
.dg.ac {
z-index: 666!important;
}
/*
* SPURM
* Copyright MIT © <2014> Andrea Bovo <i@spleen.noo.name>
**/
var SPURM = {};
SPURM.Tentacle = function (obj) {
this.init = function () {
PIXI.Graphics.call(this);
this.interactive = true;
this.is_mouse_moving = false;
this.rnd = obj.randomizer;
this.head = this.rnd.random() * 4;
this.nodes = [];
this.num_nodes = 100;
this.theta = 0;
this.theta_muscle = 0;
this.tv = 0;
this.x = 0;
this.y = 0;
this.vx = this.rnd.random() - 0.5;
this.vy = this.rnd.random() - 0.5;
this.head = 2 + this.rnd.random();
this.speed = 8 + this.rnd.random() * 12;
this.speed_cf = 1e-2 + (this.rnd.random() * 10) / 50;
this.friction = this.min_friction + (this.rnd.random() * 10) / 100;
this.muscle_rg = this.min_muscle_rg + this.rnd.random() * 50;
this.count = 0;
this.muscle_fq = this.min_muscle_fq + (this.rnd.random() * 100) / 250;
for (var i = 0; i < this.num_nodes; i++) {
this.nodes.push({
x: 0,
y: 0
});
}
this.beginFill(0xff0000);
};
this.init();
};
SPURM.Tentacle.prototype = Object.create(PIXI.Graphics.prototype);
SPURM.Tentacle.constructor = SPURM.Tentacle;
SPURM.Tentacle.prototype.rnd = {};
SPURM.Tentacle.prototype.min_friction = 9.0e-1;
SPURM.Tentacle.prototype.min_muscle_rg = 20;
SPURM.Tentacle.prototype.min_muscle_fq = 1.0e-1;
SPURM.Tentacle.prototype.move = function (pos) {
var dy = pos.y - this.position.y,
dx = pos.x - this.position.x,
dist = dx * dx + dy * dy,
deg = Math.atan2(dy, dx) * (180 / Math.PI);
this.theta = deg;
this.position.y = pos.y;
this.position.x = pos.x;
};
SPURM.Tentacle.prototype.update = function () {
this.tv += 0.5 * this.rnd.random();
this.tv *= this.friction;
this.nodes[0].x = this.head * Math.cos((Math.PI / 180) * this.theta);
this.nodes[0].y = this.head * Math.sin((Math.PI / 180) * this.theta);
this.count += this.muscle_fq;
this.theta_muscle = this.muscle_rg * Math.sin(this.count);
this.nodes[1].x =
(0 - this.head) *
Math.cos((Math.PI / 180) * (this.theta + this.theta_muscle));
this.nodes[1].y =
(0 - this.head) *
Math.sin((Math.PI / 180) * (this.theta + this.theta_muscle));
var i = 1,
dx,
dy,
dist;
while (++i < this.num_nodes) {
dx = this.nodes[i].x - this.nodes[i - 2].x;
dy = this.nodes[i].y - this.nodes[i - 2].y;
dist = Math.sqrt(dx * dx + dy * dy);
this.nodes[i].x = this.nodes[i - 1].x + (dx * this.speed) / dist;
this.nodes[i].y = this.nodes[i - 1].y + (dy * this.speed) / dist;
}
this.clear();
this.moveTo(this.nodes[1].x, this.nodes[1].y);
i = 1;
while (++i < this.num_nodes) {
this.lineStyle(this.num_nodes / (i - 1), 0x000000, 0.65);
this.moveTo(this.nodes[i].x, this.nodes[i].y);
if (i == 2) {
//head
this.lineStyle(0, 0x000000, 0.99);
this.beginFill(0x000000, 0.99);
this.drawCircle(this.nodes[i].x, this.nodes[i].y, 40, 40);
this.endFill();
} else {
this.lineTo(this.nodes[i - 1].x, this.nodes[i - 1].y);
}
}
};
// /SPURM CLASS
const AI = function () {
this.friction = 10.;
this.muscle_fq = 2.5e-1;
this.muscle_rg = 20;
this.head = 10;
};
AI.prototype.update = function (t) {
t.friction = this.friction;
t.muscle_fq = this.muscle_fq;
t.muscle_rg = this.muscle_rg;
t.head = this.head;
};
/* SPURM */
const fps = 60;
const interval = 1000 / fps;
var stage,
stats,
renderer,
t,
ai,
view,
position = {
x: 0,
y: 0
},
path = [],
tween,
mt = new MersenneTwister(),
random_range = function (min, max) {
return mt.random() * (max - min) + min;
};
var elapsed = window.performance.now();
var now = 0,
delta;
ai = new AI();
const ui = new dat.GUI();
ui.add(ai, "friction", 1, 20);
ui.add(ai, "muscle_fq", 0.1, 0.5);
ui.add(ai, "head", 0, 20);
ui.add(ai, "muscle_rg", 0, 40);
mt.seed(20);
view = document.getElementById("canvas");
renderer = PIXI.autoDetectRenderer(
window.innerWidth,
window.innerHeight,
document.getElementById("canvas")
);
stage = new PIXI.Stage(0xff0000);
stats = new Stats();
stats.setMode(0);
document.body.appendChild(renderer.view);
document.body.appendChild(stats.domElement);
t = new SPURM.Tentacle({
randomizer: mt
});
t.position.x = window.innerWidth / 2;
t.position.y = window.innerHeight / 2;
t.theta = 50;
stage.addChild(t);
// fill bezier paths
for (var i = 0; i < 10; i++) {
path[i] = {
x: random_range(0, window.innerWidth),
y: random_range(0, window.innerHeight)
};
}
tween = TweenMax.to(position, 100, {
bezier: {
type: "soft",
values: path,
autoRotate: true
},
onUpdateParams: [t, position],
onUpdate: (t, position) => t.move(position),
repeat: -1,
loop: true,
yoyo: true
});
render();
function render() {
requestAnimationFrame(render);
now = window.performance.now();
delta = now - elapsed;
if (delta > interval) {
elapsed = now - (delta % interval);
stats.begin();
t.update();
ai.update(t);
stats.end();
renderer.render(stage);
}
}
Also see: Tab Triggers