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.
<link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">
<span onclick="toggleOptions()">Options</span>
<span onclick="gen()">Generate</span>
<a href="http://xaloez.com/" target="_blank">App by Ethan Alexander Shulman</a>
<div id="out">
Generating text...
</div>
<div id="opt">
Source:<br>
<textarea id="source" cols="20" rows="10">
I am Sam.
Sam I am.
That Sam-I-am!
That Sam-I-am!
I do not like
That Sam-I-am!
Do you like
Green eggs and ham?
I do not like them,
Sam-I-am.
I do not like
Green eggs and ham.
Would you like them
Here or there?
I would not like them
Here or there.
I would not like them
Anywhere.
I do not like
Green eggs and ham.
I do not like them,
Sam-I-am.
Would you like them
In a house?
Would you like them
With a mouse?
I do not like them
In a house.
I do not like them
With a mouse.
I do not like them
Here or there.
I do not like them
Anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Would you eat them
In a box?
Would you eat them
With a fox?
Not in a box.
Not with a fox.
Not in a house.
Not with a mouse.
I would not eat them here or there.
I would not eat them anywhere.
I would not eat green eggs and ham.
I do not like them, Sam-I-am.
Would you? Could you?
In a car?
Eat them! Eat them!
Here they are.
I would not, could not,
In a car.
You may like them.
You will see.
You may like them
In a tree!
I would not, could not in a tree.
Not in a car! You let me be.
I do not like them in a box.
I do not like them with a fox.
I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere. I do not like green eggs and ham.
I do not like them, Sam-I-am.
</textarea><br><br>
Depth: <input id="depth" type="text" size="4" value="3"/><br><br>
Drunkenness: <input id="drunk" type="range" min="0" max="100" value="20"/><br><br>
Generated Text Length: <input id="len" type="text" size="8" value="512"/><br><br>
Word-space: <input id="wordspace" type="checkbox"/>
</div>
body {
background-color: #3265FF;
font-family: 'Josefin Sans', sans-serif;
font-size: 20px;
color: #253052;
text-shadow: 1px 1px 1px #079C43;
cursor: default;
}
span,a {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
margin-left: 50px;
color: #253000;
cursor: pointer;
}
span:hover,a:hover {
color: #2530FF;
}
a {
position: absolute;
bottom: 0px;
right: 5px;
}
div {
overflow-y: auto;
text-align: center;
position: absolute;
left: 50px;
top: 50px;
right: 50px;
bottom: 30px;
padding: 12px;
color: black;
background-color: #1E56FF;
}
#out {
}
#opt {
display: none;
}
input[type="text"],textarea {
background-color: #3265FF;
}
#source {
width: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
//Ethan Alexander Shulman 2016
//Procedural text generation from source text using something like a Markov Chain
var optionsOpen = false, outEle, optEle,
generating = false,
ge = function(s) {return document.getElementById(s);};
//initialize on load
window.onload = function() {
outEle = ge("out");
optEle = ge("opt");
//initial generation
setTimeout(gen);
};
//toggle options menu
function toggleOptions() {
var outD = "none",
optD = "none";
if (optionsOpen) outD = "block";
else optD = "block";
outEle.style.display = outD;
optEle.style.display = optD;
optionsOpen = !optionsOpen;
}
//generate text
function gen() {
if (generating) return;//only allow 1 generation at a time
generating = true;
var src = ge("source").value,
wordSpace = ge("wordspace").checked,
drunkenness = parseInt(ge("drunk").value)/100.0,
seqDepth = parseInt(ge("depth").value),
vocab = [], sa = new Sequence(seqDepth), ss;
//build vocabulary
if (wordSpace) {
//word vocab
ss = src.replace("\n","").split(" ");
var i = ss.length;
while (i--) {
var ind = vocab.indexOf(ss[i]);
if (ind === -1) {
var ind = vocab.length;
vocab.push(ss[i]);
ss[i] = ind;
} else {
ss[i] = ind;
}
}
} else {
//letter vocab
ss = new Uint32Array(src.length);
var i = src.length;
while (i--) {
var ind = vocab.indexOf(src[i]);
if (ind === -1) {
ss[i] = vocab.length;
vocab.push(src[i]);
} else {
ss[i] = ind;
}
}
}
//train/learn probabilities from source text
var i = -1,
l = ss.length;
while (++i < l) {
sa.In(String.fromCharCode(ss[i]));
}
//generate new text
sa.Reset();
var i = parseInt(ge("len").value),
g = new Uint32Array(i),
c = -1;
while (++c < i) {
var k = -1,
r = sa.Next(drunkenness);
if (r === -1) {
r = ~~(Math.random()*vocab.length);//no next guess because there is no current memory state, seed with random vocab character
}
g[c] = r;
sa.In(String.fromCharCode(r),true);
}
//convert generated new text from vocab to words/letters
var out = "";
var i = -1,
l = g.length;
while (++i < l) {
out += vocab[g[i]];
if (wordSpace) out += " ";
}
ge("out").innerHTML = out;
generating = false;
}
function Sequence(memorySize) {
this.memorySize = memorySize;
this.memory = "";
this.singleMemory = "";
this.currentMemoryId = -1;
this.singleMemoryId = -1;
this.memories = [];
this.probabilities = [];
this.sums = [];
this.nonWeightedSums = [];
}
//reset and clear memory
Sequence.prototype.Reset = function() {
this.memory = "";
this.currentMemoryId = -1;
}
//process input i
Sequence.prototype.In = function(i,dontLearn) {
if (this.memory.length === this.memorySize) this.memory = this.memory.slice(1)+i;
else this.memory += i;
this.singleMemory = i;
if (!dontLearn) {
if (this.currentMemoryId !== -1 && this.singleMemoryId !== this.currentMemoryId) {
var p = this.probabilities[this.currentMemoryId][i.charCodeAt(0)]++;
if (p === 0) this.nonWeightedSums[this.currentMemoryId]++;
this.sums[this.currentMemoryId]++;
}
if (this.singleMemoryId !== -1) {
var p = this.probabilities[this.singleMemoryId][i.charCodeAt(0)]++;
if (p === 0) this.nonWeightedSums[this.singleMemoryId]++;
this.sums[this.singleMemoryId]++;
}
}
this.currentMemoryId = this.memories.indexOf(this.memory);
this.singleMemoryId = this.memories.indexOf(this.singleMemory);
if (!dontLearn) {
if (this.currentMemoryId === -1 && this.currentMemoryId !== this.singleMemoryId) {
this.currentMemoryId = this.memories.length;
this.probabilities.push(new Uint32Array(65565));
this.sums.push(0);
this.nonWeightedSums.push(0);
this.memories.push(this.memory);
}
if (this.singleMemoryId === -1) {
this.singleMemoryId = this.memories.length;
this.probabilities.push(new Uint32Array(65565));
this.sums.push(0);
this.nonWeightedSums.push(0);
this.memories.push(this.singleMemory);
}
}
}
//guess next character in sequence based on probabilities with a certain randomness(0-1)
Sequence.prototype.Next = function(randomness) {
//no current memory to guess next from
var mId = this.currentMemoryId === -1 ? this.singleMemoryId : this.currentMemoryId;
if (mId === -1) return -1;
var rand = Math.random(),
min = 0.0,
i = -1,
pa = this.probabilities[mId],
ps = this.sums[mId],
ds = randomness/this.nonWeightedSums[mId];
if (ps === 0) return -1;//no probabilities to guess next from
while (++i < 65565) {
var len = (pa[i]/ps)*(1-randomness)+ds*(pa[i]>0?1:0),
max = min+len;
if (rand >= min && rand < max+1e-6) return i;
min = max;
}
return 65564;
}
Also see: Tab Triggers