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="stage"></canvas>
<div id="info">Move your mouse around :)</div>
<script id="bulgeVert" type="x-shader/x-vertex">
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat3 projectionMatrix;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
vTextureCoord = aTextureCoord;
}
</script>
<script id="bulgeFrag" type="x-shader/x-vertex">
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform float radius;
uniform float strength;
uniform vec2 center;
uniform vec4 dimensions;
uniform vec4 filterArea;
vec2 mapCoord(vec2 coord) {
coord *= filterArea.xy;
coord += filterArea.zw;
return coord;
}
vec2 unmapCoord(vec2 coord) {
coord -= filterArea.zw;
coord /= filterArea.xy;
return coord;
}
vec2 bulge(vec2 coord) {
coord -= center;
float distance = length(coord);
if (distance < radius) {
float percent = distance / radius;
if (strength > 0.0) {
coord *= mix(1.0, smoothstep(0.0, radius / distance, percent), strength * 0.75);
} else {
coord *= mix(1.0, pow(percent, 1.0 + strength * 0.75) * radius / distance, 1.0 - percent);
}
}
coord += center;
return coord;
}
void main(void) {
vec2 coord = mapCoord(vTextureCoord);
coord = bulge(coord);
vec2 clampedCoord = clamp(coord, vec2(0.0), filterArea.xy);
coord = unmapCoord(coord);
gl_FragColor = texture2D(uSampler, coord);
if (coord != clampedCoord) {
gl_FragColor.a *= max(0.0, 1.0 - length(coord - clampedCoord));
}
}
</script>
<script id="marbleFrag" type="x-shader/x-vertex">
precision mediump float;
uniform vec2 resolution;
uniform float time;
uniform vec2 mouse;
uniform float alpha;
uniform float fluid_speed;
uniform float color_intensity;
uniform vec2 size;
uniform vec4 dimensions;
uniform vec4 filterArea;
const int complexity = 40; // More points of color.
const float mouse_factor = 25.0; // Makes it more/less jumpy.
const float mouse_offset = 5.0; // Drives complexity in the amount of curls/cuves. Zero is a single whirlpool.
const float Pi = 3.14159;
float sinApprox(float x) {
x = Pi + (2.0 * Pi) * floor(x / (2.0 * Pi)) - x;
return (4.0 / Pi) * x - (4.0 / Pi / Pi) * x * abs(x);
}
float cosApprox(float x) {
return sinApprox(x + 0.5 * Pi);
}
void main(void) {
vec2 p = (2.0 * gl_FragCoord.xy - resolution) / max(resolution.x, resolution.y);
for(int i = 1; i < complexity; i++) {
vec2 newp = p;
newp.x += 0.6 / float(i) * sin(float(i) * p.y + time / fluid_speed + 0.3 * float(i)) + mouse.y / mouse_factor + mouse_offset;
newp.y += 0.6 / float(i) * sin(float(i) * p.x + time / fluid_speed + 0.3 * float(i+10)) - mouse.x / mouse_factor + mouse_offset;
p = newp;
}
vec3 col = vec3(color_intensity * sin(3.0 * p.x) + color_intensity,color_intensity * sin(3.0 * p.y) + color_intensity,color_intensity * sin(p.x + p.y) + color_intensity);
gl_FragColor = vec4(col, alpha);
}
</script>
body {
background: #000;
}
#stage {
position: fixed;
}
#info {
position: fixed;
pointer-events: none;
padding: 12px;
color: #ddd;
font-size: 16px;
user-select: none;
font-weight: bold;
text-shadow:
-1px -1px 0 rgba(0,0,0,0.5),
1px -1px 0 rgba(0,0,0,0.5),
-1px 1px 0 rgba(0,0,0,0.5),
1px 1px 0 rgba(0,0,0,0.5);
}
console.clear()
var log = console.log.bind(console);
var baseUrl = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/";
var bulgeVert = document.getElementById("bulgeVert").textContent;
var bulgeFrag = document.getElementById("bulgeFrag").textContent;
var marbleFrag = document.getElementById("marbleFrag").textContent;
//
// BULGE PINCH FILTER
// ===========================================================================
class BulgePinchFilter extends PIXI.Filter {
constructor(x, y, radius = 100, strength = 0.5) {
// super(bulgeVert, bulgeFrag);
super(PIXI.Filter.defaultVertexSrc, bulgeFrag);
this.uniforms.center = new PIXI.Point(x, y);
this.uniforms.radius = radius;
this.uniforms.strength = strength;
}
get radius() { return this.uniforms.radius; }
set radius(value) { this.uniforms.radius = value; }
get strength() { return this.uniforms.strength; }
set strength(value) { this.uniforms.strength = value; }
get center() { return this.uniforms.center; }
set center(value) { this.uniforms.center = value; }
get x() { return this.uniforms.center.x; }
set x(value) { this.uniforms.center.x = value; }
get y() { return this.uniforms.center.y; }
set y(value) { this.uniforms.center.y = value; }
apply(filterManager, input, output) {
this.uniforms.dimensions[0] = input.sourceFrame.width;
this.uniforms.dimensions[1] = input.sourceFrame.height;
filterManager.applyFilter(this, input, output);
}
}
//
// MARBLE FILTER - based on filter in Phaser
// ===========================================================================
class MarbleFilter extends PIXI.Filter {
constructor(width, height) {
super(null, marbleFrag);
this.uniforms.time = 0;
this.uniforms.alpha = 1;
this.uniforms.fluid_speed = 10;
this.uniforms.color_intensity = 0.30;
this.uniforms.mouse = new PIXI.Point();
this.uniforms.resolution = new PIXI.Point(width, height);
}
get time() { return this.uniforms.time; }
set time(value) { this.uniforms.time = value; }
get alpha() { return this.uniforms.alpha; }
set alpha(value) { this.uniforms.alpha = value; }
get speed() { return this.uniforms.fluid_speed; }
set speed(value) { this.uniforms.fluid_speed = value; }
get intensity() { return this.uniforms.color_intensity; }
set intensity(value) { this.uniforms.color_intensity = value; }
get mouse() { return this.uniforms.mouse; }
set mouse(value) { this.uniforms.mouse = value; }
get width() { return this.uniforms.resolution.width; }
set width(value) { this.uniforms.resolution.width = value; }
get height() { return this.uniforms.resolution.height; }
set height(value) { this.uniforms.resolution.height = value; }
}
var vw = window.innerWidth;
var vh = window.innerHeight;
var app = new PIXI.Application(vw, vh, {
view: document.getElementById("stage"),
resolution: window.devicePixelRatio || 1,
backgroundColor: 0x000000,
antialias: true,
autoResize: true
});
var background = new PIXI.Sprite();
var container = new PIXI.Container();
var resized = false;
var count = 0;
var moves = 0;
var loader = new PIXI.loaders.Loader(baseUrl)
.add("muppets", "statler-waldorf-1.png?v=1")
.load(onLoad);
var muppets, bulgeFilter, marbleFilter;
function onLoad(loader, resources) {
muppets = new PIXI.Sprite(resources.muppets.texture);
muppets.anchor.set(0.5, 1);
muppets.pivot.set(0.5, 1);
resize();
bulgeFilter = new BulgePinchFilter(vw / 2, vh / 2, 300, 1);
marbleFilter = new MarbleFilter(vw, vh);
background.filters = [marbleFilter];
container.filters = [bulgeFilter];
background.filterArea = app.screen;
container.filterArea = app.screen;
container.addChild(background, muppets);
app.stage.addChild(container);
app.stage.interactive = true;
app.stage.on("pointermove", onPointerMove);
app.stage.on("pointermove", removeInfo);
app.ticker.add(() => {
if (resized) { resize(); }
// count += 0.1;
count += 0.05;
marbleFilter.time = count;
});
window.addEventListener("resize", () => resized = true);
var tl1 = new TimelineMax({ repeat: -1, repeatDelay: 5, yoyo: true })
.to(bulgeFilter, 5, { strength: -0.6, ease: Power1.easeInOut });
}
function resize() {
vw = window.innerWidth;
vh = window.innerHeight;
var r1 = (vw - 100) / muppets.texture.width;
var r2 = (vh - 100) / muppets.texture.height;
muppets.scale.set(Math.min(r1, r2))
muppets.x = vw / 2;
muppets.y = vh;
background.width = vw;
background.height = vh;
app.renderer.resize(vw, vh);
resized = false;
}
function onPointerMove(event) {
var global = event.data.global;
bulgeFilter.center.copy(global);
}
function removeInfo() {
if (++moves > 1000) {
TweenLite.to("#info", 3, { autoAlpha: 0 });
app.stage.off("pointermove", removeInfo);
}
}
Also see: Tab Triggers