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>
<section>
<h1>Rough Edges Paint Worklet</h1>
<p><strong>If you Fork this:</strong> The pen's JS Panel is loaded as a paint worklet. So if you fork it, your URL has changed and you need to update it. Otherwise it will keep referring to this pen's JS!</p>
<p>All the entries have the "rough-edge" worklet applied. There are 6 custom properties to control the edge generation:</p>
</section>
<details>
<summary>show the props</summary>
<dl>
<dt><code>--rough-edge-padding: <length></code></dt>
<dd>Defines the "area" in which the curves of the edge will generate. By default, the severity of the curves will also relate to this value.</dd>
<dt><code>--rough-edge-severity: <length></code></dt>
<dd>Sets the maximum height for generating the curves. The higher the value, the higher the curves.</dd>
<dt><code>--rough-edge-max-segments: <integer></code></dt>
<dd>Sets the maximum number of horizontal segments, for which curves will be generated. Per default, the number of generated segments is based on the length of the segment</dd>
<dt><code>--rough-edge-segment-length: <length></code></dt>
<dd>Sets the length of a horizontal segment. The smaller the size, the more segments for a given length will be generated, which will create more curves for the edge.</dd>
<dt><code>--rough-edge-background: <color></code></dt>
<dd>Sets the color of the entire shape. Does not support gradients (yet!).</dd>
<dt><code>--rough-edge-debug: <integer></code></dt>
<dd>Enables the debugging mode. In this mode, many of the points will be visualized, such as the A & B Bezier Points, as well as the segments and the edge-padding. Nice to see what's happening!</dd>
</dl>
</details>
<article class="rough-edge post">
<h2>Entry #1</h2>
<p>Cupcake ipsum dolor sit amet. Cheesecake caramels donut cupcake cupcake sweet roll. Cake tart I love pudding I love lemon drops croissant cake. Tootsie roll topping danish halvah I love jelly beans halvah candy.</p>
</article>
<article class="rough-edge post">
<h2>Entry #2</h2>
<p>Dessert muffin croissant liquorice marshmallow. Shortbread jelly beans apple pie sweet tiramisu chocolate bar marshmallow I love marshmallow. Macaroon chocolate marzipan sugar plum sugar plum icing. Candy canes cupcake lemon drops gummi bears powder caramels tiramisu sesame snaps.</p>
</article>
<article class="rough-edge post">
<h2>Entry #3</h2>
<p>Muffin pie tiramisu sesame snaps jelly-o croissant caramels chocolate marzipan. Pastry I love cookie sweet shortbread toffee cookie. I love sweet sugar plum chocolate bar I love halvah cookie.</p>
</article>
</main>
<script>
if (CSS.paintWorklet) {
window.CSS.registerProperty({
name: '--rough-edge-padding',
syntax: '<length>',
inherits: false,
initialValue: '16px'
});
window.CSS.registerProperty({
name: '--rough-edge-background',
syntax: '<color>',
inherits: false,
initialValue: '#fff'
});
window.CSS.registerProperty({
name: '--rough-edge-debug',
syntax: '<integer>',
inherits: false,
initialValue: '0'
});
window.CSS.registerProperty({
name: '--rough-edge-severity',
syntax: '<length>',
inherits: false,
initialValue: '0px'
});
window.CSS.registerProperty({
name: '--rough-edge-segment-length',
syntax: '<length>',
inherits: false,
initialValue: '0px'
});
window.CSS.registerProperty({
name: '--rough-edge-max-segments',
syntax: '<integer>',
inherits: false,
initialValue: '0'
});
CSS.paintWorklet.addModule('/thomassemmler/pen/vYZogLx.js');
}
</script>
:root {
background-color: turquoise;
}
body {
display: grid;
grid-template-columns: 1fr [content-start] minmax(auto, 740px) [content-end] 1fr;
}
main {
grid-column: content-start / content-end;
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 2em;
h1 {
grid-column: 1 / -1;
grid-row: 1;
font-size: 2.5em;
}
h2 {
font-size: 1.5em;
}
& > .post {
padding-inline: var(--rough-edge-padding);
padding-block-end: var(--rough-edge-padding);
border-radius: 3px;
&:nth-of-type(1) {
--rough-edge-background: honeydew;
}
&:nth-of-type(2) {
--rough-edge-padding: clamp(1em, 10vw, 10em);
--rough-edge-segment-length: 50;
}
&:nth-of-type(3) {
--rough-edge-debug: 1;
--rough-edge-max-segments: 3;
--rough-edge-severity: 3em;
}
}
}
dl {
dt + dd + * {
margin-block-start: 1em;
}
}
// This where the paintlet is being
.rough-edge {
padding-block-start: var(--rough-edge-padding);
background: paint(rough-edge);
min-height: var(--rough-edge-padding);
}
/**
Much credit needs to go to Leif Niemczik (https://leifs.website/), who took a large chunk of his time not only to explain the math of how I could make sure that curves had mirrored bezier points (and thus smooth), but he even coded it into a demo and then helped me extract it.
*/
if (typeof registerPaint !== "undefined") {
registerPaint(
"rough-edge",
class {
static get inputProperties() {
return [
'--rough-edge-padding',
'--rough-edge-debug',
'--rough-edge-background',
'--rough-edge-severity',
'--rough-edge-segment-length',
'--rough-edge-max-segments'
]
}
drawCurve(_ctx, _curvePosition, _edgePadding, _startX = 0, _endX, _lastCurve, _curveSeverity = undefined, _debug = undefined) {
let curveYPos = _curvePosition;
let length = _endX - _startX;
// random numbers
let randomYMax = _curveSeverity || _edgePadding;
// random 1 / -1
let randomYDir = Math.ceil( (Math.random() - .5) * 2 ) < 1 ? -1 : 1;
// random number 0 / 0.5
let randomAX = Math.random() * (0.25 - 0.15) + 0.15;
// random number 0.5 / 1
let randomBX = Math.random() * (0.85 - 0.75) + 0.75;
// random number 16 / 32
let randomY = Math.floor(Math.random() * (randomYMax - (randomYMax / 2)) +1) + (randomYMax / 2);
let randomBY = Math.floor(Math.random() * randomYMax) + 1;
// Start & End
let startAt = {
x: _startX,
y: _lastCurve.curveY ? _lastCurve.curveY:randomY
};
let endAt = {
x: _endX,
y: randomY
};
// Bezier-Handle Points
let bezierA = {
x: _lastCurve.handleX ? (_startX + _lastCurve.handleX):(length * randomAX),
y: _lastCurve.handleY ? (_lastCurve.curveY + _lastCurve.handleY):randomY
};
let bezierB = {
x: _startX + (length * randomBX.toFixed(2)),
y: randomBY
};
let passOn = {
handleX: endAt.x - bezierB.x,
handleY: endAt.y - bezierB.y,
curveY: endAt.y
};
if (_debug) {
// draw a curve right in the middle
_ctx.beginPath();
_ctx.setLineDash([]);
_ctx.strokeStyle = '#000000'
_ctx.moveTo(startAt.x, startAt.y);
// the curve!
_ctx.bezierCurveTo(
bezierA.x, bezierA.y, // bezier point a
bezierB.x, bezierB.y, // bezier point b
endAt.x, endAt.y // endpoint
);
_ctx.stroke();
_ctx.closePath();
// draw the line from start, to the bezier points and to the end
_ctx.beginPath();
_ctx.strokeStyle = '#9e42f5';
_ctx.setLineDash([4, 4]);
_ctx.moveTo(startAt.x, startAt.y);
_ctx.lineTo(bezierA.x, bezierA.y);
_ctx.lineTo(bezierB.x, bezierB.y);
_ctx.lineTo(endAt.x, endAt.y);
_ctx.stroke();
_ctx.closePath();
// draw the bezier points as circles
_ctx.beginPath();
_ctx.fillStyle = '#ff0000';
_ctx.arc(bezierA.x, bezierA.y, 4, 0, 2 * Math.PI);
_ctx.fill();
_ctx.closePath();
_ctx.beginPath();
_ctx.fillStyle = '#0000ff';
_ctx.arc(bezierB.x, bezierB.y, 4, 0, 2 * Math.PI);
_ctx.fill();
_ctx.closePath();
} else {
// the curve!
_ctx.bezierCurveTo(
bezierA.x, bezierA.y, // bezier point a
bezierB.x, bezierB.y, // bezier point b
endAt.x, endAt.y // endpoint
);
}
return passOn;
}
paint(ctx, geom, props) {
// get values from custom properties
const edgePadding = props.get('--rough-edge-padding').value;
const debug = props.get('--rough-edge-debug').value;
const curveSeverity = props.get('--rough-edge-severity').value;
const segmentCustomLength = props.get('--rough-edge-segment-length').value;
const maxSegments = props.get('--rough-edge-max-segments').value;
const background = props.get('--rough-edge-background').toString();
// Segments - determines, how many individual beziercurves will be drawn, per default, grows with base-length, unless a fixed number of segments is determined
const segmentBaseLength = segmentCustomLength || 100;
const segments = maxSegments || Math.floor(geom.width / segmentBaseLength);
const segmentLength = geom.width / segments;
const curvePosition = edgePadding;
const firstCurvePosition = Math.floor(Math.random() * (curveSeverity || curvePosition - (0)) +1);
let lastCurve = {
handleX: null,
handleY: null,
curveY: firstCurvePosition
}
if (!debug) {
// draw the curves, along the top edge
ctx.beginPath();
ctx.fillStyle = background;
ctx.moveTo(0, firstCurvePosition);
// draw the curves
for (let i = 0; i <= segments; i++) {
let segmentX = segmentLength * i;
lastCurve = this.drawCurve(ctx, curvePosition, edgePadding, segmentX, segmentX + segmentLength, lastCurve, curveSeverity, false);
}
ctx.lineTo(geom.width, lastCurve.curveY);
ctx.lineTo(geom.width, geom.height);
ctx.lineTo(0, geom.height);
ctx.lineTo(0, firstCurvePosition);
ctx.closePath();
ctx.fill();
} else {
// draw the box
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(geom.width, 0);
ctx.lineTo(geom.width, geom.height);
ctx.lineTo(0, geom.height);
ctx.lineTo(0, 0);
ctx.stroke();
ctx.closePath();
// draw the curves
for (let i = 0; i <= segments; i++) {
let segmentX = segmentLength * i;
lastCurve = this.drawCurve(ctx, curvePosition, edgePadding, segmentX, segmentX + segmentLength, lastCurve, curveSeverity, true);
}
// separate each segment with a vertical line
for (let i = 1; i < segments; i++) {
let segmentX = segmentLength * i;
ctx.beginPath();
ctx.strokeStyle = '#0000ff';
ctx.setLineDash([4, 4]);
ctx.moveTo(segmentX, 0);
ctx.lineTo(segmentX, geom.height);
ctx.stroke();
ctx.closePath();
}
// draw some orientation lines, for the middle-line and their top/bottom areas in which they can move
ctx.beginPath();
ctx.setLineDash([4, 4]);
ctx.moveTo(0, curvePosition);
ctx.lineTo(geom.width, curvePosition);
ctx.strokeStyle = '#7c400f' ;
ctx.stroke();
ctx.closePath();
}
}
}
);
}
Also see: Tab Triggers