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.
<canvas id="canv"></canvas>
<video></video>
body {
margin: 0;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
video {
display: none;
}
.ui {
position: absolute;
top: 0;
left: 0;
background: rgba(0,0,0,.6);
font-family: sans-serif;
color: #fff;
padding: 16px;
border-radius: 0 0 6px 0;
}
const luminanceMapURL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEl0lEQVRYR42XjVEiQRBGZyOADCQDyQCMADIQIwAjECNAIgAiQCMAIwAz0AwwgrVe172pZsurO6qo2Znt6f7665+ZbUopbSmlLBYLhjKfz2Pkdz6fy+VyKe/v72UymcTI73g8VpnNZhPPz8/PMd7f39f5fr8vu92uyt7c3MTzYDAI3fwaAaxWq1gYDocxshEh5oy8B8BoNIrx9fU15P4G4PPzs3x9fVXjOHB3dxd6WBdMczgcWtCDCuUfHx+xqd/vx8gaz29vbzFfLpcxypgW9AhZ/ugDJM8wpjyM8gNQMLBcLtuXl5cQnE6nFbEK2SCtgLu9vb0CuF6vr5jDKMY1PJvNKt0IakMGawigEibG43H1HOMowghKZUSWtttteMaIYjzFoD/WCQU/9PLsnNCiswJAICcXVAOAv7nAZhWhXI/Y535ziHcCUlYgrAMYFprNZhNVgKHHx8fwSM8dYYGkIQ+IHUxJJSExOWEGsBmQDAEMB/IYOQAAFEAHSKEQBSYhQlYCz8ihiPeGgLls5djn0GTD6GfO2Oz3+9bNACHhjDECzqkGGWAEiO8pq16vV3vB09NTADRpHx4eyul0qsBh6fv7O0BHFUgZnugh3hrz3A9ATtUQgiwrIzCoHgxkxixBZHU6GAApHuCJvYDYEmu8yTE3w5HHC/OHHGG/oZMRABmKnAO1DGGAjCdZMnIVmxu+N8aEBgDG/zc5DOOpCYus/cVyb6bTaYTAOucswGOTBoMIsxEFvLcv2KRo24YG4HgNg+YM1cWaVYUuQ1P7ACyY3QqaG8xRiCG8QdaeLlM5B1hjriGYIJSEKOcB9oKBXPc5aVBgDGXIHDCRatv78+BJB1AMmxPObWSsBwCSUErxkriaNADQExRkZggFHpkDJK9J6Hngfjsg1ePPTtusVqtoRFKDB5YPme0dQCC2XPsA1PITDLkAAOQYc1liVMPswdFmsVi0Zj+LGDWpZKTbmgWbzwgYYB/VwQgADHg2MNo51RchAIA3H4RQpEe2ZOs3e4SMSWa7NiFNQvZxdgCIkHmzojrYy7sIgdkt5SZO7owmZzcZbUAes1mONVs4zFDelijNL07D8XjceoORslwqUokHbMKAXVCj7DO7BZDvljDi3TAf13Ea0gk16GmYc8J35IPHrZlslzNU3icA4xUP2gHDgWRocukGA5YJozngLVdhj2RbqHdDytN+kRULquuxFxzB/hOAhh2JpbdalPEMANcFYW5gqFtFAmasANyYaWStOzfj84VFr2Tyf+aGtun3+22myed8B8hl5joASEa7KKFh7vUdRjBCZ/1tXblIwuypHhtjr0+UjF0Oed/nppT1tG2oLU3DeVcK8/ws4zUEeu69348VPc7U+1XDmtc35JRFl19aVxn/C5hmNptFK6YrQYu9njEfw91G5Glm62W/n2Ky4vEuiFxhHmYRAtsi8bRn+4HhoYMB3rMRoBrhMKLJeDznTpkB+E0JGMLpLaleSm2Vntv5HoBRO5md0c7n6B1PYBjia8tSBSDHsaG+6gPefrtfONkDAMJAvvfzXkW5LDHux6hfRYasC+AHeG3RHuX02swAAAAASUVORK5CYII=';
const fontURL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAPZklEQVR4Xu2d63YbOQyDk/d/6Oyxd6erMCQ+kJIb58T91Vo3igRBSDN239/e3j7e4M/HR97l/f39PtJtv/W/+l5j16Vvbdn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+LyAsDiIQIgtWfO/vEA2N3AJANXR7oqn5BeUWRnf78SAORYt92pUeRgat9R4b+2BFTfDnYDS/3cc/YagAkDUCaTnZ12eqWtM1e3b1cDUP/WKUBloJPh3c3G/jvrO/btMEzFIKeBSQHtisYXABoikACcAegFAPJao/3FAP9/Wady20MZoBEru2uVIUR19gLQ0V1/7ZfZRu1R4+zsj5gmzq1AYZUANQE5UFFgdu05qcNxTPXvWxCclzqV9lDOV9e4k32pe4XqhVTSTdGOL98NrN5azZ6KZcJn4iByjgsy9eTOeVn1ch45lwRltR/6PAbPOUG5vqvih/9xZOZ8clA3g9QZnJztgpBUOgE8y0aXTZQPq/1lrJv1rey6Pid2TL8ergJMExJtEUOcAA9l2u7+lA/IP+7YLgDobuJpGaDjMArsCh4l1DoiKTIHAbizn5ilkxKg2NNhx7YGIJHRZQCq7456pjpY0eFqK82x2lk9n6h0RpbNnVMM2aZ8RIDEU4CzeGczp/vu2ucA8LTNnfl290drIQBOZI8zR0aHFVV36J0c4KzrzuH0U7X9Gq9K1M79QSoYnZ+KpTpzor1bazulKNK3K8ScgHb7dDVEp0x1bbn1RwboUCSJNEJvdSTqMIgCBtVxdZEzcS7poWxfUx9OdcaX/zuYaMg9bnQy1A2wyp64HoGtsu+eFe+3vPj650Q9njCAs+4YAOsXQxTKlRGOgSfKhLsOBXd3ng5AqPx0Sl+VnOscqs+WBjhFgV2QUVkg9sjGqyx/xD7dPeys/TAGIA2w204ix3XeNKt3nN4ZOw1QZ41JXxSBk0mnY0gA/e3Mne6jGleJ0NPrdOb7Bz1LHUvDY2RdAAAAAElFTkSuQmCC';
// this is just for code highlighting in VSCode
// via the glsl-literal extension
const glsl = x => x;
const frag = glsl`
precision highp float;
uniform float width;
uniform float height;
uniform float time;
uniform sampler2D camTexture;
uniform sampler2D fontTexture;
uniform sampler2D lumTexture;
const int useSobel = 0;
const int zoom = 1;
// normalize coords and correct for aspect ratio
vec2 normalizeScreenCoords()
{
float aspectRatio = width / height;
vec2 result = 2.0 * (gl_FragCoord.xy / vec2(width, height) - 0.5);
result.x *= aspectRatio;
return result;
}
float deform(vec2 p, float factor) {
return sin(time * .1 + factor * p.x) * cos(time * .1 + factor * p.y);
}
vec4 invert(vec4 color) {
return vec4(1.0 - color.rgb, 1.0);
}
vec4 grey(vec4 color) {
// float val = (color.x + color.y + color.z) / 3.0;
float val = 0.3 * color.x + 0.59 * color.y + 0.11 * color.z;
return vec4(vec3(val), 1.0);
}
vec2 getTexCoords(vec2 position) {
return 1.0 - position.xy / vec2(width, height);
}
float luminance(vec3 color) {
return 0.2126*color.x + 0.7152*color.y + 0.0722*color.z;
}
// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))
* 43758.5453123);
}
vec2 camCoord(vec2 coord) {
return 1.0 - coord / vec2(width, height);
}
vec4 sobel(in sampler2D tex, in vec2 coord) {
float w = 1.0 / width;
float h = 1.0 / height;
vec4 n0 = texture2D(tex, coord + vec2(-w, -h));
vec4 n1 = texture2D(tex, coord + vec2( 0, -h));
vec4 n2 = texture2D(tex, coord + vec2( w, -h));
vec4 n3 = texture2D(tex, coord + vec2(-w, 0));
vec4 n4 = texture2D(tex, coord);
vec4 n5 = texture2D(tex, coord + vec2( w, 0));
vec4 n6 = texture2D(tex, coord + vec2(-w, h));
vec4 n7 = texture2D(tex, coord + vec2( 0, h));
vec4 n8 = texture2D(tex, coord + vec2( w, h));
vec4 edgeH = n2 + (2.0 * n5) + n8 - (n0 + (2.0 * n3) + n6);
vec4 edgeV = n0 + (2.0 * n1) + n2 - (n6 + (2.0 * n7) + n8);
vec4 sobel = sqrt((edgeH * edgeH) + (edgeV * edgeV));
return sobel;
}
vec2 getFontCoord(int i) {
float chY = floor(float(i) / 16.);
float chX = mod(float(i), 16.);
vec2 fontCoord = vec2((chX * 8. + mod(gl_FragCoord.x / float(zoom), 8.)) / 128., (chY * 8. + (8. - mod(gl_FragCoord.y / float(zoom), 8.))) / 128.);
return fontCoord;
}
vec2 getLumCoord(in int i, in vec2 p) {
float chY = floor(float(i) / 16.);
float chX = mod(float(i), 16.);
vec2 lumCoord = vec2((chX * 2. + p.x) / 32., (chY * 2. + 2. - p.y) / 32.);
return lumCoord;
}
vec4 averageBlockColor() {
vec2 a = floor(gl_FragCoord.xy / (float(zoom) * 8.)) * float(zoom) * 8.;
vec2 b = a + vec2(4, 0) * float(zoom);
vec2 c = b + vec2(0, 4) * float(zoom);
vec2 d = c + vec2(4, 4) * float(zoom);
vec4 c0 = useSobel == 1 ? grey(sobel(camTexture, camCoord(a))) : grey(texture2D(camTexture, camCoord(a)));
vec4 c1 = useSobel == 1 ? grey(sobel(camTexture, camCoord(b))) : grey(texture2D(camTexture, camCoord(b)));
vec4 c2 = useSobel == 1 ? grey(sobel(camTexture, camCoord(c))) : grey(texture2D(camTexture, camCoord(c)));
vec4 c3 = useSobel == 1 ? grey(sobel(camTexture, camCoord(d))) : grey(texture2D(camTexture, camCoord(d)));
float minDist = 9999.;
int minIdx = 32;
for (int i = 32; i < 127; i++) {
int chY = 16 - i / 16;
int chX = int(mod(float(i), 16.));
vec4 l0 = texture2D(lumTexture, getLumCoord(i, vec2(0. ,0.)));
vec4 l1 = texture2D(lumTexture, getLumCoord(i, vec2(1. ,0.)));
vec4 l2 = texture2D(lumTexture, getLumCoord(i, vec2(0. ,1.)));
vec4 l3 = texture2D(lumTexture, getLumCoord(i, vec2(1. ,1.)));
float dist = length(vec4(
c0.x - l0.x,
c1.x - l1.x,
c2.x - l2.x,
c3.x - l3.x
));
if (dist < minDist) {
minIdx = i;
minDist = dist;
}
}
// minIdx = 64;
vec2 p = mod(gl_FragCoord.xy / 2.0, 8.);
// float cY = floor(16. - float(minIdx) / 16.);
// float cX = mod(float(minIdx), 16.);
vec2 fontCoord = getFontCoord(minIdx);
return texture2D(fontTexture, fontCoord);
// return c0;
}
void main() {
vec2 p = normalizeScreenCoords();
vec2 coord = 1.0 - gl_FragCoord.xy / vec2(width, height);
gl_FragColor = averageBlockColor(); // texture2D(camTexture, coord);
// display a single character from the charset (in a 16x16 table)
//float chX = 2.;
//float chY = 11.;
//vec2 fontCoord = vec2((chX * 8. + mod(gl_FragCoord.x / 8., 8.)) / 128., (chY * 8. + (8. - mod(gl_FragCoord.y / 8., 8.))) / 128.);
//gl_FragColor = texture2D(fontTexture, fontCoord);
//float chX = 0.;
//float chY = 1.;
//vec2 lumCoord = vec2((chX * 2. + mod(gl_FragCoord.x / 8., 2.)) / 32., (chY * 2. + (2. - mod(gl_FragCoord.y / 8., 2.))) / 32.);
//gl_FragColor = texture2D(lumTexture, lumCoord);
}
`
const vert = glsl`
precision mediump float;
attribute vec2 position;
void main () {
gl_Position = vec4(position, 0, 1.0);
}
`
let video = document.querySelector('video');
let fallbackImage = null;
let camTexture = null;
let fontTexture = null;
let lumTexture = null;
const glea = new GLea({
glOptions: {
preserveDrawingBuffer: true
},
shaders: [
GLea.fragmentShader(frag),
GLea.vertexShader(vert)
],
buffers: {
'position': GLea.buffer(2, [1, 1, -1, 1, 1,-1, -1,-1])
}
}).create();
window.addEventListener('resize', () => {
glea.resize();
});
function loop(time) {
const { gl } = glea;
// Upload the image into the texture.
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video || fallbackImage);
if (video) {
glea.setActiveTexture(0, camTexture);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, video);
}
glea.clear();
glea.uni('width', glea.width);
glea.uni('height', glea.height);
glea.uni('time', time * .005);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(loop);
}
function accessWebcam(video) {
return new Promise((resolve, reject) => {
const mediaConstraints = { audio: false, video: { width: 1280, height: 720, brightness: {ideal: 2} } };
navigator.mediaDevices.getUserMedia(mediaConstraints).then(mediaStream => {
video.srcObject = mediaStream;
video.setAttribute('playsinline', true);
video.onloadedmetadata = (e) => {
video.play();
resolve(video);
}
}).catch(err => {
reject(err);
});
});
}
function loadImage(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = 'Anonymous';
img.src = url;
img.onload = () => {
resolve(img);
};
img.onerror = () => {
reject(img);
};
});
}
function takeScreenshot() {
const { canvas } = glea;
const anchor = document.createElement('a');
anchor.setAttribute('download', 'selfie.jpg');
anchor.setAttribute('href', canvas.toDataURL('image/jpeg', 0.92));
anchor.click();
}
async function setup() {
const { gl } = glea;
try {
await accessWebcam(video);
} catch (ex) {
video = null;
console.error(ex.message);
}
// video = null;
if (! video) {
try {
fallbackImage = await loadImage('https://placekitten.com/1280/720')
} catch (ex) {
console.error(ex.message);
return false;
}
}
const [fontImage, lumImage] = await Promise.all([
loadImage(fontURL),
loadImage(luminanceMapURL)
])
camTexture = glea.createTexture(0);
// Upload the image into the texture.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video || fallbackImage);
fontTexture = glea.createTexture(1);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, fontImage);
lumTexture = glea.createTexture(2);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, lumImage);
glea.setActiveTexture(0, camTexture);
glea.uniI('camTexture', 0);
glea.uniI('fontTexture', 1);
glea.uniI('lumTexture', 2);
loop(0);
}
setup();
Also see: Tab Triggers