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="image-selector-input" style="visibility:hidden;" type="file">
<canvas></canvas>
<div class="tip">
click to break, space to toggle
</div>
<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 highp float;
precision highp sampler2D;
varying vec2 vUv;
uniform sampler2D u_image_texture;
uniform float u_edge_thickness;
uniform float u_ratio;
uniform vec2 u_pointer_position;
uniform float u_img_ratio;
uniform float u_click_randomizer;
uniform float u_rotation;
uniform float u_effect;
uniform float u_effect_active;
#define TWO_PI 6.28318530718
#define PI 3.14159265358979323846
float random(float x) {
return fract(sin(x * 12.9898) * 43758.5453);
}
float random2(vec2 p) {
return fract(sin(dot(p.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
float noise(vec2 p) {
vec2 ip = floor(p);
vec2 u = fract(p);
u = u*u*(3.0-2.0*u);
float res = mix(
mix(random2(ip), random2(ip+vec2(1.0, 0.0)), u.x),
mix(random2(ip+vec2(0.0, 1.0)), random2(ip+vec2(1.0, 1.0)), u.x), u.y);
return res*res;
}
float get_sector_shape(float d, float a, float angle, float edges) {
float angle1 = PI;
float angle2 = angle1 + angle;
float edge1 = smoothstep(angle1 - edges / d, angle1 + edges / d, a);
float edge2 = smoothstep(angle2 - edges / d, angle2 + edges / d, a);
return edge1 * (1. - edge2);
}
float get_img_frame_alpha(vec2 uv, float img_frame_width) {
float img_frame_alpha = smoothstep(0., img_frame_width, uv.x) * smoothstep(1., 1. - img_frame_width, uv.x);
img_frame_alpha *= smoothstep(0., img_frame_width, uv.y) * smoothstep(1., 1. - img_frame_width, uv.y);
return img_frame_alpha;
}
float get_simple_cracks(float a, float d, float n) {
a *= (1. + sin(2. * a + PI + 2. * u_click_randomizer));
float simple_cracks_number = 10.;
float simple_cracks_angle_step = TWO_PI / simple_cracks_number;
float simple_crack_angle = mod(a + n + u_click_randomizer, simple_cracks_angle_step);
float cracks_shape = 4. * abs(simple_crack_angle - .5 * simple_cracks_angle_step);
cracks_shape = mix(cracks_shape, 1., smoothstep(.9, 1., d));
cracks_shape *= pow(d + .4 * u_click_randomizer * max(0., cos(2. * a + u_click_randomizer) * sin(1. * a)), 12.);
cracks_shape = (1. + n) * (1. + sin(4. * a)) * step(.9, cracks_shape);
return cracks_shape;
}
vec2 get_img_uv() {
vec2 img_uv = vUv;
img_uv -= .5;
if (u_ratio > u_img_ratio) {
img_uv.x = img_uv.x * u_ratio / u_img_ratio;
} else {
img_uv.y = img_uv.y * u_img_ratio / u_ratio;
}
float scale_factor = 1.4;
img_uv *= scale_factor;
img_uv += .5;
img_uv.y = 1. - img_uv.y;
return img_uv;
}
vec2 get_disturbed_uv(vec2 uv, float section_constant, float edge, vec2 direction, float border) {
float img_distortion = u_effect * (section_constant - .5);
vec2 discurbed_uv = uv;
discurbed_uv += 2. * img_distortion;
discurbed_uv.x -= mix(.03 * edge * direction.x, -.1 * edge, border);
discurbed_uv.y -= mix(.03 * edge * direction.y, -.1 * edge, border);
vec2 center = vec2(0.5, 0.5);
discurbed_uv = discurbed_uv - center;
float cosA = cos(4. * img_distortion);
float sinA = sin(4. * img_distortion);
float perspective = 1. + img_distortion * discurbed_uv.y;
discurbed_uv = vec2(
perspective * (cosA * discurbed_uv.x - sinA * discurbed_uv.y),
perspective * (sinA * discurbed_uv.x + cosA * discurbed_uv.y)
);
discurbed_uv += center;
return discurbed_uv;
}
void main() {
vec2 uv = vUv;
uv.y = 1. - uv.y;
uv.x *= u_ratio;
vec2 pointer = u_pointer_position;
vec2 pointer_direction = normalize(u_pointer_position - vec2(vUv.x, 1. - vUv.y));
pointer.x *= u_ratio;
pointer = pointer - uv;
float pointer_angle = atan(pointer.y, pointer.x);
float pointer_distance = length(pointer);
float pointer_distance_normalized = (1. - clamp(pointer_distance, 0., 1.));
vec3 color = vec3(0.);
vec2 img_uv = get_img_uv();
float sector_constant = 0.;
float sector_start_angle = 0.;
float is_sector_edge = 0.;
float is_grid_edge = 0.;
float is_central_edge = 0.;
float angle_noise = .3 * noise(3. * img_uv);
for (int i = 0; i < 12; i++) {
float sector_seed = float(i) + u_click_randomizer + 2.;
float angle_normalised = mod((pointer_angle - sector_start_angle) / TWO_PI, 1.);
angle_normalised += .1 * angle_noise;
float angle = angle_normalised * TWO_PI;
float sector_size = (.01 + 2. * random2(vec2(float(i) + u_click_randomizer, u_pointer_position.x)));
sector_size = min(sector_size, TWO_PI - sector_start_angle);
float thickness = u_edge_thickness * (.2 + random(3. * sector_seed));
thickness += angle_noise * .03 * pow(pointer_distance_normalized, 80.);
float shape = get_sector_shape(pointer_distance, angle, sector_size, thickness);
is_sector_edge = max(is_sector_edge, smoothstep(.6, 1., shape));
sector_constant = mix(sector_constant, random(sector_seed), smoothstep(.2, .8, shape));
vec2 grid_uv = 2. * (.8 + .5 * pointer_distance_normalized) * img_uv;
float grid_noise = noise(grid_uv + sector_seed);
float grid_thickness = (.4 + .4 * random(10. * sector_seed)) * u_edge_thickness;
float grid_shape = shape * smoothstep(.27, .27 + grid_thickness, grid_noise);
is_grid_edge += (smoothstep(.1, .5, grid_shape) * smoothstep(.9, .6, grid_shape));
sector_constant = mix(sector_constant, random(sector_seed + 100.), smoothstep(.2, .8, grid_shape));
vec2 central_grid_uv = img_uv * (3. + 3. * pow(pointer_distance_normalized, 10.));
float central_grid_noise = noise(central_grid_uv + sector_seed);
float central_grid_thickness = (1. + .5 * random(-2. + sector_seed)) * u_edge_thickness;
float central_grid_shape = step(.7, shape) * smoothstep(.27, .27 + central_grid_thickness, central_grid_noise);
is_central_edge += (smoothstep(.0, .5, central_grid_shape) * smoothstep(1., .5, central_grid_shape));
is_central_edge *= (step(.8, pointer_distance_normalized));
sector_constant = mix(sector_constant, random(sector_seed + 100.), smoothstep(.2, .8, central_grid_shape));
sector_start_angle += sector_size;
}
float img_edge_alpha = get_img_frame_alpha(img_uv, .004);
is_sector_edge = 1. - is_sector_edge;
float cracks_edge = max(is_grid_edge, is_sector_edge);
cracks_edge = max(cracks_edge, is_central_edge);
float central_cracks = get_simple_cracks(pointer_angle, pointer_distance_normalized, angle_noise);
cracks_edge += central_cracks;
if (u_effect_active > 0.) {
img_uv = get_disturbed_uv(img_uv, sector_constant, cracks_edge, pointer_direction, get_img_frame_alpha(img_uv, .2));
}
vec4 img = texture2D(u_image_texture, img_uv);
color = img.rgb;
color += .12 * u_effect_active * (sector_constant - .5);
img_edge_alpha = get_img_frame_alpha(img_uv, .004);
float opacity = img_edge_alpha;
opacity -= .3 * u_effect_active * pow(is_grid_edge, 4.);
opacity -= .3 * u_effect_active * is_central_edge;
opacity -= .03 * u_effect_active * pow(central_cracks, 4.);
gl_FragColor = vec4(color, opacity);
}
</script>
body, html {
margin: 0;
padding: 0;
}
canvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
.tip {
position: fixed;
top: 90%;
left: 50%;
transform: translate(-50%, -50%);
font-family: monospace;
user-select: none;
pointer-events: none;
background-color: cornsilk;
color: red;
}
.lil-gui {
--width: 400px;
--widget-height: 20px;
font-size: 15px;
--input-font-size: 15px;
--padding: 10px;
--spacing: 10px;
--slider-knob-width: 5px;
--background-color: rgba(5, 0, 15, .9);
--widget-color: rgba(255, 255, 255, .3);
--focus-color: rgba(255, 255, 255, .4);
--hover-color: rgba(255, 255, 255, .5);
--font-family: monospace;
z-index: 1;
}
import GUI from "https://cdn.jsdelivr.net/npm/lil-gui@0.18.2/+esm"
const canvasEl = document.querySelector("canvas");
const imgInput = document.querySelector("#image-selector-input");
const devicePixelRatio = Math.min(window.devicePixelRatio, 2);
const params = {
clickRandomizer: .332,
distance: .015,
effectOn: true,
edgeThickness: .006,
loadMyImage: () => {
imgInput.click();
},
};
const pointer = {
x: .55 * window.innerWidth,
y: .5 * window.innerHeight,
};
imgInput.onchange = () => {
const [file] = imgInput.files;
if (file) {
const reader = new FileReader();
reader.onload = e => {
loadImage(e.target.result);
};
reader.readAsDataURL(file);
}
};
let image, uniforms, effectOffControl;
const gl = initShader();
updateUniforms();
loadImage("https://ksenia-k.com/img/codepen/for-glass-crack-demo-1.jpg");
setupEvents();
createControls();
render();
window.addEventListener("resize", resizeCanvas);
// ---------------
// codepen preview
let autoRunFlag = true;
function autoRun() {
params.clickRandomizer -= .03;
pointer.x += 70 * (autoRunFlag ? 1 : -1);
pointer.y += 40;
updateUniforms();
autoRunFlag = !autoRunFlag;
}
setTimeout(autoRun, 500);
setTimeout(autoRun, 1000);
// ---------------
function initShader() {
const vsSource = document.getElementById("vertShader").innerHTML;
const fsSource = document.getElementById("fragShader").innerHTML;
const gl = canvasEl.getContext("webgl");
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);
return gl;
}
function updateUniforms() {
gl.uniform1f(uniforms.u_click_randomizer, params.clickRandomizer);
gl.uniform1f(uniforms.u_rotation, params.rotation);
gl.uniform1f(uniforms.u_effect, params.distance);
gl.uniform1f(uniforms.u_effect_active, params.effectOn ? 1 : 0);
gl.uniform1f(uniforms.u_edge_thickness, params.edgeThickness);
gl.uniform2f(uniforms.u_pointer_position, pointer.x / window.innerWidth, pointer.y / window.innerHeight);
}
function loadImage(src) {
image = new Image();
image.crossOrigin = "anonymous";
image.src = src;
image.onload = () => {
const imageTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, imageTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.uniform1i(uniforms.u_image_texture, 0);
resizeCanvas();
};
}
function render() {
const currentTime = performance.now();
gl.uniform1f(uniforms.u_time, currentTime);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(render);
}
function resizeCanvas() {
const imgRatio = image.naturalWidth / image.naturalHeight;
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);
gl.uniform1f(uniforms.u_img_ratio, imgRatio);
}
function setupEvents() {
canvasEl.addEventListener("click", e => {
pointer.x = e.pageX;
pointer.y = e.pageY;
params.clickRandomizer = Math.random();
updateUniforms();
});
document.addEventListener("keydown", e => {
if (event.code === "Space") {
params.effectOn = !params.effectOn;
if (effectOffControl) {
effectOffControl.setValue(params.effectOn)
}
}
});
}
function createControls() {
const gui = new GUI();
gui.close();
gui
.add(params, "loadMyImage")
.name("load image")
const paramsFolder = gui.addFolder("shader params");
// paramsFolder.close();
effectOffControl = paramsFolder
.add(params, "effectOn")
.onChange(updateUniforms)
paramsFolder
.add(params, "distance", 0, .2)
.onChange(updateUniforms)
paramsFolder
.add(params, "clickRandomizer", 0, 1)
.onChange(updateUniforms)
paramsFolder
.add(params, "edgeThickness", 0, .02)
.onChange(updateUniforms)
}
Also see: Tab Triggers