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.
.flex_container
h1 Perlin Noise buttons
a(href='#', class='noise_btn noise_btn--border')
strong hover me
a(href='#', class='noise_btn noise_btn--bg')
strong hover me
footer
p Created by #[a(href='mailto:andrew.vereshchak@gmail.com') andrew.vereshchak@gmail.com]
=flex($direction, $wrap, $justify, $align)
display: flex
flex-flow: $direction $wrap
justify-content: $justify
align-items: $align
body
height: 100%
padding: 0
display: block
margin: 0
background: #202344
font: 20px Helvetica
font-weight: 300
a
&:hover
text-decoration: none
h1
font-size: 35px
margin: auto 0 25px
color: #fff
letter-spacing: 3px
font-weight: 600
width: 100%
text-align: center
.flex_container
+flex(row, wrap, center, center)
min-height: 100vh
letter-spacing: 3px
color: rgba(255, 255, 255, 0.8)
padding: 40px 0
box-sizing: border-box
overflow: hidden
.noise_btn
display: block
position: relative
width: 282px
height: 80px
font-weight: 700
line-height: 26px
text-transform: uppercase
letter-spacing: 2.7px
margin: 20px 30px
font-size: 13px
color: #fff
margin-bottom: auto
opacity: 0
cursor: pointer
&.canvas-ready
opacity: 1
&--bg
color: #202344
strong
position: absolute
+flex(row, wrap, center, center)
top: 0px
left: 0px
height: 100%
width: 100%
z-index: 2
.noise-container
display: block
position: relative
.noise-canvas
position: absolute
top: -20px
width: calc(100% + 40px)
height: calc(100% + 40px)
left: -20px
pointer-events: none
z-index: 1
footer
font-size: 16px
letter-spacing: 2px
text-align: center
color: #fff
padding: 30px
a
color: #f33232
text-decoration: none
const setupExamples = () => {
// -----------------
// with border -----
// -----------------
new NoiseButton({
element: document.querySelector(".noise_btn--border"),
polygon: "30, 0, 30, 0",
wavesPos: { x: 0, y: 0 },
borderWidth: 5,
borderColor: "0xFFFFFF",
backgroundAlpha: 1,
wavesAlpha: 0.8,
waves: "https://cdn.rawgit.com/av-dev/noise-button/930cbd38/Z3hB7It.png",
displacementMap:
"https://cdn.rawgit.com/av-dev/noise-button/930cbd38/displace-map.jpeg"
});
// -----------------
// without border --
// -----------------
new NoiseButton({
element: document.querySelector(".noise_btn--bg"),
wavesPos: { x: 0, y: 0.3 },
polygon: "30, 0, 30, 0",
backgroundColor: "0xFFFFFF",
backgroundAlpha: 0
});
};
class NoiseButton extends PIXI.Application {
constructor(options) {
super({
autoStart: false,
autoResize: true,
transparent: true
});
this.options = Object.assign(
{
backgroundColor: 0x4875cc,
backgroundAlpha: 1,
polygon: "0, 0, 0, 0",
borderColor: 0x4875cc,
borderWidth: 0,
wavesAlpha: 1,
displacementScale: { x: 30, y: 50 },
displacementMap: "http://digitalfreepen.com/images/2017/whitenoise.png"
},
options
);
// example of the received polygon string
// '30, 0, 30, 0'
this.polygon = this.options.polygon
.replace(/\s/g, "")
.split(",")
.map(el => {
const number = el | 0;
return number > this.options.borderWidth
? number - this.options.borderWidth / 2
: number;
});
this.offset = 20;
this.animate = false;
return this.createCanvas();
}
async createCanvas() {
this.options.element.classList.add("noise-container");
this.view.classList.add("noise-canvas");
this.options.element.appendChild(this.view);
this.container = new PIXI.Container();
this.stage.addChild(this.container);
if(this.options.waves) {
const wavesTexture = await this.loadTexture(this.options.waves);
this.waves = new PIXI.Sprite(wavesTexture);
}
this.noiseSprite = PIXI.Sprite.fromImage(this.options.displacementMap);
this.setSize();
this.addGraphics();
this.bindEvents();
this.render();
this.options.element.classList.add("canvas-ready");
}
static debounce(func, context, wait, immediate) {
let timeout;
return () => {
const args = arguments;
const later = () => {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
addGraphics() {
this.container.addChild(this.getPolygon(true));
if (this.options.waves) this.drawWaves();
const rect = new PIXI.Graphics();
rect.beginFill(0, 0);
rect.drawRect(0, 0, this.width, this.width);
this.container.addChild(rect);
this.container.addChild(this.getPolygon());
this.setMask();
this.addFilter();
}
setMask() {
let mask = this.getPolygon();
this.stage.addChild(mask);
this.container.mask = mask;
}
drawWaves() {
this.waves.alpha = 1 - this.options.wavesAlpha;
this.waves.y = this.height * this.options.wavesPos.y;
this.container.addChild(this.waves);
this.waves.width = this.waves.height = this.width;
}
setSize() {
const parentWidth = this.options.element.offsetWidth;
const parentHeight = this.options.element.offsetHeight;
this.width = parentWidth + this.offset * 2;
this.height = parentHeight + this.offset * 2;
if (this.oldWidth !== this.width) {
this.renderer.resize(this.width, this.height);
this.oldWidth = this.width;
return true;
} else return false;
}
resize = NoiseButton.debounce(
async () => {
if (this.setSize()) {
this.container.removeChildren(0, this.container.children.length - 1);
this.addGraphics();
this.createTimeLine();
this.render();
}
},
this,
100
);
loadTexture(src) {
return new Promise(resolve => {
const loader = new PIXI.loaders.Loader();
loader.add("waves", src);
loader.load((loader, resources) => resolve(resources.waves.texture));
});
}
addFilter() {
this.container.addChild(this.noiseSprite);
this.noiseFilter = new PIXI.filters.DisplacementFilter(this.noiseSprite);
this.container.filters = [this.noiseFilter];
this.noiseSprite.position.x = -this.width;
this.noiseSprite.width = this.width * 3;
this.noiseFilter.scale.x = 0;
this.noiseFilter.scale.y = 0;
}
createTimeLine() {
this.timeline = new TimelineMax({
onUpdate: this::this.render,
paused: true,
onStart: () => (this.animate = true),
onComplete: () => (this.animate = false)
})
.to(this.noiseFilter.scale, 0.2, {
x: this.options.displacementScale.x,
y: this.options.displacementScale.y
})
.fromTo(
this.noiseSprite,
0.5,
{ x: -(this.noiseSprite.width * 0.66) },
{ x: 0 },
"-=.2"
)
.to(this.noiseFilter.scale, 0.2, { x: 0, y: 0 }, "-=.2");
}
play() {
if (!this.animate) this.timeline.play(0);
}
bindEvents() {
this.createTimeLine();
this.options.element.addEventListener("mouseenter", this::this.play);
window.addEventListener("resize", this::this.resize);
}
getPolygon(background) {
const points = this.polygon;
const graphics = new PIXI.Graphics();
const width = this.width - this.offset * 2 - this.options.borderWidth;
const height = this.height - this.offset * 2 - this.options.borderWidth;
graphics.position.x = this.offset + this.options.borderWidth / 2;
graphics.position.y = this.offset + this.options.borderWidth / 2;
const arrayLines = [
[0, points[0]],
[points[0], 0],
[width - points[1], 0],
[width, points[1]],
[width, height - points[2]],
[width - points[2], height],
[points[3], height],
[0, height - points[3]],
[0, points[0]]
];
graphics.lineStyle(this.options.borderWidth, this.options.borderColor);
graphics.beginFill(
this.options.backgroundColor,
background ? 1 - this.options.backgroundAlpha : 0
);
for (let i = 0, prevCoords = []; i < arrayLines.length; i++) {
if (
prevCoords.length &&
prevCoords[0] === arrayLines[i][0] &&
prevCoords[1] === arrayLines[i][1]
)
continue;
if (i === 0) {
graphics.moveTo(arrayLines[i][0], arrayLines[i][1]);
prevCoords = arrayLines[i];
continue;
}
prevCoords = arrayLines[i];
graphics.lineTo(arrayLines[i][0], arrayLines[i][1]);
}
graphics.endFill();
return graphics;
}
}
setupExamples()
Also see: Tab Triggers