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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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 hasWedgeTop">
<img class="canvasify" data-image-frame="wedgeTop" data-defaults-wedge-height="100" src="http://placekitten.com/1280/500" />
<h1>Kitty with a clipped top</h1>
<p>A regular image with a <code>wedgeTop</code> clipping. It looks weird because it has nothing to overlap. This would be a good case for clipping from the bottom, rather than the top.</p>
</div>
<div class="container hasWedgeTop hasWedgeBottom">
<video controls muted class="canvasify" loop autoplay data-image-frame="wedgeBottom">
<source src="https://poc5.ssl.cdn.sdlmedia.com/web/635663565028367012PU.mp4">
</video>
<h1>Video clipping</h1>
<p>This is a <code>wedgeTop</code> and <code>wedgeBottomReverse</code>clipping. It overlaps the one above.</p>
</div>
<div class="container ">
<img class="canvasify" src="http://placekitten.com/1280/600" />
<h1>No Clip Kitty!</h1>
<p>Here's an example where we don't do any clipping at all. </p>
</div>
<div class="container hasWedgeTop hasWedgeBottom">
<img class="canvasify" data-image-frame="wedgeTop, wedgeBottom,wedgeBottomReverse" src="http://placekitten.com/g/1280/500" />
<h1>Gray kitty with many clips</h1>
<p><code>wedgeTop</code>, <code>wedgeBottom</code>, <code>wedgeBottomReverse</code>. It overlaps the top and also sets the bottom to overlap whatever's after. </p>
</div>
<div class="container ">
<img class="canvasify" src="http://placebear.com/1280/500" />
<h1>No Clippings for This Bear!</h1>
<p>A regular image. There's no clippings here at all. But you'd think there were, because of the top and bottom image. If you don't see a bear, resize the screen really quick. It'll show up. </p>
</div>
<div class="container hasWedgeTop hasWedgeBottom">
<img class="canvasify" data-image-frame="wedgeTopReverse, wedgeBottom" src="http://placebear.com/1280/700" />
<h1>Top and Bottom Clipped bear</h1>
<p><code>WedgeTopReverse</code> and <code>wedgeBottom</code> for this bad bear </p>
</div>
<div class="container hasWedgeTop">
<img class="canvasify" data-image-frame="wedgeTop" src="http://placebear.com/g/1400/600" />
<h1>Top Clipped bear</h1>
<p>Items at the end of the page should only be clipped on top. So the ultimate ursidae is unaffected by bottom clippings. e.g.: <code>wedgeTop</code>.</p>
</div>
body {
background-color: #333;
font-family: arial;
}
.container {
height: 400px;
width: 100%;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
}
.hasWedgeTop {
margin-top: -50px;
z-index: -2;
}
.hasWedgeTop:first-child {
margin-top: 0;
}
.hasWedgeTop + * {
margin-bottom: 50px;
}
.hasWedgeBottom {
z-index: 1;
margin-bottom: -50px;
}
.hasWedgeBottom + .hasWedgeTop {
z-index: 1;
}
.hasWedgeBottom + * {
top:0px
}
.canvasify {
position:absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
z-index: -1;
}
video {
width: 100%;
max-height: 400px;
}
h1, p {
padding: .5em;
width: 22em;
}
h1 {
font-size: 2em;
color: rgb(125,150,225);
background-color: rgba(30,30,30,.6);
}
p {
color: rgb(125,150,225);
background-color: rgba(30,30,30,.6);
}
const imageFrames = function () {
const defaults = {
wedgeHeight: 50
};
};
class ImageCanvasModule {
constructor(module) {
this.originalElement = module;
this.defaults = {
wedgeHeight: 50
};
if(this.isVideo) {
/*drawFrame has a check where it'll only draw on reqAnimationFrame if video.paused === false,
so we need to fire drawFrame on both events because that boolean will be false when it's paused, thus cancelling the animation frame
*/
this.media.addEventListener('play', ()=>{
this.drawOnCanvas();
});
this.media.addEventListener('pause', ()=> {
this.drawOnCanvas();
});
}
}
get imageFrames() {
return {
//all wedges draw paths clockwise: top right, bottom right, bottom left, top left
wedgeTop: (wedgeHeight = this.defaults.wedgeHeight) => {
var wedge = new Path2D();
wedge.moveTo(this.dimensions.width, 0);
wedge.lineTo(this.dimensions.width, this.dimensions.height);
wedge.lineTo(0, this.dimensions.height);
wedge.lineTo(0, wedgeHeight);
wedge.closePath();
this.frameContext.clip(wedge);
},
wedgeTopReverse: (wedgeHeight = this.defaults.wedgeHeight) => {
var wedge = new Path2D();
wedge.moveTo(this.dimensions.width, wedgeHeight);
wedge.lineTo(this.dimensions.width, this.dimensions.height);
wedge.lineTo(0, this.dimensions.height);
wedge.lineTo(0, 0);
wedge.closePath();
this.frameContext.clip(wedge);
},
wedgeBottom: (wedgeHeight = this.defaults.wedgeHeight) => {
const wedge = new Path2D();
wedge.moveTo(this.dimensions.width, 0);
wedge.lineTo(this.dimensions.width, this.dimensions.height - wedgeHeight);
wedge.lineTo(0, this.dimensions.height);
wedge.lineTo(0,0);
wedge.lineTo(this.dimensions.width,0);
wedge.closePath();
this.frameContext.clip(wedge);
},
wedgeBottomReverse: (wedgeHeight = this.defaults.wedgeHeight) => {
const wedge = new Path2D();
wedge.moveTo(this.dimensions.width, 0);
wedge.lineTo(this.dimensions.width, this.dimensions.height);
wedge.lineTo(0, this.dimensions.height - wedgeHeight);
wedge.lineTo(0, 0);
wedge.closePath();
this.frameContext.clip(wedge);
}
};
}
get isPicture() {
return (this.originalElement.nodeName === 'PICTURE');
}
get isVideo() {
return (this.originalElement.nodeName === 'VIDEO');
}
get isFitParent() {
return this.originalElement.dataset.hasOwnProperty('fitparent');
}
get media() {
return this.isPicture ? this.originalElement.querySelector('img') : this.originalElement;
}
get hasMatchMedia() {
return (this.originalElement.dataset.matchmedia && this.originalElement.dataset.matchmedia.length > 1) ;
}
get matchMediaResult() {
return window.matchMedia(this.originalElement.dataset.matchmedia).matches;
}
get context() {
return this.canvas.getContext('2d');
}
get dimensions() {
return {
width: this.originalElement.offsetWidth,
height: this.originalElement.offsetHeight
};
}
getPictureFillSrc() {
const style = this.media.currentStyle || window.getComputedStyle(this.media, false); // #IE11-2
return style.backgroundImage.slice(4, -1).replace(/"/g,'');
}
getPictureFillMedia(src) {
const img = new Image(this.dimensions.width, this.dimensions.height); // #IE11-3
img.src = src;
return img;
}
createCanvas () {
let canvas = document.createElement('canvas');
let frameCanvas = document.createElement('canvas');
this.originalElement.parentNode.insertBefore(canvas, this.originalElement.nextSibling);
canvas.className = this.originalElement.className;
this.canvas = canvas;
this.frameCanvas = frameCanvas;
this.frameContext = this.frameCanvas.getContext('2d');
}
sizeParent () {
if (this.hasMatchMedia && this.matchMediaResult ) {
this.originalElement.parentElement.style.height = `${this.dimensions.height}px` ;
}
if (this.hasMatchMedia && !this.matchMediaResult) {
this.originalElement.parentElement.style.height = null;
}
}
sizeCanvas () {
this.canvas.height = this.dimensions.height;
this.canvas.width = this.dimensions.width;
this.frameCanvas.height = this.dimensions.height;
this.frameCanvas.width = this.dimensions.width;
if (this.isFitParent) this.sizeParent();
}
drawFrame (media) { //#IE11-4
if (this.isVideo && media.paused) return false;
if (this.hasMatchMedia && !this.matchMediaResult ) return false;
let imageFrames = this.originalElement.dataset.imageFrame.split(',');
imageFrames.forEach((imageFrame)=>{
imageFrame = imageFrame.replace(/\s/g, '');
if (imageFrame in this.imageFrames) this.imageFrames[imageFrame]();
this.frameContext.globalCompositeOperation = 'copy';
});
var _this = this;
var toggle = false; // toggle is used b/c we don't need 60fps. 30 will do.
(function loop() {
toggle= !toggle;
/*Firefox and IE will show DOMException error b/c video hasn't fully loaded. Video will still play, though. so only start drawing when width > 0 */
if (toggle && _this.dimensions.width > 0) {
let x = 0;
let width = _this.media.offsetWidth;
let y = 0;
_this.frameContext.drawImage(media, x, y, width, _this.canvas.height);
_this.context.drawImage(_this.frameCanvas, x, y, width, _this.canvas.height);
}
_this.context.globalCompositeOperation = 'source-atop'; //smooths out jagged edges
if (_this.isVideo) {
window.requestAnimationFrame(loop);
}
})(); // this is an IIFE so that it will execute at least once
}
drawOnCanvas (media = this.media) { // #IE11-4
this.sizeCanvas();
this.drawFrame(media);
}
hideOriginal () {
//don't use display: none .... you can't get image dimensions when you do that.
this.originalElement.style.opacity = 0;
}
initImageFrame (media = this.media) { // #IE11-4
this.drawOnCanvas(media);
this.hideOriginal();
}
}
// stuff unrelated to module
console.clear();
window.addEventListener('DOMContentLoaded', ()=> {
var els = document.querySelectorAll('.canvasify');
window.canvasified = [];
// loop through and apply methods. I prefer `for of` but even with Babel, IE doesn't like it. #ThanksIE
for (let i = 0; i < els.length; i++) {
let el = els[i];
// don't go applying anything unless there's an imageFrame attribute
if (el.dataset.imageFrame) {
let imageModule = new ImageCanvasModule(el);
console.log(imageModule.originalElement.dataset);
if (imageModule.originalElement.dataset.defaultsWedgeHeight) {
imageModule.defaults.wedgeHeight = imageModule.originalElement.dataset.defaultsWedgeHeight;
}
imageModule.createCanvas();
imageModule.initImageFrame();
window.canvasified.push(imageModule);
window.addEventListener('resize', ()=> {
imageModule.drawOnCanvas(imageModule.media);
})
}
}
console.log(canvasified);
});
Also see: Tab Triggers