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.
<canvas id="eyes-pattern"></canvas>
<script type="x-shader/x-fragment" id="vertShader">
precision mediump float;
varying vec2 vUv;
attribute vec2 a_position;
void main() {
vUv = .5 * (a_position + 1.);
gl_Position = vec4(a_position, 0.0, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragShader">
precision mediump float;
varying vec2 vUv;
uniform float u_scale;
uniform float u_time;
uniform float u_speed;
uniform float u_ratio;
uniform float u_saturation;
uniform float u_redness;
uniform float u_blue_ratio;
uniform vec2 u_pointer;
#define TWO_PI 6.28318530718
// =================================================
// cell-related helpers
vec2 hash(vec2 p) {
p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
return fract(sin(p)*18.5453);
}
// polynomial-based smooth minimum;
// used for rounded Voronoi shaping
float smin(float angle, float b, float k) {
float h = clamp(.5 + .5 * (b - angle) / k, 0., 1.);
return mix(b, angle, h) - k * h * (1. - h);
}
// =================================================
// eye-related helpers
float rand(vec2 n) {
return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}
float noise(vec2 n) {
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
}
float fbm(vec2 n) {
float total = 0.0, amplitude = .4;
for (int i = 0; i < 4; i++) {
total += noise(n) * amplitude;
n += n;
amplitude *= 0.6;
}
return total;
}
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
// =================================================
// vessels-related helpers
mat2 rotate2D(float r) {
return mat2(cos(r), sin(r), -sin(r), cos(r));
}
float vessels(vec2 uv, float t) {
float S = 10.;
vec2 n = vec2(0);
vec2 N = vec2(0);
mat2 m = rotate2D(1.);
for (int j = 0; j < 15; j++) {
uv *= m;
n *= m;
vec2 q = uv * S + float(j) + n + t;
n += sin(q);
N += cos(q) / S * (1. + .2);
S *= 1.2;
}
return (N.x + N.y + .2);
}
// =================================================
vec3 eye_pattern(vec2 uv, float tile_time, float pointer_angle, float pointer_distance) {
// tiles coordinates
vec2 i_uv = floor(uv);
vec2 f_uv = fract(uv);
// outputs
vec2 randomizer = vec2(0.);
vec3 distance = vec3(1.);
float angle = 0.;
// get Voronoi cell data
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
vec2 tile_offset = vec2(float(x), float(y));
vec2 blick_tile_offset = tile_offset;
vec2 o = hash(i_uv + tile_offset);
tile_offset += (.5 + (.25 + pointer_distance) * sin(tile_time + TWO_PI * o)) - f_uv;
blick_tile_offset += (.9 - f_uv);
float dist = dot(tile_offset, tile_offset);
float old_min_dist = distance.x;
distance.z = max(distance.x, max(distance.y, min(distance.z, dist)));
distance.y = max(distance.x, min(distance.y, dist));
distance.x = min(distance.x, dist);
if (old_min_dist > distance.x) {
angle = atan(tile_offset.x, tile_offset.y);
randomizer = o;
}
}
}
distance = sqrt(distance);
distance = sqrt(distance);
float cell_shape = min(smin(distance.z, distance.y, .1) - distance.x, 1.);
float cell_radius = distance.x;
float eye_radius = 2. * cell_radius - .5 * cell_shape;
// at this point, we have
// -- randomizer (x2)
// -- angle to use as polar coordinate
// -- cell_shape - Voronoi cell w/ rounded endges
// -- cell_radius - exact circle in the mid of cell
// -- eye_radius - mix of two
// ============================================================
float redness_angle = angle * 2. + randomizer.y;
float eye_ball_redness = vessels(vec2(redness_angle * .2, cell_shape * 2.), .5 * u_time);
eye_ball_redness *= pow(cell_radius, 1. / u_redness);// more on edges
eye_ball_redness *= (.5 + .5 * randomizer.y);// less for some cells
eye_ball_redness *= smoothstep(3., 1.5, angle);// hide the seam
eye_ball_redness *= smoothstep(-3., -2., angle);// hide the seam
vec3 eye_ball_color = vec3(1., 1. - eye_ball_redness, 1. - eye_ball_redness);
// iris color
float iris_color_1_hue = (1. - u_blue_ratio) * pow(randomizer.x, 3. - 2. * u_blue_ratio) + u_blue_ratio * pow(randomizer.x, 1.3 - u_blue_ratio);
iris_color_1_hue = mix(.07, .59, iris_color_1_hue);
vec3 iris_color_1 = hsv2rgb(vec3(iris_color_1_hue, u_saturation, .5 + iris_color_1_hue));
vec3 iris_color_2 = hsv2rgb(vec3(.67 * randomizer.x - .1 * randomizer.y, .5, .1 + .2 * randomizer.y));
float outer_color_noise = fbm(vec2(angle * 4., cell_radius));
vec3 color = iris_color_1;
color = mix(color, iris_color_2, outer_color_noise);
vec3 iris_center_color = hsv2rgb(vec3(.2 - .1 * randomizer.y, u_saturation, .5));
color = mix(iris_center_color, color, smoothstep(.05 + randomizer.y * .25, .45, cell_radius - .2 * pointer_distance));
float white_incertion_noise = smoothstep(.4, 1., fbm(vec2(8. * angle, 10. * cell_shape)));
white_incertion_noise *= (.9 + .5 * randomizer.x);
color = mix(color, vec3(1.), white_incertion_noise);
float dark_incertion_noise = smoothstep(.5, 1., fbm(vec2(3. * angle, 11. * cell_shape)));
color = mix(color, vec3(0.), dark_incertion_noise);
// dark pupil
float pupil_shape = smoothstep(.35, .45, 1.2 * eye_radius - pointer_distance);
color = mix(vec3(.0), color, pupil_shape);
// darkness on the edge of iris
color *= pow(smoothstep(1., .6, eye_radius), .3);
// crop the iris
float outer_shape = smoothstep(.9, 1., eye_radius);
color = mix(color, eye_ball_color, outer_shape);
float blick = smoothstep(1.6, .2, eye_radius + .1 * randomizer.y * sin(3. * u_time * randomizer.x));
blick *= smoothstep(.5 - pointer_distance, .7, eye_radius - .2 * randomizer.y);
blick *= (1. - sin(angle + pointer_angle));
blick = step(1., blick);
blick *= step(.5, fbm(2. * uv + vec2(0., .5 * u_time)));
// dark cell border
color -= .1 * pow(1. - cell_shape, 6.);
color -= .4 * pow(1. - cell_shape, 100.);
float round_shadow = -sin(angle + pointer_angle);
round_shadow *= smoothstep(.4, .5, cell_radius);
round_shadow = .13 * mix(0., round_shadow, 1. - smoothstep(.1, .2, pointer_distance));
color += round_shadow;
color = mix(color, vec3(1.), blick);
return color;
}
void main() {
vec2 uv = vUv;
uv.x *= u_ratio;
vec2 _uv = (vUv - .5) / u_scale + .5;
_uv.x *= u_ratio;
float tile_floating_speed = u_speed * u_time;
vec2 point = u_pointer;
point.x *= u_ratio;
point -= uv;
float pointer_angle = atan(point.y, point.x);
float pointer_distance = pow(1. - .5 * length(point), 2.);
pointer_distance *= .2;
vec3 color = eye_pattern(_uv, tile_floating_speed, pointer_angle, pointer_distance);
gl_FragColor = vec4(color, 1.);
}
</script>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
}
canvas#eyes-pattern {
display: block;
width: 100%;
}
.lil-gui {
--width: 450px;
max-width: 90%;
--widget-height: 20px;
font-size: 15px;
--input-font-size: 15px;
--padding: 10px;
--spacing: 10px;
--slider-knob-width: 5px;
--background-color: rgba(5, 0, 15, .8);
--widget-color: rgba(255, 255, 255, .3);
--focus-color: rgba(255, 255, 255, .4);
--hover-color: rgba(255, 255, 255, .5);
--font-family: monospace;
}
import GUI from 'https://cdn.jsdelivr.net/npm/lil-gui@0.18.2/+esm'
const canvasEl = document.querySelector("#eyes-pattern");
const mouseThreshold = .3;
const devicePixelRatio = Math.min(window.devicePixelRatio, 2);
const mouse = {
x: .5 * window.innerWidth,
y: .5 * window.innerHeight,
tX: .5 * window.innerWidth,
tY: .5 * window.innerHeight,
}
const params = {
scale: .2,
speed: .3,
saturation: .7,
blueRatio: .5,
redness: .25
}
let uniforms;
const gl = initShader();
createControls();
render();
window.addEventListener("resize", resizeCanvas);
resizeCanvas();
window.addEventListener("mousemove", e => {
updateMousePosition(e.pageX, e.pageY);
});
window.addEventListener("touchmove", e => {
updateMousePosition(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
});
canvasEl.addEventListener("click", e => {
updateMousePosition(e.pageX, e.pageY);
});
function updateMousePosition(eX, eY) {
mouse.tX = eX;
mouse.tY = eY;
}
function initShader() {
const vsSource = document.getElementById("vertShader").innerHTML;
const fsSource = document.getElementById("fragShader").innerHTML;
const gl = canvasEl.getContext("webgl") || canvasEl.getContext("experimental-webgl");
if (!gl) {
alert("WebGL is not supported by your browser.");
}
function createShader(gl, sourceCode, type) {
const shader = gl.createShader(type);
gl.shaderSource(shader, sourceCode);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
const vertexShader = createShader(gl, vsSource, gl.VERTEX_SHADER);
const fragmentShader = createShader(gl, fsSource, gl.FRAGMENT_SHADER);
function createShaderProgram(gl, vertexShader, fragmentShader) {
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error("Unable to initialize the shader program: " + gl.getProgramInfoLog(program));
return null;
}
return program;
}
const shaderProgram = createShaderProgram(gl, vertexShader, fragmentShader);
uniforms = getUniforms(shaderProgram);
function getUniforms(program) {
let uniforms = [];
let uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < uniformCount; i++) {
let uniformName = gl.getActiveUniform(program, i).name;
uniforms[uniformName] = gl.getUniformLocation(program, uniformName);
}
return uniforms;
}
const vertices = new Float32Array([-1., -1., 1., -1., -1., 1., 1., 1.]);
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
gl.useProgram(shaderProgram);
const positionLocation = gl.getAttribLocation(shaderProgram, "a_position");
gl.enableVertexAttribArray(positionLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.uniform1f(uniforms.u_scale, params.scale);
gl.uniform1f(uniforms.u_speed, params.speed);
gl.uniform1f(uniforms.u_saturation, params.saturation);
gl.uniform1f(uniforms.u_redness, params.redness);
gl.uniform1f(uniforms.u_blue_ratio, params.blueRatio);
return gl;
}
function render() {
const currentTime = .001 * performance.now();
gl.uniform1f(uniforms.u_time, currentTime);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
mouse.x += (mouse.tX - mouse.x) * mouseThreshold;
mouse.y += (mouse.tY - mouse.y) * mouseThreshold;
gl.uniform2f(uniforms.u_pointer, mouse.x / window.innerWidth, 1. - mouse.y / window.innerHeight);
requestAnimationFrame(render);
}
function resizeCanvas() {
canvasEl.width = window.innerWidth * devicePixelRatio;
canvasEl.height = window.innerHeight * devicePixelRatio;
gl.viewport(0, 0, canvasEl.width, canvasEl.height);
gl.uniform1f(uniforms.u_ratio, canvasEl.width / canvasEl.height);
}
function createControls() {
const gui = new GUI();
gui.add(params, "scale", .05, .6)
.onChange(v => {
gl.uniform1f(uniforms.u_scale, v);
});
gui.add(params, "blueRatio", 0, 1)
.onChange(v => {
gl.uniform1f(uniforms.u_blue_ratio, v);
});
gui.add(params, "redness", 0, .5)
.onChange(v => {
gl.uniform1f(uniforms.u_redness, v);
});
}
Also see: Tab Triggers