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.
<input id="cursor-input" type="password" />
<canvas id="paper"></canvas>
<canvas id="text-canvas"></canvas>
<canvas id="cursor-canvas"></canvas>
<div id="welcome">
Typewrite Something!
</div>
@font-face {
font-family: 'Special Elite';
src: url('http://www.typewritesomething.com/inc/specialelite-webfont.eot');
src: url('http://www.typewritesomething.com/inc/specialelite-webfont.eot?#iefix') format('embedded-opentype'),
url('http://www.typewritesomething.com/inc/specialelite-webfont.woff2') format('woff2'),
url('http://www.typewritesomething.com/inc/specialelite-webfont.woff') format('woff'),
url('http://www.typewritesomething.com/inc/specialelite-webfont.ttf') format('truetype'),
url('http://www.typewritesomething.com/inc/specialelite-webfont.svg#special_eliteregular') format('svg');
font-weight: normal;
font-style: normal;
}
html, body {
height: 100%;
width: 100%;
margin: 0;
overflow-x:hidden;
position: relative;
font-family: 'Special Elite';
}
canvas {
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
}
#cursor-input {
width: 1px;
height: 1px;
border: none;
padding: 0;
margin: -10px;
overflow: hidden;
position: relative;
margin-left: -1px;
}
#welcome {
position: relative;
z-index:9;
top: 50%;
transform: translateY(-50%);
opacity:.5;
text-align:center;
font-size:2em;
animation: fadeout 3s linear 3s forwards;
}
@keyframes fadeout {
100% {opacity:0;visibility:hidden;}
}
/*
*
* Inspired by a girl, Ms. Jayme Bergman
*
* With help from Ben Wheeler and his online typewriter simulator:
* http://uniqcode.com/typewriter/
*
*/
var ptrn = (function() {
var cnv = document.createElement('canvas'),
ctx = cnv.getContext('2d');
cnv.width = 500;
cnv.height = 500;
for (var x = 0; x < cnv.width; x++) {
for (var y = 0; y < cnv.width; y++) {
var fill = randInt(240, 255),
blend = randInt(240, 255);
ctx.fillStyle = "rgb(" + fill + "," + fill + "," + fill + ")";
ctx.fillRect(x, y, 2, 2);
ctx.fillStyle = "rgba(" + blend + "," + blend + "," + blend + ", .5)";
ctx.fillRect(x, y, 15, 10);
}
}
return cnv;
})(),
paperCanvas = document.getElementById('paper'),
paper = paperCanvas.getContext('2d'),
textCanvas = document.getElementById('text-canvas'),
textCtx = textCanvas.getContext('2d'),
cursorCanvas = document.getElementById('cursor-canvas'),
cursorCtx = cursorCanvas.getContext('2d'),
cursorInput = document.getElementById('cursor-input'),
canvases = [paperCanvas, textCanvas, cursorCanvas],
docElem = document.documentElement,
keypress_audio = new makeMultiAudio('https://typewritesomething.com/static/audio/keypress.mp3'),
newline_audio = new Audio('https://typewritesomething.com/static/audio/return.mp3'),
requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
// constants !
var IS_IOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g),
IS_TOUCH = ('ontouchstart' in docElem),
CLICK_EVENT = IS_TOUCH ? 'touchstart' : 'click',
DEVICE_PIXEL_RATIO = window.devicePixelRatio,
PADDINGMIN = 10,
PADDINGMAX = 100,
ALPHA_MAX = .8,
ALPHA_MIN = .7,
ALPHA_DECREMENT = 0.001,
ALPHA_VARIANCE = 0.05,
LETTER_JITTER = 0.1,
LETTER_ROTATE = 0.02,
TEXT_COLOR = 'rgb(50,40,40)',
CURSOR_COLOR = 'rgba(175,0,0,0.5)',
NAV_BUTTONS = {
8: 'moveleft',
37: 'moveleft',
38: 'moveup',
39: 'moveright',
40: 'movedown',
13: 'newline',
},
NO_AUDIO = {
8: 'moveleft',
9: 'TAB',
16: 'SHIFT',
17: 'CTRL',
18: 'ALT',
20: 'CAPSLOCK',
32: 'SPACE',
37: 'moveleft',
38: 'moveup',
39: 'moveright',
40: 'movedown'
};
// variables
var canvasWidth,
canvasHeight = docElem.clientHeight,
chars = [],
paddingx,
paddingy,
posx,
posy,
letter_width,
letter_height,
letter_size,
cursorposx,
cursorposy,
alpha,
is_focused,
jittered_char_pos = {},
rotated_char_pos = {},
char_opacity = {};
drawText();
window.onresize = drawText;
if (!IS_IOS) {
focus();
}
// event handlers
function focus() {
is_focused = true;
updateCursor();
cursorInput.value = ' ';
if (IS_IOS) {
cursorInput.focus();
} else {
setTimeout(function() {
cursorInput.focus();
}, 150);
}
}
function blur() {
is_focused = false;
removeCursor();
cursorInput.blur();
}
document.addEventListener('click', function(e) {
if (is_focused) {
blur();
} else {
posx = e.offsetX;
posy = e.offsetY;
focus();
}
});
document.addEventListener('touchstart', function(e) {
var touch = e.changedTouches[0];
e.preventDefault();
if (is_focused) {
blur();
} else {
if (chars.length) {
posx = touch.clientX;
posy = touch.clientY;
}
focus();
}
});
cursorInput.addEventListener('keydown', function(e) {
var no_audio = NO_AUDIO[e.which];
if (!no_audio) {
if (e.which == 13) {
newline_audio.play();
} else {
keypress_audio.play();
}
return true;
}
if (no_audio == 'TAB') {
/* refocus */
setTimeout(focus, 10);
}
});
cursorInput.addEventListener('keyup', function(e) {
var nav_button = NAV_BUTTONS[e.which],
value = nav_button || this.value.substr(1);
if (!value) return;
// wipe input to handle one character at a time.
// leave a single space so that mobile isn't forced to upper case
this.value = ' ';
if (nav_button) {
updateCursor(value);
} else {
// update multiple characters in case they keydown more than keyup
for (var i = 0, len = value.length; i < len; i++) {
var single_char = value[i];
if (!jittered_char_pos[single_char]) {
// save general position
jittered_char_pos[single_char] = randRange(-LETTER_JITTER, LETTER_JITTER);
rotated_char_pos[single_char] = randRange(-LETTER_ROTATE, LETTER_ROTATE);
char_opacity[single_char] = randRange(ALPHA_MIN, ALPHA_MAX);
}
addToChars(single_char);
updateCursor('moveright');
}
}
});
function drawText() {
var _chars = chars;
// resize
canvasWidth = docElem.clientWidth;
for (var i = 0, len = canvases.length; i < len; i++) {
var canvas = canvases[i];
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// make Canvas Retina Proof
if (DEVICE_PIXEL_RATIO) {
canvas.width = canvasWidth * DEVICE_PIXEL_RATIO;
canvas.height = canvasHeight * DEVICE_PIXEL_RATIO;
canvas.style.width = canvasWidth + 'px';
canvas.style.height = canvasHeight + 'px';
}
}
// redraw bg
drawBackground();
// pad as necessary with size change
paddingx = Math.max(Math.min((canvasWidth / 2 / 10), PADDINGMAX), PADDINGMIN);
paddingy = Math.min(paddingx * 2, 100);
posx = posx || paddingx;
posy = posy || paddingy;
cursorposx = cursorposx || posx;
cursorposy = cursorposy || posy;
// change letter size as necessary with size change
letter_width = linearInterpolate(paddingx, [PADDINGMIN, PADDINGMAX], [12, 15]);
letter_height = letter_width * 20 / 12;
letter_size = letter_height;
alpha = ALPHA_MAX;
// reset contexts, because resizing wipes them
textCtx.font = letter_size + "px Special Elite, serif";
textCtx.fillStyle = TEXT_COLOR;
textCtx.globalAlpha = alpha;
textCtx.scale(DEVICE_PIXEL_RATIO, DEVICE_PIXEL_RATIO);
cursorCtx.fillStyle = CURSOR_COLOR;
cursorCtx.globalAlpha = ALPHA_MAX;
cursorCtx.scale(DEVICE_PIXEL_RATIO, DEVICE_PIXEL_RATIO);
// wipe and redraw any characters
chars = [];
for (var i = 0, len = _chars.length; i < len; i++) {
var char = _chars[i];
updateText(char);
}
}
function updateCursor(value) {
if (value === 'moveleft') {
posx -= letter_width;
} else if (value === 'moveup') {
posy -= letter_height;
} else if (value === 'movedown') {
posy += letter_height;
} else if (value === 'moveright') {
posx += letter_width;
} else if (value === 'newline') {
posx = paddingx;
posy += letter_height;
}
drawCursor(posx, posy);
}
function removeCursor() {
cursorCtx.clearRect(cursorposx, cursorposy, letter_width + 20, letter_height / 10 + 20);
}
function drawCursor(x, y) {
var diff = y - (canvasHeight - (docElem.clientHeight / 2));
cursorInput.style.top = (y + 100) + 'px';
if (diff > 0) {
canvasHeight += diff;
drawText();
}
removeCursor();
y += 2;
cursorCtx.fillRect(x, y, letter_width, letter_height / 10);
cursorposx = x - 10;
cursorposy = y - 10;
}
function addToChars(char) {
var jitter_y = (function() {
var general_position = jittered_char_pos[char];
// add more random
return general_position + randRange(-LETTER_JITTER, LETTER_JITTER);
})(),
rotate_xy = (function() {
var general_position = rotated_char_pos[char];
// add more random
return general_position + randRange(-LETTER_ROTATE, LETTER_ROTATE);
})(),
opacity = (function() {
var _opacity = char_opacity[char];
return _opacity + randRange(-ALPHA_VARIANCE, ALPHA_VARIANCE);
})();
alpha -= ALPHA_DECREMENT;
updateText({
opacity: opacity,
value: char,
rotate_xy: rotate_xy,
x: posx,
y: posy + jitter_y
});
}
function updateText(charobj) {
var value = charobj.value,
opacity = charobj.opacity,
rotate_xy = charobj.rotate_xy,
x = charobj.x,
y = charobj.y;
chars.push(charobj);
textCtx.save();
textCtx.translate(x, y);
textCtx.rotate(rotate_xy);
textCtx.globalAlpha = opacity;
textCtx.fillText(value, 0, 0);
textCtx.restore();
}
function drawBackground() {
var pattern = paper.createPattern(ptrn, 'repeat');
paper.fillStyle = pattern;
paper.fillRect(0, 0, paperCanvas.width, paperCanvas.height);
}
function randRange(min, max) {
var value = (Math.random() * (max - min)) + min;
return value;
}
function randInt(min, max) {
return Math.round(randRange(min, max));
}
function makeMultiAudio(src) {
var output = [],
current = 0,
num = 5;
for (var i = 0; i < num; i++) {
output.push(new Audio(src));
}
this.play = function() {
output[current++ % num].play();
};
}
function linearInterpolate(val, from_range, to_range) {
var minX = from_range[0],
minY = to_range[0],
rangeX = from_range[1] - from_range[0],
rangeY = to_range[1] - to_range[0];
return (val - minX) * rangeY / rangeX + minY;
}
Also see: Tab Triggers