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.
<main>
<div>
<label for="input-image">Image</label>
<input type="file" accept="image/*" id="input-image">
</div>
<div class="control-list">
<div>
<label for="input-brightness">Brightness</label>
<input type="range" min="-1" max="1" step="0.05" id="input-brightness">
</div>
<div>
<label for="input-contrast">Contrast</label>
<input type="range" min="-1" max="1" step="0.05" id="input-contrast">
</div>
<div>
<label for="input-saturation">Saturation</label>
<input type="range" min="-1" max="1" step="0.05" id="input-saturation">
</div>
<div>
<label for="input-exposure">Exposure</label>
<input type="range" min="-1" max="1" step="0.05" id="input-exposure">
</div>
</div>
<div class="control-list">
<label><input type="radio" name="filter" value="none" checked> No filter</label>
<label><input type="radio" name="filter" value="chrome"> Chrome</label>
<label><input type="radio" name="filter" value="fade"> Fade</label>
<label><input type="radio" name="filter" value="monochrome"> Monochrome</label>
<label><input type="radio" name="filter" value="noir"> Noir</label>
<label><input type="radio" name="filter" value="rgb2gbr"> RGB to GBR</label>
<label><input type="radio" name="filter" value="summer"> Summer</label>
</div>
<div id="canvas-container"></div>
</main>
canvas {
display: block;
height: auto !important;
width: 36rem !important;
image-rendering: pixelated;
}
label {
display: block;
font-weight: bold;
margin-bottom: 0.25rem;
}
main {
padding: 1rem;
}
main > * {
margin-bottom: 1rem;
}
.control-list {
display: flex;
flex-flow: row wrap;
}
.control-list > :not(:last-child) {
margin-right: 1rem;
}
import {
Plane,
Program,
Mat4,
Mesh,
Renderer,
Texture,
Vec4,
} from 'https://unpkg.com/[email protected]/src/index.mjs';
import { transpose } from 'https://unpkg.com/[email protected]/src/math/functions/Mat4Func.js';
class ColorMatrix {
constructor() {
this._columnCount = 5;
this._rowCount = 4;
this._cellCount = this._columnCount * this._rowCount;
this._matrix = new Array(this._cellCount).fill(0).map((_, index) => {
const row = Math.floor(index / this._columnCount);
return index % this._columnCount === row ? 1 : 0;
});
}
getOffsetVector() {
return this._matrix.filter((_, index) => index % this._columnCount === 4);
}
getRgbaMatrix() {
return this._matrix.filter((_, index) => {
const column = index % this._columnCount;
return column < 4;
});
}
set(...args) {
if (args.length !== this._cellCount) {
throw new Error(`Number of values must be ${this._cellCount}`);
}
this._matrix = args;
return this;
}
}
class ImageLoader {
static loadFromUrl(src) {
const image = new Image();
image.setAttribute('anonymous', true);
image.setAttribute('crossorigin', true);
return new Promise((resolve, reject) => {
image.addEventListener('load', () => resolve(image));
image.addEventListener('error', () => reject(new Error('Unable to load image')));
image.src = src;
});
}
static loadFromFile(file) {
return ImageLoader.loadFromUrl(URL.createObjectURL(file));
}
}
class Vec4Utils {
static multiply(a, b) {
return new Vec4(new Array(4).fill(1).map((_, index) => {
return a[index] * b[index];
}));
}
}
class Application {
constructor() {
this._renderer = new Renderer();
this._map = new Texture(this._gl);
this._mesh = this.createMesh(this._map);
}
get canvas() {
return this._renderer.gl.canvas;
}
createMesh(map) {
const geometry = new Plane(this._gl, {
height: 2,
width: 2,
});
const program = new Program(this._gl, {
fragment: `
precision mediump float;
uniform sampler2D u_map;
uniform mat4 u_brightnessMatrix;
uniform vec4 u_brightnessOffset;
uniform mat4 u_contrastMatrix;
uniform vec4 u_contrastOffset;
uniform mat4 u_filterMatrix;
uniform vec4 u_filterOffset;
uniform mat4 u_exposureMatrix;
uniform vec4 u_exposureOffset;
uniform mat4 u_saturationMatrix;
uniform vec4 u_saturationOffset;
varying vec2 v_uv;
void main() {
vec4 texel = texture2D(u_map, v_uv);
mat4 matrix = u_brightnessMatrix * u_contrastMatrix * u_exposureMatrix * u_saturationMatrix * u_filterMatrix;
vec4 offset = u_brightnessOffset + u_contrastOffset + u_exposureOffset + u_saturationOffset * u_filterOffset;
// Diff view
// gl_FragColor = mix(texel, matrix * texel + offset, step(0.5, v_uv.x + 0.75 * (-v_uv.y + 0.5)));
gl_FragColor = matrix * texel + offset;
}
`,
vertex: `
attribute vec4 position;
attribute vec2 uv;
varying vec2 v_uv;
void main() {
v_uv = uv;
gl_Position = position;
}
`,
uniforms: {
u_brightnessMatrix: { value: new Mat4() },
u_brightnessOffset: { value: new Vec4() },
u_contrastMatrix: { value: new Mat4() },
u_contrastOffset: { value: new Vec4() },
u_filterMatrix: { value: new Mat4() },
u_filterOffset: { value: new Vec4() },
u_exposureMatrix: { value: new Mat4() },
u_exposureOffset: { value: new Vec4() },
u_saturationMatrix: { value: new Mat4() },
u_saturationOffset: { value: new Vec4() },
u_map: { value: map },
},
});
return new Mesh(this._gl, {
geometry,
program,
});
}
setBrightness(brightness) {
this._mesh.program.uniforms.u_brightnessOffset.value.set(
brightness,
brightness,
brightness,
0,
);
this.render();
}
setContrast(contrast) {
const c = 1 + contrast;
const o = 0.5 * (1 - c);
transpose(this._mesh.program.uniforms.u_contrastMatrix.value, [
c, 0, 0, 0,
0, c, 0, 0,
0, 0, c, 0,
0, 0, 0, 1,
]);
this._mesh.program.uniforms.u_contrastOffset.value.set(o, o, o, 0);
this.render();
}
setExposure(exposure) {
const e = 1 + exposure;
transpose(this._mesh.program.uniforms.u_exposureMatrix.value, [
e, 0, 0, 0,
0, e, 0, 0,
0, 0, e, 0,
0, 0, 0, 1,
]);
this.render();
}
setFilter(filterName) {
const colorMatrix = Application._filterMatrixMap.get(filterName);
const matrix = colorMatrix.getRgbaMatrix();
const offset = colorMatrix.getOffsetVector();
transpose(this._mesh.program.uniforms.u_filterMatrix.value, matrix);
this._mesh.program.uniforms.u_filterOffset.value.set(...offset);
this.render();
}
setImage(image) {
this._map.image = image;
this._map.needsUpdate = true;
this.setSize(image.naturalWidth, image.naturalHeight);
this.render();
}
setSaturation(saturation) {
const s = 1 + saturation;
// https://www.w3.org/TR/WCAG20/#relativeluminancedef
const lr = 0.2126;
const lg = 0.7152;
const lb = 0.0722;
const sr = (1 - s) * lr;
const sg = (1 - s) * lg;
const sb = (1 - s) * lb;
this._mesh.program.uniforms.u_saturationMatrix.value.set(
sr + s, sr , sr , 0,
sg , sg + s, sg , 0,
sb , sb , sb + s, 0,
0 , 0 , 0 , 1,
);
this.render();
}
setSize(width, height) {
this._renderer.setSize(width, height);
}
render() {
this._renderer.render({
scene: this._mesh,
});
}
get _gl() {
return this._renderer.gl;
}
static get _filterMatrixMap() {
return new Map([
['none', new ColorMatrix().set(
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
)],
['chrome', new ColorMatrix().set(
1.398, -0.316, 0.065, -0.273, 0.201,
-0.051, 1.278, -0.080, -0.273, 0.201,
-0.051, 0.119, 1.151, -0.290, 0.215,
0.000, 0.000, 0.000, 1.000, 0.000,
)],
['fade', new ColorMatrix().set(
1.073, -0.015, 0.092, -0.115, -0.017,
0.107, 0.859, 0.184, -0.115, -0.017,
0.015, 0.077, 1.104, -0.115, -0.017,
0.000, 0.000, 0.000, 1.000, 0.000,
)],
['monochrome', new ColorMatrix().set(
0.212, 0.715, 0.114, 0.000, 0.000,
0.212, 0.715, 0.114, 0.000, 0.000,
0.212, 0.715, 0.114, 0.000, 0.000,
0.000, 0.000, 0.000, 1.000, 0.000
)],
['noir', new ColorMatrix().set(
0.150, 1.300, -0.250, 0.100, -0.200,
0.150, 1.300, -0.250, 0.100, -0.200,
0.150, 1.300, -0.250, 0.100, -0.200,
0.000, 0.000, 0.000, 1.000, 0.000
)],
['rgb2gbr', new ColorMatrix().set(
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
1, 0, 0, 0, 0,
0, 0, 0, 1, 0,
)],
['summer', new ColorMatrix().set(
0.95, 0.20, 0.00, 0.00, 0.00,
0.10, 0.90, 0.00, 0.00, 0.00,
0.00, 0.00, 0.90, 0.00, 0.00,
0.00, 0.00, 0.00, 1.00, 0.00,
)],
]);
}
}
(async () => {
const inputBrightness = document.getElementById('input-brightness');
const inputContrast = document.getElementById('input-contrast');
const inputExposure = document.getElementById('input-exposure');
const inputFilterList = Array.from(document.querySelectorAll('input[name=filter]'));
const inputImage = document.getElementById('input-image');
const inputSaturation = document.getElementById('input-saturation');
const image = await ImageLoader.loadFromUrl('https://images.unsplash.com/photo-1525253013412-55c1a69a5738?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1024&q=90');
const app = new Application();
app.setImage(image);
app.render();
document.getElementById('canvas-container').appendChild(app.canvas);
inputBrightness.addEventListener('input', () => app
.setBrightness(Number.parseFloat(inputBrightness.value)));
inputContrast.addEventListener('input', () => app
.setContrast(Number.parseFloat(inputContrast.value)));
inputExposure.addEventListener('input', () => app
.setExposure(Number.parseFloat(inputExposure.value)));
inputSaturation.addEventListener('input', () => app
.setSaturation(Number.parseFloat(inputSaturation.value)));
inputImage.addEventListener('change', async (event) => {
if (event.target.files.length > 0) {
const image = await ImageLoader.loadFromFile(event.target.files[0]);
app.setImage(image);
}
});
window.addEventListener('change', (event) => {
if (!inputFilterList.includes(event.target)) {
return;
}
app.setFilter(event.target.value);
});
})();
Also see: Tab Triggers