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.
<div class="container-outer">
<div class="container-inner">
<button data-sketchy="button" class="sketchy-button">
<!--So Sketchy-->
</button>
</div>
</div>
html, body {
height: 100%;
margin: 0;
}
html {
text-align: center;
background: white;
}
.container-outer {
display: table;
width: 100%;
height: 100%;
}
.container-inner {
display: table-cell;
vertical-align: middle;
width: 100%;
}
.sketchy-button {
position: relative;
font-size: 3rem;
margin: 1em;
padding: 1.5em 4em;
border: 0;
color: white;
background: none;
font-family: "Comic Sans MS", sans-serif;
> svg {
position: absolute;
top: 0;
left: 0;
z-index: -1;
// Scale and transform to render SVG filters at a higher resolution.
width: 200%;
transform-origin: 0% 0%;
transform: translateZ(0) scale(0.5);
// SVG's default rendering looks fine on Retina screens, so disable the scaling.
// (This class is added to <html> on load.)
.hi-dpi & {
width: 100%;
transform: none;
}
}
&:focus {
outline: none;
> svg rect {
stroke: #66afe9;
stroke-width: 2;
stroke-opacity: 1;
}
}
}
// Initialize button(s) on load
window.onload = function() {
var sketchyBtns = document.querySelectorAll('[data-sketchy="button"]')
for (var i = 0; i < sketchyBtns.length; i++) {
var btn = sketchyBtns[i]
var btnBG = new SketchySVG(btn)
btn.appendChild(btnBG.svg)
}
// Add class for retina
if(isRetina()) {
document.documentElement.classList.add('hi-dpi');
}
}
// Detect Hi-DPI screens
function isRetina() {
if(window.devicePixelRatio > 1) {
return true;
}
if(window.matchMedia && window.matchMedia('(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)').matches) {
return true;
}
return false;
}
// Global variables
var svgNS = 'http://www.w3.org/2000/svg' // SVG namespace
var svgID = 0 // Iterates to to allow each instance to have a unique ID
// Constructor
function SketchySVG(element) {
this.el = element
var filterID = 'SketchyFilter' + svgID
svgID++
var svg = document.createElementNS(svgNS, 'svg')
var defs = document.createElementNS(svgNS, 'defs')
var filter = document.createElementNS(svgNS, 'filter')
filter.setAttribute('id', filterID)
// Turbulence filter
// Generates a randomized pattern to use for displacement
var turbulence = document.createElementNS(svgNS, 'feTurbulence')
turbulence.setAttribute('baseFrequency', '0.009')
turbulence.setAttribute('numOctaves', '10')
turbulence.setAttribute('type', 'turbulence')
var seed = Math.round(Math.random() * 1000)
turbulence.setAttribute('seed', seed)
turbulence.setAttribute('result', 'turbulenceResult')
// Displacement filter
// Offsets pixels according to turbulence output
var displacement = document.createElementNS(svgNS, 'feDisplacementMap')
displacement.setAttribute('in', 'SourceGraphic')
displacement.setAttribute('in2', 'turbulenceResult')
displacement.setAttribute('xChannelSelector', 'R')
displacement.setAttribute('scale', '7')
displacement.setAttribute('result', 'displacementResult')
// Create a rectangle and apply the filter
var rect = document.createElementNS(svgNS, 'rect')
rect.setAttribute('fill', '#000000')
rect.setAttribute('filter', 'url(#' + filterID + ')')
// Add text within the SVG and apply the same filter
var text = document.createElementNS(svgNS, 'text')
text.setAttribute('text-anchor', 'middle')
text.setAttribute('fill', '#ffffff')
text.appendChild(document.createTextNode('So Sketchy'))
text.setAttribute('filter', 'url(#' + filterID + ')')
// Add everything to the SVG DOM object
filter.appendChild(turbulence)
filter.appendChild(displacement)
defs.appendChild(filter)
svg.appendChild(defs)
svg.appendChild(rect)
svg.appendChild(text)
// Save references to SVG elements
this.svg = svg
this.rect = rect
this.text = text
this.turbFilter = turbulence
this.resize()
window.addEventListener('resize', this.resize.bind(this))
// Animation properties
this.isSketching = false
this.sketchingTicks = 0
this.sketchingTimeout = 0
this.seed = seed
this.fps = 12
this.startSketching()
}
// Resize to fit container
SketchySVG.prototype.resize = function() {
var w = this.el.offsetWidth
var h = this.el.offsetHeight
this.svg.setAttribute('viewBox', '0 0 ' + w + ' ' + h)
this.rect.setAttribute('width', (w - 8))
this.rect.setAttribute('height', (h - 8))
this.rect.setAttribute('x', '4')
this.rect.setAttribute('y', '4')
this.text.setAttribute('x', w / 2)
this.text.setAttribute('y', h * 7 / 11)
}
SketchySVG.prototype.startSketching = function() {
clearTimeout(this.sketchingTimeout)
this.isSketching = true
this.sketchingTicks = 0
setTimeout(this.tickSketching.bind(this), (1000 / this.fps))
}
SketchySVG.prototype.tickSketching = function() {
clearTimeout(this.sketchingTimeout)
this.sketchingTicks++
// Reset seed every few frames to "loop" the animation
if (this.sketchingTicks % 25 === 0) {
this.seed -= 24
} else {
this.seed++
}
this.turbFilter.setAttribute('seed', this.seed)
if (this.isSketching) {
this.sketchingTimeout = setTimeout(this.tickSketching.bind(this), (1000 / this.fps))
}
}
SketchySVG.prototype.stopSketching = function() {
clearTimeout(this.sketchingTimeout)
this.isSketching = false
}
Also see: Tab Triggers